aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/feature-removal-schedule.txt7
-rw-r--r--Documentation/filesystems/ocfs2.txt6
-rw-r--r--Documentation/i2c/busses/i2c-viapro8
-rw-r--r--Documentation/i2c/dev-interface110
-rw-r--r--Documentation/i2c/smbus-protocol4
-rw-r--r--Documentation/i2c/writing-clients4
-rw-r--r--arch/arm/mach-omap1/board-h3.c4
-rw-r--r--arch/arm/mach-omap2/board-h4.c11
-rw-r--r--arch/x86/kernel/rtc.c22
-rw-r--r--drivers/acpi/glue.c112
-rw-r--r--drivers/hid/Kconfig221
-rw-r--r--drivers/hid/Makefile35
-rw-r--r--drivers/hid/hid-a4tech.c162
-rw-r--r--drivers/hid/hid-apple.c484
-rw-r--r--drivers/hid/hid-belkin.c105
-rw-r--r--drivers/hid/hid-bright.c71
-rw-r--r--drivers/hid/hid-cherry.c87
-rw-r--r--drivers/hid/hid-chicony.c80
-rw-r--r--drivers/hid/hid-core.c1181
-rw-r--r--drivers/hid/hid-cypress.c158
-rw-r--r--drivers/hid/hid-dell.c75
-rw-r--r--drivers/hid/hid-dummy.c72
-rw-r--r--drivers/hid/hid-ezkey.c95
-rw-r--r--drivers/hid/hid-gyration.c96
-rw-r--r--drivers/hid/hid-ids.h404
-rw-r--r--drivers/hid/hid-input-quirks.c484
-rw-r--r--drivers/hid/hid-input.c915
-rw-r--r--drivers/hid/hid-lg.c342
-rw-r--r--drivers/hid/hid-lg.h18
-rw-r--r--drivers/hid/hid-lg2ff.c (renamed from drivers/hid/usbhid/hid-lg2ff.c)14
-rw-r--r--drivers/hid/hid-lgff.c (renamed from drivers/hid/usbhid/hid-lgff.c)34
-rw-r--r--drivers/hid/hid-microsoft.c219
-rw-r--r--drivers/hid/hid-monterey.c82
-rw-r--r--drivers/hid/hid-petalynx.c122
-rw-r--r--drivers/hid/hid-pl.c (renamed from drivers/hid/usbhid/hid-plff.c)83
-rw-r--r--drivers/hid/hid-samsung.c100
-rw-r--r--drivers/hid/hid-sony.c110
-rw-r--r--drivers/hid/hid-sunplus.c82
-rw-r--r--drivers/hid/hid-tmff.c (renamed from drivers/hid/usbhid/hid-tmff.c)138
-rw-r--r--drivers/hid/hid-zpff.c (renamed from drivers/hid/usbhid/hid-zpff.c)71
-rw-r--r--drivers/hid/hidraw.c8
-rw-r--r--drivers/hid/usbhid/Kconfig75
-rw-r--r--drivers/hid/usbhid/Makefile18
-rw-r--r--drivers/hid/usbhid/hid-core.c463
-rw-r--r--drivers/hid/usbhid/hid-ff.c95
-rw-r--r--drivers/hid/usbhid/hid-pidff.c5
-rw-r--r--drivers/hid/usbhid/hid-quirks.c934
-rw-r--r--drivers/hid/usbhid/hiddev.c20
-rw-r--r--drivers/hid/usbhid/usbhid.h4
-rw-r--r--drivers/hid/usbhid/usbkbd.c12
-rw-r--r--drivers/hid/usbhid/usbmouse.c8
-rw-r--r--drivers/hwmon/dme1737.c320
-rw-r--r--drivers/i2c/busses/Kconfig20
-rw-r--r--drivers/i2c/busses/Makefile1
-rw-r--r--drivers/i2c/busses/i2c-highlander.c498
-rw-r--r--drivers/i2c/busses/i2c-parport-light.c39
-rw-r--r--drivers/i2c/busses/i2c-pca-isa.c20
-rw-r--r--drivers/i2c/busses/i2c-viapro.c17
-rw-r--r--drivers/i2c/chips/isp1301_omap.c141
-rw-r--r--drivers/i2c/chips/tps65010.c12
-rw-r--r--drivers/i2c/i2c-core.c35
-rw-r--r--drivers/rtc/rtc-cmos.c122
-rw-r--r--fs/Kconfig34
-rw-r--r--fs/ocfs2/Makefile3
-rw-r--r--fs/ocfs2/alloc.c913
-rw-r--r--fs/ocfs2/alloc.h86
-rw-r--r--fs/ocfs2/aops.c60
-rw-r--r--fs/ocfs2/buffer_head_io.c134
-rw-r--r--fs/ocfs2/buffer_head_io.h23
-rw-r--r--fs/ocfs2/cluster/masklog.c1
-rw-r--r--fs/ocfs2/cluster/masklog.h1
-rw-r--r--fs/ocfs2/dir.c109
-rw-r--r--fs/ocfs2/dlmglue.c9
-rw-r--r--fs/ocfs2/extent_map.c70
-rw-r--r--fs/ocfs2/extent_map.h4
-rw-r--r--fs/ocfs2/file.c333
-rw-r--r--fs/ocfs2/file.h32
-rw-r--r--fs/ocfs2/inode.c87
-rw-r--r--fs/ocfs2/inode.h6
-rw-r--r--fs/ocfs2/ioctl.c3
-rw-r--r--fs/ocfs2/journal.c89
-rw-r--r--fs/ocfs2/journal.h52
-rw-r--r--fs/ocfs2/localalloc.c384
-rw-r--r--fs/ocfs2/localalloc.h4
-rw-r--r--fs/ocfs2/locks.c15
-rw-r--r--fs/ocfs2/locks.h1
-rw-r--r--fs/ocfs2/namei.c101
-rw-r--r--fs/ocfs2/ocfs2.h56
-rw-r--r--fs/ocfs2/ocfs2_fs.h220
-rw-r--r--fs/ocfs2/ocfs2_jbd_compat.h82
-rw-r--r--fs/ocfs2/resize.c11
-rw-r--r--fs/ocfs2/slot_map.c7
-rw-r--r--fs/ocfs2/stack_user.c33
-rw-r--r--fs/ocfs2/stackglue.c20
-rw-r--r--fs/ocfs2/stackglue.h19
-rw-r--r--fs/ocfs2/suballoc.c248
-rw-r--r--fs/ocfs2/suballoc.h26
-rw-r--r--fs/ocfs2/super.c62
-rw-r--r--fs/ocfs2/symlink.c18
-rw-r--r--fs/ocfs2/uptodate.c38
-rw-r--r--fs/ocfs2/uptodate.h3
-rw-r--r--fs/ocfs2/xattr.c4834
-rw-r--r--fs/ocfs2/xattr.h68
-rw-r--r--include/linux/hid.h373
-rw-r--r--include/linux/hiddev.h46
-rw-r--r--include/linux/mod_devicetable.h10
-rw-r--r--net/bluetooth/hidp/core.c214
-rw-r--r--net/bluetooth/hidp/hidp.h2
-rw-r--r--scripts/mod/file2alias.c18
109 files changed, 13905 insertions, 4352 deletions
diff --git a/Documentation/feature-removal-schedule.txt b/Documentation/feature-removal-schedule.txt
index cc8093c15cf5..4d2566a7d168 100644
--- a/Documentation/feature-removal-schedule.txt
+++ b/Documentation/feature-removal-schedule.txt
@@ -287,6 +287,13 @@ Who: Glauber Costa <gcosta@redhat.com>
287 287
288--------------------------- 288---------------------------
289 289
290What: remove HID compat support
291When: 2.6.29
292Why: needed only as a temporary solution until distros fix themselves up
293Who: Jiri Slaby <jirislaby@gmail.com>
294
295---------------------------
296
290What: /sys/o2cb symlink 297What: /sys/o2cb symlink
291When: January 2010 298When: January 2010
292Why: /sys/fs/o2cb is the proper location for this information - /sys/o2cb 299Why: /sys/fs/o2cb is the proper location for this information - /sys/o2cb
diff --git a/Documentation/filesystems/ocfs2.txt b/Documentation/filesystems/ocfs2.txt
index c318a8bbb1ef..4340cc825796 100644
--- a/Documentation/filesystems/ocfs2.txt
+++ b/Documentation/filesystems/ocfs2.txt
@@ -76,3 +76,9 @@ localalloc=8(*) Allows custom localalloc size in MB. If the value is too
76 large, the fs will silently revert it to the default. 76 large, the fs will silently revert it to the default.
77 Localalloc is not enabled for local mounts. 77 Localalloc is not enabled for local mounts.
78localflocks This disables cluster aware flock. 78localflocks This disables cluster aware flock.
79inode64 Indicates that Ocfs2 is allowed to create inodes at
80 any location in the filesystem, including those which
81 will result in inode numbers occupying more than 32
82 bits of significance.
83user_xattr (*) Enables Extended User Attributes.
84nouser_xattr Disables Extended User Attributes.
diff --git a/Documentation/i2c/busses/i2c-viapro b/Documentation/i2c/busses/i2c-viapro
index 1405fb69984c..22efedf60c87 100644
--- a/Documentation/i2c/busses/i2c-viapro
+++ b/Documentation/i2c/busses/i2c-viapro
@@ -16,6 +16,9 @@ Supported adapters:
16 * VIA Technologies, Inc. CX700 16 * VIA Technologies, Inc. CX700
17 Datasheet: available on request and under NDA from VIA 17 Datasheet: available on request and under NDA from VIA
18 18
19 * VIA Technologies, Inc. VX800/VX820
20 Datasheet: available on http://linux.via.com.tw
21
19Authors: 22Authors:
20 Kyösti Mälkki <kmalkki@cc.hut.fi>, 23 Kyösti Mälkki <kmalkki@cc.hut.fi>,
21 Mark D. Studebaker <mdsxyz123@yahoo.com>, 24 Mark D. Studebaker <mdsxyz123@yahoo.com>,
@@ -49,6 +52,7 @@ Your lspci -n listing must show one of these :
49 device 1106:3372 (VT8237S) 52 device 1106:3372 (VT8237S)
50 device 1106:3287 (VT8251) 53 device 1106:3287 (VT8251)
51 device 1106:8324 (CX700) 54 device 1106:8324 (CX700)
55 device 1106:8353 (VX800/VX820)
52 56
53If none of these show up, you should look in the BIOS for settings like 57If none of these show up, you should look in the BIOS for settings like
54enable ACPI / SMBus or even USB. 58enable ACPI / SMBus or even USB.
@@ -57,5 +61,5 @@ Except for the oldest chips (VT82C596A/B, VT82C686A and most probably
57VT8231), this driver supports I2C block transactions. Such transactions 61VT8231), this driver supports I2C block transactions. Such transactions
58are mainly useful to read from and write to EEPROMs. 62are mainly useful to read from and write to EEPROMs.
59 63
60The CX700 additionally appears to support SMBus PEC, although this driver 64The CX700/VX800/VX820 additionally appears to support SMBus PEC, although
61doesn't implement it yet. 65this driver doesn't implement it yet.
diff --git a/Documentation/i2c/dev-interface b/Documentation/i2c/dev-interface
index 9dd79123ddd9..3e742ba25536 100644
--- a/Documentation/i2c/dev-interface
+++ b/Documentation/i2c/dev-interface
@@ -4,6 +4,10 @@ the /dev interface. You need to load module i2c-dev for this.
4 4
5Each registered i2c adapter gets a number, counting from 0. You can 5Each registered i2c adapter gets a number, counting from 0. You can
6examine /sys/class/i2c-dev/ to see what number corresponds to which adapter. 6examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
7Alternatively, you can run "i2cdetect -l" to obtain a formated list of all
8i2c adapters present on your system at a given time. i2cdetect is part of
9the i2c-tools package.
10
7I2C device files are character device files with major device number 89 11I2C device files are character device files with major device number 89
8and a minor device number corresponding to the number assigned as 12and a minor device number corresponding to the number assigned as
9explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 13explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
@@ -17,30 +21,34 @@ So let's say you want to access an i2c adapter from a C program. The
17first thing to do is "#include <linux/i2c-dev.h>". Please note that 21first thing to do is "#include <linux/i2c-dev.h>". Please note that
18there are two files named "i2c-dev.h" out there, one is distributed 22there are two files named "i2c-dev.h" out there, one is distributed
19with the Linux kernel and is meant to be included from kernel 23with the Linux kernel and is meant to be included from kernel
20driver code, the other one is distributed with lm_sensors and is 24driver code, the other one is distributed with i2c-tools and is
21meant to be included from user-space programs. You obviously want 25meant to be included from user-space programs. You obviously want
22the second one here. 26the second one here.
23 27
24Now, you have to decide which adapter you want to access. You should 28Now, you have to decide which adapter you want to access. You should
25inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned 29inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
26somewhat dynamically, so you can not even assume /dev/i2c-0 is the 30Adapter numbers are assigned somewhat dynamically, so you can not
27first adapter. 31assume much about them. They can even change from one boot to the next.
28 32
29Next thing, open the device file, as follows: 33Next thing, open the device file, as follows:
34
30 int file; 35 int file;
31 int adapter_nr = 2; /* probably dynamically determined */ 36 int adapter_nr = 2; /* probably dynamically determined */
32 char filename[20]; 37 char filename[20];
33 38
34 sprintf(filename,"/dev/i2c-%d",adapter_nr); 39 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
35 if ((file = open(filename,O_RDWR)) < 0) { 40 file = open(filename, O_RDWR);
41 if (file < 0) {
36 /* ERROR HANDLING; you can check errno to see what went wrong */ 42 /* ERROR HANDLING; you can check errno to see what went wrong */
37 exit(1); 43 exit(1);
38 } 44 }
39 45
40When you have opened the device, you must specify with what device 46When you have opened the device, you must specify with what device
41address you want to communicate: 47address you want to communicate:
48
42 int addr = 0x40; /* The I2C address */ 49 int addr = 0x40; /* The I2C address */
43 if (ioctl(file,I2C_SLAVE,addr) < 0) { 50
51 if (ioctl(file, I2C_SLAVE, addr) < 0) {
44 /* ERROR HANDLING; you can check errno to see what went wrong */ 52 /* ERROR HANDLING; you can check errno to see what went wrong */
45 exit(1); 53 exit(1);
46 } 54 }
@@ -48,31 +56,41 @@ address you want to communicate:
48Well, you are all set up now. You can now use SMBus commands or plain 56Well, you are all set up now. You can now use SMBus commands or plain
49I2C to communicate with your device. SMBus commands are preferred if 57I2C to communicate with your device. SMBus commands are preferred if
50the device supports them. Both are illustrated below. 58the device supports them. Both are illustrated below.
59
51 __u8 register = 0x10; /* Device register to access */ 60 __u8 register = 0x10; /* Device register to access */
52 __s32 res; 61 __s32 res;
53 char buf[10]; 62 char buf[10];
63
54 /* Using SMBus commands */ 64 /* Using SMBus commands */
55 res = i2c_smbus_read_word_data(file,register); 65 res = i2c_smbus_read_word_data(file, register);
56 if (res < 0) { 66 if (res < 0) {
57 /* ERROR HANDLING: i2c transaction failed */ 67 /* ERROR HANDLING: i2c transaction failed */
58 } else { 68 } else {
59 /* res contains the read word */ 69 /* res contains the read word */
60 } 70 }
71
61 /* Using I2C Write, equivalent of 72 /* Using I2C Write, equivalent of
62 i2c_smbus_write_word_data(file,register,0x6543) */ 73 i2c_smbus_write_word_data(file, register, 0x6543) */
63 buf[0] = register; 74 buf[0] = register;
64 buf[1] = 0x43; 75 buf[1] = 0x43;
65 buf[2] = 0x65; 76 buf[2] = 0x65;
66 if ( write(file,buf,3) != 3) { 77 if (write(file, buf, 3) ! =3) {
67 /* ERROR HANDLING: i2c transaction failed */ 78 /* ERROR HANDLING: i2c transaction failed */
68 } 79 }
80
69 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ 81 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
70 if (read(file,buf,1) != 1) { 82 if (read(file, buf, 1) != 1) {
71 /* ERROR HANDLING: i2c transaction failed */ 83 /* ERROR HANDLING: i2c transaction failed */
72 } else { 84 } else {
73 /* buf[0] contains the read byte */ 85 /* buf[0] contains the read byte */
74 } 86 }
75 87
88Note that only a subset of the I2C and SMBus protocols can be achieved by
89the means of read() and write() calls. In particular, so-called combined
90transactions (mixing read and write messages in the same transaction)
91aren't supported. For this reason, this interface is almost never used by
92user-space programs.
93
76IMPORTANT: because of the use of inline functions, you *have* to use 94IMPORTANT: because of the use of inline functions, you *have* to use
77'-O' or some variation when you compile your program! 95'-O' or some variation when you compile your program!
78 96
@@ -80,31 +98,29 @@ IMPORTANT: because of the use of inline functions, you *have* to use
80Full interface description 98Full interface description
81========================== 99==========================
82 100
83The following IOCTLs are defined and fully supported 101The following IOCTLs are defined:
84(see also i2c-dev.h):
85 102
86ioctl(file,I2C_SLAVE,long addr) 103ioctl(file, I2C_SLAVE, long addr)
87 Change slave address. The address is passed in the 7 lower bits of the 104 Change slave address. The address is passed in the 7 lower bits of the
88 argument (except for 10 bit addresses, passed in the 10 lower bits in this 105 argument (except for 10 bit addresses, passed in the 10 lower bits in this
89 case). 106 case).
90 107
91ioctl(file,I2C_TENBIT,long select) 108ioctl(file, I2C_TENBIT, long select)
92 Selects ten bit addresses if select not equals 0, selects normal 7 bit 109 Selects ten bit addresses if select not equals 0, selects normal 7 bit
93 addresses if select equals 0. Default 0. This request is only valid 110 addresses if select equals 0. Default 0. This request is only valid
94 if the adapter has I2C_FUNC_10BIT_ADDR. 111 if the adapter has I2C_FUNC_10BIT_ADDR.
95 112
96ioctl(file,I2C_PEC,long select) 113ioctl(file, I2C_PEC, long select)
97 Selects SMBus PEC (packet error checking) generation and verification 114 Selects SMBus PEC (packet error checking) generation and verification
98 if select not equals 0, disables if select equals 0. Default 0. 115 if select not equals 0, disables if select equals 0. Default 0.
99 Used only for SMBus transactions. This request only has an effect if the 116 Used only for SMBus transactions. This request only has an effect if the
100 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just 117 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
101 doesn't have any effect. 118 doesn't have any effect.
102 119
103ioctl(file,I2C_FUNCS,unsigned long *funcs) 120ioctl(file, I2C_FUNCS, unsigned long *funcs)
104 Gets the adapter functionality and puts it in *funcs. 121 Gets the adapter functionality and puts it in *funcs.
105 122
106ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset) 123ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
107
108 Do combined read/write transaction without stop in between. 124 Do combined read/write transaction without stop in between.
109 Only valid if the adapter has I2C_FUNC_I2C. The argument is 125 Only valid if the adapter has I2C_FUNC_I2C. The argument is
110 a pointer to a 126 a pointer to a
@@ -120,10 +136,9 @@ ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset)
120 The slave address and whether to use ten bit address mode has to be 136 The slave address and whether to use ten bit address mode has to be
121 set in each message, overriding the values set with the above ioctl's. 137 set in each message, overriding the values set with the above ioctl's.
122 138
123 139ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
124Other values are NOT supported at this moment, except for I2C_SMBUS, 140 Not meant to be called directly; instead, use the access functions
125which you should never directly call; instead, use the access functions 141 below.
126below.
127 142
128You can do plain i2c transactions by using read(2) and write(2) calls. 143You can do plain i2c transactions by using read(2) and write(2) calls.
129You do not need to pass the address byte; instead, set it through 144You do not need to pass the address byte; instead, set it through
@@ -148,7 +163,52 @@ what happened. The 'write' transactions return 0 on success; the
148returns the number of values read. The block buffers need not be longer 163returns the number of values read. The block buffers need not be longer
149than 32 bytes. 164than 32 bytes.
150 165
151The above functions are all macros, that resolve to calls to the 166The above functions are all inline functions, that resolve to calls to
152i2c_smbus_access function, that on its turn calls a specific ioctl 167the i2c_smbus_access function, that on its turn calls a specific ioctl
153with the data in a specific format. Read the source code if you 168with the data in a specific format. Read the source code if you
154want to know what happens behind the screens. 169want to know what happens behind the screens.
170
171
172Implementation details
173======================
174
175For the interested, here's the code flow which happens inside the kernel
176when you use the /dev interface to I2C:
177
1781* Your program opens /dev/i2c-N and calls ioctl() on it, as described in
179section "C example" above.
180
1812* These open() and ioctl() calls are handled by the i2c-dev kernel
182driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(),
183respectively. You can think of i2c-dev as a generic I2C chip driver
184that can be programmed from user-space.
185
1863* Some ioctl() calls are for administrative tasks and are handled by
187i2c-dev directly. Examples include I2C_SLAVE (set the address of the
188device you want to access) and I2C_PEC (enable or disable SMBus error
189checking on future transactions.)
190
1914* Other ioctl() calls are converted to in-kernel function calls by
192i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter
193functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which
194performs an SMBus transaction using i2c-core.c:i2c_smbus_xfer().
195
196The i2c-dev driver is responsible for checking all the parameters that
197come from user-space for validity. After this point, there is no
198difference between these calls that came from user-space through i2c-dev
199and calls that would have been performed by kernel I2C chip drivers
200directly. This means that I2C bus drivers don't need to implement
201anything special to support access from user-space.
202
2035* These i2c-core.c/i2c.h functions are wrappers to the actual
204implementation of your I2C bus driver. Each adapter must declare
205callback functions implementing these standard calls.
206i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(),
207while i2c-core.c:i2c_smbus_xfer() calls either
208adapter.algo->smbus_xfer() if it is implemented, or if not,
209i2c-core.c:i2c_smbus_xfer_emulated() which in turn calls
210i2c_adapter.algo->master_xfer().
211
212After your I2C bus driver has processed these requests, execution runs
213up the call chain, with almost no processing done, except by i2c-dev to
214package the returned data, if any, in suitable format for the ioctl.
diff --git a/Documentation/i2c/smbus-protocol b/Documentation/i2c/smbus-protocol
index 24bfb65da17d..9df47441f0e7 100644
--- a/Documentation/i2c/smbus-protocol
+++ b/Documentation/i2c/smbus-protocol
@@ -109,8 +109,8 @@ specified through the Comm byte.
109S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P 109S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P
110 110
111 111
112SMBus Process Call 112SMBus Process Call: i2c_smbus_process_call()
113================== 113=============================================
114 114
115This command selects a device register (through the Comm byte), sends 115This command selects a device register (through the Comm byte), sends
11616 bits of data to it, and reads 16 bits of data in return. 11616 bits of data to it, and reads 16 bits of data in return.
diff --git a/Documentation/i2c/writing-clients b/Documentation/i2c/writing-clients
index 6b61b3a2e90b..d73ee117a8ca 100644
--- a/Documentation/i2c/writing-clients
+++ b/Documentation/i2c/writing-clients
@@ -606,6 +606,8 @@ SMBus communication
606 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command); 606 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
607 extern s32 i2c_smbus_write_word_data(struct i2c_client * client, 607 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
608 u8 command, u16 value); 608 u8 command, u16 value);
609 extern s32 i2c_smbus_process_call(struct i2c_client *client,
610 u8 command, u16 value);
609 extern s32 i2c_smbus_read_block_data(struct i2c_client * client, 611 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
610 u8 command, u8 *values); 612 u8 command, u8 *values);
611 extern s32 i2c_smbus_write_block_data(struct i2c_client * client, 613 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
@@ -621,8 +623,6 @@ These ones were removed from i2c-core because they had no users, but could
621be added back later if needed: 623be added back later if needed:
622 624
623 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value); 625 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
624 extern s32 i2c_smbus_process_call(struct i2c_client * client,
625 u8 command, u16 value);
626 extern s32 i2c_smbus_block_process_call(struct i2c_client *client, 626 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
627 u8 command, u8 length, 627 u8 command, u8 length,
628 u8 *values) 628 u8 *values)
diff --git a/arch/arm/mach-omap1/board-h3.c b/arch/arm/mach-omap1/board-h3.c
index 2ced6d9984d2..adfcd7b51393 100644
--- a/arch/arm/mach-omap1/board-h3.c
+++ b/arch/arm/mach-omap1/board-h3.c
@@ -476,6 +476,10 @@ static struct i2c_board_info __initdata h3_i2c_board_info[] = {
476 I2C_BOARD_INFO("tps65013", 0x48), 476 I2C_BOARD_INFO("tps65013", 0x48),
477 /* .irq = OMAP_GPIO_IRQ(??), */ 477 /* .irq = OMAP_GPIO_IRQ(??), */
478 }, 478 },
479 {
480 I2C_BOARD_INFO("isp1301_omap", 0x2d),
481 .irq = OMAP_GPIO_IRQ(14),
482 },
479}; 483};
480 484
481static struct omap_gpio_switch h3_gpio_switches[] __initdata = { 485static struct omap_gpio_switch h3_gpio_switches[] __initdata = {
diff --git a/arch/arm/mach-omap2/board-h4.c b/arch/arm/mach-omap2/board-h4.c
index d4e3b6fc4705..2fef2c845083 100644
--- a/arch/arm/mach-omap2/board-h4.c
+++ b/arch/arm/mach-omap2/board-h4.c
@@ -18,6 +18,7 @@
18#include <linux/mtd/partitions.h> 18#include <linux/mtd/partitions.h>
19#include <linux/delay.h> 19#include <linux/delay.h>
20#include <linux/workqueue.h> 20#include <linux/workqueue.h>
21#include <linux/i2c.h>
21#include <linux/input.h> 22#include <linux/input.h>
22#include <linux/err.h> 23#include <linux/err.h>
23#include <linux/clk.h> 24#include <linux/clk.h>
@@ -391,6 +392,13 @@ static struct omap_board_config_kernel h4_config[] = {
391 { OMAP_TAG_LCD, &h4_lcd_config }, 392 { OMAP_TAG_LCD, &h4_lcd_config },
392}; 393};
393 394
395static struct i2c_board_info __initdata h4_i2c_board_info[] = {
396 {
397 I2C_BOARD_INFO("isp1301_omap", 0x2d),
398 .irq = OMAP_GPIO_IRQ(125),
399 },
400};
401
394static void __init omap_h4_init(void) 402static void __init omap_h4_init(void)
395{ 403{
396 /* 404 /*
@@ -411,6 +419,9 @@ static void __init omap_h4_init(void)
411 } 419 }
412#endif 420#endif
413 421
422 i2c_register_board_info(1, h4_i2c_board_info,
423 ARRAY_SIZE(h4_i2c_board_info));
424
414 platform_add_devices(h4_devices, ARRAY_SIZE(h4_devices)); 425 platform_add_devices(h4_devices, ARRAY_SIZE(h4_devices));
415 omap_board_config = h4_config; 426 omap_board_config = h4_config;
416 omap_board_config_size = ARRAY_SIZE(h4_config); 427 omap_board_config_size = ARRAY_SIZE(h4_config);
diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c
index 05191bbc68b8..0a23b5795b25 100644
--- a/arch/x86/kernel/rtc.c
+++ b/arch/x86/kernel/rtc.c
@@ -223,11 +223,25 @@ static struct platform_device rtc_device = {
223static __init int add_rtc_cmos(void) 223static __init int add_rtc_cmos(void)
224{ 224{
225#ifdef CONFIG_PNP 225#ifdef CONFIG_PNP
226 if (!pnp_platform_devices) 226 static const char *ids[] __initconst =
227 platform_device_register(&rtc_device); 227 { "PNP0b00", "PNP0b01", "PNP0b02", };
228#else 228 struct pnp_dev *dev;
229 struct pnp_id *id;
230 int i;
231
232 pnp_for_each_dev(dev) {
233 for (id = dev->id; id; id = id->next) {
234 for (i = 0; i < ARRAY_SIZE(ids); i++) {
235 if (compare_pnp_id(id, ids[i]) != 0)
236 return 0;
237 }
238 }
239 }
240#endif
241
229 platform_device_register(&rtc_device); 242 platform_device_register(&rtc_device);
230#endif /* CONFIG_PNP */ 243 dev_info(&rtc_device.dev,
244 "registered platform RTC device (no PNP device found)\n");
231 return 0; 245 return 0;
232} 246}
233device_initcall(add_rtc_cmos); 247device_initcall(add_rtc_cmos);
diff --git a/drivers/acpi/glue.c b/drivers/acpi/glue.c
index 3c578ef78c48..24649ada08df 100644
--- a/drivers/acpi/glue.c
+++ b/drivers/acpi/glue.c
@@ -260,115 +260,3 @@ static int __init init_acpi_device_notify(void)
260} 260}
261 261
262arch_initcall(init_acpi_device_notify); 262arch_initcall(init_acpi_device_notify);
263
264
265#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
266
267#ifdef CONFIG_PM
268static u32 rtc_handler(void *context)
269{
270 acpi_clear_event(ACPI_EVENT_RTC);
271 acpi_disable_event(ACPI_EVENT_RTC, 0);
272 return ACPI_INTERRUPT_HANDLED;
273}
274
275static inline void rtc_wake_setup(void)
276{
277 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
278 /*
279 * After the RTC handler is installed, the Fixed_RTC event should
280 * be disabled. Only when the RTC alarm is set will it be enabled.
281 */
282 acpi_clear_event(ACPI_EVENT_RTC);
283 acpi_disable_event(ACPI_EVENT_RTC, 0);
284}
285
286static void rtc_wake_on(struct device *dev)
287{
288 acpi_clear_event(ACPI_EVENT_RTC);
289 acpi_enable_event(ACPI_EVENT_RTC, 0);
290}
291
292static void rtc_wake_off(struct device *dev)
293{
294 acpi_disable_event(ACPI_EVENT_RTC, 0);
295}
296#else
297#define rtc_wake_setup() do{}while(0)
298#define rtc_wake_on NULL
299#define rtc_wake_off NULL
300#endif
301
302/* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
303 * its device node and pass extra config data. This helps its driver use
304 * capabilities that the now-obsolete mc146818 didn't have, and informs it
305 * that this board's RTC is wakeup-capable (per ACPI spec).
306 */
307#include <linux/mc146818rtc.h>
308
309static struct cmos_rtc_board_info rtc_info;
310
311
312/* PNP devices are registered in a subsys_initcall();
313 * ACPI specifies the PNP IDs to use.
314 */
315#include <linux/pnp.h>
316
317static int __init pnp_match(struct device *dev, void *data)
318{
319 static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
320 struct pnp_dev *pnp = to_pnp_dev(dev);
321 int i;
322
323 for (i = 0; i < ARRAY_SIZE(ids); i++) {
324 if (compare_pnp_id(pnp->id, ids[i]) != 0)
325 return 1;
326 }
327 return 0;
328}
329
330static struct device *__init get_rtc_dev(void)
331{
332 return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
333}
334
335static int __init acpi_rtc_init(void)
336{
337 struct device *dev = get_rtc_dev();
338
339 if (acpi_disabled)
340 return 0;
341
342 if (dev) {
343 rtc_wake_setup();
344 rtc_info.wake_on = rtc_wake_on;
345 rtc_info.wake_off = rtc_wake_off;
346
347 /* workaround bug in some ACPI tables */
348 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
349 DBG("bogus FADT month_alarm\n");
350 acpi_gbl_FADT.month_alarm = 0;
351 }
352
353 rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
354 rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
355 rtc_info.rtc_century = acpi_gbl_FADT.century;
356
357 /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
358 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
359 printk(PREFIX "RTC can wake from S4\n");
360
361
362 dev->platform_data = &rtc_info;
363
364 /* RTC always wakes from S1/S2/S3, and often S4/STD */
365 device_init_wakeup(dev, 1);
366
367 put_device(dev);
368 } else
369 DBG("RTC unavailable?\n");
370 return 0;
371}
372module_init(acpi_rtc_init);
373
374#endif
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index cacf89e65af4..da64108de775 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -17,6 +17,25 @@ config HID
17 tristate "Generic HID support" 17 tristate "Generic HID support"
18 depends on INPUT 18 depends on INPUT
19 default y 19 default y
20 select HID_A4TECH if !EMBEDDED
21 select HID_APPLE if !EMBEDDED
22 select HID_BELKIN if !EMBEDDED
23 select HID_BRIGHT if !EMBEDDED
24 select HID_CHERRY if !EMBEDDED
25 select HID_CHICONY if !EMBEDDED
26 select HID_CYPRESS if !EMBEDDED
27 select HID_DELL if !EMBEDDED
28 select HID_EZKEY if !EMBEDDED
29 select HID_GYRATION if !EMBEDDED
30 select HID_LOGITECH if !EMBEDDED
31 select HID_MICROSOFT if !EMBEDDED
32 select HID_MONTEREY if !EMBEDDED
33 select HID_PANTHERLORD if !EMBEDDED
34 select HID_PETALYNX if !EMBEDDED
35 select HID_SAMSUNG if !EMBEDDED
36 select HID_SONY if !EMBEDDED
37 select HID_SUNPLUS if !EMBEDDED
38
20 ---help--- 39 ---help---
21 A human interface device (HID) is a type of computer device that 40 A human interface device (HID) is a type of computer device that
22 interacts directly with and takes input from humans. The term "HID" 41 interacts directly with and takes input from humans. The term "HID"
@@ -67,4 +86,206 @@ config HIDRAW
67 86
68source "drivers/hid/usbhid/Kconfig" 87source "drivers/hid/usbhid/Kconfig"
69 88
89menu "Special HID drivers"
90 depends on HID
91
92config HID_COMPAT
93 bool "Load all HID drivers on hid core load"
94 default y
95 ---help---
96 Compatible option for older userspace. If you have system without udev
97 support of module loading through aliases and also old
98 module-init-tools which can't handle hid bus, choose Y here. Otherwise
99 say N. If you say N and your userspace is old enough, the only
100 functionality you lose is modules autoloading.
101
102 If unsure, say Y.
103
104config HID_A4TECH
105 tristate "A4 tech"
106 default m
107 depends on USB_HID
108 ---help---
109 Support for A4 tech X5 and WOP-35 / Trust 450L mice.
110
111config HID_APPLE
112 tristate "Apple"
113 default m
114 depends on (USB_HID || BT_HIDP)
115 ---help---
116 Support for some Apple devices which less or more break
117 HID specification.
118
119 Say Y here if you want support for the special keys (Fn, Numlock) on
120 Apple iBooks, PowerBooks, MacBooks, MacBook Pros and aluminum USB
121 keyboards.
122
123 If unsure, say M.
124
125config HID_BELKIN
126 tristate "Belkin"
127 default m
128 depends on USB_HID
129 ---help---
130 Support for Belkin Flip KVM and Wireless keyboard.
131
132config HID_BRIGHT
133 tristate "Bright"
134 default m
135 depends on USB_HID
136 ---help---
137 Support for Bright ABNT-2 keyboard.
138
139config HID_CHERRY
140 tristate "Cherry"
141 default m
142 depends on USB_HID
143 ---help---
144 Support for Cherry Cymotion.
145
146config HID_CHICONY
147 tristate "Chicony"
148 default m
149 depends on USB_HID
150 ---help---
151 Support for Chicony Tactical pad.
152
153config HID_CYPRESS
154 tristate "Cypress"
155 default m
156 depends on USB_HID
157 ---help---
158 Support for Cypress mouse and barcodes.
159
160config HID_DELL
161 tristate "Dell"
162 default m
163 depends on USB_HID
164 ---help---
165 Support for Dell W7658.
166
167config HID_EZKEY
168 tristate "Ezkey"
169 default m
170 depends on USB_HID
171 ---help---
172 Support for Ezkey mouse and barcodes.
173
174config HID_GYRATION
175 tristate "Gyration"
176 default m
177 depends on USB_HID
178 ---help---
179 Support for Gyration remote.
180
181config HID_LOGITECH
182 tristate "Logitech"
183 default m
184 depends on USB_HID
185 ---help---
186 Support for some Logitech devices which breaks less or more
187 HID specification.
188
189config LOGITECH_FF
190 bool "Logitech force feedback"
191 depends on HID_LOGITECH
192 select INPUT_FF_MEMLESS
193 help
194 Say Y here if you have one of these devices:
195 - Logitech WingMan Cordless RumblePad
196 - Logitech WingMan Cordless RumblePad 2
197 - Logitech WingMan Force 3D
198 - Logitech Formula Force EX
199 - Logitech MOMO Force wheel
200
201 and if you want to enable force feedback for them.
202 Note: if you say N here, this device will still be supported, but without
203 force feedback.
204
205config LOGIRUMBLEPAD2_FF
206 bool "Logitech Rumblepad 2 force feedback"
207 depends on HID_LOGITECH
208 select INPUT_FF_MEMLESS
209 help
210 Say Y here if you want to enable force feedback support for Logitech
211 Rumblepad 2 devices.
212
213config HID_MICROSOFT
214 tristate "Microsoft"
215 default m
216 depends on USB_HID
217 ---help---
218 Support for some Microsoft devices which breaks less or more
219 HID specification.
220
221config HID_MONTEREY
222 tristate "Monterey"
223 default m
224 depends on USB_HID
225 ---help---
226 Support for Monterey Genius KB29E.
227
228config HID_PANTHERLORD
229 tristate "Pantherlord devices support"
230 default m
231 depends on USB_HID
232 ---help---
233 Support for PantherLord/GreenAsia based device support.
234
235
236config PANTHERLORD_FF
237 bool "Pantherlord force feedback support"
238 depends on HID_PANTHERLORD
239 select INPUT_FF_MEMLESS
240 help
241 Say Y here if you have a PantherLord/GreenAsia based game controller
242 or adapter and want to enable force feedback support for it.
243
244config HID_PETALYNX
245 tristate "Petalynx"
246 default m
247 depends on USB_HID
248 ---help---
249 Support for Petalynx Maxter remote.
250
251config HID_SAMSUNG
252 tristate "Samsung"
253 default m
254 depends on USB_HID
255 ---help---
256 Support for Samsung IR remote.
257
258config HID_SONY
259 tristate "Sony"
260 default m
261 depends on USB_HID
262 ---help---
263 Support for Sony PS3 controller.
264
265config HID_SUNPLUS
266 tristate "Sunplus"
267 default m
268 depends on USB_HID
269 ---help---
270 Support for Sunplus WDesktop input device.
271
272config THRUSTMASTER_FF
273 tristate "ThrustMaster devices support"
274 default m
275 depends on USB_HID
276 select INPUT_FF_MEMLESS
277 help
278 Say Y here if you have a THRUSTMASTER FireStore Dual Power 2 or
279 a THRUSTMASTER Ferrari GT Rumble Force or Force Feedback Wheel.
280
281config ZEROPLUS_FF
282 tristate "Zeroplus based game controller support"
283 default m
284 depends on USB_HID
285 select INPUT_FF_MEMLESS
286 help
287 Say Y here if you have a Zeroplus based game controller.
288
289endmenu
290
70endif # HID_SUPPORT 291endif # HID_SUPPORT
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 275dc522c738..b09e43e7413e 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -1,13 +1,46 @@
1# 1#
2# Makefile for the HID driver 2# Makefile for the HID driver
3# 3#
4hid-objs := hid-core.o hid-input.o hid-input-quirks.o 4hid-objs := hid-core.o hid-input.o
5 5
6obj-$(CONFIG_HID) += hid.o 6obj-$(CONFIG_HID) += hid.o
7 7
8hid-$(CONFIG_HID_DEBUG) += hid-debug.o 8hid-$(CONFIG_HID_DEBUG) += hid-debug.o
9hid-$(CONFIG_HIDRAW) += hidraw.o 9hid-$(CONFIG_HIDRAW) += hidraw.o
10 10
11ifdef CONFIG_HID_COMPAT
12obj-m += hid-dummy.o
13endif
14
15hid-logitech-objs := hid-lg.o
16ifdef CONFIG_LOGITECH_FF
17 hid-logitech-objs += hid-lgff.o
18endif
19ifdef CONFIG_LOGIRUMBLEPAD2_FF
20 hid-logitech-objs += hid-lg2ff.o
21endif
22
23obj-$(CONFIG_HID_A4TECH) += hid-a4tech.o
24obj-$(CONFIG_HID_APPLE) += hid-apple.o
25obj-$(CONFIG_HID_BELKIN) += hid-belkin.o
26obj-$(CONFIG_HID_BRIGHT) += hid-bright.o
27obj-$(CONFIG_HID_CHERRY) += hid-cherry.o
28obj-$(CONFIG_HID_CHICONY) += hid-chicony.o
29obj-$(CONFIG_HID_CYPRESS) += hid-cypress.o
30obj-$(CONFIG_HID_DELL) += hid-dell.o
31obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o
32obj-$(CONFIG_HID_GYRATION) += hid-gyration.o
33obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
34obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o
35obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o
36obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o
37obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o
38obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o
39obj-$(CONFIG_HID_SONY) += hid-sony.o
40obj-$(CONFIG_HID_SUNPLUS) += hid-sunplus.o
41obj-$(CONFIG_THRUSTMASTER_FF) += hid-tmff.o
42obj-$(CONFIG_ZEROPLUS_FF) += hid-zpff.o
43
11obj-$(CONFIG_USB_HID) += usbhid/ 44obj-$(CONFIG_USB_HID) += usbhid/
12obj-$(CONFIG_USB_MOUSE) += usbhid/ 45obj-$(CONFIG_USB_MOUSE) += usbhid/
13obj-$(CONFIG_USB_KBD) += usbhid/ 46obj-$(CONFIG_USB_KBD) += usbhid/
diff --git a/drivers/hid/hid-a4tech.c b/drivers/hid/hid-a4tech.c
new file mode 100644
index 000000000000..ebca00e6c103
--- /dev/null
+++ b/drivers/hid/hid-a4tech.c
@@ -0,0 +1,162 @@
1/*
2 * HID driver for some a4tech "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define A4_2WHEEL_MOUSE_HACK_7 0x01
27#define A4_2WHEEL_MOUSE_HACK_B8 0x02
28
29struct a4tech_sc {
30 unsigned long quirks;
31 unsigned int hw_wheel;
32 __s32 delayed_value;
33};
34
35static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi,
36 struct hid_field *field, struct hid_usage *usage,
37 unsigned long **bit, int *max)
38{
39 struct a4tech_sc *a4 = hid_get_drvdata(hdev);
40
41 if (usage->type == EV_REL && usage->code == REL_WHEEL)
42 set_bit(REL_HWHEEL, *bit);
43
44 if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007)
45 return -1;
46
47 return 0;
48}
49
50static int a4_event(struct hid_device *hdev, struct hid_field *field,
51 struct hid_usage *usage, __s32 value)
52{
53 struct a4tech_sc *a4 = hid_get_drvdata(hdev);
54 struct input_dev *input;
55
56 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
57 !usage->type)
58 return 0;
59
60 input = field->hidinput->input;
61
62 if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) {
63 if (usage->type == EV_REL && usage->code == REL_WHEEL) {
64 a4->delayed_value = value;
65 return 1;
66 }
67
68 if (usage->hid == 0x000100b8) {
69 input_event(input, EV_REL, value ? REL_HWHEEL :
70 REL_WHEEL, a4->delayed_value);
71 return 1;
72 }
73 }
74
75 if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) {
76 a4->hw_wheel = !!value;
77 return 1;
78 }
79
80 if (usage->code == REL_WHEEL && a4->hw_wheel) {
81 input_event(input, usage->type, REL_HWHEEL, value);
82 return 1;
83 }
84
85 return 0;
86}
87
88static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)
89{
90 struct a4tech_sc *a4;
91 int ret;
92
93 a4 = kzalloc(sizeof(*a4), GFP_KERNEL);
94 if (a4 == NULL) {
95 dev_err(&hdev->dev, "can't alloc device descriptor\n");
96 ret = -ENOMEM;
97 goto err_free;
98 }
99
100 a4->quirks = id->driver_data;
101
102 hid_set_drvdata(hdev, a4);
103
104 ret = hid_parse(hdev);
105 if (ret) {
106 dev_err(&hdev->dev, "parse failed\n");
107 goto err_free;
108 }
109
110 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
111 if (ret) {
112 dev_err(&hdev->dev, "hw start failed\n");
113 goto err_free;
114 }
115
116 return 0;
117err_free:
118 kfree(a4);
119 return ret;
120}
121
122static void a4_remove(struct hid_device *hdev)
123{
124 struct a4tech_sc *a4 = hid_get_drvdata(hdev);
125
126 hid_hw_stop(hdev);
127 kfree(a4);
128}
129
130static const struct hid_device_id a4_devices[] = {
131 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU),
132 .driver_data = A4_2WHEEL_MOUSE_HACK_7 },
133 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D),
134 .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
135 { }
136};
137MODULE_DEVICE_TABLE(hid, a4_devices);
138
139static struct hid_driver a4_driver = {
140 .name = "a4tech",
141 .id_table = a4_devices,
142 .input_mapped = a4_input_mapped,
143 .event = a4_event,
144 .probe = a4_probe,
145 .remove = a4_remove,
146};
147
148static int a4_init(void)
149{
150 return hid_register_driver(&a4_driver);
151}
152
153static void a4_exit(void)
154{
155 hid_unregister_driver(&a4_driver);
156}
157
158module_init(a4_init);
159module_exit(a4_exit);
160MODULE_LICENSE("GPL");
161
162HID_COMPAT_LOAD_DRIVER(a4tech);
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
new file mode 100644
index 000000000000..fd7f896b34f7
--- /dev/null
+++ b/drivers/hid/hid-apple.c
@@ -0,0 +1,484 @@
1/*
2 * USB HID quirks support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/usb.h>
23
24#include "hid-ids.h"
25
26#define APPLE_RDESC_JIS 0x0001
27#define APPLE_IGNORE_MOUSE 0x0002
28#define APPLE_HAS_FN 0x0004
29#define APPLE_HIDDEV 0x0008
30#define APPLE_ISO_KEYBOARD 0x0010
31#define APPLE_MIGHTYMOUSE 0x0020
32#define APPLE_INVERT_HWHEEL 0x0040
33#define APPLE_IGNORE_HIDINPUT 0x0080
34#define APPLE_NUMLOCK_EMULATION 0x0100
35
36#define APPLE_FLAG_FKEY 0x01
37
38static unsigned int fnmode = 1;
39module_param(fnmode, uint, 0644);
40MODULE_PARM_DESC(fnmode, "Mode of fn key on Apple keyboards (0 = disabled, "
41 "[1] = fkeyslast, 2 = fkeysfirst)");
42
43struct apple_sc {
44 unsigned long quirks;
45 unsigned int fn_on;
46 DECLARE_BITMAP(pressed_fn, KEY_CNT);
47 DECLARE_BITMAP(pressed_numlock, KEY_CNT);
48};
49
50struct apple_key_translation {
51 u16 from;
52 u16 to;
53 u8 flags;
54};
55
56static struct apple_key_translation apple_fn_keys[] = {
57 { KEY_BACKSPACE, KEY_DELETE },
58 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
59 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
60 { KEY_F3, KEY_FN_F5, APPLE_FLAG_FKEY }, /* Exposé */
61 { KEY_F4, KEY_FN_F4, APPLE_FLAG_FKEY }, /* Dashboard */
62 { KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
63 { KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
64 { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
65 { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
66 { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
67 { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
68 { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
69 { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
70 { KEY_UP, KEY_PAGEUP },
71 { KEY_DOWN, KEY_PAGEDOWN },
72 { KEY_LEFT, KEY_HOME },
73 { KEY_RIGHT, KEY_END },
74 { }
75};
76
77static struct apple_key_translation powerbook_fn_keys[] = {
78 { KEY_BACKSPACE, KEY_DELETE },
79 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
80 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
81 { KEY_F3, KEY_MUTE, APPLE_FLAG_FKEY },
82 { KEY_F4, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
83 { KEY_F5, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
84 { KEY_F6, KEY_NUMLOCK, APPLE_FLAG_FKEY },
85 { KEY_F7, KEY_SWITCHVIDEOMODE, APPLE_FLAG_FKEY },
86 { KEY_F8, KEY_KBDILLUMTOGGLE, APPLE_FLAG_FKEY },
87 { KEY_F9, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
88 { KEY_F10, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
89 { KEY_UP, KEY_PAGEUP },
90 { KEY_DOWN, KEY_PAGEDOWN },
91 { KEY_LEFT, KEY_HOME },
92 { KEY_RIGHT, KEY_END },
93 { }
94};
95
96static struct apple_key_translation powerbook_numlock_keys[] = {
97 { KEY_J, KEY_KP1 },
98 { KEY_K, KEY_KP2 },
99 { KEY_L, KEY_KP3 },
100 { KEY_U, KEY_KP4 },
101 { KEY_I, KEY_KP5 },
102 { KEY_O, KEY_KP6 },
103 { KEY_7, KEY_KP7 },
104 { KEY_8, KEY_KP8 },
105 { KEY_9, KEY_KP9 },
106 { KEY_M, KEY_KP0 },
107 { KEY_DOT, KEY_KPDOT },
108 { KEY_SLASH, KEY_KPPLUS },
109 { KEY_SEMICOLON, KEY_KPMINUS },
110 { KEY_P, KEY_KPASTERISK },
111 { KEY_MINUS, KEY_KPEQUAL },
112 { KEY_0, KEY_KPSLASH },
113 { KEY_F6, KEY_NUMLOCK },
114 { KEY_KPENTER, KEY_KPENTER },
115 { KEY_BACKSPACE, KEY_BACKSPACE },
116 { }
117};
118
119static struct apple_key_translation apple_iso_keyboard[] = {
120 { KEY_GRAVE, KEY_102ND },
121 { KEY_102ND, KEY_GRAVE },
122 { }
123};
124
125static struct apple_key_translation *apple_find_translation(
126 struct apple_key_translation *table, u16 from)
127{
128 struct apple_key_translation *trans;
129
130 /* Look for the translation */
131 for (trans = table; trans->from; trans++)
132 if (trans->from == from)
133 return trans;
134
135 return NULL;
136}
137
138static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
139 struct hid_usage *usage, __s32 value)
140{
141 struct apple_sc *asc = hid_get_drvdata(hid);
142 struct apple_key_translation *trans;
143
144 if (usage->code == KEY_FN) {
145 asc->fn_on = !!value;
146 input_event(input, usage->type, usage->code, value);
147 return 1;
148 }
149
150 if (fnmode) {
151 int do_translate;
152
153 trans = apple_find_translation((hid->product < 0x220 ||
154 hid->product >= 0x300) ?
155 powerbook_fn_keys : apple_fn_keys,
156 usage->code);
157 if (trans) {
158 if (test_bit(usage->code, asc->pressed_fn))
159 do_translate = 1;
160 else if (trans->flags & APPLE_FLAG_FKEY)
161 do_translate = (fnmode == 2 && asc->fn_on) ||
162 (fnmode == 1 && !asc->fn_on);
163 else
164 do_translate = asc->fn_on;
165
166 if (do_translate) {
167 if (value)
168 set_bit(usage->code, asc->pressed_fn);
169 else
170 clear_bit(usage->code, asc->pressed_fn);
171
172 input_event(input, usage->type, trans->to,
173 value);
174
175 return 1;
176 }
177 }
178
179 if (asc->quirks & APPLE_NUMLOCK_EMULATION &&
180 (test_bit(usage->code, asc->pressed_numlock) ||
181 test_bit(LED_NUML, input->led))) {
182 trans = apple_find_translation(powerbook_numlock_keys,
183 usage->code);
184
185 if (trans) {
186 if (value)
187 set_bit(usage->code,
188 asc->pressed_numlock);
189 else
190 clear_bit(usage->code,
191 asc->pressed_numlock);
192
193 input_event(input, usage->type, trans->to,
194 value);
195 }
196
197 return 1;
198 }
199 }
200
201 if (asc->quirks & APPLE_ISO_KEYBOARD) {
202 trans = apple_find_translation(apple_iso_keyboard, usage->code);
203 if (trans) {
204 input_event(input, usage->type, trans->to, value);
205 return 1;
206 }
207 }
208
209 return 0;
210}
211
212static int apple_event(struct hid_device *hdev, struct hid_field *field,
213 struct hid_usage *usage, __s32 value)
214{
215 struct apple_sc *asc = hid_get_drvdata(hdev);
216
217 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
218 !usage->type)
219 return 0;
220
221 if ((asc->quirks & APPLE_INVERT_HWHEEL) &&
222 usage->code == REL_HWHEEL) {
223 input_event(field->hidinput->input, usage->type, usage->code,
224 -value);
225 return 1;
226 }
227
228 if ((asc->quirks & APPLE_HAS_FN) &&
229 hidinput_apple_event(hdev, field->hidinput->input,
230 usage, value))
231 return 1;
232
233
234 return 0;
235}
236
237/*
238 * MacBook JIS keyboard has wrong logical maximum
239 */
240static void apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
241 unsigned int rsize)
242{
243 struct apple_sc *asc = hid_get_drvdata(hdev);
244
245 if ((asc->quirks & APPLE_RDESC_JIS) && rsize >= 60 &&
246 rdesc[53] == 0x65 && rdesc[59] == 0x65) {
247 dev_info(&hdev->dev, "fixing up MacBook JIS keyboard report "
248 "descriptor\n");
249 rdesc[53] = rdesc[59] = 0xe7;
250 }
251}
252
253static void apple_setup_input(struct input_dev *input)
254{
255 struct apple_key_translation *trans;
256
257 set_bit(KEY_NUMLOCK, input->keybit);
258
259 /* Enable all needed keys */
260 for (trans = apple_fn_keys; trans->from; trans++)
261 set_bit(trans->to, input->keybit);
262
263 for (trans = powerbook_fn_keys; trans->from; trans++)
264 set_bit(trans->to, input->keybit);
265
266 for (trans = powerbook_numlock_keys; trans->from; trans++)
267 set_bit(trans->to, input->keybit);
268
269 for (trans = apple_iso_keyboard; trans->from; trans++)
270 set_bit(trans->to, input->keybit);
271}
272
273static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
274 struct hid_field *field, struct hid_usage *usage,
275 unsigned long **bit, int *max)
276{
277 if (usage->hid == (HID_UP_CUSTOM | 0x0003)) {
278 /* The fn key on Apple USB keyboards */
279 set_bit(EV_REP, hi->input->evbit);
280 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_FN);
281 apple_setup_input(hi->input);
282 return 1;
283 }
284
285 /* we want the hid layer to go through standard path (set and ignore) */
286 return 0;
287}
288
289static int apple_input_mapped(struct hid_device *hdev, struct hid_input *hi,
290 struct hid_field *field, struct hid_usage *usage,
291 unsigned long **bit, int *max)
292{
293 struct apple_sc *asc = hid_get_drvdata(hdev);
294
295 if (asc->quirks & APPLE_MIGHTYMOUSE) {
296 if (usage->hid == HID_GD_Z)
297 hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
298 else if (usage->code == BTN_1)
299 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_2);
300 else if (usage->code == BTN_2)
301 hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_1);
302 }
303
304 return 0;
305}
306
307static int apple_probe(struct hid_device *hdev,
308 const struct hid_device_id *id)
309{
310 unsigned long quirks = id->driver_data;
311 struct apple_sc *asc;
312 unsigned int connect_mask = HID_CONNECT_DEFAULT;
313 int ret;
314
315 /* return something else or move to hid layer? device will reside
316 allocated */
317 if (id->bus == BUS_USB && (quirks & APPLE_IGNORE_MOUSE) &&
318 to_usb_interface(hdev->dev.parent)->cur_altsetting->
319 desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
320 return -ENODEV;
321
322 asc = kzalloc(sizeof(*asc), GFP_KERNEL);
323 if (asc == NULL) {
324 dev_err(&hdev->dev, "can't alloc apple descriptor\n");
325 return -ENOMEM;
326 }
327
328 asc->quirks = quirks;
329
330 hid_set_drvdata(hdev, asc);
331
332 ret = hid_parse(hdev);
333 if (ret) {
334 dev_err(&hdev->dev, "parse failed\n");
335 goto err_free;
336 }
337
338 if (quirks & APPLE_HIDDEV)
339 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
340 if (quirks & APPLE_IGNORE_HIDINPUT)
341 connect_mask &= ~HID_CONNECT_HIDINPUT;
342
343 ret = hid_hw_start(hdev, connect_mask);
344 if (ret) {
345 dev_err(&hdev->dev, "hw start failed\n");
346 goto err_free;
347 }
348
349 return 0;
350err_free:
351 kfree(asc);
352 return ret;
353}
354
355static void apple_remove(struct hid_device *hdev)
356{
357 hid_hw_stop(hdev);
358 kfree(hid_get_drvdata(hdev));
359}
360
361static const struct hid_device_id apple_devices[] = {
362 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL),
363 .driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
364 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4),
365 .driver_data = APPLE_HIDDEV | APPLE_IGNORE_HIDINPUT },
366 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
367 .driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
368
369 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI),
370 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
371 APPLE_IGNORE_MOUSE },
372 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO),
373 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
374 APPLE_IGNORE_MOUSE },
375 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI),
376 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
377 APPLE_IGNORE_MOUSE },
378 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO),
379 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
380 APPLE_IGNORE_MOUSE | APPLE_ISO_KEYBOARD },
381 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS),
382 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
383 APPLE_IGNORE_MOUSE },
384 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI),
385 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
386 APPLE_IGNORE_MOUSE },
387 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO),
388 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
389 APPLE_IGNORE_MOUSE | APPLE_ISO_KEYBOARD },
390 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS),
391 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
392 APPLE_IGNORE_MOUSE | APPLE_RDESC_JIS },
393 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI),
394 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
395 APPLE_IGNORE_MOUSE },
396 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO),
397 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
398 APPLE_IGNORE_MOUSE | APPLE_ISO_KEYBOARD },
399 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS),
400 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
401 APPLE_IGNORE_MOUSE | APPLE_RDESC_JIS},
402 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI),
403 .driver_data = APPLE_HAS_FN },
404 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO),
405 .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD },
406 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS),
407 .driver_data = APPLE_HAS_FN },
408 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI),
409 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
410 APPLE_IGNORE_MOUSE },
411 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO),
412 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
413 APPLE_IGNORE_MOUSE },
414 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS),
415 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
416 APPLE_IGNORE_MOUSE | APPLE_RDESC_JIS },
417 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI),
418 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
419 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO),
420 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
421 APPLE_ISO_KEYBOARD },
422 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS),
423 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
424 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
425 .driver_data = APPLE_HAS_FN | APPLE_IGNORE_MOUSE },
426 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
427 .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD |
428 APPLE_IGNORE_MOUSE },
429 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS),
430 .driver_data = APPLE_HAS_FN | APPLE_IGNORE_MOUSE | APPLE_RDESC_JIS },
431 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI),
432 .driver_data = APPLE_HAS_FN | APPLE_IGNORE_MOUSE },
433 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO),
434 .driver_data = APPLE_HAS_FN | APPLE_ISO_KEYBOARD |
435 APPLE_IGNORE_MOUSE },
436 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS),
437 .driver_data = APPLE_HAS_FN | APPLE_IGNORE_MOUSE | APPLE_RDESC_JIS },
438 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY),
439 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
440 APPLE_IGNORE_MOUSE },
441 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY),
442 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN |
443 APPLE_IGNORE_MOUSE },
444
445 /* Apple wireless Mighty Mouse */
446 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c),
447 .driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
448
449 { }
450};
451MODULE_DEVICE_TABLE(hid, apple_devices);
452
453static struct hid_driver apple_driver = {
454 .name = "apple",
455 .id_table = apple_devices,
456 .report_fixup = apple_report_fixup,
457 .probe = apple_probe,
458 .remove = apple_remove,
459 .event = apple_event,
460 .input_mapping = apple_input_mapping,
461 .input_mapped = apple_input_mapped,
462};
463
464static int apple_init(void)
465{
466 int ret;
467
468 ret = hid_register_driver(&apple_driver);
469 if (ret)
470 printk(KERN_ERR "can't register apple driver\n");
471
472 return ret;
473}
474
475static void apple_exit(void)
476{
477 hid_unregister_driver(&apple_driver);
478}
479
480module_init(apple_init);
481module_exit(apple_exit);
482MODULE_LICENSE("GPL");
483
484HID_COMPAT_LOAD_DRIVER(apple);
diff --git a/drivers/hid/hid-belkin.c b/drivers/hid/hid-belkin.c
new file mode 100644
index 000000000000..12c8a9ba6ed6
--- /dev/null
+++ b/drivers/hid/hid-belkin.c
@@ -0,0 +1,105 @@
1/*
2 * HID driver for some belkin "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25#define BELKIN_HIDDEV 0x01
26#define BELKIN_WKBD 0x02
27
28#define belkin_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
29 EV_KEY, (c))
30static int belkin_input_mapping(struct hid_device *hdev, struct hid_input *hi,
31 struct hid_field *field, struct hid_usage *usage,
32 unsigned long **bit, int *max)
33{
34 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
35
36 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER ||
37 !(quirks & BELKIN_WKBD))
38 return 0;
39
40 switch (usage->hid & HID_USAGE) {
41 case 0x03a: belkin_map_key_clear(KEY_SOUND); break;
42 case 0x03b: belkin_map_key_clear(KEY_CAMERA); break;
43 case 0x03c: belkin_map_key_clear(KEY_DOCUMENTS); break;
44 default:
45 return 0;
46 }
47 return 1;
48}
49
50static int belkin_probe(struct hid_device *hdev, const struct hid_device_id *id)
51{
52 unsigned long quirks = id->driver_data;
53 int ret;
54
55 hid_set_drvdata(hdev, (void *)quirks);
56
57 ret = hid_parse(hdev);
58 if (ret) {
59 dev_err(&hdev->dev, "parse failed\n");
60 goto err_free;
61 }
62
63 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
64 ((quirks & BELKIN_HIDDEV) ? HID_CONNECT_HIDDEV_FORCE : 0));
65 if (ret) {
66 dev_err(&hdev->dev, "hw start failed\n");
67 goto err_free;
68 }
69
70 return 0;
71err_free:
72 return ret;
73}
74
75static const struct hid_device_id belkin_devices[] = {
76 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM),
77 .driver_data = BELKIN_HIDDEV },
78 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD),
79 .driver_data = BELKIN_WKBD },
80 { }
81};
82MODULE_DEVICE_TABLE(hid, belkin_devices);
83
84static struct hid_driver belkin_driver = {
85 .name = "belkin",
86 .id_table = belkin_devices,
87 .input_mapping = belkin_input_mapping,
88 .probe = belkin_probe,
89};
90
91static int belkin_init(void)
92{
93 return hid_register_driver(&belkin_driver);
94}
95
96static void belkin_exit(void)
97{
98 hid_unregister_driver(&belkin_driver);
99}
100
101module_init(belkin_init);
102module_exit(belkin_exit);
103MODULE_LICENSE("GPL");
104
105HID_COMPAT_LOAD_DRIVER(belkin);
diff --git a/drivers/hid/hid-bright.c b/drivers/hid/hid-bright.c
new file mode 100644
index 000000000000..38517a117dfd
--- /dev/null
+++ b/drivers/hid/hid-bright.c
@@ -0,0 +1,71 @@
1/*
2 * HID driver for some bright "special" devices
3 *
4 * Copyright (c) 2008 Mauro Carvalho Chehab <mchehab@redhat.com>
5 *
6 * Based on hid-dell driver
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 */
15
16#include <linux/device.h>
17#include <linux/hid.h>
18#include <linux/module.h>
19
20#include "hid-ids.h"
21
22static int bright_probe(struct hid_device *hdev, const struct hid_device_id *id)
23{
24 int ret;
25
26 ret = hid_parse(hdev);
27 if (ret) {
28 dev_err(&hdev->dev, "parse failed\n");
29 goto err_free;
30 }
31
32 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
33 if (ret) {
34 dev_err(&hdev->dev, "hw start failed\n");
35 goto err_free;
36 }
37
38 usbhid_set_leds(hdev);
39
40 return 0;
41err_free:
42 return ret;
43}
44
45static const struct hid_device_id bright_devices[] = {
46 { HID_USB_DEVICE(USB_VENDOR_ID_BRIGHT, USB_DEVICE_ID_BRIGHT_ABNT2) },
47 { }
48};
49MODULE_DEVICE_TABLE(hid, bright_devices);
50
51static struct hid_driver bright_driver = {
52 .name = "bright",
53 .id_table = bright_devices,
54 .probe = bright_probe,
55};
56
57static int bright_init(void)
58{
59 return hid_register_driver(&bright_driver);
60}
61
62static void bright_exit(void)
63{
64 hid_unregister_driver(&bright_driver);
65}
66
67module_init(bright_init);
68module_exit(bright_exit);
69MODULE_LICENSE("GPL");
70
71HID_COMPAT_LOAD_DRIVER(bright);
diff --git a/drivers/hid/hid-cherry.c b/drivers/hid/hid-cherry.c
new file mode 100644
index 000000000000..b833b9769aba
--- /dev/null
+++ b/drivers/hid/hid-cherry.c
@@ -0,0 +1,87 @@
1/*
2 * HID driver for some cherry "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25/*
26 * Cherry Cymotion keyboard have an invalid HID report descriptor,
27 * that needs fixing before we can parse it.
28 */
29static void ch_report_fixup(struct hid_device *hdev, __u8 *rdesc,
30 unsigned int rsize)
31{
32 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
33 dev_info(&hdev->dev, "fixing up Cherry Cymotion report "
34 "descriptor\n");
35 rdesc[11] = rdesc[16] = 0xff;
36 rdesc[12] = rdesc[17] = 0x03;
37 }
38}
39
40#define ch_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
41 EV_KEY, (c))
42static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
43 struct hid_field *field, struct hid_usage *usage,
44 unsigned long **bit, int *max)
45{
46 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
47 return 0;
48
49 switch (usage->hid & HID_USAGE) {
50 case 0x301: ch_map_key_clear(KEY_PROG1); break;
51 case 0x302: ch_map_key_clear(KEY_PROG2); break;
52 case 0x303: ch_map_key_clear(KEY_PROG3); break;
53 default:
54 return 0;
55 }
56
57 return 1;
58}
59
60static const struct hid_device_id ch_devices[] = {
61 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
62 { }
63};
64MODULE_DEVICE_TABLE(hid, ch_devices);
65
66static struct hid_driver ch_driver = {
67 .name = "cherry",
68 .id_table = ch_devices,
69 .report_fixup = ch_report_fixup,
70 .input_mapping = ch_input_mapping,
71};
72
73static int ch_init(void)
74{
75 return hid_register_driver(&ch_driver);
76}
77
78static void ch_exit(void)
79{
80 hid_unregister_driver(&ch_driver);
81}
82
83module_init(ch_init);
84module_exit(ch_exit);
85MODULE_LICENSE("GPL");
86
87HID_COMPAT_LOAD_DRIVER(cherry);
diff --git a/drivers/hid/hid-chicony.c b/drivers/hid/hid-chicony.c
new file mode 100644
index 000000000000..a54d4096e0f7
--- /dev/null
+++ b/drivers/hid/hid-chicony.c
@@ -0,0 +1,80 @@
1/*
2 * HID driver for some chicony "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define ch_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
27 EV_KEY, (c))
28static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
29 struct hid_field *field, struct hid_usage *usage,
30 unsigned long **bit, int *max)
31{
32 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
33 return 0;
34
35 set_bit(EV_REP, hi->input->evbit);
36 switch (usage->hid & HID_USAGE) {
37 case 0xff01: ch_map_key_clear(BTN_1); break;
38 case 0xff02: ch_map_key_clear(BTN_2); break;
39 case 0xff03: ch_map_key_clear(BTN_3); break;
40 case 0xff04: ch_map_key_clear(BTN_4); break;
41 case 0xff05: ch_map_key_clear(BTN_5); break;
42 case 0xff06: ch_map_key_clear(BTN_6); break;
43 case 0xff07: ch_map_key_clear(BTN_7); break;
44 case 0xff08: ch_map_key_clear(BTN_8); break;
45 case 0xff09: ch_map_key_clear(BTN_9); break;
46 case 0xff0a: ch_map_key_clear(BTN_A); break;
47 case 0xff0b: ch_map_key_clear(BTN_B); break;
48 default:
49 return 0;
50 }
51 return 1;
52}
53
54static const struct hid_device_id ch_devices[] = {
55 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
56 { }
57};
58MODULE_DEVICE_TABLE(hid, ch_devices);
59
60static struct hid_driver ch_driver = {
61 .name = "chicony",
62 .id_table = ch_devices,
63 .input_mapping = ch_input_mapping,
64};
65
66static int ch_init(void)
67{
68 return hid_register_driver(&ch_driver);
69}
70
71static void ch_exit(void)
72{
73 hid_unregister_driver(&ch_driver);
74}
75
76module_init(ch_init);
77module_exit(ch_exit);
78MODULE_LICENSE("GPL");
79
80HID_COMPAT_LOAD_DRIVER(chicony);
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 426ac5add585..8a7d9dbb4d07 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -33,6 +33,8 @@
33#include <linux/hid-debug.h> 33#include <linux/hid-debug.h>
34#include <linux/hidraw.h> 34#include <linux/hidraw.h>
35 35
36#include "hid-ids.h"
37
36/* 38/*
37 * Version Information 39 * Version Information
38 */ 40 */
@@ -268,9 +270,9 @@ static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsign
268static u32 item_udata(struct hid_item *item) 270static u32 item_udata(struct hid_item *item)
269{ 271{
270 switch (item->size) { 272 switch (item->size) {
271 case 1: return item->data.u8; 273 case 1: return item->data.u8;
272 case 2: return item->data.u16; 274 case 2: return item->data.u16;
273 case 4: return item->data.u32; 275 case 4: return item->data.u32;
274 } 276 }
275 return 0; 277 return 0;
276} 278}
@@ -278,9 +280,9 @@ static u32 item_udata(struct hid_item *item)
278static s32 item_sdata(struct hid_item *item) 280static s32 item_sdata(struct hid_item *item)
279{ 281{
280 switch (item->size) { 282 switch (item->size) {
281 case 1: return item->data.s8; 283 case 1: return item->data.s8;
282 case 2: return item->data.s16; 284 case 2: return item->data.s16;
283 case 4: return item->data.s32; 285 case 4: return item->data.s32;
284 } 286 }
285 return 0; 287 return 0;
286} 288}
@@ -292,87 +294,91 @@ static s32 item_sdata(struct hid_item *item)
292static int hid_parser_global(struct hid_parser *parser, struct hid_item *item) 294static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
293{ 295{
294 switch (item->tag) { 296 switch (item->tag) {
297 case HID_GLOBAL_ITEM_TAG_PUSH:
295 298
296 case HID_GLOBAL_ITEM_TAG_PUSH: 299 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
297 300 dbg_hid("global enviroment stack overflow\n");
298 if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) { 301 return -1;
299 dbg_hid("global enviroment stack overflow\n"); 302 }
300 return -1;
301 }
302 303
303 memcpy(parser->global_stack + parser->global_stack_ptr++, 304 memcpy(parser->global_stack + parser->global_stack_ptr++,
304 &parser->global, sizeof(struct hid_global)); 305 &parser->global, sizeof(struct hid_global));
305 return 0; 306 return 0;
306 307
307 case HID_GLOBAL_ITEM_TAG_POP: 308 case HID_GLOBAL_ITEM_TAG_POP:
308 309
309 if (!parser->global_stack_ptr) { 310 if (!parser->global_stack_ptr) {
310 dbg_hid("global enviroment stack underflow\n"); 311 dbg_hid("global enviroment stack underflow\n");
311 return -1; 312 return -1;
312 } 313 }
313
314 memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
315 sizeof(struct hid_global));
316 return 0;
317 314
318 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE: 315 memcpy(&parser->global, parser->global_stack +
319 parser->global.usage_page = item_udata(item); 316 --parser->global_stack_ptr, sizeof(struct hid_global));
320 return 0; 317 return 0;
321 318
322 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM: 319 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
323 parser->global.logical_minimum = item_sdata(item); 320 parser->global.usage_page = item_udata(item);
324 return 0; 321 return 0;
325 322
326 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM: 323 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
327 if (parser->global.logical_minimum < 0) 324 parser->global.logical_minimum = item_sdata(item);
328 parser->global.logical_maximum = item_sdata(item); 325 return 0;
329 else
330 parser->global.logical_maximum = item_udata(item);
331 return 0;
332 326
333 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM: 327 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
334 parser->global.physical_minimum = item_sdata(item); 328 if (parser->global.logical_minimum < 0)
335 return 0; 329 parser->global.logical_maximum = item_sdata(item);
330 else
331 parser->global.logical_maximum = item_udata(item);
332 return 0;
336 333
337 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM: 334 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
338 if (parser->global.physical_minimum < 0) 335 parser->global.physical_minimum = item_sdata(item);
339 parser->global.physical_maximum = item_sdata(item); 336 return 0;
340 else
341 parser->global.physical_maximum = item_udata(item);
342 return 0;
343 337
344 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT: 338 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
345 parser->global.unit_exponent = item_sdata(item); 339 if (parser->global.physical_minimum < 0)
346 return 0; 340 parser->global.physical_maximum = item_sdata(item);
341 else
342 parser->global.physical_maximum = item_udata(item);
343 return 0;
347 344
348 case HID_GLOBAL_ITEM_TAG_UNIT: 345 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
349 parser->global.unit = item_udata(item); 346 parser->global.unit_exponent = item_sdata(item);
350 return 0; 347 return 0;
351 348
352 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE: 349 case HID_GLOBAL_ITEM_TAG_UNIT:
353 if ((parser->global.report_size = item_udata(item)) > 32) { 350 parser->global.unit = item_udata(item);
354 dbg_hid("invalid report_size %d\n", parser->global.report_size); 351 return 0;
355 return -1;
356 }
357 return 0;
358 352
359 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT: 353 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
360 if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) { 354 parser->global.report_size = item_udata(item);
361 dbg_hid("invalid report_count %d\n", parser->global.report_count); 355 if (parser->global.report_size > 32) {
362 return -1; 356 dbg_hid("invalid report_size %d\n",
363 } 357 parser->global.report_size);
364 return 0; 358 return -1;
359 }
360 return 0;
365 361
366 case HID_GLOBAL_ITEM_TAG_REPORT_ID: 362 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
367 if ((parser->global.report_id = item_udata(item)) == 0) { 363 parser->global.report_count = item_udata(item);
368 dbg_hid("report_id 0 is invalid\n"); 364 if (parser->global.report_count > HID_MAX_USAGES) {
369 return -1; 365 dbg_hid("invalid report_count %d\n",
370 } 366 parser->global.report_count);
371 return 0; 367 return -1;
368 }
369 return 0;
372 370
373 default: 371 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
374 dbg_hid("unknown global tag 0x%x\n", item->tag); 372 parser->global.report_id = item_udata(item);
373 if (parser->global.report_id == 0) {
374 dbg_hid("report_id 0 is invalid\n");
375 return -1; 375 return -1;
376 }
377 return 0;
378
379 default:
380 dbg_hid("unknown global tag 0x%x\n", item->tag);
381 return -1;
376 } 382 }
377} 383}
378 384
@@ -393,77 +399,76 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
393 data = item_udata(item); 399 data = item_udata(item);
394 400
395 switch (item->tag) { 401 switch (item->tag) {
396 402 case HID_LOCAL_ITEM_TAG_DELIMITER:
397 case HID_LOCAL_ITEM_TAG_DELIMITER: 403
398 404 if (data) {
399 if (data) { 405 /*
400 /* 406 * We treat items before the first delimiter
401 * We treat items before the first delimiter 407 * as global to all usage sets (branch 0).
402 * as global to all usage sets (branch 0). 408 * In the moment we process only these global
403 * In the moment we process only these global 409 * items and the first delimiter set.
404 * items and the first delimiter set. 410 */
405 */ 411 if (parser->local.delimiter_depth != 0) {
406 if (parser->local.delimiter_depth != 0) { 412 dbg_hid("nested delimiters\n");
407 dbg_hid("nested delimiters\n"); 413 return -1;
408 return -1;
409 }
410 parser->local.delimiter_depth++;
411 parser->local.delimiter_branch++;
412 } else {
413 if (parser->local.delimiter_depth < 1) {
414 dbg_hid("bogus close delimiter\n");
415 return -1;
416 }
417 parser->local.delimiter_depth--;
418 } 414 }
419 return 1; 415 parser->local.delimiter_depth++;
420 416 parser->local.delimiter_branch++;
421 case HID_LOCAL_ITEM_TAG_USAGE: 417 } else {
422 418 if (parser->local.delimiter_depth < 1) {
423 if (parser->local.delimiter_branch > 1) { 419 dbg_hid("bogus close delimiter\n");
424 dbg_hid("alternative usage ignored\n"); 420 return -1;
425 return 0;
426 } 421 }
422 parser->local.delimiter_depth--;
423 }
424 return 1;
427 425
428 if (item->size <= 2) 426 case HID_LOCAL_ITEM_TAG_USAGE:
429 data = (parser->global.usage_page << 16) + data;
430 427
431 return hid_add_usage(parser, data); 428 if (parser->local.delimiter_branch > 1) {
429 dbg_hid("alternative usage ignored\n");
430 return 0;
431 }
432 432
433 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM: 433 if (item->size <= 2)
434 data = (parser->global.usage_page << 16) + data;
434 435
435 if (parser->local.delimiter_branch > 1) { 436 return hid_add_usage(parser, data);
436 dbg_hid("alternative usage ignored\n");
437 return 0;
438 }
439 437
440 if (item->size <= 2) 438 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
441 data = (parser->global.usage_page << 16) + data;
442 439
443 parser->local.usage_minimum = data; 440 if (parser->local.delimiter_branch > 1) {
441 dbg_hid("alternative usage ignored\n");
444 return 0; 442 return 0;
443 }
445 444
446 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM: 445 if (item->size <= 2)
446 data = (parser->global.usage_page << 16) + data;
447 447
448 if (parser->local.delimiter_branch > 1) { 448 parser->local.usage_minimum = data;
449 dbg_hid("alternative usage ignored\n"); 449 return 0;
450 return 0;
451 }
452 450
453 if (item->size <= 2) 451 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
454 data = (parser->global.usage_page << 16) + data;
455 452
456 for (n = parser->local.usage_minimum; n <= data; n++) 453 if (parser->local.delimiter_branch > 1) {
457 if (hid_add_usage(parser, n)) { 454 dbg_hid("alternative usage ignored\n");
458 dbg_hid("hid_add_usage failed\n");
459 return -1;
460 }
461 return 0; 455 return 0;
456 }
462 457
463 default: 458 if (item->size <= 2)
459 data = (parser->global.usage_page << 16) + data;
464 460
465 dbg_hid("unknown local item tag 0x%x\n", item->tag); 461 for (n = parser->local.usage_minimum; n <= data; n++)
466 return 0; 462 if (hid_add_usage(parser, n)) {
463 dbg_hid("hid_add_usage failed\n");
464 return -1;
465 }
466 return 0;
467
468 default:
469
470 dbg_hid("unknown local item tag 0x%x\n", item->tag);
471 return 0;
467 } 472 }
468 return 0; 473 return 0;
469} 474}
@@ -480,24 +485,24 @@ static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
480 data = item_udata(item); 485 data = item_udata(item);
481 486
482 switch (item->tag) { 487 switch (item->tag) {
483 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION: 488 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
484 ret = open_collection(parser, data & 0xff); 489 ret = open_collection(parser, data & 0xff);
485 break; 490 break;
486 case HID_MAIN_ITEM_TAG_END_COLLECTION: 491 case HID_MAIN_ITEM_TAG_END_COLLECTION:
487 ret = close_collection(parser); 492 ret = close_collection(parser);
488 break; 493 break;
489 case HID_MAIN_ITEM_TAG_INPUT: 494 case HID_MAIN_ITEM_TAG_INPUT:
490 ret = hid_add_field(parser, HID_INPUT_REPORT, data); 495 ret = hid_add_field(parser, HID_INPUT_REPORT, data);
491 break; 496 break;
492 case HID_MAIN_ITEM_TAG_OUTPUT: 497 case HID_MAIN_ITEM_TAG_OUTPUT:
493 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data); 498 ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
494 break; 499 break;
495 case HID_MAIN_ITEM_TAG_FEATURE: 500 case HID_MAIN_ITEM_TAG_FEATURE:
496 ret = hid_add_field(parser, HID_FEATURE_REPORT, data); 501 ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
497 break; 502 break;
498 default: 503 default:
499 dbg_hid("unknown main item tag 0x%x\n", item->tag); 504 dbg_hid("unknown main item tag 0x%x\n", item->tag);
500 ret = 0; 505 ret = 0;
501 } 506 }
502 507
503 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */ 508 memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
@@ -534,9 +539,10 @@ static void hid_free_report(struct hid_report *report)
534 * Free a device structure, all reports, and all fields. 539 * Free a device structure, all reports, and all fields.
535 */ 540 */
536 541
537void hid_free_device(struct hid_device *device) 542static void hid_device_release(struct device *dev)
538{ 543{
539 unsigned i,j; 544 struct hid_device *device = container_of(dev, struct hid_device, dev);
545 unsigned i, j;
540 546
541 for (i = 0; i < HID_REPORT_TYPES; i++) { 547 for (i = 0; i < HID_REPORT_TYPES; i++) {
542 struct hid_report_enum *report_enum = device->report_enum + i; 548 struct hid_report_enum *report_enum = device->report_enum + i;
@@ -552,7 +558,6 @@ void hid_free_device(struct hid_device *device)
552 kfree(device->collection); 558 kfree(device->collection);
553 kfree(device); 559 kfree(device);
554} 560}
555EXPORT_SYMBOL_GPL(hid_free_device);
556 561
557/* 562/*
558 * Fetch a report description item from the data stream. We support long 563 * Fetch a report description item from the data stream. We support long
@@ -593,47 +598,52 @@ static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
593 item->size = b & 3; 598 item->size = b & 3;
594 599
595 switch (item->size) { 600 switch (item->size) {
601 case 0:
602 return start;
596 603
597 case 0: 604 case 1:
598 return start; 605 if ((end - start) < 1)
599 606 return NULL;
600 case 1: 607 item->data.u8 = *start++;
601 if ((end - start) < 1) 608 return start;
602 return NULL;
603 item->data.u8 = *start++;
604 return start;
605 609
606 case 2: 610 case 2:
607 if ((end - start) < 2) 611 if ((end - start) < 2)
608 return NULL; 612 return NULL;
609 item->data.u16 = get_unaligned_le16(start); 613 item->data.u16 = get_unaligned_le16(start);
610 start = (__u8 *)((__le16 *)start + 1); 614 start = (__u8 *)((__le16 *)start + 1);
611 return start; 615 return start;
612 616
613 case 3: 617 case 3:
614 item->size++; 618 item->size++;
615 if ((end - start) < 4) 619 if ((end - start) < 4)
616 return NULL; 620 return NULL;
617 item->data.u32 = get_unaligned_le32(start); 621 item->data.u32 = get_unaligned_le32(start);
618 start = (__u8 *)((__le32 *)start + 1); 622 start = (__u8 *)((__le32 *)start + 1);
619 return start; 623 return start;
620 } 624 }
621 625
622 return NULL; 626 return NULL;
623} 627}
624 628
625/* 629/**
630 * hid_parse_report - parse device report
631 *
632 * @device: hid device
633 * @start: report start
634 * @size: report size
635 *
626 * Parse a report description into a hid_device structure. Reports are 636 * Parse a report description into a hid_device structure. Reports are
627 * enumerated, fields are attached to these reports. 637 * enumerated, fields are attached to these reports.
638 * 0 returned on success, otherwise nonzero error value.
628 */ 639 */
629 640int hid_parse_report(struct hid_device *device, __u8 *start,
630struct hid_device *hid_parse_report(__u8 *start, unsigned size) 641 unsigned size)
631{ 642{
632 struct hid_device *device;
633 struct hid_parser *parser; 643 struct hid_parser *parser;
634 struct hid_item item; 644 struct hid_item item;
635 __u8 *end; 645 __u8 *end;
636 unsigned i; 646 int ret;
637 static int (*dispatch_type[])(struct hid_parser *parser, 647 static int (*dispatch_type[])(struct hid_parser *parser,
638 struct hid_item *item) = { 648 struct hid_item *item) = {
639 hid_parser_main, 649 hid_parser_main,
@@ -642,76 +652,57 @@ struct hid_device *hid_parse_report(__u8 *start, unsigned size)
642 hid_parser_reserved 652 hid_parser_reserved
643 }; 653 };
644 654
645 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL))) 655 if (device->driver->report_fixup)
646 return NULL; 656 device->driver->report_fixup(device, start, size);
647 657
648 if (!(device->collection = kzalloc(sizeof(struct hid_collection) * 658 device->rdesc = kmalloc(size, GFP_KERNEL);
649 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) { 659 if (device->rdesc == NULL)
650 kfree(device); 660 return -ENOMEM;
651 return NULL;
652 }
653 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
654
655 for (i = 0; i < HID_REPORT_TYPES; i++)
656 INIT_LIST_HEAD(&device->report_enum[i].report_list);
657
658 if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
659 kfree(device->collection);
660 kfree(device);
661 return NULL;
662 }
663 memcpy(device->rdesc, start, size); 661 memcpy(device->rdesc, start, size);
664 device->rsize = size; 662 device->rsize = size;
665 663
666 if (!(parser = vmalloc(sizeof(struct hid_parser)))) { 664 parser = vmalloc(sizeof(struct hid_parser));
667 kfree(device->rdesc); 665 if (!parser) {
668 kfree(device->collection); 666 ret = -ENOMEM;
669 kfree(device); 667 goto err;
670 return NULL;
671 } 668 }
669
672 memset(parser, 0, sizeof(struct hid_parser)); 670 memset(parser, 0, sizeof(struct hid_parser));
673 parser->device = device; 671 parser->device = device;
674 672
675 end = start + size; 673 end = start + size;
674 ret = -EINVAL;
676 while ((start = fetch_item(start, end, &item)) != NULL) { 675 while ((start = fetch_item(start, end, &item)) != NULL) {
677 676
678 if (item.format != HID_ITEM_FORMAT_SHORT) { 677 if (item.format != HID_ITEM_FORMAT_SHORT) {
679 dbg_hid("unexpected long global item\n"); 678 dbg_hid("unexpected long global item\n");
680 hid_free_device(device); 679 goto err;
681 vfree(parser);
682 return NULL;
683 } 680 }
684 681
685 if (dispatch_type[item.type](parser, &item)) { 682 if (dispatch_type[item.type](parser, &item)) {
686 dbg_hid("item %u %u %u %u parsing failed\n", 683 dbg_hid("item %u %u %u %u parsing failed\n",
687 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag); 684 item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
688 hid_free_device(device); 685 goto err;
689 vfree(parser);
690 return NULL;
691 } 686 }
692 687
693 if (start == end) { 688 if (start == end) {
694 if (parser->collection_stack_ptr) { 689 if (parser->collection_stack_ptr) {
695 dbg_hid("unbalanced collection at end of report description\n"); 690 dbg_hid("unbalanced collection at end of report description\n");
696 hid_free_device(device); 691 goto err;
697 vfree(parser);
698 return NULL;
699 } 692 }
700 if (parser->local.delimiter_depth) { 693 if (parser->local.delimiter_depth) {
701 dbg_hid("unbalanced delimiter at end of report description\n"); 694 dbg_hid("unbalanced delimiter at end of report description\n");
702 hid_free_device(device); 695 goto err;
703 vfree(parser);
704 return NULL;
705 } 696 }
706 vfree(parser); 697 vfree(parser);
707 return device; 698 return 0;
708 } 699 }
709 } 700 }
710 701
711 dbg_hid("item fetching failed at offset %d\n", (int)(end - start)); 702 dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
712 hid_free_device(device); 703err:
713 vfree(parser); 704 vfree(parser);
714 return NULL; 705 return ret;
715} 706}
716EXPORT_SYMBOL_GPL(hid_parse_report); 707EXPORT_SYMBOL_GPL(hid_parse_report);
717 708
@@ -724,9 +715,9 @@ EXPORT_SYMBOL_GPL(hid_parse_report);
724static s32 snto32(__u32 value, unsigned n) 715static s32 snto32(__u32 value, unsigned n)
725{ 716{
726 switch (n) { 717 switch (n) {
727 case 8: return ((__s8)value); 718 case 8: return ((__s8)value);
728 case 16: return ((__s16)value); 719 case 16: return ((__s16)value);
729 case 32: return ((__s32)value); 720 case 32: return ((__s32)value);
730 } 721 }
731 return value & (1 << (n - 1)) ? value | (-1 << n) : value; 722 return value & (1 << (n - 1)) ? value | (-1 << n) : value;
732} 723}
@@ -815,9 +806,73 @@ static __inline__ int search(__s32 *array, __s32 value, unsigned n)
815 return -1; 806 return -1;
816} 807}
817 808
818static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt) 809/**
810 * hid_match_report - check if driver's raw_event should be called
811 *
812 * @hid: hid device
813 * @report_type: type to match against
814 *
815 * compare hid->driver->report_table->report_type to report->type
816 */
817static int hid_match_report(struct hid_device *hid, struct hid_report *report)
818{
819 const struct hid_report_id *id = hid->driver->report_table;
820
821 if (!id) /* NULL means all */
822 return 1;
823
824 for (; id->report_type != HID_TERMINATOR; id++)
825 if (id->report_type == HID_ANY_ID ||
826 id->report_type == report->type)
827 return 1;
828 return 0;
829}
830
831/**
832 * hid_match_usage - check if driver's event should be called
833 *
834 * @hid: hid device
835 * @usage: usage to match against
836 *
837 * compare hid->driver->usage_table->usage_{type,code} to
838 * usage->usage_{type,code}
839 */
840static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
819{ 841{
842 const struct hid_usage_id *id = hid->driver->usage_table;
843
844 if (!id) /* NULL means all */
845 return 1;
846
847 for (; id->usage_type != HID_ANY_ID - 1; id++)
848 if ((id->usage_hid == HID_ANY_ID ||
849 id->usage_hid == usage->hid) &&
850 (id->usage_type == HID_ANY_ID ||
851 id->usage_type == usage->type) &&
852 (id->usage_code == HID_ANY_ID ||
853 id->usage_code == usage->code))
854 return 1;
855 return 0;
856}
857
858static void hid_process_event(struct hid_device *hid, struct hid_field *field,
859 struct hid_usage *usage, __s32 value, int interrupt)
860{
861 struct hid_driver *hdrv = hid->driver;
862 int ret;
863
820 hid_dump_input(usage, value); 864 hid_dump_input(usage, value);
865
866 if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
867 ret = hdrv->event(hid, field, usage, value);
868 if (ret != 0) {
869 if (ret < 0)
870 dbg_hid("%s's event failed with %d\n",
871 hdrv->name, ret);
872 return;
873 }
874 }
875
821 if (hid->claimed & HID_CLAIMED_INPUT) 876 if (hid->claimed & HID_CLAIMED_INPUT)
822 hidinput_hid_event(hid, field, usage, value); 877 hidinput_hid_event(hid, field, usage, value);
823 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event) 878 if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
@@ -946,44 +1001,47 @@ int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
946} 1001}
947EXPORT_SYMBOL_GPL(hid_set_field); 1002EXPORT_SYMBOL_GPL(hid_set_field);
948 1003
949int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt) 1004static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
1005 const u8 *data)
950{ 1006{
951 struct hid_report_enum *report_enum = hid->report_enum + type;
952 struct hid_report *report; 1007 struct hid_report *report;
953 int n, rsize, i; 1008 unsigned int n = 0; /* Normally report number is 0 */
954 1009
955 if (!hid) 1010 /* Device uses numbered reports, data[0] is report number */
956 return -ENODEV; 1011 if (report_enum->numbered)
1012 n = *data;
957 1013
958 if (!size) { 1014 report = report_enum->report_id_hash[n];
959 dbg_hid("empty report\n"); 1015 if (report == NULL)
960 return -1; 1016 dbg_hid("undefined report_id %u received\n", n);
961 }
962 1017
963 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un"); 1018 return report;
1019}
964 1020
965 n = 0; /* Normally report number is 0 */ 1021void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
966 if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */ 1022 int interrupt)
967 n = *data++; 1023{
968 size--; 1024 struct hid_report_enum *report_enum = hid->report_enum + type;
969 } 1025 struct hid_report *report;
1026 unsigned int a;
1027 int rsize, csize = size;
1028 u8 *cdata = data;
970 1029
971 /* dump the report */ 1030 report = hid_get_report(report_enum, data);
972 dbg_hid("report %d (size %u) = ", n, size); 1031 if (!report)
973 for (i = 0; i < size; i++) 1032 return;
974 dbg_hid_line(" %02x", data[i]);
975 dbg_hid_line("\n");
976 1033
977 if (!(report = report_enum->report_id_hash[n])) { 1034 if (report_enum->numbered) {
978 dbg_hid("undefined report_id %d received\n", n); 1035 cdata++;
979 return -1; 1036 csize--;
980 } 1037 }
981 1038
982 rsize = ((report->size - 1) >> 3) + 1; 1039 rsize = ((report->size - 1) >> 3) + 1;
983 1040
984 if (size < rsize) { 1041 if (csize < rsize) {
985 dbg_hid("report %d is too short, (%d < %d)\n", report->id, size, rsize); 1042 dbg_hid("report %d is too short, (%d < %d)\n", report->id,
986 memset(data + size, 0, rsize - size); 1043 csize, rsize);
1044 memset(cdata + csize, 0, rsize - csize);
987 } 1045 }
988 1046
989 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event) 1047 if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
@@ -996,24 +1054,661 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int i
996 hidraw_report_event(hid, data, size); 1054 hidraw_report_event(hid, data, size);
997 } 1055 }
998 1056
999 for (n = 0; n < report->maxfield; n++) 1057 for (a = 0; a < report->maxfield; a++)
1000 hid_input_field(hid, report->field[n], data, interrupt); 1058 hid_input_field(hid, report->field[a], cdata, interrupt);
1001 1059
1002 if (hid->claimed & HID_CLAIMED_INPUT) 1060 if (hid->claimed & HID_CLAIMED_INPUT)
1003 hidinput_report_event(hid, report); 1061 hidinput_report_event(hid, report);
1062}
1063EXPORT_SYMBOL_GPL(hid_report_raw_event);
1064
1065/**
1066 * hid_input_report - report data from lower layer (usb, bt...)
1067 *
1068 * @hid: hid device
1069 * @type: HID report type (HID_*_REPORT)
1070 * @data: report contents
1071 * @size: size of data parameter
1072 * @interrupt: called from atomic?
1073 *
1074 * This is data entry for lower layers.
1075 */
1076int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
1077{
1078 struct hid_report_enum *report_enum = hid->report_enum + type;
1079 struct hid_driver *hdrv = hid->driver;
1080 struct hid_report *report;
1081 unsigned int i;
1082 int ret;
1083
1084 if (!hid || !hid->driver)
1085 return -ENODEV;
1086
1087 if (!size) {
1088 dbg_hid("empty report\n");
1089 return -1;
1090 }
1091
1092 dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
1093
1094 report = hid_get_report(report_enum, data);
1095 if (!report)
1096 return -1;
1097
1098 /* dump the report */
1099 dbg_hid("report %d (size %u) = ", report->id, size);
1100 for (i = 0; i < size; i++)
1101 dbg_hid_line(" %02x", data[i]);
1102 dbg_hid_line("\n");
1103
1104 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
1105 ret = hdrv->raw_event(hid, report, data, size);
1106 if (ret != 0)
1107 return ret < 0 ? ret : 0;
1108 }
1109
1110 hid_report_raw_event(hid, type, data, size, interrupt);
1004 1111
1005 return 0; 1112 return 0;
1006} 1113}
1007EXPORT_SYMBOL_GPL(hid_input_report); 1114EXPORT_SYMBOL_GPL(hid_input_report);
1008 1115
1116static bool hid_match_one_id(struct hid_device *hdev,
1117 const struct hid_device_id *id)
1118{
1119 return id->bus == hdev->bus &&
1120 (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
1121 (id->product == HID_ANY_ID || id->product == hdev->product);
1122}
1123
1124static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
1125 const struct hid_device_id *id)
1126{
1127 for (; id->bus; id++)
1128 if (hid_match_one_id(hdev, id))
1129 return id;
1130
1131 return NULL;
1132}
1133
1134static const struct hid_device_id hid_hiddev_list[] = {
1135 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
1136 { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
1137 { }
1138};
1139
1140static bool hid_hiddev(struct hid_device *hdev)
1141{
1142 return !!hid_match_id(hdev, hid_hiddev_list);
1143}
1144
1145int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
1146{
1147 static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
1148 "Joystick", "Gamepad", "Keyboard", "Keypad",
1149 "Multi-Axis Controller"
1150 };
1151 const char *type, *bus;
1152 char buf[64];
1153 unsigned int i;
1154 int len;
1155
1156 if (hdev->bus != BUS_USB)
1157 connect_mask &= ~HID_CONNECT_HIDDEV;
1158 if (hid_hiddev(hdev))
1159 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1160
1161 if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
1162 connect_mask & HID_CONNECT_HIDINPUT_FORCE))
1163 hdev->claimed |= HID_CLAIMED_INPUT;
1164 if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
1165 !hdev->hiddev_connect(hdev,
1166 connect_mask & HID_CONNECT_HIDDEV_FORCE))
1167 hdev->claimed |= HID_CLAIMED_HIDDEV;
1168 if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
1169 hdev->claimed |= HID_CLAIMED_HIDRAW;
1170
1171 if (!hdev->claimed) {
1172 dev_err(&hdev->dev, "claimed by neither input, hiddev nor "
1173 "hidraw\n");
1174 return -ENODEV;
1175 }
1176
1177 if ((hdev->claimed & HID_CLAIMED_INPUT) &&
1178 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
1179 hdev->ff_init(hdev);
1180
1181 len = 0;
1182 if (hdev->claimed & HID_CLAIMED_INPUT)
1183 len += sprintf(buf + len, "input");
1184 if (hdev->claimed & HID_CLAIMED_HIDDEV)
1185 len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
1186 hdev->minor);
1187 if (hdev->claimed & HID_CLAIMED_HIDRAW)
1188 len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
1189 ((struct hidraw *)hdev->hidraw)->minor);
1190
1191 type = "Device";
1192 for (i = 0; i < hdev->maxcollection; i++) {
1193 struct hid_collection *col = &hdev->collection[i];
1194 if (col->type == HID_COLLECTION_APPLICATION &&
1195 (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1196 (col->usage & 0xffff) < ARRAY_SIZE(types)) {
1197 type = types[col->usage & 0xffff];
1198 break;
1199 }
1200 }
1201
1202 switch (hdev->bus) {
1203 case BUS_USB:
1204 bus = "USB";
1205 break;
1206 case BUS_BLUETOOTH:
1207 bus = "BLUETOOTH";
1208 break;
1209 default:
1210 bus = "<UNKNOWN>";
1211 }
1212
1213 dev_info(&hdev->dev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
1214 buf, bus, hdev->version >> 8, hdev->version & 0xff,
1215 type, hdev->name, hdev->phys);
1216
1217 return 0;
1218}
1219EXPORT_SYMBOL_GPL(hid_connect);
1220
1221static const struct hid_device_id hid_blacklist[] = {
1222 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
1223 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
1224 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ATV_IRCONTROL) },
1225 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
1226 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE) },
1227 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI) },
1228 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO) },
1229 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI) },
1230 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO) },
1231 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS) },
1232 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI) },
1233 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO) },
1234 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS) },
1235 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI) },
1236 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO) },
1237 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS) },
1238 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI) },
1239 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO) },
1240 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS) },
1241 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI) },
1242 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO) },
1243 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS) },
1244 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI) },
1245 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO) },
1246 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS) },
1247 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI) },
1248 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO) },
1249 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS) },
1250 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI) },
1251 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO) },
1252 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS) },
1253 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
1254 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
1255 { HID_USB_DEVICE(USB_VENDOR_ID_AVERMEDIA, USB_DEVICE_ID_AVER_FM_MR800) },
1256 { HID_USB_DEVICE(USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM) },
1257 { HID_USB_DEVICE(USB_VENDOR_ID_BRIGHT, USB_DEVICE_ID_BRIGHT_ABNT2) },
1258 { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) },
1259 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1260 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) },
1261 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) },
1262 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE) },
1263 { HID_USB_DEVICE(USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658) },
1264 { HID_USB_DEVICE(USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_SK8115) },
1265 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
1266 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1267 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
1268 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
1269 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
1270 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
1271 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
1272 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
1273 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
1274 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI) },
1275 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD) },
1276 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD) },
1277 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500) },
1278 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3) },
1279 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150) },
1280 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D) },
1281 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL) },
1282 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD) },
1283 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2) },
1284 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D) },
1285 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO) },
1286 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL) },
1287 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2) },
1288 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
1289 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) },
1290 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) },
1291 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) },
1292 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB) },
1293 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0) },
1294 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
1295 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
1296 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
1297 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
1298 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
1299
1300 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, 0x030c) },
1301 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT) },
1302 { }
1303};
1304
1305static int hid_bus_match(struct device *dev, struct device_driver *drv)
1306{
1307 struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
1308 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1309
1310 if (!hid_match_id(hdev, hdrv->id_table))
1311 return 0;
1312
1313 /* generic wants all non-blacklisted */
1314 if (!strncmp(hdrv->name, "generic-", 8))
1315 return !hid_match_id(hdev, hid_blacklist);
1316
1317 return 1;
1318}
1319
1320static int hid_device_probe(struct device *dev)
1321{
1322 struct hid_driver *hdrv = container_of(dev->driver,
1323 struct hid_driver, driver);
1324 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1325 const struct hid_device_id *id;
1326 int ret = 0;
1327
1328 if (!hdev->driver) {
1329 id = hid_match_id(hdev, hdrv->id_table);
1330 if (id == NULL)
1331 return -ENODEV;
1332
1333 hdev->driver = hdrv;
1334 if (hdrv->probe) {
1335 ret = hdrv->probe(hdev, id);
1336 } else { /* default probe */
1337 ret = hid_parse(hdev);
1338 if (!ret)
1339 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1340 }
1341 if (ret)
1342 hdev->driver = NULL;
1343 }
1344 return ret;
1345}
1346
1347static int hid_device_remove(struct device *dev)
1348{
1349 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1350 struct hid_driver *hdrv = hdev->driver;
1351
1352 if (hdrv) {
1353 if (hdrv->remove)
1354 hdrv->remove(hdev);
1355 else /* default remove */
1356 hid_hw_stop(hdev);
1357 hdev->driver = NULL;
1358 }
1359
1360 return 0;
1361}
1362
1363static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
1364{
1365 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1366
1367 if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
1368 hdev->bus, hdev->vendor, hdev->product))
1369 return -ENOMEM;
1370
1371 if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
1372 return -ENOMEM;
1373
1374 if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
1375 return -ENOMEM;
1376
1377 if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
1378 return -ENOMEM;
1379
1380 if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
1381 hdev->bus, hdev->vendor, hdev->product))
1382 return -ENOMEM;
1383
1384 return 0;
1385}
1386
1387static struct bus_type hid_bus_type = {
1388 .name = "hid",
1389 .match = hid_bus_match,
1390 .probe = hid_device_probe,
1391 .remove = hid_device_remove,
1392 .uevent = hid_uevent,
1393};
1394
1395static const struct hid_device_id hid_ignore_list[] = {
1396 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR) },
1397 { HID_USB_DEVICE(USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302) },
1398 { HID_USB_DEVICE(USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X) },
1399 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01) },
1400 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10) },
1401 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20) },
1402 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21) },
1403 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22) },
1404 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23) },
1405 { HID_USB_DEVICE(USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24) },
1406 { HID_USB_DEVICE(USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1) },
1407 { HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232) },
1408 { HID_USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM)},
1409 { HID_USB_DEVICE(USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD) },
1410 { HID_USB_DEVICE(USB_VENDOR_ID_CIDC, 0x0103) },
1411 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X) },
1412 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109) },
1413 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM) },
1414 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE) },
1415 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE) },
1416 { HID_USB_DEVICE(USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20) },
1417 { HID_USB_DEVICE(USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5) },
1418 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) },
1419 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0001) },
1420 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0002) },
1421 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0003) },
1422 { HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH, 0x0004) },
1423 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30) },
1424 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30) },
1425 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT) },
1426 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT) },
1427 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT) },
1428 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT) },
1429 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT) },
1430 { HID_USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL) },
1431 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2) },
1432 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN) },
1433 { HID_USB_DEVICE(USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER) },
1434 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003) },
1435 { HID_USB_DEVICE(USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY) },
1436 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE) },
1437 { HID_USB_DEVICE(USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB) },
1438 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90) },
1439 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100) },
1440 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101) },
1441 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103) },
1442 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104) },
1443 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105) },
1444 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106) },
1445 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107) },
1446 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108) },
1447 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200) },
1448 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201) },
1449 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202) },
1450 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203) },
1451 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204) },
1452 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205) },
1453 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206) },
1454 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207) },
1455 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300) },
1456 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301) },
1457 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302) },
1458 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303) },
1459 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304) },
1460 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305) },
1461 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306) },
1462 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307) },
1463 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308) },
1464 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309) },
1465 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400) },
1466 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401) },
1467 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402) },
1468 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403) },
1469 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404) },
1470 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405) },
1471 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500) },
1472 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501) },
1473 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502) },
1474 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503) },
1475 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504) },
1476 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000) },
1477 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001) },
1478 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002) },
1479 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003) },
1480 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004) },
1481 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005) },
1482 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) },
1483 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) },
1484 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) },
1485 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) },
1486 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
1487 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
1488 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
1489 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
1490 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
1491 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
1492 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
1493 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
1494 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
1495 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
1496 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
1497 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
1498 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
1499 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
1500 { HID_USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
1501 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS) },
1502 { HID_USB_DEVICE(USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS) },
1503 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1) },
1504 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2) },
1505 { HID_USB_DEVICE(USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY) },
1506 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100) },
1507 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20) },
1508 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30) },
1509 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100) },
1510 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108) },
1511 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118) },
1512 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200) },
1513 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300) },
1514 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400) },
1515 { HID_USB_DEVICE(USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500) },
1516 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0001) },
1517 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0002) },
1518 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0003) },
1519 { HID_USB_DEVICE(USB_VENDOR_ID_PANJIT, 0x0004) },
1520 { HID_USB_DEVICE(USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD) },
1521 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY1) },
1522 { HID_USB_DEVICE(USB_VENDOR_ID_TENX, USB_DEVICE_ID_TENX_IBUDDY2) },
1523 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300) },
1524 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304) },
1525 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651) },
1526 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654) },
1527 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
1528 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
1529 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
1530 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
1531 { HID_USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
1532 { HID_USB_DEVICE(USB_VENDOR_ID_WACOM, HID_ANY_ID) },
1533 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20) },
1534 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20) },
1535 { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT) },
1536 { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K) },
1537 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
1538 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
1539 { }
1540};
1541
1542static bool hid_ignore(struct hid_device *hdev)
1543{
1544 switch (hdev->vendor) {
1545 case USB_VENDOR_ID_CODEMERCS:
1546 /* ignore all Code Mercenaries IOWarrior devices */
1547 if (hdev->product >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
1548 hdev->product <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
1549 return true;
1550 break;
1551 case USB_VENDOR_ID_LOGITECH:
1552 if (hdev->product >= USB_DEVICE_ID_LOGITECH_HARMONY_FIRST &&
1553 hdev->product <= USB_DEVICE_ID_LOGITECH_HARMONY_LAST)
1554 return true;
1555 break;
1556 }
1557
1558 return !!hid_match_id(hdev, hid_ignore_list);
1559}
1560
1561int hid_add_device(struct hid_device *hdev)
1562{
1563 static atomic_t id = ATOMIC_INIT(0);
1564 int ret;
1565
1566 if (WARN_ON(hdev->status & HID_STAT_ADDED))
1567 return -EBUSY;
1568
1569 /* we need to kill them here, otherwise they will stay allocated to
1570 * wait for coming driver */
1571 if (hid_ignore(hdev))
1572 return -ENODEV;
1573
1574 /* XXX hack, any other cleaner solution < 20 bus_id bytes? */
1575 sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
1576 hdev->vendor, hdev->product, atomic_inc_return(&id));
1577
1578 ret = device_add(&hdev->dev);
1579 if (!ret)
1580 hdev->status |= HID_STAT_ADDED;
1581
1582 return ret;
1583}
1584EXPORT_SYMBOL_GPL(hid_add_device);
1585
1586/**
1587 * hid_allocate_device - allocate new hid device descriptor
1588 *
1589 * Allocate and initialize hid device, so that hid_destroy_device might be
1590 * used to free it.
1591 *
1592 * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
1593 * error value.
1594 */
1595struct hid_device *hid_allocate_device(void)
1596{
1597 struct hid_device *hdev;
1598 unsigned int i;
1599 int ret = -ENOMEM;
1600
1601 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1602 if (hdev == NULL)
1603 return ERR_PTR(ret);
1604
1605 device_initialize(&hdev->dev);
1606 hdev->dev.release = hid_device_release;
1607 hdev->dev.bus = &hid_bus_type;
1608
1609 hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
1610 sizeof(struct hid_collection), GFP_KERNEL);
1611 if (hdev->collection == NULL)
1612 goto err;
1613 hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
1614
1615 for (i = 0; i < HID_REPORT_TYPES; i++)
1616 INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
1617
1618 return hdev;
1619err:
1620 put_device(&hdev->dev);
1621 return ERR_PTR(ret);
1622}
1623EXPORT_SYMBOL_GPL(hid_allocate_device);
1624
1625static void hid_remove_device(struct hid_device *hdev)
1626{
1627 if (hdev->status & HID_STAT_ADDED) {
1628 device_del(&hdev->dev);
1629 hdev->status &= ~HID_STAT_ADDED;
1630 }
1631}
1632
1633/**
1634 * hid_destroy_device - free previously allocated device
1635 *
1636 * @hdev: hid device
1637 *
1638 * If you allocate hid_device through hid_allocate_device, you should ever
1639 * free by this function.
1640 */
1641void hid_destroy_device(struct hid_device *hdev)
1642{
1643 hid_remove_device(hdev);
1644 put_device(&hdev->dev);
1645}
1646EXPORT_SYMBOL_GPL(hid_destroy_device);
1647
1648int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
1649 const char *mod_name)
1650{
1651 hdrv->driver.name = hdrv->name;
1652 hdrv->driver.bus = &hid_bus_type;
1653 hdrv->driver.owner = owner;
1654 hdrv->driver.mod_name = mod_name;
1655
1656 return driver_register(&hdrv->driver);
1657}
1658EXPORT_SYMBOL_GPL(__hid_register_driver);
1659
1660void hid_unregister_driver(struct hid_driver *hdrv)
1661{
1662 driver_unregister(&hdrv->driver);
1663}
1664EXPORT_SYMBOL_GPL(hid_unregister_driver);
1665
1666#ifdef CONFIG_HID_COMPAT
1667static void hid_compat_load(struct work_struct *ws)
1668{
1669 request_module("hid-dummy");
1670}
1671static DECLARE_WORK(hid_compat_work, hid_compat_load);
1672static struct workqueue_struct *hid_compat_wq;
1673#endif
1674
1009static int __init hid_init(void) 1675static int __init hid_init(void)
1010{ 1676{
1011 return hidraw_init(); 1677 int ret;
1678
1679 ret = bus_register(&hid_bus_type);
1680 if (ret) {
1681 printk(KERN_ERR "HID: can't register hid bus\n");
1682 goto err;
1683 }
1684
1685 ret = hidraw_init();
1686 if (ret)
1687 goto err_bus;
1688
1689#ifdef CONFIG_HID_COMPAT
1690 hid_compat_wq = create_workqueue("hid_compat");
1691 if (!hid_compat_wq) {
1692 hidraw_exit();
1693 goto err;
1694 }
1695 queue_work(hid_compat_wq, &hid_compat_work);
1696#endif
1697
1698 return 0;
1699err_bus:
1700 bus_unregister(&hid_bus_type);
1701err:
1702 return ret;
1012} 1703}
1013 1704
1014static void __exit hid_exit(void) 1705static void __exit hid_exit(void)
1015{ 1706{
1707#ifdef CONFIG_HID_COMPAT
1708 destroy_workqueue(hid_compat_wq);
1709#endif
1016 hidraw_exit(); 1710 hidraw_exit();
1711 bus_unregister(&hid_bus_type);
1017} 1712}
1018 1713
1019module_init(hid_init); 1714module_init(hid_init);
diff --git a/drivers/hid/hid-cypress.c b/drivers/hid/hid-cypress.c
new file mode 100644
index 000000000000..5d69d27b935d
--- /dev/null
+++ b/drivers/hid/hid-cypress.c
@@ -0,0 +1,158 @@
1/*
2 * HID driver for some cypress "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/input.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define CP_RDESC_SWAPPED_MIN_MAX 0x01
27#define CP_2WHEEL_MOUSE_HACK 0x02
28#define CP_2WHEEL_MOUSE_HACK_ON 0x04
29
30/*
31 * Some USB barcode readers from cypress have usage min and usage max in
32 * the wrong order
33 */
34static void cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
35 unsigned int rsize)
36{
37 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
38 unsigned int i;
39
40 if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX))
41 return;
42
43 for (i = 0; i < rsize - 4; i++)
44 if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
45 __u8 tmp;
46
47 rdesc[i] = 0x19;
48 rdesc[i + 2] = 0x29;
49 tmp = rdesc[i + 3];
50 rdesc[i + 3] = rdesc[i + 1];
51 rdesc[i + 1] = tmp;
52 }
53}
54
55static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
56 struct hid_field *field, struct hid_usage *usage,
57 unsigned long **bit, int *max)
58{
59 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
60
61 if (!(quirks & CP_2WHEEL_MOUSE_HACK))
62 return 0;
63
64 if (usage->type == EV_REL && usage->code == REL_WHEEL)
65 set_bit(REL_HWHEEL, *bit);
66 if (usage->hid == 0x00090005)
67 return -1;
68
69 return 0;
70}
71
72static int cp_event(struct hid_device *hdev, struct hid_field *field,
73 struct hid_usage *usage, __s32 value)
74{
75 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
76
77 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
78 !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
79 return 0;
80
81 if (usage->hid == 0x00090005) {
82 if (value)
83 quirks |= CP_2WHEEL_MOUSE_HACK_ON;
84 else
85 quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
86 hid_set_drvdata(hdev, (void *)quirks);
87 return 1;
88 }
89
90 if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
91 struct input_dev *input = field->hidinput->input;
92
93 input_event(input, usage->type, REL_HWHEEL, value);
94 return 1;
95 }
96
97 return 0;
98}
99
100static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
101{
102 unsigned long quirks = id->driver_data;
103 int ret;
104
105 hid_set_drvdata(hdev, (void *)quirks);
106
107 ret = hid_parse(hdev);
108 if (ret) {
109 dev_err(&hdev->dev, "parse failed\n");
110 goto err_free;
111 }
112
113 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
114 if (ret) {
115 dev_err(&hdev->dev, "hw start failed\n");
116 goto err_free;
117 }
118
119 return 0;
120err_free:
121 return ret;
122}
123
124static const struct hid_device_id cp_devices[] = {
125 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
126 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
127 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
128 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
129 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE),
130 .driver_data = CP_2WHEEL_MOUSE_HACK },
131 { }
132};
133MODULE_DEVICE_TABLE(hid, cp_devices);
134
135static struct hid_driver cp_driver = {
136 .name = "cypress",
137 .id_table = cp_devices,
138 .report_fixup = cp_report_fixup,
139 .input_mapped = cp_input_mapped,
140 .event = cp_event,
141 .probe = cp_probe,
142};
143
144static int cp_init(void)
145{
146 return hid_register_driver(&cp_driver);
147}
148
149static void cp_exit(void)
150{
151 hid_unregister_driver(&cp_driver);
152}
153
154module_init(cp_init);
155module_exit(cp_exit);
156MODULE_LICENSE("GPL");
157
158HID_COMPAT_LOAD_DRIVER(cypress);
diff --git a/drivers/hid/hid-dell.c b/drivers/hid/hid-dell.c
new file mode 100644
index 000000000000..1a0d0dfc62fc
--- /dev/null
+++ b/drivers/hid/hid-dell.c
@@ -0,0 +1,75 @@
1/*
2 * HID driver for some dell "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25static int dell_probe(struct hid_device *hdev, const struct hid_device_id *id)
26{
27 int ret;
28
29 ret = hid_parse(hdev);
30 if (ret) {
31 dev_err(&hdev->dev, "parse failed\n");
32 goto err_free;
33 }
34
35 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
36 if (ret) {
37 dev_err(&hdev->dev, "hw start failed\n");
38 goto err_free;
39 }
40
41 usbhid_set_leds(hdev);
42
43 return 0;
44err_free:
45 return ret;
46}
47
48static const struct hid_device_id dell_devices[] = {
49 { HID_USB_DEVICE(USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658) },
50 { HID_USB_DEVICE(USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_SK8115) },
51 { }
52};
53MODULE_DEVICE_TABLE(hid, dell_devices);
54
55static struct hid_driver dell_driver = {
56 .name = "dell",
57 .id_table = dell_devices,
58 .probe = dell_probe,
59};
60
61static int dell_init(void)
62{
63 return hid_register_driver(&dell_driver);
64}
65
66static void dell_exit(void)
67{
68 hid_unregister_driver(&dell_driver);
69}
70
71module_init(dell_init);
72module_exit(dell_exit);
73MODULE_LICENSE("GPL");
74
75HID_COMPAT_LOAD_DRIVER(dell);
diff --git a/drivers/hid/hid-dummy.c b/drivers/hid/hid-dummy.c
new file mode 100644
index 000000000000..e148f86fb58e
--- /dev/null
+++ b/drivers/hid/hid-dummy.c
@@ -0,0 +1,72 @@
1#include <linux/autoconf.h>
2#include <linux/module.h>
3#include <linux/hid.h>
4
5static int __init hid_dummy_init(void)
6{
7#ifdef CONFIG_HID_A4TECH_MODULE
8 HID_COMPAT_CALL_DRIVER(a4tech);
9#endif
10#ifdef CONFIG_HID_APPLE_MODULE
11 HID_COMPAT_CALL_DRIVER(apple);
12#endif
13#ifdef CONFIG_HID_BELKIN_MODULE
14 HID_COMPAT_CALL_DRIVER(belkin);
15#endif
16#ifdef CONFIG_HID_BRIGHT_MODULE
17 HID_COMPAT_CALL_DRIVER(bright);
18#endif
19#ifdef CONFIG_HID_CHERRY_MODULE
20 HID_COMPAT_CALL_DRIVER(cherry);
21#endif
22#ifdef CONFIG_HID_CHICONY_MODULE
23 HID_COMPAT_CALL_DRIVER(chicony);
24#endif
25#ifdef CONFIG_HID_CYPRESS_MODULE
26 HID_COMPAT_CALL_DRIVER(cypress);
27#endif
28#ifdef CONFIG_HID_DELL_MODULE
29 HID_COMPAT_CALL_DRIVER(dell);
30#endif
31#ifdef CONFIG_HID_EZKEY_MODULE
32 HID_COMPAT_CALL_DRIVER(ezkey);
33#endif
34#ifdef CONFIG_HID_GYRATION_MODULE
35 HID_COMPAT_CALL_DRIVER(gyration);
36#endif
37#ifdef CONFIG_HID_LOGITECH_MODULE
38 HID_COMPAT_CALL_DRIVER(logitech);
39#endif
40#ifdef CONFIG_HID_MICROSOFT_MODULE
41 HID_COMPAT_CALL_DRIVER(microsoft);
42#endif
43#ifdef CONFIG_HID_MONTEREY_MODULE
44 HID_COMPAT_CALL_DRIVER(monterey);
45#endif
46#ifdef CONFIG_HID_PANTHERLORD_MODULE
47 HID_COMPAT_CALL_DRIVER(pantherlord);
48#endif
49#ifdef CONFIG_HID_PETALYNX_MODULE
50 HID_COMPAT_CALL_DRIVER(petalynx);
51#endif
52#ifdef CONFIG_HID_SAMSUNG_MODULE
53 HID_COMPAT_CALL_DRIVER(samsung);
54#endif
55#ifdef CONFIG_HID_SONY_MODULE
56 HID_COMPAT_CALL_DRIVER(sony);
57#endif
58#ifdef CONFIG_HID_SUNPLUS_MODULE
59 HID_COMPAT_CALL_DRIVER(sunplus);
60#endif
61#ifdef CONFIG_THRUSTMASTER_FF_MODULE
62 HID_COMPAT_CALL_DRIVER(thrustmaster);
63#endif
64#ifdef CONFIG_ZEROPLUS_FF_MODULE
65 HID_COMPAT_CALL_DRIVER(zeroplus);
66#endif
67
68 return -EIO;
69}
70module_init(hid_dummy_init);
71
72MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-ezkey.c b/drivers/hid/hid-ezkey.c
new file mode 100644
index 000000000000..deb42f931b7e
--- /dev/null
+++ b/drivers/hid/hid-ezkey.c
@@ -0,0 +1,95 @@
1/*
2 * HID driver for some ezkey "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define ez_map_rel(c) hid_map_usage(hi, usage, bit, max, EV_REL, (c))
27#define ez_map_key(c) hid_map_usage(hi, usage, bit, max, EV_KEY, (c))
28
29static int ez_input_mapping(struct hid_device *hdev, struct hid_input *hi,
30 struct hid_field *field, struct hid_usage *usage,
31 unsigned long **bit, int *max)
32{
33 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
34 return 0;
35
36 switch (usage->hid & HID_USAGE) {
37 case 0x230: ez_map_key(BTN_MOUSE); break;
38 case 0x231: ez_map_rel(REL_WHEEL); break;
39 /*
40 * this keyboard has a scrollwheel implemented in
41 * totally broken way. We map this usage temporarily
42 * to HWHEEL and handle it in the event quirk handler
43 */
44 case 0x232: ez_map_rel(REL_HWHEEL); break;
45 default:
46 return 0;
47 }
48 return 1;
49}
50
51static int ez_event(struct hid_device *hdev, struct hid_field *field,
52 struct hid_usage *usage, __s32 value)
53{
54 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
55 !usage->type)
56 return 0;
57
58 /* handle the temporary quirky mapping to HWHEEL */
59 if (usage->type == EV_REL && usage->code == REL_HWHEEL) {
60 struct input_dev *input = field->hidinput->input;
61 input_event(input, usage->type, REL_WHEEL, -value);
62 return 1;
63 }
64
65 return 0;
66}
67
68static const struct hid_device_id ez_devices[] = {
69 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) },
70 { }
71};
72MODULE_DEVICE_TABLE(hid, ez_devices);
73
74static struct hid_driver ez_driver = {
75 .name = "ezkey",
76 .id_table = ez_devices,
77 .input_mapping = ez_input_mapping,
78 .event = ez_event,
79};
80
81static int ez_init(void)
82{
83 return hid_register_driver(&ez_driver);
84}
85
86static void ez_exit(void)
87{
88 hid_unregister_driver(&ez_driver);
89}
90
91module_init(ez_init);
92module_exit(ez_exit);
93MODULE_LICENSE("GPL");
94
95HID_COMPAT_LOAD_DRIVER(ezkey);
diff --git a/drivers/hid/hid-gyration.c b/drivers/hid/hid-gyration.c
new file mode 100644
index 000000000000..ac5120f542cc
--- /dev/null
+++ b/drivers/hid/hid-gyration.c
@@ -0,0 +1,96 @@
1/*
2 * HID driver for some gyration "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define gy_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
27 EV_KEY, (c))
28static int gyration_input_mapping(struct hid_device *hdev, struct hid_input *hi,
29 struct hid_field *field, struct hid_usage *usage,
30 unsigned long **bit, int *max)
31{
32 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
33 return 0;
34
35 set_bit(EV_REP, hi->input->evbit);
36 switch (usage->hid & HID_USAGE) {
37 /* Reported on Gyration MCE Remote */
38 case 0x00d: gy_map_key_clear(KEY_HOME); break;
39 case 0x024: gy_map_key_clear(KEY_DVD); break;
40 case 0x025: gy_map_key_clear(KEY_PVR); break;
41 case 0x046: gy_map_key_clear(KEY_MEDIA); break;
42 case 0x047: gy_map_key_clear(KEY_MP3); break;
43 case 0x049: gy_map_key_clear(KEY_CAMERA); break;
44 case 0x04a: gy_map_key_clear(KEY_VIDEO); break;
45
46 default:
47 return 0;
48 }
49 return 1;
50}
51
52static int gyration_event(struct hid_device *hdev, struct hid_field *field,
53 struct hid_usage *usage, __s32 value)
54{
55 struct input_dev *input = field->hidinput->input;
56
57 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK &&
58 (usage->hid & 0xff) == 0x82) {
59 input_event(input, usage->type, usage->code, 1);
60 input_sync(input);
61 input_event(input, usage->type, usage->code, 0);
62 input_sync(input);
63 return 1;
64 }
65
66 return 0;
67}
68
69static const struct hid_device_id gyration_devices[] = {
70 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
71 { }
72};
73MODULE_DEVICE_TABLE(hid, gyration_devices);
74
75static struct hid_driver gyration_driver = {
76 .name = "gyration",
77 .id_table = gyration_devices,
78 .input_mapping = gyration_input_mapping,
79 .event = gyration_event,
80};
81
82static int gyration_init(void)
83{
84 return hid_register_driver(&gyration_driver);
85}
86
87static void gyration_exit(void)
88{
89 hid_unregister_driver(&gyration_driver);
90}
91
92module_init(gyration_init);
93module_exit(gyration_exit);
94MODULE_LICENSE("GPL");
95
96HID_COMPAT_LOAD_DRIVER(gyration);
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
new file mode 100644
index 000000000000..aad9ed1b406e
--- /dev/null
+++ b/drivers/hid/hid-ids.h
@@ -0,0 +1,404 @@
1/*
2 * USB HID quirks support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 */
17
18#ifndef HID_IDS_H_FILE
19#define HID_IDS_H_FILE
20
21#define USB_VENDOR_ID_A4TECH 0x09da
22#define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
23#define USB_DEVICE_ID_A4TECH_X5_005D 0x000a
24
25#define USB_VENDOR_ID_AASHIMA 0x06d6
26#define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
27#define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
28
29#define USB_VENDOR_ID_ACECAD 0x0460
30#define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
31#define USB_DEVICE_ID_ACECAD_302 0x0008
32
33#define USB_VENDOR_ID_ADS_TECH 0x06e1
34#define USB_DEVICE_ID_ADS_TECH_RADIO_SI470X 0xa155
35
36#define USB_VENDOR_ID_AFATECH 0x15a4
37#define USB_DEVICE_ID_AFATECH_AF9016 0x9016
38
39#define USB_VENDOR_ID_AIPTEK 0x08ca
40#define USB_DEVICE_ID_AIPTEK_01 0x0001
41#define USB_DEVICE_ID_AIPTEK_10 0x0010
42#define USB_DEVICE_ID_AIPTEK_20 0x0020
43#define USB_DEVICE_ID_AIPTEK_21 0x0021
44#define USB_DEVICE_ID_AIPTEK_22 0x0022
45#define USB_DEVICE_ID_AIPTEK_23 0x0023
46#define USB_DEVICE_ID_AIPTEK_24 0x0024
47
48#define USB_VENDOR_ID_AIRCABLE 0x16CA
49#define USB_DEVICE_ID_AIRCABLE1 0x1502
50
51#define USB_VENDOR_ID_ALCOR 0x058f
52#define USB_DEVICE_ID_ALCOR_USBRS232 0x9720
53
54#define USB_VENDOR_ID_ALPS 0x0433
55#define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
56
57#define USB_VENDOR_ID_APPLE 0x05ac
58#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
59#define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
60#define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
61#define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
62#define USB_DEVICE_ID_APPLE_GEYSER_ISO 0x0215
63#define USB_DEVICE_ID_APPLE_GEYSER_JIS 0x0216
64#define USB_DEVICE_ID_APPLE_GEYSER3_ANSI 0x0217
65#define USB_DEVICE_ID_APPLE_GEYSER3_ISO 0x0218
66#define USB_DEVICE_ID_APPLE_GEYSER3_JIS 0x0219
67#define USB_DEVICE_ID_APPLE_GEYSER4_ANSI 0x021a
68#define USB_DEVICE_ID_APPLE_GEYSER4_ISO 0x021b
69#define USB_DEVICE_ID_APPLE_GEYSER4_JIS 0x021c
70#define USB_DEVICE_ID_APPLE_ALU_ANSI 0x0220
71#define USB_DEVICE_ID_APPLE_ALU_ISO 0x0221
72#define USB_DEVICE_ID_APPLE_ALU_JIS 0x0222
73#define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
74#define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
75#define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
76#define USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI 0x0229
77#define USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO 0x022a
78#define USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS 0x022b
79#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI 0x022c
80#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO 0x022d
81#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS 0x022e
82#define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
83#define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
84#define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
85#define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY 0x030a
86#define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY 0x030b
87#define USB_DEVICE_ID_APPLE_ATV_IRCONTROL 0x8241
88#define USB_DEVICE_ID_APPLE_IRCONTROL4 0x8242
89
90#define USB_VENDOR_ID_ASUS 0x0b05
91#define USB_DEVICE_ID_ASUS_LCM 0x1726
92
93#define USB_VENDOR_ID_ATEN 0x0557
94#define USB_DEVICE_ID_ATEN_UC100KM 0x2004
95#define USB_DEVICE_ID_ATEN_CS124U 0x2202
96#define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
97#define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
98#define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
99
100#define USB_VENDOR_ID_AVERMEDIA 0x07ca
101#define USB_DEVICE_ID_AVER_FM_MR800 0xb800
102
103#define USB_VENDOR_ID_BELKIN 0x050d
104#define USB_DEVICE_ID_FLIP_KVM 0x3201
105
106#define USB_VENDOR_ID_BRIGHT 0x1241
107#define USB_DEVICE_ID_BRIGHT_ABNT2 0x1503
108
109#define USB_VENDOR_ID_BERKSHIRE 0x0c98
110#define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
111
112#define USB_VENDOR_ID_CHERRY 0x046a
113#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
114
115#define USB_VENDOR_ID_CHIC 0x05fe
116#define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
117
118#define USB_VENDOR_ID_CHICONY 0x04f2
119#define USB_DEVICE_ID_CHICONY_TACTICAL_PAD 0x0418
120
121#define USB_VENDOR_ID_CIDC 0x1677
122
123#define USB_VENDOR_ID_CMEDIA 0x0d8c
124#define USB_DEVICE_ID_CM109 0x000e
125
126#define USB_VENDOR_ID_CODEMERCS 0x07c0
127#define USB_DEVICE_ID_CODEMERCS_IOW_FIRST 0x1500
128#define USB_DEVICE_ID_CODEMERCS_IOW_LAST 0x15ff
129
130#define USB_VENDOR_ID_CYGNAL 0x10c4
131#define USB_DEVICE_ID_CYGNAL_RADIO_SI470X 0x818a
132
133#define USB_VENDOR_ID_CYPRESS 0x04b4
134#define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
135#define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
136#define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
137#define USB_DEVICE_ID_CYPRESS_BARCODE_1 0xde61
138#define USB_DEVICE_ID_CYPRESS_BARCODE_2 0xde64
139
140#define USB_VENDOR_ID_DELL 0x413c
141#define USB_DEVICE_ID_DELL_W7658 0x2005
142#define USB_DEVICE_ID_DELL_SK8115 0x2105
143
144#define USB_VENDOR_ID_DELORME 0x1163
145#define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
146#define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
147
148#define USB_VENDOR_ID_DMI 0x0c0b
149#define USB_DEVICE_ID_DMI_ENC 0x5fab
150
151#define USB_VENDOR_ID_ELO 0x04E7
152#define USB_DEVICE_ID_ELO_TS2700 0x0020
153
154#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
155#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
156
157#define USB_VENDOR_ID_EZKEY 0x0518
158#define USB_DEVICE_ID_BTC_8193 0x0002
159
160#define USB_VENDOR_ID_GAMERON 0x0810
161#define USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR 0x0001
162
163#define USB_VENDOR_ID_GENERAL_TOUCH 0x0dfc
164
165#define USB_VENDOR_ID_GLAB 0x06c2
166#define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
167#define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
168#define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
169#define USB_DEVICE_ID_0_16_16_IF_KIT 0x0044
170#define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
171#define USB_DEVICE_ID_0_8_7_IF_KIT 0x0051
172#define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
173#define USB_DEVICE_ID_PHIDGET_MOTORCONTROL 0x0058
174
175#define USB_VENDOR_ID_GOTOP 0x08f2
176#define USB_DEVICE_ID_SUPER_Q2 0x007f
177#define USB_DEVICE_ID_GOGOPEN 0x00ce
178#define USB_DEVICE_ID_PENPOWER 0x00f4
179
180#define USB_VENDOR_ID_GREENASIA 0x0e8f
181
182#define USB_VENDOR_ID_GRETAGMACBETH 0x0971
183#define USB_DEVICE_ID_GRETAGMACBETH_HUEY 0x2005
184
185#define USB_VENDOR_ID_GRIFFIN 0x077d
186#define USB_DEVICE_ID_POWERMATE 0x0410
187#define USB_DEVICE_ID_SOUNDKNOB 0x04AA
188
189#define USB_VENDOR_ID_GTCO 0x078c
190#define USB_DEVICE_ID_GTCO_90 0x0090
191#define USB_DEVICE_ID_GTCO_100 0x0100
192#define USB_DEVICE_ID_GTCO_101 0x0101
193#define USB_DEVICE_ID_GTCO_103 0x0103
194#define USB_DEVICE_ID_GTCO_104 0x0104
195#define USB_DEVICE_ID_GTCO_105 0x0105
196#define USB_DEVICE_ID_GTCO_106 0x0106
197#define USB_DEVICE_ID_GTCO_107 0x0107
198#define USB_DEVICE_ID_GTCO_108 0x0108
199#define USB_DEVICE_ID_GTCO_200 0x0200
200#define USB_DEVICE_ID_GTCO_201 0x0201
201#define USB_DEVICE_ID_GTCO_202 0x0202
202#define USB_DEVICE_ID_GTCO_203 0x0203
203#define USB_DEVICE_ID_GTCO_204 0x0204
204#define USB_DEVICE_ID_GTCO_205 0x0205
205#define USB_DEVICE_ID_GTCO_206 0x0206
206#define USB_DEVICE_ID_GTCO_207 0x0207
207#define USB_DEVICE_ID_GTCO_300 0x0300
208#define USB_DEVICE_ID_GTCO_301 0x0301
209#define USB_DEVICE_ID_GTCO_302 0x0302
210#define USB_DEVICE_ID_GTCO_303 0x0303
211#define USB_DEVICE_ID_GTCO_304 0x0304
212#define USB_DEVICE_ID_GTCO_305 0x0305
213#define USB_DEVICE_ID_GTCO_306 0x0306
214#define USB_DEVICE_ID_GTCO_307 0x0307
215#define USB_DEVICE_ID_GTCO_308 0x0308
216#define USB_DEVICE_ID_GTCO_309 0x0309
217#define USB_DEVICE_ID_GTCO_400 0x0400
218#define USB_DEVICE_ID_GTCO_401 0x0401
219#define USB_DEVICE_ID_GTCO_402 0x0402
220#define USB_DEVICE_ID_GTCO_403 0x0403
221#define USB_DEVICE_ID_GTCO_404 0x0404
222#define USB_DEVICE_ID_GTCO_405 0x0405
223#define USB_DEVICE_ID_GTCO_500 0x0500
224#define USB_DEVICE_ID_GTCO_501 0x0501
225#define USB_DEVICE_ID_GTCO_502 0x0502
226#define USB_DEVICE_ID_GTCO_503 0x0503
227#define USB_DEVICE_ID_GTCO_504 0x0504
228#define USB_DEVICE_ID_GTCO_1000 0x1000
229#define USB_DEVICE_ID_GTCO_1001 0x1001
230#define USB_DEVICE_ID_GTCO_1002 0x1002
231#define USB_DEVICE_ID_GTCO_1003 0x1003
232#define USB_DEVICE_ID_GTCO_1004 0x1004
233#define USB_DEVICE_ID_GTCO_1005 0x1005
234#define USB_DEVICE_ID_GTCO_1006 0x1006
235#define USB_DEVICE_ID_GTCO_1007 0x1007
236
237#define USB_VENDOR_ID_GYRATION 0x0c16
238#define USB_DEVICE_ID_GYRATION_REMOTE 0x0002
239
240#define USB_VENDOR_ID_HAPP 0x078b
241#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
242#define USB_DEVICE_ID_UGCI_FLYING 0x0020
243#define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
244
245#define USB_VENDOR_ID_IMATION 0x0718
246#define USB_DEVICE_ID_DISC_STAKKA 0xd000
247
248#define USB_VENDOR_ID_KBGEAR 0x084e
249#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
250
251#define USB_VENDOR_ID_LABTEC 0x1020
252#define USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD 0x0006
253
254#define USB_VENDOR_ID_LD 0x0f11
255#define USB_DEVICE_ID_LD_CASSY 0x1000
256#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
257#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
258#define USB_DEVICE_ID_LD_JWM 0x1080
259#define USB_DEVICE_ID_LD_DMMP 0x1081
260#define USB_DEVICE_ID_LD_UMIP 0x1090
261#define USB_DEVICE_ID_LD_XRAY1 0x1100
262#define USB_DEVICE_ID_LD_XRAY2 0x1101
263#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
264#define USB_DEVICE_ID_LD_COM3LAB 0x2000
265#define USB_DEVICE_ID_LD_TELEPORT 0x2010
266#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
267#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
268#define USB_DEVICE_ID_LD_MACHINETEST 0x2040
269
270#define USB_VENDOR_ID_LOGITECH 0x046d
271#define USB_DEVICE_ID_LOGITECH_LX3 0xc044
272#define USB_DEVICE_ID_LOGITECH_V150 0xc047
273#define USB_DEVICE_ID_LOGITECH_RECEIVER 0xc101
274#define USB_DEVICE_ID_LOGITECH_HARMONY_FIRST 0xc110
275#define USB_DEVICE_ID_LOGITECH_HARMONY_LAST 0xc14f
276#define USB_DEVICE_ID_LOGITECH_RUMBLEPAD 0xc211
277#define USB_DEVICE_ID_LOGITECH_EXTREME_3D 0xc215
278#define USB_DEVICE_ID_LOGITECH_RUMBLEPAD2 0xc218
279#define USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2 0xc219
280#define USB_DEVICE_ID_LOGITECH_WINGMAN_F3D 0xc283
281#define USB_DEVICE_ID_LOGITECH_FORCE3D_PRO 0xc286
282#define USB_DEVICE_ID_LOGITECH_WHEEL 0xc294
283#define USB_DEVICE_ID_LOGITECH_MOMO_WHEEL 0xc295
284#define USB_DEVICE_ID_LOGITECH_ELITE_KBD 0xc30a
285#define USB_DEVICE_ID_LOGITECH_KBD 0xc311
286#define USB_DEVICE_ID_S510_RECEIVER 0xc50c
287#define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
288#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
289#define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
290#define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704
291#define USB_DEVICE_ID_DINOVO_EDGE 0xc714
292#define USB_DEVICE_ID_DINOVO_MINI 0xc71f
293#define USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2 0xca03
294
295#define USB_VENDOR_ID_MCC 0x09db
296#define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
297#define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
298
299#define USB_VENDOR_ID_MGE 0x0463
300#define USB_DEVICE_ID_MGE_UPS 0xffff
301#define USB_DEVICE_ID_MGE_UPS1 0x0001
302
303#define USB_VENDOR_ID_MICROCHIP 0x04d8
304#define USB_DEVICE_ID_PICKIT1 0x0032
305#define USB_DEVICE_ID_PICKIT2 0x0033
306
307#define USB_VENDOR_ID_MICROSOFT 0x045e
308#define USB_DEVICE_ID_SIDEWINDER_GV 0x003b
309#define USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0 0x009d
310#define USB_DEVICE_ID_MS_NE4K 0x00db
311#define USB_DEVICE_ID_MS_LK6K 0x00f9
312#define USB_DEVICE_ID_MS_PRESENTER_8K_BT 0x0701
313#define USB_DEVICE_ID_MS_PRESENTER_8K_USB 0x0713
314
315
316#define USB_VENDOR_ID_MONTEREY 0x0566
317#define USB_DEVICE_ID_GENIUS_KB29E 0x3004
318
319#define USB_VENDOR_ID_NCR 0x0404
320#define USB_DEVICE_ID_NCR_FIRST 0x0300
321#define USB_DEVICE_ID_NCR_LAST 0x03ff
322
323#define USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR 0x0400
324#define USB_DEVICE_ID_N_S_HARMONY 0xc359
325
326#define USB_VENDOR_ID_NATSU 0x08b7
327#define USB_DEVICE_ID_NATSU_GAMEPAD 0x0001
328
329#define USB_VENDOR_ID_NEC 0x073e
330#define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
331
332#define USB_VENDOR_ID_ONTRAK 0x0a07
333#define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
334
335#define USB_VENDOR_ID_PANJIT 0x134c
336
337#define USB_VENDOR_ID_PANTHERLORD 0x0810
338#define USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK 0x0001
339
340#define USB_VENDOR_ID_PETALYNX 0x18b1
341#define USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE 0x0037
342
343#define USB_VENDOR_ID_PLAYDOTCOM 0x0b43
344#define USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII 0x0003
345
346#define USB_VENDOR_ID_SAITEK 0x06a3
347#define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
348
349#define USB_VENDOR_ID_SAMSUNG 0x0419
350#define USB_DEVICE_ID_SAMSUNG_IR_REMOTE 0x0001
351
352#define USB_VENDOR_ID_SONY 0x054c
353#define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
354
355#define USB_VENDOR_ID_SOUNDGRAPH 0x15c2
356#define USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD 0x0038
357
358#define USB_VENDOR_ID_SUN 0x0430
359#define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
360
361#define USB_VENDOR_ID_SUNPLUS 0x04fc
362#define USB_DEVICE_ID_SUNPLUS_WDESKTOP 0x05d8
363
364#define USB_VENDOR_ID_TENX 0x1130
365#define USB_DEVICE_ID_TENX_IBUDDY1 0x0001
366#define USB_DEVICE_ID_TENX_IBUDDY2 0x0002
367
368#define USB_VENDOR_ID_THRUSTMASTER 0x044f
369
370#define USB_VENDOR_ID_TOPMAX 0x0663
371#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
372
373#define USB_VENDOR_ID_TURBOX 0x062a
374#define USB_DEVICE_ID_TURBOX_KEYBOARD 0x0201
375
376#define USB_VENDOR_ID_VERNIER 0x08f7
377#define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
378#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
379#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
380#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
381#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
382
383#define USB_VENDOR_ID_WACOM 0x056a
384
385#define USB_VENDOR_ID_WISEGROUP 0x0925
386#define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
387#define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
388#define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
389#define USB_DEVICE_ID_QUAD_USB_JOYPAD 0x8800
390#define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
391
392#define USB_VENDOR_ID_WISEGROUP_LTD 0x6666
393#define USB_VENDOR_ID_WISEGROUP_LTD2 0x6677
394#define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
395
396#define USB_VENDOR_ID_YEALINK 0x6993
397#define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
398
399#define USB_VENDOR_ID_ZEROPLUS 0x0c12
400
401#define USB_VENDOR_ID_KYE 0x0458
402#define USB_DEVICE_ID_KYE_GPEN_560 0x5003
403
404#endif
diff --git a/drivers/hid/hid-input-quirks.c b/drivers/hid/hid-input-quirks.c
deleted file mode 100644
index 16feea014494..000000000000
--- a/drivers/hid/hid-input-quirks.c
+++ /dev/null
@@ -1,484 +0,0 @@
1/*
2 * HID-input usage mapping quirks
3 *
4 * This is used to handle HID-input mappings for devices violating
5 * HUT 1.12 specification.
6 *
7 * Copyright (c) 2007-2008 Jiri Kosina
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License
14 */
15
16#include <linux/input.h>
17#include <linux/hid.h>
18
19#define map_abs(c) do { usage->code = c; usage->type = EV_ABS; *bit = input->absbit; *max = ABS_MAX; } while (0)
20#define map_rel(c) do { usage->code = c; usage->type = EV_REL; *bit = input->relbit; *max = REL_MAX; } while (0)
21#define map_key(c) do { usage->code = c; usage->type = EV_KEY; *bit = input->keybit; *max = KEY_MAX; } while (0)
22#define map_led(c) do { usage->code = c; usage->type = EV_LED; *bit = input->ledbit; *max = LED_MAX; } while (0)
23
24#define map_abs_clear(c) do { map_abs(c); clear_bit(c, *bit); } while (0)
25#define map_key_clear(c) do { map_key(c); clear_bit(c, *bit); } while (0)
26
27static int quirk_belkin_wkbd(struct hid_usage *usage, struct input_dev *input,
28 unsigned long **bit, int *max)
29{
30 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
31 return 0;
32
33 switch (usage->hid & HID_USAGE) {
34 case 0x03a: map_key_clear(KEY_SOUND); break;
35 case 0x03b: map_key_clear(KEY_CAMERA); break;
36 case 0x03c: map_key_clear(KEY_DOCUMENTS); break;
37 default:
38 return 0;
39 }
40 return 1;
41}
42
43static int quirk_cherry_cymotion(struct hid_usage *usage, struct input_dev *input,
44 unsigned long **bit, int *max)
45{
46 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
47 return 0;
48
49 switch (usage->hid & HID_USAGE) {
50 case 0x301: map_key_clear(KEY_PROG1); break;
51 case 0x302: map_key_clear(KEY_PROG2); break;
52 case 0x303: map_key_clear(KEY_PROG3); break;
53 default:
54 return 0;
55 }
56 return 1;
57}
58
59static int quirk_logitech_ultrax_remote(struct hid_usage *usage, struct input_dev *input,
60 unsigned long **bit, int *max)
61{
62 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
63 return 0;
64
65 set_bit(EV_REP, input->evbit);
66 switch(usage->hid & HID_USAGE) {
67 /* Reported on Logitech Ultra X Media Remote */
68 case 0x004: map_key_clear(KEY_AGAIN); break;
69 case 0x00d: map_key_clear(KEY_HOME); break;
70 case 0x024: map_key_clear(KEY_SHUFFLE); break;
71 case 0x025: map_key_clear(KEY_TV); break;
72 case 0x026: map_key_clear(KEY_MENU); break;
73 case 0x031: map_key_clear(KEY_AUDIO); break;
74 case 0x032: map_key_clear(KEY_TEXT); break;
75 case 0x033: map_key_clear(KEY_LAST); break;
76 case 0x047: map_key_clear(KEY_MP3); break;
77 case 0x048: map_key_clear(KEY_DVD); break;
78 case 0x049: map_key_clear(KEY_MEDIA); break;
79 case 0x04a: map_key_clear(KEY_VIDEO); break;
80 case 0x04b: map_key_clear(KEY_ANGLE); break;
81 case 0x04c: map_key_clear(KEY_LANGUAGE); break;
82 case 0x04d: map_key_clear(KEY_SUBTITLE); break;
83 case 0x051: map_key_clear(KEY_RED); break;
84 case 0x052: map_key_clear(KEY_CLOSE); break;
85
86 default:
87 return 0;
88 }
89 return 1;
90}
91
92static int quirk_gyration_remote(struct hid_usage *usage, struct input_dev *input,
93 unsigned long **bit, int *max)
94{
95 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
96 return 0;
97
98 set_bit(EV_REP, input->evbit);
99 switch(usage->hid & HID_USAGE) {
100 /* Reported on Gyration MCE Remote */
101 case 0x00d: map_key_clear(KEY_HOME); break;
102 case 0x024: map_key_clear(KEY_DVD); break;
103 case 0x025: map_key_clear(KEY_PVR); break;
104 case 0x046: map_key_clear(KEY_MEDIA); break;
105 case 0x047: map_key_clear(KEY_MP3); break;
106 case 0x049: map_key_clear(KEY_CAMERA); break;
107 case 0x04a: map_key_clear(KEY_VIDEO); break;
108
109 default:
110 return 0;
111 }
112 return 1;
113}
114
115static int quirk_chicony_tactical_pad(struct hid_usage *usage, struct input_dev *input,
116 unsigned long **bit, int *max)
117{
118 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
119 return 0;
120
121 set_bit(EV_REP, input->evbit);
122 switch (usage->hid & HID_USAGE) {
123 case 0xff01: map_key_clear(BTN_1); break;
124 case 0xff02: map_key_clear(BTN_2); break;
125 case 0xff03: map_key_clear(BTN_3); break;
126 case 0xff04: map_key_clear(BTN_4); break;
127 case 0xff05: map_key_clear(BTN_5); break;
128 case 0xff06: map_key_clear(BTN_6); break;
129 case 0xff07: map_key_clear(BTN_7); break;
130 case 0xff08: map_key_clear(BTN_8); break;
131 case 0xff09: map_key_clear(BTN_9); break;
132 case 0xff0a: map_key_clear(BTN_A); break;
133 case 0xff0b: map_key_clear(BTN_B); break;
134 default:
135 return 0;
136 }
137 return 1;
138}
139
140static int quirk_microsoft_ergonomy_kb(struct hid_usage *usage, struct input_dev *input,
141 unsigned long **bit, int *max)
142{
143 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
144 return 0;
145
146 switch(usage->hid & HID_USAGE) {
147 case 0xfd06: map_key_clear(KEY_CHAT); break;
148 case 0xfd07: map_key_clear(KEY_PHONE); break;
149 case 0xff05:
150 set_bit(EV_REP, input->evbit);
151 map_key_clear(KEY_F13);
152 set_bit(KEY_F14, input->keybit);
153 set_bit(KEY_F15, input->keybit);
154 set_bit(KEY_F16, input->keybit);
155 set_bit(KEY_F17, input->keybit);
156 set_bit(KEY_F18, input->keybit);
157 default:
158 return 0;
159 }
160 return 1;
161}
162
163static int quirk_microsoft_presenter_8k(struct hid_usage *usage, struct input_dev *input,
164 unsigned long **bit, int *max)
165{
166 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
167 return 0;
168
169 set_bit(EV_REP, input->evbit);
170 switch(usage->hid & HID_USAGE) {
171 case 0xfd08: map_key_clear(KEY_FORWARD); break;
172 case 0xfd09: map_key_clear(KEY_BACK); break;
173 case 0xfd0b: map_key_clear(KEY_PLAYPAUSE); break;
174 case 0xfd0e: map_key_clear(KEY_CLOSE); break;
175 case 0xfd0f: map_key_clear(KEY_PLAY); break;
176 default:
177 return 0;
178 }
179 return 1;
180}
181
182static int quirk_petalynx_remote(struct hid_usage *usage, struct input_dev *input,
183 unsigned long **bit, int *max)
184{
185 if (((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR) &&
186 ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER))
187 return 0;
188
189 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_LOGIVENDOR)
190 switch(usage->hid & HID_USAGE) {
191 case 0x05a: map_key_clear(KEY_TEXT); break;
192 case 0x05b: map_key_clear(KEY_RED); break;
193 case 0x05c: map_key_clear(KEY_GREEN); break;
194 case 0x05d: map_key_clear(KEY_YELLOW); break;
195 case 0x05e: map_key_clear(KEY_BLUE); break;
196 default:
197 return 0;
198 }
199
200 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
201 switch(usage->hid & HID_USAGE) {
202 case 0x0f6: map_key_clear(KEY_NEXT); break;
203 case 0x0fa: map_key_clear(KEY_BACK); break;
204 default:
205 return 0;
206 }
207 return 1;
208}
209
210static int quirk_logitech_wireless(struct hid_usage *usage, struct input_dev *input,
211 unsigned long **bit, int *max)
212{
213 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
214 return 0;
215
216 switch (usage->hid & HID_USAGE) {
217 case 0x1001: map_key_clear(KEY_MESSENGER); break;
218 case 0x1003: map_key_clear(KEY_SOUND); break;
219 case 0x1004: map_key_clear(KEY_VIDEO); break;
220 case 0x1005: map_key_clear(KEY_AUDIO); break;
221 case 0x100a: map_key_clear(KEY_DOCUMENTS); break;
222 case 0x1011: map_key_clear(KEY_PREVIOUSSONG); break;
223 case 0x1012: map_key_clear(KEY_NEXTSONG); break;
224 case 0x1013: map_key_clear(KEY_CAMERA); break;
225 case 0x1014: map_key_clear(KEY_MESSENGER); break;
226 case 0x1015: map_key_clear(KEY_RECORD); break;
227 case 0x1016: map_key_clear(KEY_PLAYER); break;
228 case 0x1017: map_key_clear(KEY_EJECTCD); break;
229 case 0x1018: map_key_clear(KEY_MEDIA); break;
230 case 0x1019: map_key_clear(KEY_PROG1); break;
231 case 0x101a: map_key_clear(KEY_PROG2); break;
232 case 0x101b: map_key_clear(KEY_PROG3); break;
233 case 0x101f: map_key_clear(KEY_ZOOMIN); break;
234 case 0x1020: map_key_clear(KEY_ZOOMOUT); break;
235 case 0x1021: map_key_clear(KEY_ZOOMRESET); break;
236 case 0x1023: map_key_clear(KEY_CLOSE); break;
237 case 0x1027: map_key_clear(KEY_MENU); break;
238 /* this one is marked as 'Rotate' */
239 case 0x1028: map_key_clear(KEY_ANGLE); break;
240 case 0x1029: map_key_clear(KEY_SHUFFLE); break;
241 case 0x102a: map_key_clear(KEY_BACK); break;
242 case 0x102b: map_key_clear(KEY_CYCLEWINDOWS); break;
243 case 0x1041: map_key_clear(KEY_BATTERY); break;
244 case 0x1042: map_key_clear(KEY_WORDPROCESSOR); break;
245 case 0x1043: map_key_clear(KEY_SPREADSHEET); break;
246 case 0x1044: map_key_clear(KEY_PRESENTATION); break;
247 case 0x1045: map_key_clear(KEY_UNDO); break;
248 case 0x1046: map_key_clear(KEY_REDO); break;
249 case 0x1047: map_key_clear(KEY_PRINT); break;
250 case 0x1048: map_key_clear(KEY_SAVE); break;
251 case 0x1049: map_key_clear(KEY_PROG1); break;
252 case 0x104a: map_key_clear(KEY_PROG2); break;
253 case 0x104b: map_key_clear(KEY_PROG3); break;
254 case 0x104c: map_key_clear(KEY_PROG4); break;
255
256 default:
257 return 0;
258 }
259 return 1;
260}
261
262static int quirk_cherry_genius_29e(struct hid_usage *usage, struct input_dev *input,
263 unsigned long **bit, int *max)
264{
265 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
266 return 0;
267
268 switch (usage->hid & HID_USAGE) {
269 case 0x156: map_key_clear(KEY_WORDPROCESSOR); break;
270 case 0x157: map_key_clear(KEY_SPREADSHEET); break;
271 case 0x158: map_key_clear(KEY_PRESENTATION); break;
272 case 0x15c: map_key_clear(KEY_STOP); break;
273
274 default:
275 return 0;
276 }
277 return 1;
278}
279
280static int quirk_btc_8193(struct hid_usage *usage, struct input_dev *input,
281 unsigned long **bit, int *max)
282{
283 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
284 return 0;
285
286 switch (usage->hid & HID_USAGE) {
287 case 0x230: map_key(BTN_MOUSE); break;
288 case 0x231: map_rel(REL_WHEEL); break;
289 /*
290 * this keyboard has a scrollwheel implemented in
291 * totally broken way. We map this usage temporarily
292 * to HWHEEL and handle it in the event quirk handler
293 */
294 case 0x232: map_rel(REL_HWHEEL); break;
295
296 default:
297 return 0;
298 }
299 return 1;
300}
301
302static int quirk_sunplus_wdesktop(struct hid_usage *usage, struct input_dev *input,
303 unsigned long **bit, int *max)
304{
305 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
306 return 0;
307
308 switch (usage->hid & HID_USAGE) {
309 case 0x2003: map_key_clear(KEY_ZOOMIN); break;
310 case 0x2103: map_key_clear(KEY_ZOOMOUT); break;
311 default:
312 return 0;
313 }
314 return 1;
315}
316
317#define VENDOR_ID_BELKIN 0x1020
318#define DEVICE_ID_BELKIN_WIRELESS_KEYBOARD 0x0006
319
320#define VENDOR_ID_CHERRY 0x046a
321#define DEVICE_ID_CHERRY_CYMOTION 0x0023
322
323#define VENDOR_ID_CHICONY 0x04f2
324#define DEVICE_ID_CHICONY_TACTICAL_PAD 0x0418
325
326#define VENDOR_ID_EZKEY 0x0518
327#define DEVICE_ID_BTC_8193 0x0002
328
329#define VENDOR_ID_GYRATION 0x0c16
330#define DEVICE_ID_GYRATION_REMOTE 0x0002
331
332#define VENDOR_ID_LOGITECH 0x046d
333#define DEVICE_ID_LOGITECH_RECEIVER 0xc101
334#define DEVICE_ID_S510_RECEIVER 0xc50c
335#define DEVICE_ID_S510_RECEIVER_2 0xc517
336#define DEVICE_ID_MX3000_RECEIVER 0xc513
337
338#define VENDOR_ID_MICROSOFT 0x045e
339#define DEVICE_ID_MS4K 0x00db
340#define DEVICE_ID_MS6K 0x00f9
341#define DEVICE_IS_MS_PRESENTER_8K_BT 0x0701
342#define DEVICE_ID_MS_PRESENTER_8K_USB 0x0713
343
344#define VENDOR_ID_MONTEREY 0x0566
345#define DEVICE_ID_GENIUS_KB29E 0x3004
346
347#define VENDOR_ID_PETALYNX 0x18b1
348#define DEVICE_ID_PETALYNX_MAXTER_REMOTE 0x0037
349
350#define VENDOR_ID_SUNPLUS 0x04fc
351#define DEVICE_ID_SUNPLUS_WDESKTOP 0x05d8
352
353static const struct hid_input_blacklist {
354 __u16 idVendor;
355 __u16 idProduct;
356 int (*quirk)(struct hid_usage *, struct input_dev *, unsigned long **, int *);
357} hid_input_blacklist[] = {
358 { VENDOR_ID_BELKIN, DEVICE_ID_BELKIN_WIRELESS_KEYBOARD, quirk_belkin_wkbd },
359
360 { VENDOR_ID_CHERRY, DEVICE_ID_CHERRY_CYMOTION, quirk_cherry_cymotion },
361
362 { VENDOR_ID_CHICONY, DEVICE_ID_CHICONY_TACTICAL_PAD, quirk_chicony_tactical_pad },
363
364 { VENDOR_ID_EZKEY, DEVICE_ID_BTC_8193, quirk_btc_8193 },
365
366 { VENDOR_ID_GYRATION, DEVICE_ID_GYRATION_REMOTE, quirk_gyration_remote },
367
368 { VENDOR_ID_LOGITECH, DEVICE_ID_LOGITECH_RECEIVER, quirk_logitech_ultrax_remote },
369 { VENDOR_ID_LOGITECH, DEVICE_ID_S510_RECEIVER, quirk_logitech_wireless },
370 { VENDOR_ID_LOGITECH, DEVICE_ID_S510_RECEIVER_2, quirk_logitech_wireless },
371 { VENDOR_ID_LOGITECH, DEVICE_ID_MX3000_RECEIVER, quirk_logitech_wireless },
372
373 { VENDOR_ID_MICROSOFT, DEVICE_ID_MS4K, quirk_microsoft_ergonomy_kb },
374 { VENDOR_ID_MICROSOFT, DEVICE_ID_MS6K, quirk_microsoft_ergonomy_kb },
375 { VENDOR_ID_MICROSOFT, DEVICE_IS_MS_PRESENTER_8K_BT, quirk_microsoft_presenter_8k },
376 { VENDOR_ID_MICROSOFT, DEVICE_ID_MS_PRESENTER_8K_USB, quirk_microsoft_presenter_8k },
377
378 { VENDOR_ID_MONTEREY, DEVICE_ID_GENIUS_KB29E, quirk_cherry_genius_29e },
379
380 { VENDOR_ID_PETALYNX, DEVICE_ID_PETALYNX_MAXTER_REMOTE, quirk_petalynx_remote },
381
382 { VENDOR_ID_SUNPLUS, DEVICE_ID_SUNPLUS_WDESKTOP, quirk_sunplus_wdesktop },
383
384 { 0, 0, NULL }
385};
386
387int hidinput_mapping_quirks(struct hid_usage *usage,
388 struct input_dev *input,
389 unsigned long **bit, int *max)
390{
391 struct hid_device *device = input_get_drvdata(input);
392 int i = 0;
393
394 while (hid_input_blacklist[i].quirk) {
395 if (hid_input_blacklist[i].idVendor == device->vendor &&
396 hid_input_blacklist[i].idProduct == device->product)
397 return hid_input_blacklist[i].quirk(usage, input, bit, max);
398 i++;
399 }
400 return 0;
401}
402
403int hidinput_event_quirks(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
404{
405 struct input_dev *input;
406
407 input = field->hidinput->input;
408
409 if (((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
410 || ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007))) {
411 if (value) hid->quirks |= HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
412 else hid->quirks &= ~HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
413 return 1;
414 }
415
416 if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
417 (usage->type == EV_REL) &&
418 (usage->code == REL_WHEEL)) {
419 hid->delayed_value = value;
420 return 1;
421 }
422
423 if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
424 (usage->hid == 0x000100b8)) {
425 input_event(input, EV_REL, value ? REL_HWHEEL : REL_WHEEL, hid->delayed_value);
426 return 1;
427 }
428
429 if ((hid->quirks & HID_QUIRK_INVERT_HWHEEL) && (usage->code == REL_HWHEEL)) {
430 input_event(input, usage->type, usage->code, -value);
431 return 1;
432 }
433
434 if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_ON) && (usage->code == REL_WHEEL)) {
435 input_event(input, usage->type, REL_HWHEEL, value);
436 return 1;
437 }
438
439 if ((hid->quirks & HID_QUIRK_APPLE_HAS_FN) && hidinput_apple_event(hid, input, usage, value))
440 return 1;
441
442 /* Handling MS keyboards special buttons */
443 if (hid->quirks & HID_QUIRK_MICROSOFT_KEYS &&
444 usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
445 int key = 0;
446 static int last_key = 0;
447 switch (value) {
448 case 0x01: key = KEY_F14; break;
449 case 0x02: key = KEY_F15; break;
450 case 0x04: key = KEY_F16; break;
451 case 0x08: key = KEY_F17; break;
452 case 0x10: key = KEY_F18; break;
453 default: break;
454 }
455 if (key) {
456 input_event(input, usage->type, key, 1);
457 last_key = key;
458 } else {
459 input_event(input, usage->type, last_key, 0);
460 }
461 }
462
463 /* handle the temporary quirky mapping to HWHEEL */
464 if (hid->quirks & HID_QUIRK_HWHEEL_WHEEL_INVERT &&
465 usage->type == EV_REL && usage->code == REL_HWHEEL) {
466 input_event(input, usage->type, REL_WHEEL, -value);
467 return 1;
468 }
469
470 /* Gyration MCE remote "Sleep" key */
471 if (hid->vendor == VENDOR_ID_GYRATION &&
472 hid->product == DEVICE_ID_GYRATION_REMOTE &&
473 (usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK &&
474 (usage->hid & 0xff) == 0x82) {
475 input_event(input, usage->type, usage->code, 1);
476 input_sync(input);
477 input_event(input, usage->type, usage->code, 0);
478 input_sync(input);
479 return 1;
480 }
481 return 0;
482}
483
484
diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 1b2e8dc3398d..7f183b7147e1 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -32,11 +32,6 @@
32#include <linux/hid.h> 32#include <linux/hid.h>
33#include <linux/hid-debug.h> 33#include <linux/hid-debug.h>
34 34
35static int hid_apple_fnmode = 1;
36module_param_named(pb_fnmode, hid_apple_fnmode, int, 0644);
37MODULE_PARM_DESC(pb_fnmode,
38 "Mode of fn key on Apple keyboards (0 = disabled, 1 = fkeyslast, 2 = fkeysfirst)");
39
40#define unk KEY_UNKNOWN 35#define unk KEY_UNKNOWN
41 36
42static const unsigned char hid_keyboard[256] = { 37static const unsigned char hid_keyboard[256] = {
@@ -58,227 +53,20 @@ static const unsigned char hid_keyboard[256] = {
58 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk 53 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
59}; 54};
60 55
61/* extended mapping for certain Logitech hardware (Logitech cordless desktop LX500) */
62#define LOGITECH_EXPANDED_KEYMAP_SIZE 80
63static int logitech_expanded_keymap[LOGITECH_EXPANDED_KEYMAP_SIZE] = {
64 0,216, 0,213,175,156, 0, 0, 0, 0,
65 144, 0, 0, 0, 0, 0, 0, 0, 0,212,
66 174,167,152,161,112, 0, 0, 0,154, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0,183,184,185,186,187,
71 188,189,190,191,192,193,194, 0, 0, 0
72};
73
74static const struct { 56static const struct {
75 __s32 x; 57 __s32 x;
76 __s32 y; 58 __s32 y;
77} hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 59} hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
78 60
79#define map_abs(c) do { usage->code = c; usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX; } while (0) 61#define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
80#define map_rel(c) do { usage->code = c; usage->type = EV_REL; bit = input->relbit; max = REL_MAX; } while (0) 62#define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
81#define map_key(c) do { usage->code = c; usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX; } while (0) 63#define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
82#define map_led(c) do { usage->code = c; usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; } while (0) 64#define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
83
84#define map_abs_clear(c) do { map_abs(c); clear_bit(c, bit); } while (0)
85#define map_key_clear(c) do { map_key(c); clear_bit(c, bit); } while (0)
86
87#ifdef CONFIG_USB_HIDINPUT_POWERBOOK
88
89struct hidinput_key_translation {
90 u16 from;
91 u16 to;
92 u8 flags;
93};
94
95#define APPLE_FLAG_FKEY 0x01
96
97static struct hidinput_key_translation apple_fn_keys[] = {
98 { KEY_BACKSPACE, KEY_DELETE },
99 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
100 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
101 { KEY_F3, KEY_FN_F5, APPLE_FLAG_FKEY }, /* Exposé */
102 { KEY_F4, KEY_FN_F4, APPLE_FLAG_FKEY }, /* Dashboard */
103 { KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
104 { KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
105 { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
106 { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
107 { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
108 { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
109 { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
110 { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
111 { KEY_UP, KEY_PAGEUP },
112 { KEY_DOWN, KEY_PAGEDOWN },
113 { KEY_LEFT, KEY_HOME },
114 { KEY_RIGHT, KEY_END },
115 { }
116};
117
118static struct hidinput_key_translation powerbook_fn_keys[] = {
119 { KEY_BACKSPACE, KEY_DELETE },
120 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
121 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
122 { KEY_F3, KEY_MUTE, APPLE_FLAG_FKEY },
123 { KEY_F4, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
124 { KEY_F5, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
125 { KEY_F6, KEY_NUMLOCK, APPLE_FLAG_FKEY },
126 { KEY_F7, KEY_SWITCHVIDEOMODE, APPLE_FLAG_FKEY },
127 { KEY_F8, KEY_KBDILLUMTOGGLE, APPLE_FLAG_FKEY },
128 { KEY_F9, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
129 { KEY_F10, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
130 { KEY_UP, KEY_PAGEUP },
131 { KEY_DOWN, KEY_PAGEDOWN },
132 { KEY_LEFT, KEY_HOME },
133 { KEY_RIGHT, KEY_END },
134 { }
135};
136
137static struct hidinput_key_translation powerbook_numlock_keys[] = {
138 { KEY_J, KEY_KP1 },
139 { KEY_K, KEY_KP2 },
140 { KEY_L, KEY_KP3 },
141 { KEY_U, KEY_KP4 },
142 { KEY_I, KEY_KP5 },
143 { KEY_O, KEY_KP6 },
144 { KEY_7, KEY_KP7 },
145 { KEY_8, KEY_KP8 },
146 { KEY_9, KEY_KP9 },
147 { KEY_M, KEY_KP0 },
148 { KEY_DOT, KEY_KPDOT },
149 { KEY_SLASH, KEY_KPPLUS },
150 { KEY_SEMICOLON, KEY_KPMINUS },
151 { KEY_P, KEY_KPASTERISK },
152 { KEY_MINUS, KEY_KPEQUAL },
153 { KEY_0, KEY_KPSLASH },
154 { KEY_F6, KEY_NUMLOCK },
155 { KEY_KPENTER, KEY_KPENTER },
156 { KEY_BACKSPACE, KEY_BACKSPACE },
157 { }
158};
159
160static struct hidinput_key_translation apple_iso_keyboard[] = {
161 { KEY_GRAVE, KEY_102ND },
162 { KEY_102ND, KEY_GRAVE },
163 { }
164};
165 65
166static struct hidinput_key_translation *find_translation(struct hidinput_key_translation *table, u16 from) 66#define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
167{ 67 &max, EV_ABS, (c))
168 struct hidinput_key_translation *trans; 68#define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
169 69 &max, EV_KEY, (c))
170 /* Look for the translation */
171 for (trans = table; trans->from; trans++)
172 if (trans->from == from)
173 return trans;
174
175 return NULL;
176}
177
178int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
179 struct hid_usage *usage, __s32 value)
180{
181 struct hidinput_key_translation *trans;
182
183 if (usage->code == KEY_FN) {
184 if (value) hid->quirks |= HID_QUIRK_APPLE_FN_ON;
185 else hid->quirks &= ~HID_QUIRK_APPLE_FN_ON;
186
187 input_event(input, usage->type, usage->code, value);
188
189 return 1;
190 }
191
192 if (hid_apple_fnmode) {
193 int do_translate;
194
195 trans = find_translation((hid->product < 0x220 ||
196 hid->product >= 0x300) ?
197 powerbook_fn_keys : apple_fn_keys,
198 usage->code);
199 if (trans) {
200 if (test_bit(usage->code, hid->apple_pressed_fn))
201 do_translate = 1;
202 else if (trans->flags & APPLE_FLAG_FKEY)
203 do_translate =
204 (hid_apple_fnmode == 2 && (hid->quirks & HID_QUIRK_APPLE_FN_ON)) ||
205 (hid_apple_fnmode == 1 && !(hid->quirks & HID_QUIRK_APPLE_FN_ON));
206 else
207 do_translate = (hid->quirks & HID_QUIRK_APPLE_FN_ON);
208
209 if (do_translate) {
210 if (value)
211 set_bit(usage->code, hid->apple_pressed_fn);
212 else
213 clear_bit(usage->code, hid->apple_pressed_fn);
214
215 input_event(input, usage->type, trans->to, value);
216
217 return 1;
218 }
219 }
220
221 if (hid->quirks & HID_QUIRK_APPLE_NUMLOCK_EMULATION && (
222 test_bit(usage->code, hid->pb_pressed_numlock) ||
223 test_bit(LED_NUML, input->led))) {
224 trans = find_translation(powerbook_numlock_keys, usage->code);
225
226 if (trans) {
227 if (value)
228 set_bit(usage->code, hid->pb_pressed_numlock);
229 else
230 clear_bit(usage->code, hid->pb_pressed_numlock);
231
232 input_event(input, usage->type, trans->to, value);
233 }
234
235 return 1;
236 }
237 }
238
239 if (hid->quirks & HID_QUIRK_APPLE_ISO_KEYBOARD) {
240 trans = find_translation(apple_iso_keyboard, usage->code);
241 if (trans) {
242 input_event(input, usage->type, trans->to, value);
243 return 1;
244 }
245 }
246
247 return 0;
248}
249
250static void hidinput_apple_setup(struct input_dev *input)
251{
252 struct hidinput_key_translation *trans;
253
254 set_bit(KEY_NUMLOCK, input->keybit);
255
256 /* Enable all needed keys */
257 for (trans = apple_fn_keys; trans->from; trans++)
258 set_bit(trans->to, input->keybit);
259
260 for (trans = powerbook_fn_keys; trans->from; trans++)
261 set_bit(trans->to, input->keybit);
262
263 for (trans = powerbook_numlock_keys; trans->from; trans++)
264 set_bit(trans->to, input->keybit);
265
266 for (trans = apple_iso_keyboard; trans->from; trans++)
267 set_bit(trans->to, input->keybit);
268
269}
270#else
271inline int hidinput_apple_event(struct hid_device *hid,
272 struct input_dev *input,
273 struct hid_usage *usage, __s32 value)
274{
275 return 0;
276}
277
278static inline void hidinput_apple_setup(struct input_dev *input)
279{
280}
281#endif
282 70
283static inline int match_scancode(int code, int scancode) 71static inline int match_scancode(int code, int scancode)
284{ 72{
@@ -366,7 +154,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
366{ 154{
367 struct input_dev *input = hidinput->input; 155 struct input_dev *input = hidinput->input;
368 struct hid_device *device = input_get_drvdata(input); 156 struct hid_device *device = input_get_drvdata(input);
369 int max = 0, code, ret; 157 int max = 0, code;
370 unsigned long *bit = NULL; 158 unsigned long *bit = NULL;
371 159
372 field->hidinput = hidinput; 160 field->hidinput = hidinput;
@@ -385,406 +173,345 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
385 goto ignore; 173 goto ignore;
386 } 174 }
387 175
388 /* handle input mappings for quirky devices */ 176 if (device->driver->input_mapping) {
389 ret = hidinput_mapping_quirks(usage, input, &bit, &max); 177 int ret = device->driver->input_mapping(device, hidinput, field,
390 if (ret) 178 usage, &bit, &max);
391 goto mapped; 179 if (ret > 0)
180 goto mapped;
181 if (ret < 0)
182 goto ignore;
183 }
392 184
393 switch (usage->hid & HID_USAGE_PAGE) { 185 switch (usage->hid & HID_USAGE_PAGE) {
186 case HID_UP_UNDEFINED:
187 goto ignore;
394 188
395 case HID_UP_UNDEFINED: 189 case HID_UP_KEYBOARD:
396 goto ignore; 190 set_bit(EV_REP, input->evbit);
397
398 case HID_UP_KEYBOARD:
399 191
400 set_bit(EV_REP, input->evbit); 192 if ((usage->hid & HID_USAGE) < 256) {
193 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
194 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
195 } else
196 map_key(KEY_UNKNOWN);
401 197
402 if ((usage->hid & HID_USAGE) < 256) { 198 break;
403 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
404 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
405 } else
406 map_key(KEY_UNKNOWN);
407 199
408 break; 200 case HID_UP_BUTTON:
201 code = ((usage->hid - 1) & 0xf);
409 202
410 case HID_UP_BUTTON: 203 switch (field->application) {
411 204 case HID_GD_MOUSE:
412 code = ((usage->hid - 1) & 0xf); 205 case HID_GD_POINTER: code += 0x110; break;
413 206 case HID_GD_JOYSTICK: code += 0x120; break;
414 switch (field->application) { 207 case HID_GD_GAMEPAD: code += 0x130; break;
415 case HID_GD_MOUSE: 208 default:
416 case HID_GD_POINTER: code += 0x110; break; 209 switch (field->physical) {
417 case HID_GD_JOYSTICK: code += 0x120; break; 210 case HID_GD_MOUSE:
418 case HID_GD_GAMEPAD: code += 0x130; break; 211 case HID_GD_POINTER: code += 0x110; break;
419 default: 212 case HID_GD_JOYSTICK: code += 0x120; break;
420 switch (field->physical) { 213 case HID_GD_GAMEPAD: code += 0x130; break;
421 case HID_GD_MOUSE: 214 default: code += 0x100;
422 case HID_GD_POINTER: code += 0x110; break;
423 case HID_GD_JOYSTICK: code += 0x120; break;
424 case HID_GD_GAMEPAD: code += 0x130; break;
425 default: code += 0x100;
426 }
427 }
428
429 /* Special handling for Logitech Cordless Desktop */
430 if (field->application != HID_GD_MOUSE) {
431 if (device->quirks & HID_QUIRK_LOGITECH_EXPANDED_KEYMAP) {
432 int hid = usage->hid & HID_USAGE;
433 if (hid < LOGITECH_EXPANDED_KEYMAP_SIZE && logitech_expanded_keymap[hid] != 0)
434 code = logitech_expanded_keymap[hid];
435 }
436 } else {
437 if (device->quirks & HID_QUIRK_LOGITECH_IGNORE_DOUBLED_WHEEL) {
438 int hid = usage->hid & HID_USAGE;
439 if (hid == 7 || hid == 8)
440 goto ignore;
441 }
442 } 215 }
216 }
443 217
444 map_key(code); 218 map_key(code);
445 break; 219 break;
446 220
447 221 case HID_UP_SIMULATION:
448 case HID_UP_SIMULATION: 222 switch (usage->hid & 0xffff) {
449 223 case 0xba: map_abs(ABS_RUDDER); break;
450 switch (usage->hid & 0xffff) { 224 case 0xbb: map_abs(ABS_THROTTLE); break;
451 case 0xba: map_abs(ABS_RUDDER); break; 225 case 0xc4: map_abs(ABS_GAS); break;
452 case 0xbb: map_abs(ABS_THROTTLE); break; 226 case 0xc5: map_abs(ABS_BRAKE); break;
453 case 0xc4: map_abs(ABS_GAS); break; 227 case 0xc8: map_abs(ABS_WHEEL); break;
454 case 0xc5: map_abs(ABS_BRAKE); break; 228 default: goto ignore;
455 case 0xc8: map_abs(ABS_WHEEL); break; 229 }
456 default: goto ignore; 230 break;
231
232 case HID_UP_GENDESK:
233 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
234 switch (usage->hid & 0xf) {
235 case 0x1: map_key_clear(KEY_POWER); break;
236 case 0x2: map_key_clear(KEY_SLEEP); break;
237 case 0x3: map_key_clear(KEY_WAKEUP); break;
238 default: goto unknown;
457 } 239 }
458 break; 240 break;
241 }
459 242
460 case HID_UP_GENDESK: 243 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
461
462 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
463 switch (usage->hid & 0xf) {
464 case 0x1: map_key_clear(KEY_POWER); break;
465 case 0x2: map_key_clear(KEY_SLEEP); break;
466 case 0x3: map_key_clear(KEY_WAKEUP); break;
467 default: goto unknown;
468 }
469 break;
470 }
471
472 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
473 switch (usage->hid) {
474 case HID_GD_UP: usage->hat_dir = 1; break;
475 case HID_GD_DOWN: usage->hat_dir = 5; break;
476 case HID_GD_RIGHT: usage->hat_dir = 3; break;
477 case HID_GD_LEFT: usage->hat_dir = 7; break;
478 default: goto unknown;
479 }
480 if (field->dpad) {
481 map_abs(field->dpad);
482 goto ignore;
483 }
484 map_abs(ABS_HAT0X);
485 break;
486 }
487
488 switch (usage->hid) { 244 switch (usage->hid) {
489 245 case HID_GD_UP: usage->hat_dir = 1; break;
490 /* These usage IDs map directly to the usage codes. */ 246 case HID_GD_DOWN: usage->hat_dir = 5; break;
491 case HID_GD_X: case HID_GD_Y: case HID_GD_Z: 247 case HID_GD_RIGHT: usage->hat_dir = 3; break;
492 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ: 248 case HID_GD_LEFT: usage->hat_dir = 7; break;
493 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL: 249 default: goto unknown;
494 if (field->flags & HID_MAIN_ITEM_RELATIVE)
495 map_rel(usage->hid & 0xf);
496 else
497 map_abs(usage->hid & 0xf);
498 break;
499
500 case HID_GD_HATSWITCH:
501 usage->hat_min = field->logical_minimum;
502 usage->hat_max = field->logical_maximum;
503 map_abs(ABS_HAT0X);
504 break;
505
506 case HID_GD_START: map_key_clear(BTN_START); break;
507 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
508
509 default: goto unknown;
510 } 250 }
511 251 if (field->dpad) {
512 break; 252 map_abs(field->dpad);
513 253 goto ignore;
514 case HID_UP_LED:
515
516 switch (usage->hid & 0xffff) { /* HID-Value: */
517 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
518 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
519 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
520 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
521 case 0x05: map_led (LED_KANA); break; /* "Kana" */
522 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
523 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
524 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
525 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
526 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
527 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
528
529 default: goto ignore;
530 } 254 }
255 map_abs(ABS_HAT0X);
531 break; 256 break;
257 }
532 258
533 case HID_UP_DIGITIZER: 259 switch (usage->hid) {
534 260 /* These usage IDs map directly to the usage codes. */
535 switch (usage->hid & 0xff) { 261 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
536 262 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
537 case 0x30: /* TipPressure */ 263 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
538 if (!test_bit(BTN_TOUCH, input->keybit)) { 264 if (field->flags & HID_MAIN_ITEM_RELATIVE)
539 device->quirks |= HID_QUIRK_NOTOUCH; 265 map_rel(usage->hid & 0xf);
540 set_bit(EV_KEY, input->evbit); 266 else
541 set_bit(BTN_TOUCH, input->keybit); 267 map_abs(usage->hid & 0xf);
542 } 268 break;
543
544 map_abs_clear(ABS_PRESSURE);
545 break;
546
547 case 0x32: /* InRange */
548 switch (field->physical & 0xff) {
549 case 0x21: map_key(BTN_TOOL_MOUSE); break;
550 case 0x22: map_key(BTN_TOOL_FINGER); break;
551 default: map_key(BTN_TOOL_PEN); break;
552 }
553 break;
554 269
555 case 0x3c: /* Invert */ 270 case HID_GD_HATSWITCH:
556 map_key_clear(BTN_TOOL_RUBBER); 271 usage->hat_min = field->logical_minimum;
557 break; 272 usage->hat_max = field->logical_maximum;
273 map_abs(ABS_HAT0X);
274 break;
558 275
559 case 0x33: /* Touch */ 276 case HID_GD_START: map_key_clear(BTN_START); break;
560 case 0x42: /* TipSwitch */ 277 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
561 case 0x43: /* TipSwitch2 */
562 device->quirks &= ~HID_QUIRK_NOTOUCH;
563 map_key_clear(BTN_TOUCH);
564 break;
565 278
566 case 0x44: /* BarrelSwitch */ 279 default: goto unknown;
567 map_key_clear(BTN_STYLUS); 280 }
568 break;
569 281
570 default: goto unknown; 282 break;
283
284 case HID_UP_LED:
285 switch (usage->hid & 0xffff) { /* HID-Value: */
286 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
287 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
288 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
289 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
290 case 0x05: map_led (LED_KANA); break; /* "Kana" */
291 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
292 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
293 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
294 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
295 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
296 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
297
298 default: goto ignore;
299 }
300 break;
301
302 case HID_UP_DIGITIZER:
303 switch (usage->hid & 0xff) {
304 case 0x30: /* TipPressure */
305 if (!test_bit(BTN_TOUCH, input->keybit)) {
306 device->quirks |= HID_QUIRK_NOTOUCH;
307 set_bit(EV_KEY, input->evbit);
308 set_bit(BTN_TOUCH, input->keybit);
571 } 309 }
310 map_abs_clear(ABS_PRESSURE);
572 break; 311 break;
573 312
574 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */ 313 case 0x32: /* InRange */
575 314 switch (field->physical & 0xff) {
576 switch (usage->hid & HID_USAGE) { 315 case 0x21: map_key(BTN_TOOL_MOUSE); break;
577 case 0x000: goto ignore; 316 case 0x22: map_key(BTN_TOOL_FINGER); break;
578 case 0x034: map_key_clear(KEY_SLEEP); break; 317 default: map_key(BTN_TOOL_PEN); break;
579 case 0x036: map_key_clear(BTN_MISC); break;
580
581 case 0x040: map_key_clear(KEY_MENU); break;
582 case 0x045: map_key_clear(KEY_RADIO); break;
583
584 case 0x083: map_key_clear(KEY_LAST); break;
585 case 0x088: map_key_clear(KEY_PC); break;
586 case 0x089: map_key_clear(KEY_TV); break;
587 case 0x08a: map_key_clear(KEY_WWW); break;
588 case 0x08b: map_key_clear(KEY_DVD); break;
589 case 0x08c: map_key_clear(KEY_PHONE); break;
590 case 0x08d: map_key_clear(KEY_PROGRAM); break;
591 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
592 case 0x08f: map_key_clear(KEY_GAMES); break;
593 case 0x090: map_key_clear(KEY_MEMO); break;
594 case 0x091: map_key_clear(KEY_CD); break;
595 case 0x092: map_key_clear(KEY_VCR); break;
596 case 0x093: map_key_clear(KEY_TUNER); break;
597 case 0x094: map_key_clear(KEY_EXIT); break;
598 case 0x095: map_key_clear(KEY_HELP); break;
599 case 0x096: map_key_clear(KEY_TAPE); break;
600 case 0x097: map_key_clear(KEY_TV2); break;
601 case 0x098: map_key_clear(KEY_SAT); break;
602 case 0x09a: map_key_clear(KEY_PVR); break;
603
604 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
605 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
606 case 0x0a0: map_key_clear(KEY_VCR2); break;
607
608 case 0x0b0: map_key_clear(KEY_PLAY); break;
609 case 0x0b1: map_key_clear(KEY_PAUSE); break;
610 case 0x0b2: map_key_clear(KEY_RECORD); break;
611 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
612 case 0x0b4: map_key_clear(KEY_REWIND); break;
613 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
614 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
615 case 0x0b7: map_key_clear(KEY_STOPCD); break;
616 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
617 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
618
619 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
620 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
621 case 0x0e2: map_key_clear(KEY_MUTE); break;
622 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
623 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
624 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
625
626 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
627 case 0x183: map_key_clear(KEY_CONFIG); break;
628 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
629 case 0x185: map_key_clear(KEY_EDITOR); break;
630 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
631 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
632 case 0x188: map_key_clear(KEY_PRESENTATION); break;
633 case 0x189: map_key_clear(KEY_DATABASE); break;
634 case 0x18a: map_key_clear(KEY_MAIL); break;
635 case 0x18b: map_key_clear(KEY_NEWS); break;
636 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
637 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
638 case 0x18e: map_key_clear(KEY_CALENDAR); break;
639 case 0x191: map_key_clear(KEY_FINANCE); break;
640 case 0x192: map_key_clear(KEY_CALC); break;
641 case 0x194: map_key_clear(KEY_FILE); break;
642 case 0x196: map_key_clear(KEY_WWW); break;
643 case 0x19c: map_key_clear(KEY_LOGOFF); break;
644 case 0x19e: map_key_clear(KEY_COFFEE); break;
645 case 0x1a6: map_key_clear(KEY_HELP); break;
646 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
647 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
648 case 0x1b6: map_key_clear(KEY_MEDIA); break;
649 case 0x1b7: map_key_clear(KEY_SOUND); break;
650 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
651 case 0x1bd: map_key_clear(KEY_INFO); break;
652 case 0x201: map_key_clear(KEY_NEW); break;
653 case 0x202: map_key_clear(KEY_OPEN); break;
654 case 0x203: map_key_clear(KEY_CLOSE); break;
655 case 0x204: map_key_clear(KEY_EXIT); break;
656 case 0x207: map_key_clear(KEY_SAVE); break;
657 case 0x208: map_key_clear(KEY_PRINT); break;
658 case 0x209: map_key_clear(KEY_PROPS); break;
659 case 0x21a: map_key_clear(KEY_UNDO); break;
660 case 0x21b: map_key_clear(KEY_COPY); break;
661 case 0x21c: map_key_clear(KEY_CUT); break;
662 case 0x21d: map_key_clear(KEY_PASTE); break;
663 case 0x21f: map_key_clear(KEY_FIND); break;
664 case 0x221: map_key_clear(KEY_SEARCH); break;
665 case 0x222: map_key_clear(KEY_GOTO); break;
666 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
667 case 0x224: map_key_clear(KEY_BACK); break;
668 case 0x225: map_key_clear(KEY_FORWARD); break;
669 case 0x226: map_key_clear(KEY_STOP); break;
670 case 0x227: map_key_clear(KEY_REFRESH); break;
671 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
672 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
673 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
674 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
675 case 0x233: map_key_clear(KEY_SCROLLUP); break;
676 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
677 case 0x238: map_rel(REL_HWHEEL); break;
678 case 0x25f: map_key_clear(KEY_CANCEL); break;
679 case 0x279: map_key_clear(KEY_REDO); break;
680
681 case 0x289: map_key_clear(KEY_REPLY); break;
682 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
683 case 0x28c: map_key_clear(KEY_SEND); break;
684
685 default: goto ignore;
686 } 318 }
687 break; 319 break;
688 320
689 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */ 321 case 0x3c: /* Invert */
690 322 map_key_clear(BTN_TOOL_RUBBER);
691 set_bit(EV_REP, input->evbit);
692 switch (usage->hid & HID_USAGE) {
693 case 0x021: map_key_clear(KEY_PRINT); break;
694 case 0x070: map_key_clear(KEY_HP); break;
695 case 0x071: map_key_clear(KEY_CAMERA); break;
696 case 0x072: map_key_clear(KEY_SOUND); break;
697 case 0x073: map_key_clear(KEY_QUESTION); break;
698 case 0x080: map_key_clear(KEY_EMAIL); break;
699 case 0x081: map_key_clear(KEY_CHAT); break;
700 case 0x082: map_key_clear(KEY_SEARCH); break;
701 case 0x083: map_key_clear(KEY_CONNECT); break;
702 case 0x084: map_key_clear(KEY_FINANCE); break;
703 case 0x085: map_key_clear(KEY_SPORT); break;
704 case 0x086: map_key_clear(KEY_SHOP); break;
705 default: goto ignore;
706 }
707 break; 323 break;
708 324
709 case HID_UP_MSVENDOR: 325 case 0x33: /* Touch */
710 326 case 0x42: /* TipSwitch */
711 goto ignore; 327 case 0x43: /* TipSwitch2 */
712 328 device->quirks &= ~HID_QUIRK_NOTOUCH;
713 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */ 329 map_key_clear(BTN_TOUCH);
714 330 break;
715 set_bit(EV_REP, input->evbit);
716 switch(usage->hid & HID_USAGE) {
717 case 0x003:
718 /* The fn key on Apple USB keyboards */
719 map_key_clear(KEY_FN);
720 hidinput_apple_setup(input);
721 break;
722 331
723 default: goto ignore; 332 case 0x44: /* BarrelSwitch */
724 } 333 map_key_clear(BTN_STYLUS);
725 break; 334 break;
726 335
727 case HID_UP_LOGIVENDOR: 336 default: goto unknown;
337 }
338 break;
339
340 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
341 switch (usage->hid & HID_USAGE) {
342 case 0x000: goto ignore;
343 case 0x034: map_key_clear(KEY_SLEEP); break;
344 case 0x036: map_key_clear(BTN_MISC); break;
345
346 case 0x040: map_key_clear(KEY_MENU); break;
347 case 0x045: map_key_clear(KEY_RADIO); break;
348
349 case 0x083: map_key_clear(KEY_LAST); break;
350 case 0x088: map_key_clear(KEY_PC); break;
351 case 0x089: map_key_clear(KEY_TV); break;
352 case 0x08a: map_key_clear(KEY_WWW); break;
353 case 0x08b: map_key_clear(KEY_DVD); break;
354 case 0x08c: map_key_clear(KEY_PHONE); break;
355 case 0x08d: map_key_clear(KEY_PROGRAM); break;
356 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
357 case 0x08f: map_key_clear(KEY_GAMES); break;
358 case 0x090: map_key_clear(KEY_MEMO); break;
359 case 0x091: map_key_clear(KEY_CD); break;
360 case 0x092: map_key_clear(KEY_VCR); break;
361 case 0x093: map_key_clear(KEY_TUNER); break;
362 case 0x094: map_key_clear(KEY_EXIT); break;
363 case 0x095: map_key_clear(KEY_HELP); break;
364 case 0x096: map_key_clear(KEY_TAPE); break;
365 case 0x097: map_key_clear(KEY_TV2); break;
366 case 0x098: map_key_clear(KEY_SAT); break;
367 case 0x09a: map_key_clear(KEY_PVR); break;
368
369 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
370 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
371 case 0x0a0: map_key_clear(KEY_VCR2); break;
372
373 case 0x0b0: map_key_clear(KEY_PLAY); break;
374 case 0x0b1: map_key_clear(KEY_PAUSE); break;
375 case 0x0b2: map_key_clear(KEY_RECORD); break;
376 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
377 case 0x0b4: map_key_clear(KEY_REWIND); break;
378 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
379 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
380 case 0x0b7: map_key_clear(KEY_STOPCD); break;
381 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
382 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
383
384 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
385 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
386 case 0x0e2: map_key_clear(KEY_MUTE); break;
387 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
388 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
389 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
390
391 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
392 case 0x183: map_key_clear(KEY_CONFIG); break;
393 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
394 case 0x185: map_key_clear(KEY_EDITOR); break;
395 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
396 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
397 case 0x188: map_key_clear(KEY_PRESENTATION); break;
398 case 0x189: map_key_clear(KEY_DATABASE); break;
399 case 0x18a: map_key_clear(KEY_MAIL); break;
400 case 0x18b: map_key_clear(KEY_NEWS); break;
401 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
402 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
403 case 0x18e: map_key_clear(KEY_CALENDAR); break;
404 case 0x191: map_key_clear(KEY_FINANCE); break;
405 case 0x192: map_key_clear(KEY_CALC); break;
406 case 0x194: map_key_clear(KEY_FILE); break;
407 case 0x196: map_key_clear(KEY_WWW); break;
408 case 0x19c: map_key_clear(KEY_LOGOFF); break;
409 case 0x19e: map_key_clear(KEY_COFFEE); break;
410 case 0x1a6: map_key_clear(KEY_HELP); break;
411 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
412 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
413 case 0x1b6: map_key_clear(KEY_MEDIA); break;
414 case 0x1b7: map_key_clear(KEY_SOUND); break;
415 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
416 case 0x1bd: map_key_clear(KEY_INFO); break;
417 case 0x201: map_key_clear(KEY_NEW); break;
418 case 0x202: map_key_clear(KEY_OPEN); break;
419 case 0x203: map_key_clear(KEY_CLOSE); break;
420 case 0x204: map_key_clear(KEY_EXIT); break;
421 case 0x207: map_key_clear(KEY_SAVE); break;
422 case 0x208: map_key_clear(KEY_PRINT); break;
423 case 0x209: map_key_clear(KEY_PROPS); break;
424 case 0x21a: map_key_clear(KEY_UNDO); break;
425 case 0x21b: map_key_clear(KEY_COPY); break;
426 case 0x21c: map_key_clear(KEY_CUT); break;
427 case 0x21d: map_key_clear(KEY_PASTE); break;
428 case 0x21f: map_key_clear(KEY_FIND); break;
429 case 0x221: map_key_clear(KEY_SEARCH); break;
430 case 0x222: map_key_clear(KEY_GOTO); break;
431 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
432 case 0x224: map_key_clear(KEY_BACK); break;
433 case 0x225: map_key_clear(KEY_FORWARD); break;
434 case 0x226: map_key_clear(KEY_STOP); break;
435 case 0x227: map_key_clear(KEY_REFRESH); break;
436 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
437 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
438 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
439 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
440 case 0x233: map_key_clear(KEY_SCROLLUP); break;
441 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
442 case 0x238: map_rel(REL_HWHEEL); break;
443 case 0x25f: map_key_clear(KEY_CANCEL); break;
444 case 0x279: map_key_clear(KEY_REDO); break;
445
446 case 0x289: map_key_clear(KEY_REPLY); break;
447 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
448 case 0x28c: map_key_clear(KEY_SEND); break;
449
450 default: goto ignore;
451 }
452 break;
453
454 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
455 set_bit(EV_REP, input->evbit);
456 switch (usage->hid & HID_USAGE) {
457 case 0x021: map_key_clear(KEY_PRINT); break;
458 case 0x070: map_key_clear(KEY_HP); break;
459 case 0x071: map_key_clear(KEY_CAMERA); break;
460 case 0x072: map_key_clear(KEY_SOUND); break;
461 case 0x073: map_key_clear(KEY_QUESTION); break;
462 case 0x080: map_key_clear(KEY_EMAIL); break;
463 case 0x081: map_key_clear(KEY_CHAT); break;
464 case 0x082: map_key_clear(KEY_SEARCH); break;
465 case 0x083: map_key_clear(KEY_CONNECT); break;
466 case 0x084: map_key_clear(KEY_FINANCE); break;
467 case 0x085: map_key_clear(KEY_SPORT); break;
468 case 0x086: map_key_clear(KEY_SHOP); break;
469 default: goto ignore;
470 }
471 break;
728 472
729 goto ignore; 473 case HID_UP_MSVENDOR:
730 474 goto ignore;
731 case HID_UP_PID:
732 475
733 switch(usage->hid & HID_USAGE) { 476 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
734 case 0xa4: map_key_clear(BTN_DEAD); break; 477 set_bit(EV_REP, input->evbit);
735 default: goto ignore; 478 goto ignore;
736 }
737 break;
738 479
739 default: 480 case HID_UP_LOGIVENDOR:
740 unknown: 481 goto ignore;
741 if (field->report_size == 1) { 482
742 if (field->report->type == HID_OUTPUT_REPORT) { 483 case HID_UP_PID:
743 map_led(LED_MISC); 484 switch (usage->hid & HID_USAGE) {
744 break; 485 case 0xa4: map_key_clear(BTN_DEAD); break;
745 } 486 default: goto ignore;
746 map_key(BTN_MISC); 487 }
747 break; 488 break;
748 } 489
749 if (field->flags & HID_MAIN_ITEM_RELATIVE) { 490 default:
750 map_rel(REL_MISC); 491 unknown:
492 if (field->report_size == 1) {
493 if (field->report->type == HID_OUTPUT_REPORT) {
494 map_led(LED_MISC);
751 break; 495 break;
752 } 496 }
753 map_abs(ABS_MISC); 497 map_key(BTN_MISC);
498 break;
499 }
500 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
501 map_rel(REL_MISC);
754 break; 502 break;
503 }
504 map_abs(ABS_MISC);
505 break;
755 } 506 }
756 507
757mapped: 508mapped:
758 if (device->quirks & HID_QUIRK_MIGHTYMOUSE) { 509 if (device->driver->input_mapped && device->driver->input_mapped(device,
759 if (usage->hid == HID_GD_Z) 510 hidinput, field, usage, &bit, &max) < 0)
760 map_rel(REL_HWHEEL);
761 else if (usage->code == BTN_1)
762 map_key(BTN_2);
763 else if (usage->code == BTN_2)
764 map_key(BTN_1);
765 }
766
767 if ((device->quirks & (HID_QUIRK_2WHEEL_MOUSE_HACK_7 | HID_QUIRK_2WHEEL_MOUSE_HACK_5 |
768 HID_QUIRK_2WHEEL_MOUSE_HACK_B8)) && (usage->type == EV_REL) &&
769 (usage->code == REL_WHEEL))
770 set_bit(REL_HWHEEL, bit);
771
772 if (((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
773 || ((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007)))
774 goto ignore; 511 goto ignore;
775 512
776 if ((device->quirks & HID_QUIRK_BAD_RELATIVE_KEYS) &&
777 usage->type == EV_KEY && (field->flags & HID_MAIN_ITEM_RELATIVE))
778 field->flags &= ~HID_MAIN_ITEM_RELATIVE;
779
780 set_bit(usage->type, input->evbit); 513 set_bit(usage->type, input->evbit);
781 514
782 if (device->quirks & HID_QUIRK_DUPLICATE_USAGES &&
783 (usage->type == EV_KEY ||
784 usage->type == EV_REL ||
785 usage->type == EV_ABS))
786 clear_bit(usage->code, bit);
787
788 while (usage->code <= max && test_and_set_bit(usage->code, bit)) 515 while (usage->code <= max && test_and_set_bit(usage->code, bit))
789 usage->code = find_next_zero_bit(bit, max + 1, usage->code); 516 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
790 517
@@ -858,10 +585,6 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
858 if (!usage->type) 585 if (!usage->type)
859 return; 586 return;
860 587
861 /* handle input events for quirky devices */
862 if (hidinput_event_quirks(hid, field, usage, value))
863 return;
864
865 if (usage->hat_min < usage->hat_max || usage->hat_dir) { 588 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
866 int hat_dir = usage->hat_dir; 589 int hat_dir = usage->hat_dir;
867 if (!hat_dir) 590 if (!hat_dir)
@@ -961,14 +684,14 @@ static int hidinput_open(struct input_dev *dev)
961{ 684{
962 struct hid_device *hid = input_get_drvdata(dev); 685 struct hid_device *hid = input_get_drvdata(dev);
963 686
964 return hid->hid_open(hid); 687 return hid->ll_driver->open(hid);
965} 688}
966 689
967static void hidinput_close(struct input_dev *dev) 690static void hidinput_close(struct input_dev *dev)
968{ 691{
969 struct hid_device *hid = input_get_drvdata(dev); 692 struct hid_device *hid = input_get_drvdata(dev);
970 693
971 hid->hid_close(hid); 694 hid->ll_driver->close(hid);
972} 695}
973 696
974/* 697/*
@@ -977,7 +700,7 @@ static void hidinput_close(struct input_dev *dev)
977 * Read all reports and initialize the absolute field values. 700 * Read all reports and initialize the absolute field values.
978 */ 701 */
979 702
980int hidinput_connect(struct hid_device *hid) 703int hidinput_connect(struct hid_device *hid, unsigned int force)
981{ 704{
982 struct hid_report *report; 705 struct hid_report *report;
983 struct hid_input *hidinput = NULL; 706 struct hid_input *hidinput = NULL;
@@ -985,19 +708,20 @@ int hidinput_connect(struct hid_device *hid)
985 int i, j, k; 708 int i, j, k;
986 int max_report_type = HID_OUTPUT_REPORT; 709 int max_report_type = HID_OUTPUT_REPORT;
987 710
988 if (hid->quirks & HID_QUIRK_IGNORE_HIDINPUT)
989 return -1;
990
991 INIT_LIST_HEAD(&hid->inputs); 711 INIT_LIST_HEAD(&hid->inputs);
992 712
993 for (i = 0; i < hid->maxcollection; i++) 713 if (!force) {
994 if (hid->collection[i].type == HID_COLLECTION_APPLICATION || 714 for (i = 0; i < hid->maxcollection; i++) {
995 hid->collection[i].type == HID_COLLECTION_PHYSICAL) 715 struct hid_collection *col = &hid->collection[i];
996 if (IS_INPUT_APPLICATION(hid->collection[i].usage)) 716 if (col->type == HID_COLLECTION_APPLICATION ||
997 break; 717 col->type == HID_COLLECTION_PHYSICAL)
718 if (IS_INPUT_APPLICATION(col->usage))
719 break;
720 }
998 721
999 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDINPUT) == 0) 722 if (i == hid->maxcollection)
1000 return -1; 723 return -1;
724 }
1001 725
1002 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS) 726 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1003 max_report_type = HID_INPUT_REPORT; 727 max_report_type = HID_INPUT_REPORT;
@@ -1019,7 +743,8 @@ int hidinput_connect(struct hid_device *hid)
1019 } 743 }
1020 744
1021 input_set_drvdata(input_dev, hid); 745 input_set_drvdata(input_dev, hid);
1022 input_dev->event = hid->hidinput_input_event; 746 input_dev->event =
747 hid->ll_driver->hidinput_input_event;
1023 input_dev->open = hidinput_open; 748 input_dev->open = hidinput_open;
1024 input_dev->close = hidinput_close; 749 input_dev->close = hidinput_close;
1025 input_dev->setkeycode = hidinput_setkeycode; 750 input_dev->setkeycode = hidinput_setkeycode;
@@ -1032,7 +757,7 @@ int hidinput_connect(struct hid_device *hid)
1032 input_dev->id.vendor = hid->vendor; 757 input_dev->id.vendor = hid->vendor;
1033 input_dev->id.product = hid->product; 758 input_dev->id.product = hid->product;
1034 input_dev->id.version = hid->version; 759 input_dev->id.version = hid->version;
1035 input_dev->dev.parent = hid->dev; 760 input_dev->dev.parent = hid->dev.parent;
1036 hidinput->input = input_dev; 761 hidinput->input = input_dev;
1037 list_add_tail(&hidinput->list, &hid->inputs); 762 list_add_tail(&hidinput->list, &hid->inputs);
1038 } 763 }
diff --git a/drivers/hid/hid-lg.c b/drivers/hid/hid-lg.c
new file mode 100644
index 000000000000..406d8c82abf1
--- /dev/null
+++ b/drivers/hid/hid-lg.c
@@ -0,0 +1,342 @@
1/*
2 * HID driver for some logitech "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24#include "hid-lg.h"
25
26#define LG_RDESC 0x001
27#define LG_BAD_RELATIVE_KEYS 0x002
28#define LG_DUPLICATE_USAGES 0x004
29#define LG_RESET_LEDS 0x008
30#define LG_EXPANDED_KEYMAP 0x010
31#define LG_IGNORE_DOUBLED_WHEEL 0x020
32#define LG_WIRELESS 0x040
33#define LG_INVERT_HWHEEL 0x080
34#define LG_NOGET 0x100
35#define LG_FF 0x200
36#define LG_FF2 0x400
37
38/*
39 * Certain Logitech keyboards send in report #3 keys which are far
40 * above the logical maximum described in descriptor. This extends
41 * the original value of 0x28c of logical maximum to 0x104d
42 */
43static void lg_report_fixup(struct hid_device *hdev, __u8 *rdesc,
44 unsigned int rsize)
45{
46 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
47
48 if ((quirks & LG_RDESC) && rsize >= 90 && rdesc[83] == 0x26 &&
49 rdesc[84] == 0x8c && rdesc[85] == 0x02) {
50 dev_info(&hdev->dev, "fixing up Logitech keyboard report "
51 "descriptor\n");
52 rdesc[84] = rdesc[89] = 0x4d;
53 rdesc[85] = rdesc[90] = 0x10;
54 }
55}
56
57#define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
58 EV_KEY, (c))
59
60static int lg_ultrax_remote_mapping(struct hid_input *hi,
61 struct hid_usage *usage, unsigned long **bit, int *max)
62{
63 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
64 return 0;
65
66 set_bit(EV_REP, hi->input->evbit);
67 switch (usage->hid & HID_USAGE) {
68 /* Reported on Logitech Ultra X Media Remote */
69 case 0x004: lg_map_key_clear(KEY_AGAIN); break;
70 case 0x00d: lg_map_key_clear(KEY_HOME); break;
71 case 0x024: lg_map_key_clear(KEY_SHUFFLE); break;
72 case 0x025: lg_map_key_clear(KEY_TV); break;
73 case 0x026: lg_map_key_clear(KEY_MENU); break;
74 case 0x031: lg_map_key_clear(KEY_AUDIO); break;
75 case 0x032: lg_map_key_clear(KEY_TEXT); break;
76 case 0x033: lg_map_key_clear(KEY_LAST); break;
77 case 0x047: lg_map_key_clear(KEY_MP3); break;
78 case 0x048: lg_map_key_clear(KEY_DVD); break;
79 case 0x049: lg_map_key_clear(KEY_MEDIA); break;
80 case 0x04a: lg_map_key_clear(KEY_VIDEO); break;
81 case 0x04b: lg_map_key_clear(KEY_ANGLE); break;
82 case 0x04c: lg_map_key_clear(KEY_LANGUAGE); break;
83 case 0x04d: lg_map_key_clear(KEY_SUBTITLE); break;
84 case 0x051: lg_map_key_clear(KEY_RED); break;
85 case 0x052: lg_map_key_clear(KEY_CLOSE); break;
86
87 default:
88 return 0;
89 }
90 return 1;
91}
92
93static int lg_wireless_mapping(struct hid_input *hi, struct hid_usage *usage,
94 unsigned long **bit, int *max)
95{
96 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
97 return 0;
98
99 switch (usage->hid & HID_USAGE) {
100 case 0x1001: lg_map_key_clear(KEY_MESSENGER); break;
101 case 0x1003: lg_map_key_clear(KEY_SOUND); break;
102 case 0x1004: lg_map_key_clear(KEY_VIDEO); break;
103 case 0x1005: lg_map_key_clear(KEY_AUDIO); break;
104 case 0x100a: lg_map_key_clear(KEY_DOCUMENTS); break;
105 case 0x1011: lg_map_key_clear(KEY_PREVIOUSSONG); break;
106 case 0x1012: lg_map_key_clear(KEY_NEXTSONG); break;
107 case 0x1013: lg_map_key_clear(KEY_CAMERA); break;
108 case 0x1014: lg_map_key_clear(KEY_MESSENGER); break;
109 case 0x1015: lg_map_key_clear(KEY_RECORD); break;
110 case 0x1016: lg_map_key_clear(KEY_PLAYER); break;
111 case 0x1017: lg_map_key_clear(KEY_EJECTCD); break;
112 case 0x1018: lg_map_key_clear(KEY_MEDIA); break;
113 case 0x1019: lg_map_key_clear(KEY_PROG1); break;
114 case 0x101a: lg_map_key_clear(KEY_PROG2); break;
115 case 0x101b: lg_map_key_clear(KEY_PROG3); break;
116 case 0x101f: lg_map_key_clear(KEY_ZOOMIN); break;
117 case 0x1020: lg_map_key_clear(KEY_ZOOMOUT); break;
118 case 0x1021: lg_map_key_clear(KEY_ZOOMRESET); break;
119 case 0x1023: lg_map_key_clear(KEY_CLOSE); break;
120 case 0x1027: lg_map_key_clear(KEY_MENU); break;
121 /* this one is marked as 'Rotate' */
122 case 0x1028: lg_map_key_clear(KEY_ANGLE); break;
123 case 0x1029: lg_map_key_clear(KEY_SHUFFLE); break;
124 case 0x102a: lg_map_key_clear(KEY_BACK); break;
125 case 0x102b: lg_map_key_clear(KEY_CYCLEWINDOWS); break;
126 case 0x1041: lg_map_key_clear(KEY_BATTERY); break;
127 case 0x1042: lg_map_key_clear(KEY_WORDPROCESSOR); break;
128 case 0x1043: lg_map_key_clear(KEY_SPREADSHEET); break;
129 case 0x1044: lg_map_key_clear(KEY_PRESENTATION); break;
130 case 0x1045: lg_map_key_clear(KEY_UNDO); break;
131 case 0x1046: lg_map_key_clear(KEY_REDO); break;
132 case 0x1047: lg_map_key_clear(KEY_PRINT); break;
133 case 0x1048: lg_map_key_clear(KEY_SAVE); break;
134 case 0x1049: lg_map_key_clear(KEY_PROG1); break;
135 case 0x104a: lg_map_key_clear(KEY_PROG2); break;
136 case 0x104b: lg_map_key_clear(KEY_PROG3); break;
137 case 0x104c: lg_map_key_clear(KEY_PROG4); break;
138
139 default:
140 return 0;
141 }
142 return 1;
143}
144
145static int lg_input_mapping(struct hid_device *hdev, struct hid_input *hi,
146 struct hid_field *field, struct hid_usage *usage,
147 unsigned long **bit, int *max)
148{
149 /* extended mapping for certain Logitech hardware (Logitech cordless
150 desktop LX500) */
151 static const u8 e_keymap[] = {
152 0,216, 0,213,175,156, 0, 0, 0, 0,
153 144, 0, 0, 0, 0, 0, 0, 0, 0,212,
154 174,167,152,161,112, 0, 0, 0,154, 0,
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0,183,184,185,186,187,
159 188,189,190,191,192,193,194, 0, 0, 0
160 };
161 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
162 unsigned int hid = usage->hid;
163
164 if (hdev->product == USB_DEVICE_ID_LOGITECH_RECEIVER &&
165 lg_ultrax_remote_mapping(hi, usage, bit, max))
166 return 1;
167
168 if ((quirks & LG_WIRELESS) && lg_wireless_mapping(hi, usage, bit, max))
169 return 1;
170
171 if ((hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
172 return 0;
173
174 hid &= HID_USAGE;
175
176 /* Special handling for Logitech Cordless Desktop */
177 if (field->application == HID_GD_MOUSE) {
178 if ((quirks & LG_IGNORE_DOUBLED_WHEEL) &&
179 (hid == 7 || hid == 8))
180 return -1;
181 } else {
182 if ((quirks & LG_EXPANDED_KEYMAP) &&
183 hid < ARRAY_SIZE(e_keymap) &&
184 e_keymap[hid] != 0) {
185 hid_map_usage(hi, usage, bit, max, EV_KEY,
186 e_keymap[hid]);
187 return 1;
188 }
189 }
190
191 return 0;
192}
193
194static int lg_input_mapped(struct hid_device *hdev, struct hid_input *hi,
195 struct hid_field *field, struct hid_usage *usage,
196 unsigned long **bit, int *max)
197{
198 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
199
200 if ((quirks & LG_BAD_RELATIVE_KEYS) && usage->type == EV_KEY &&
201 (field->flags & HID_MAIN_ITEM_RELATIVE))
202 field->flags &= ~HID_MAIN_ITEM_RELATIVE;
203
204 if ((quirks & LG_DUPLICATE_USAGES) && (usage->type == EV_KEY ||
205 usage->type == EV_REL || usage->type == EV_ABS))
206 clear_bit(usage->code, *bit);
207
208 return 0;
209}
210
211static int lg_event(struct hid_device *hdev, struct hid_field *field,
212 struct hid_usage *usage, __s32 value)
213{
214 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
215
216 if ((quirks & LG_INVERT_HWHEEL) && usage->code == REL_HWHEEL) {
217 input_event(field->hidinput->input, usage->type, usage->code,
218 -value);
219 return 1;
220 }
221
222 return 0;
223}
224
225static int lg_probe(struct hid_device *hdev, const struct hid_device_id *id)
226{
227 unsigned long quirks = id->driver_data;
228 unsigned int connect_mask = HID_CONNECT_DEFAULT;
229 int ret;
230
231 hid_set_drvdata(hdev, (void *)quirks);
232
233 if (quirks & LG_NOGET)
234 hdev->quirks |= HID_QUIRK_NOGET;
235
236 ret = hid_parse(hdev);
237 if (ret) {
238 dev_err(&hdev->dev, "parse failed\n");
239 goto err_free;
240 }
241
242 if (quirks & (LG_FF | LG_FF2))
243 connect_mask &= ~HID_CONNECT_FF;
244
245 ret = hid_hw_start(hdev, connect_mask);
246 if (ret) {
247 dev_err(&hdev->dev, "hw start failed\n");
248 goto err_free;
249 }
250
251 if (quirks & LG_RESET_LEDS)
252 usbhid_set_leds(hdev);
253
254 if (quirks & LG_FF)
255 lgff_init(hdev);
256 if (quirks & LG_FF2)
257 lg2ff_init(hdev);
258
259 return 0;
260err_free:
261 return ret;
262}
263
264static const struct hid_device_id lg_devices[] = {
265 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
266 .driver_data = LG_RDESC | LG_WIRELESS },
267 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER),
268 .driver_data = LG_RDESC | LG_WIRELESS },
269 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2),
270 .driver_data = LG_RDESC | LG_WIRELESS },
271
272 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER),
273 .driver_data = LG_BAD_RELATIVE_KEYS },
274
275 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP),
276 .driver_data = LG_DUPLICATE_USAGES },
277 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE),
278 .driver_data = LG_DUPLICATE_USAGES },
279 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI),
280 .driver_data = LG_DUPLICATE_USAGES },
281
282 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD),
283 .driver_data = LG_RESET_LEDS },
284
285 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD),
286 .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
287 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500),
288 .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
289
290 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3),
291 .driver_data = LG_INVERT_HWHEEL },
292 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150),
293 .driver_data = LG_INVERT_HWHEEL },
294
295 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D),
296 .driver_data = LG_NOGET },
297 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL),
298 .driver_data = LG_NOGET | LG_FF },
299
300 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD),
301 .driver_data = LG_FF },
302 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2),
303 .driver_data = LG_FF },
304 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D),
305 .driver_data = LG_FF },
306 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO),
307 .driver_data = LG_FF },
308 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL),
309 .driver_data = LG_FF },
310 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2),
311 .driver_data = LG_FF },
312 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2),
313 .driver_data = LG_FF2 },
314 { }
315};
316MODULE_DEVICE_TABLE(hid, lg_devices);
317
318static struct hid_driver lg_driver = {
319 .name = "logitech",
320 .id_table = lg_devices,
321 .report_fixup = lg_report_fixup,
322 .input_mapping = lg_input_mapping,
323 .input_mapped = lg_input_mapped,
324 .event = lg_event,
325 .probe = lg_probe,
326};
327
328static int lg_init(void)
329{
330 return hid_register_driver(&lg_driver);
331}
332
333static void lg_exit(void)
334{
335 hid_unregister_driver(&lg_driver);
336}
337
338module_init(lg_init);
339module_exit(lg_exit);
340MODULE_LICENSE("GPL");
341
342HID_COMPAT_LOAD_DRIVER(logitech);
diff --git a/drivers/hid/hid-lg.h b/drivers/hid/hid-lg.h
new file mode 100644
index 000000000000..27ae750ca878
--- /dev/null
+++ b/drivers/hid/hid-lg.h
@@ -0,0 +1,18 @@
1#ifndef __HID_LG_H
2#define __HID_LG_H
3
4#include <linux/autoconf.h>
5
6#ifdef CONFIG_LOGITECH_FF
7int lgff_init(struct hid_device *hdev);
8#else
9static inline int lgff_init(struct hid_device *hdev) { return -1; }
10#endif
11
12#ifdef CONFIG_LOGIRUMBLEPAD2_FF
13int lg2ff_init(struct hid_device *hdev);
14#else
15static inline int lg2ff_init(struct hid_device *hdev) { return -1; }
16#endif
17
18#endif
diff --git a/drivers/hid/usbhid/hid-lg2ff.c b/drivers/hid/hid-lg2ff.c
index d469bd0061c9..4e6dc6e26523 100644
--- a/drivers/hid/usbhid/hid-lg2ff.c
+++ b/drivers/hid/hid-lg2ff.c
@@ -24,7 +24,9 @@
24#include <linux/input.h> 24#include <linux/input.h>
25#include <linux/usb.h> 25#include <linux/usb.h>
26#include <linux/hid.h> 26#include <linux/hid.h>
27#include "usbhid.h" 27
28#include "usbhid/usbhid.h"
29#include "hid-lg.h"
28 30
29struct lg2ff_device { 31struct lg2ff_device {
30 struct hid_report *report; 32 struct hid_report *report;
@@ -57,7 +59,7 @@ static int play_effect(struct input_dev *dev, void *data,
57 return 0; 59 return 0;
58} 60}
59 61
60int hid_lg2ff_init(struct hid_device *hid) 62int lg2ff_init(struct hid_device *hid)
61{ 63{
62 struct lg2ff_device *lg2ff; 64 struct lg2ff_device *lg2ff;
63 struct hid_report *report; 65 struct hid_report *report;
@@ -69,18 +71,18 @@ int hid_lg2ff_init(struct hid_device *hid)
69 int error; 71 int error;
70 72
71 if (list_empty(report_list)) { 73 if (list_empty(report_list)) {
72 printk(KERN_ERR "hid-lg2ff: no output report found\n"); 74 dev_err(&hid->dev, "no output report found\n");
73 return -ENODEV; 75 return -ENODEV;
74 } 76 }
75 77
76 report = list_entry(report_list->next, struct hid_report, list); 78 report = list_entry(report_list->next, struct hid_report, list);
77 79
78 if (report->maxfield < 1) { 80 if (report->maxfield < 1) {
79 printk(KERN_ERR "hid-lg2ff: output report is empty\n"); 81 dev_err(&hid->dev, "output report is empty\n");
80 return -ENODEV; 82 return -ENODEV;
81 } 83 }
82 if (report->field[0]->report_count < 7) { 84 if (report->field[0]->report_count < 7) {
83 printk(KERN_ERR "hid-lg2ff: not enough values in the field\n"); 85 dev_err(&hid->dev, "not enough values in the field\n");
84 return -ENODEV; 86 return -ENODEV;
85 } 87 }
86 88
@@ -107,7 +109,7 @@ int hid_lg2ff_init(struct hid_device *hid)
107 109
108 usbhid_submit_report(hid, report, USB_DIR_OUT); 110 usbhid_submit_report(hid, report, USB_DIR_OUT);
109 111
110 printk(KERN_INFO "Force feedback for Logitech Rumblepad 2 by " 112 dev_info(&hid->dev, "Force feedback for Logitech Rumblepad 2 by "
111 "Anssi Hannula <anssi.hannula@gmail.com>\n"); 113 "Anssi Hannula <anssi.hannula@gmail.com>\n");
112 114
113 return 0; 115 return 0;
diff --git a/drivers/hid/usbhid/hid-lgff.c b/drivers/hid/hid-lgff.c
index 4b7ab6a46d93..51aff08e10ce 100644
--- a/drivers/hid/usbhid/hid-lgff.c
+++ b/drivers/hid/hid-lgff.c
@@ -30,7 +30,9 @@
30#include <linux/input.h> 30#include <linux/input.h>
31#include <linux/usb.h> 31#include <linux/usb.h>
32#include <linux/hid.h> 32#include <linux/hid.h>
33#include "usbhid.h" 33
34#include "usbhid/usbhid.h"
35#include "hid-lg.h"
34 36
35struct dev_type { 37struct dev_type {
36 u16 idVendor; 38 u16 idVendor;
@@ -48,6 +50,12 @@ static const signed short ff_joystick[] = {
48 -1 50 -1
49}; 51};
50 52
53static const signed short ff_wheel[] = {
54 FF_CONSTANT,
55 FF_AUTOCENTER,
56 -1
57};
58
51static const struct dev_type devices[] = { 59static const struct dev_type devices[] = {
52 { 0x046d, 0xc211, ff_rumble }, 60 { 0x046d, 0xc211, ff_rumble },
53 { 0x046d, 0xc219, ff_rumble }, 61 { 0x046d, 0xc219, ff_rumble },
@@ -55,7 +63,7 @@ static const struct dev_type devices[] = {
55 { 0x046d, 0xc286, ff_joystick }, 63 { 0x046d, 0xc286, ff_joystick },
56 { 0x046d, 0xc294, ff_joystick }, 64 { 0x046d, 0xc294, ff_joystick },
57 { 0x046d, 0xc295, ff_joystick }, 65 { 0x046d, 0xc295, ff_joystick },
58 { 0x046d, 0xca03, ff_joystick }, 66 { 0x046d, 0xca03, ff_wheel },
59}; 67};
60 68
61static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 69static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
@@ -100,7 +108,24 @@ static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *ef
100 return 0; 108 return 0;
101} 109}
102 110
103int hid_lgff_init(struct hid_device* hid) 111static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
112{
113 struct hid_device *hid = input_get_drvdata(dev);
114 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
115 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
116 __s32 *value = report->field[0]->value;
117 magnitude = (magnitude >> 12) & 0xf;
118 *value++ = 0xfe;
119 *value++ = 0x0d;
120 *value++ = magnitude; /* clockwise strength */
121 *value++ = magnitude; /* counter-clockwise strength */
122 *value++ = 0x80;
123 *value++ = 0x00;
124 *value = 0x00;
125 usbhid_submit_report(hid, report, USB_DIR_OUT);
126}
127
128int lgff_init(struct hid_device* hid)
104{ 129{
105 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 130 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
106 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 131 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
@@ -145,6 +170,9 @@ int hid_lgff_init(struct hid_device* hid)
145 if (error) 170 if (error)
146 return error; 171 return error;
147 172
173 if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
174 dev->ff->set_autocenter = hid_lgff_set_autocenter;
175
148 printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n"); 176 printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
149 177
150 return 0; 178 return 0;
diff --git a/drivers/hid/hid-microsoft.c b/drivers/hid/hid-microsoft.c
new file mode 100644
index 000000000000..d718b1607d0f
--- /dev/null
+++ b/drivers/hid/hid-microsoft.c
@@ -0,0 +1,219 @@
1/*
2 * HID driver for some microsoft "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23
24#include "hid-ids.h"
25
26#define MS_HIDINPUT 0x01
27#define MS_ERGONOMY 0x02
28#define MS_PRESENTER 0x04
29#define MS_RDESC 0x08
30#define MS_NOGET 0x10
31
32/*
33 * Microsoft Wireless Desktop Receiver (Model 1028) has several
34 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
35 */
36static void ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
37 unsigned int rsize)
38{
39 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
40
41 if ((quirks & MS_RDESC) && rsize == 571 && rdesc[284] == 0x19 &&
42 rdesc[286] == 0x2a && rdesc[304] == 0x19 &&
43 rdesc[306] == 0x29 && rdesc[352] == 0x1a &&
44 rdesc[355] == 0x2a && rdesc[557] == 0x19 &&
45 rdesc[559] == 0x29) {
46 dev_info(&hdev->dev, "fixing up Microsoft Wireless Receiver "
47 "Model 1028 report descriptor\n");
48 rdesc[284] = rdesc[304] = rdesc[557] = 0x35;
49 rdesc[352] = 0x36;
50 rdesc[286] = rdesc[355] = 0x46;
51 rdesc[306] = rdesc[559] = 0x45;
52 }
53}
54
55#define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
56 EV_KEY, (c))
57static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
58 unsigned long **bit, int *max)
59{
60 struct input_dev *input = hi->input;
61
62 switch (usage->hid & HID_USAGE) {
63 case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
64 case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
65 case 0xff05:
66 set_bit(EV_REP, input->evbit);
67 ms_map_key_clear(KEY_F13);
68 set_bit(KEY_F14, input->keybit);
69 set_bit(KEY_F15, input->keybit);
70 set_bit(KEY_F16, input->keybit);
71 set_bit(KEY_F17, input->keybit);
72 set_bit(KEY_F18, input->keybit);
73 default:
74 return 0;
75 }
76 return 1;
77}
78
79static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
80 unsigned long **bit, int *max)
81{
82 set_bit(EV_REP, hi->input->evbit);
83 switch (usage->hid & HID_USAGE) {
84 case 0xfd08: ms_map_key_clear(KEY_FORWARD); break;
85 case 0xfd09: ms_map_key_clear(KEY_BACK); break;
86 case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break;
87 case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break;
88 case 0xfd0f: ms_map_key_clear(KEY_PLAY); break;
89 default:
90 return 0;
91 }
92 return 1;
93}
94
95static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
96 struct hid_field *field, struct hid_usage *usage,
97 unsigned long **bit, int *max)
98{
99 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
100
101 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
102 return 0;
103
104 if (quirks & MS_ERGONOMY) {
105 int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
106 if (ret)
107 return ret;
108 }
109
110 if ((quirks & MS_PRESENTER) &&
111 ms_presenter_8k_quirk(hi, usage, bit, max))
112 return 1;
113
114 return 0;
115}
116
117static int ms_event(struct hid_device *hdev, struct hid_field *field,
118 struct hid_usage *usage, __s32 value)
119{
120 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
121
122 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
123 !usage->type)
124 return 0;
125
126 /* Handling MS keyboards special buttons */
127 if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
128 struct input_dev *input = field->hidinput->input;
129 static unsigned int last_key = 0;
130 unsigned int key = 0;
131 switch (value) {
132 case 0x01: key = KEY_F14; break;
133 case 0x02: key = KEY_F15; break;
134 case 0x04: key = KEY_F16; break;
135 case 0x08: key = KEY_F17; break;
136 case 0x10: key = KEY_F18; break;
137 }
138 if (key) {
139 input_event(input, usage->type, key, 1);
140 last_key = key;
141 } else
142 input_event(input, usage->type, last_key, 0);
143
144 return 1;
145 }
146
147 return 0;
148}
149
150static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
151{
152 unsigned long quirks = id->driver_data;
153 int ret;
154
155 hid_set_drvdata(hdev, (void *)quirks);
156
157 if (quirks & MS_NOGET)
158 hdev->quirks |= HID_QUIRK_NOGET;
159
160 ret = hid_parse(hdev);
161 if (ret) {
162 dev_err(&hdev->dev, "parse failed\n");
163 goto err_free;
164 }
165
166 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
167 HID_CONNECT_HIDINPUT_FORCE : 0));
168 if (ret) {
169 dev_err(&hdev->dev, "hw start failed\n");
170 goto err_free;
171 }
172
173 return 0;
174err_free:
175 return ret;
176}
177
178static const struct hid_device_id ms_devices[] = {
179 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
180 .driver_data = MS_HIDINPUT },
181 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
182 .driver_data = MS_ERGONOMY },
183 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
184 .driver_data = MS_ERGONOMY | MS_RDESC },
185 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
186 .driver_data = MS_PRESENTER },
187 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
188 .driver_data = MS_NOGET },
189
190 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
191 .driver_data = MS_PRESENTER },
192 { }
193};
194MODULE_DEVICE_TABLE(hid, ms_devices);
195
196static struct hid_driver ms_driver = {
197 .name = "microsoft",
198 .id_table = ms_devices,
199 .report_fixup = ms_report_fixup,
200 .input_mapping = ms_input_mapping,
201 .event = ms_event,
202 .probe = ms_probe,
203};
204
205static int ms_init(void)
206{
207 return hid_register_driver(&ms_driver);
208}
209
210static void ms_exit(void)
211{
212 hid_unregister_driver(&ms_driver);
213}
214
215module_init(ms_init);
216module_exit(ms_exit);
217MODULE_LICENSE("GPL");
218
219HID_COMPAT_LOAD_DRIVER(microsoft);
diff --git a/drivers/hid/hid-monterey.c b/drivers/hid/hid-monterey.c
new file mode 100644
index 000000000000..f3a85a065f18
--- /dev/null
+++ b/drivers/hid/hid-monterey.c
@@ -0,0 +1,82 @@
1/*
2 * HID driver for some monterey "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25static void mr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
26 unsigned int rsize)
27{
28 if (rsize >= 30 && rdesc[29] == 0x05 && rdesc[30] == 0x09) {
29 dev_info(&hdev->dev, "fixing up button/consumer in HID report "
30 "descriptor\n");
31 rdesc[30] = 0x0c;
32 }
33}
34
35#define mr_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
36 EV_KEY, (c))
37static int mr_input_mapping(struct hid_device *hdev, struct hid_input *hi,
38 struct hid_field *field, struct hid_usage *usage,
39 unsigned long **bit, int *max)
40{
41 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
42 return 0;
43
44 switch (usage->hid & HID_USAGE) {
45 case 0x156: mr_map_key_clear(KEY_WORDPROCESSOR); break;
46 case 0x157: mr_map_key_clear(KEY_SPREADSHEET); break;
47 case 0x158: mr_map_key_clear(KEY_PRESENTATION); break;
48 case 0x15c: mr_map_key_clear(KEY_STOP); break;
49 default:
50 return 0;
51 }
52 return 1;
53}
54
55static const struct hid_device_id mr_devices[] = {
56 { HID_USB_DEVICE(USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E) },
57 { }
58};
59MODULE_DEVICE_TABLE(hid, mr_devices);
60
61static struct hid_driver mr_driver = {
62 .name = "monterey",
63 .id_table = mr_devices,
64 .report_fixup = mr_report_fixup,
65 .input_mapping = mr_input_mapping,
66};
67
68static int mr_init(void)
69{
70 return hid_register_driver(&mr_driver);
71}
72
73static void mr_exit(void)
74{
75 hid_unregister_driver(&mr_driver);
76}
77
78module_init(mr_init);
79module_exit(mr_exit);
80MODULE_LICENSE("GPL");
81
82HID_COMPAT_LOAD_DRIVER(monterey);
diff --git a/drivers/hid/hid-petalynx.c b/drivers/hid/hid-petalynx.c
new file mode 100644
index 000000000000..10945fe12d50
--- /dev/null
+++ b/drivers/hid/hid-petalynx.c
@@ -0,0 +1,122 @@
1/*
2 * HID driver for some petalynx "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25/* Petalynx Maxter Remote has maximum for consumer page set too low */
26static void pl_report_fixup(struct hid_device *hdev, __u8 *rdesc,
27 unsigned int rsize)
28{
29 if (rsize >= 60 && rdesc[39] == 0x2a && rdesc[40] == 0xf5 &&
30 rdesc[41] == 0x00 && rdesc[59] == 0x26 &&
31 rdesc[60] == 0xf9 && rdesc[61] == 0x00) {
32 dev_info(&hdev->dev, "fixing up Petalynx Maxter Remote report "
33 "descriptor\n");
34 rdesc[60] = 0xfa;
35 rdesc[40] = 0xfa;
36 }
37}
38
39#define pl_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
40 EV_KEY, (c))
41static int pl_input_mapping(struct hid_device *hdev, struct hid_input *hi,
42 struct hid_field *field, struct hid_usage *usage,
43 unsigned long **bit, int *max)
44{
45 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_LOGIVENDOR) {
46 switch (usage->hid & HID_USAGE) {
47 case 0x05a: pl_map_key_clear(KEY_TEXT); break;
48 case 0x05b: pl_map_key_clear(KEY_RED); break;
49 case 0x05c: pl_map_key_clear(KEY_GREEN); break;
50 case 0x05d: pl_map_key_clear(KEY_YELLOW); break;
51 case 0x05e: pl_map_key_clear(KEY_BLUE); break;
52 default:
53 return 0;
54 }
55 return 1;
56 }
57
58 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
59 switch (usage->hid & HID_USAGE) {
60 case 0x0f6: pl_map_key_clear(KEY_NEXT); break;
61 case 0x0fa: pl_map_key_clear(KEY_BACK); break;
62 default:
63 return 0;
64 }
65 return 1;
66 }
67
68 return 0;
69}
70
71static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
72{
73 int ret;
74
75 hdev->quirks |= HID_QUIRK_NOGET;
76
77 ret = hid_parse(hdev);
78 if (ret) {
79 dev_err(&hdev->dev, "parse failed\n");
80 goto err_free;
81 }
82
83 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
84 if (ret) {
85 dev_err(&hdev->dev, "hw start failed\n");
86 goto err_free;
87 }
88
89 return 0;
90err_free:
91 return ret;
92}
93
94static const struct hid_device_id pl_devices[] = {
95 { HID_USB_DEVICE(USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE) },
96 { }
97};
98MODULE_DEVICE_TABLE(hid, pl_devices);
99
100static struct hid_driver pl_driver = {
101 .name = "petalynx",
102 .id_table = pl_devices,
103 .report_fixup = pl_report_fixup,
104 .input_mapping = pl_input_mapping,
105 .probe = pl_probe,
106};
107
108static int pl_init(void)
109{
110 return hid_register_driver(&pl_driver);
111}
112
113static void pl_exit(void)
114{
115 hid_unregister_driver(&pl_driver);
116}
117
118module_init(pl_init);
119module_exit(pl_exit);
120MODULE_LICENSE("GPL");
121
122HID_COMPAT_LOAD_DRIVER(petalynx);
diff --git a/drivers/hid/usbhid/hid-plff.c b/drivers/hid/hid-pl.c
index 9eb83cf9d22b..acd815586182 100644
--- a/drivers/hid/usbhid/hid-plff.c
+++ b/drivers/hid/hid-pl.c
@@ -9,7 +9,7 @@
9 * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT) 9 * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
10 * 10 *
11 * 0e8f:0003 "GreenAsia Inc. USB Joystick " 11 * 0e8f:0003 "GreenAsia Inc. USB Joystick "
12 * - tested with Köng Gaming gamepad 12 * - tested with K??ng Gaming gamepad
13 * 13 *
14 * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com> 14 * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
15 */ 15 */
@@ -38,7 +38,11 @@
38#include <linux/input.h> 38#include <linux/input.h>
39#include <linux/usb.h> 39#include <linux/usb.h>
40#include <linux/hid.h> 40#include <linux/hid.h>
41#include "usbhid.h" 41
42#include "hid-ids.h"
43
44#ifdef CONFIG_PANTHERLORD_FF
45#include "usbhid/usbhid.h"
42 46
43struct plff_device { 47struct plff_device {
44 struct hid_report *report; 48 struct hid_report *report;
@@ -66,7 +70,7 @@ static int hid_plff_play(struct input_dev *dev, void *data,
66 return 0; 70 return 0;
67} 71}
68 72
69int hid_plff_init(struct hid_device *hid) 73static int plff_init(struct hid_device *hid)
70{ 74{
71 struct plff_device *plff; 75 struct plff_device *plff;
72 struct hid_report *report; 76 struct hid_report *report;
@@ -86,7 +90,7 @@ int hid_plff_init(struct hid_device *hid)
86 currently unknown. */ 90 currently unknown. */
87 91
88 if (list_empty(report_list)) { 92 if (list_empty(report_list)) {
89 printk(KERN_ERR "hid-plff: no output reports found\n"); 93 dev_err(&hid->dev, "no output reports found\n");
90 return -ENODEV; 94 return -ENODEV;
91 } 95 }
92 96
@@ -95,18 +99,19 @@ int hid_plff_init(struct hid_device *hid)
95 report_ptr = report_ptr->next; 99 report_ptr = report_ptr->next;
96 100
97 if (report_ptr == report_list) { 101 if (report_ptr == report_list) {
98 printk(KERN_ERR "hid-plff: required output report is missing\n"); 102 dev_err(&hid->dev, "required output report is "
103 "missing\n");
99 return -ENODEV; 104 return -ENODEV;
100 } 105 }
101 106
102 report = list_entry(report_ptr, struct hid_report, list); 107 report = list_entry(report_ptr, struct hid_report, list);
103 if (report->maxfield < 1) { 108 if (report->maxfield < 1) {
104 printk(KERN_ERR "hid-plff: no fields in the report\n"); 109 dev_err(&hid->dev, "no fields in the report\n");
105 return -ENODEV; 110 return -ENODEV;
106 } 111 }
107 112
108 if (report->field[0]->report_count < 4) { 113 if (report->field[0]->report_count < 4) {
109 printk(KERN_ERR "hid-plff: not enough values in the field\n"); 114 dev_err(&hid->dev, "not enough values in the field\n");
110 return -ENODEV; 115 return -ENODEV;
111 } 116 }
112 117
@@ -132,8 +137,70 @@ int hid_plff_init(struct hid_device *hid)
132 usbhid_submit_report(hid, plff->report, USB_DIR_OUT); 137 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
133 } 138 }
134 139
135 printk(KERN_INFO "hid-plff: Force feedback for PantherLord/GreenAsia " 140 dev_info(&hid->dev, "Force feedback for PantherLord/GreenAsia "
136 "devices by Anssi Hannula <anssi.hannula@gmail.com>\n"); 141 "devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
137 142
138 return 0; 143 return 0;
139} 144}
145#else
146static inline int plff_init(struct hid_device *hid)
147{
148 return 0;
149}
150#endif
151
152static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
153{
154 int ret;
155
156 if (id->driver_data)
157 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
158
159 ret = hid_parse(hdev);
160 if (ret) {
161 dev_err(&hdev->dev, "parse failed\n");
162 goto err;
163 }
164
165 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
166 if (ret) {
167 dev_err(&hdev->dev, "hw start failed\n");
168 goto err;
169 }
170
171 plff_init(hdev);
172
173 return 0;
174err:
175 return ret;
176}
177
178static const struct hid_device_id pl_devices[] = {
179 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
180 .driver_data = 1 }, /* Twin USB Joystick */
181 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), }, /* GreenAsia Inc. USB Joystick */
182 { }
183};
184MODULE_DEVICE_TABLE(hid, pl_devices);
185
186static struct hid_driver pl_driver = {
187 .name = "pantherlord",
188 .id_table = pl_devices,
189 .probe = pl_probe,
190};
191
192static int pl_init(void)
193{
194 return hid_register_driver(&pl_driver);
195}
196
197static void pl_exit(void)
198{
199 hid_unregister_driver(&pl_driver);
200}
201
202module_init(pl_init);
203module_exit(pl_exit);
204MODULE_LICENSE("GPL");
205
206HID_COMPAT_LOAD_DRIVER(pantherlord);
diff --git a/drivers/hid/hid-samsung.c b/drivers/hid/hid-samsung.c
new file mode 100644
index 000000000000..15f3c0492450
--- /dev/null
+++ b/drivers/hid/hid-samsung.c
@@ -0,0 +1,100 @@
1/*
2 * HID driver for some samsung "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25/*
26 * Samsung IrDA remote controller (reports as Cypress USB Mouse).
27 *
28 * Vendor specific report #4 has a size of 48 bit,
29 * and therefore is not accepted when inspecting the descriptors.
30 * As a workaround we reinterpret the report as:
31 * Variable type, count 6, size 8 bit, log. maximum 255
32 * The burden to reconstruct the data is moved into user space.
33 */
34static void samsung_report_fixup(struct hid_device *hdev, __u8 *rdesc,
35 unsigned int rsize)
36{
37 if (rsize >= 182 && rdesc[175] == 0x25 && rdesc[176] == 0x40 &&
38 rdesc[177] == 0x75 && rdesc[178] == 0x30 &&
39 rdesc[179] == 0x95 && rdesc[180] == 0x01 &&
40 rdesc[182] == 0x40) {
41 dev_info(&hdev->dev, "fixing up Samsung IrDA report "
42 "descriptor\n");
43 rdesc[176] = 0xff;
44 rdesc[178] = 0x08;
45 rdesc[180] = 0x06;
46 rdesc[182] = 0x42;
47 }
48}
49
50static int samsung_probe(struct hid_device *hdev,
51 const struct hid_device_id *id)
52{
53 int ret;
54
55 ret = hid_parse(hdev);
56 if (ret) {
57 dev_err(&hdev->dev, "parse failed\n");
58 goto err_free;
59 }
60
61 ret = hid_hw_start(hdev, (HID_CONNECT_DEFAULT & ~HID_CONNECT_HIDINPUT) |
62 HID_CONNECT_HIDDEV_FORCE);
63 if (ret) {
64 dev_err(&hdev->dev, "hw start failed\n");
65 goto err_free;
66 }
67
68 return 0;
69err_free:
70 return ret;
71}
72
73static const struct hid_device_id samsung_devices[] = {
74 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) },
75 { }
76};
77MODULE_DEVICE_TABLE(hid, samsung_devices);
78
79static struct hid_driver samsung_driver = {
80 .name = "samsung",
81 .id_table = samsung_devices,
82 .report_fixup = samsung_report_fixup,
83 .probe = samsung_probe,
84};
85
86static int samsung_init(void)
87{
88 return hid_register_driver(&samsung_driver);
89}
90
91static void samsung_exit(void)
92{
93 hid_unregister_driver(&samsung_driver);
94}
95
96module_init(samsung_init);
97module_exit(samsung_exit);
98MODULE_LICENSE("GPL");
99
100HID_COMPAT_LOAD_DRIVER(samsung);
diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c
new file mode 100644
index 000000000000..3af8095a7de1
--- /dev/null
+++ b/drivers/hid/hid-sony.c
@@ -0,0 +1,110 @@
1/*
2 * HID driver for some sony "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/usb.h>
23
24#include "hid-ids.h"
25
26/*
27 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
28 * to "operational". Without this, the ps3 controller will not report any
29 * events.
30 */
31static int sony_set_operational(struct hid_device *hdev)
32{
33 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
34 struct usb_device *dev = interface_to_usbdev(intf);
35 __u16 ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
36 int ret;
37 char *buf = kmalloc(18, GFP_KERNEL);
38
39 if (!buf)
40 return -ENOMEM;
41
42 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
43 HID_REQ_GET_REPORT,
44 USB_DIR_IN | USB_TYPE_CLASS |
45 USB_RECIP_INTERFACE,
46 (3 << 8) | 0xf2, ifnum, buf, 17,
47 USB_CTRL_GET_TIMEOUT);
48 if (ret < 0)
49 dev_err(&hdev->dev, "can't set operational mode\n");
50
51 kfree(buf);
52
53 return ret;
54}
55
56static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
57{
58 int ret;
59
60 ret = hid_parse(hdev);
61 if (ret) {
62 dev_err(&hdev->dev, "parse failed\n");
63 goto err_free;
64 }
65
66 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
67 HID_CONNECT_HIDDEV_FORCE);
68 if (ret) {
69 dev_err(&hdev->dev, "hw start failed\n");
70 goto err_free;
71 }
72
73 ret = sony_set_operational(hdev);
74 if (ret)
75 goto err_stop;
76
77 return 0;
78err_stop:
79 hid_hw_stop(hdev);
80err_free:
81 return ret;
82}
83
84static const struct hid_device_id sony_devices[] = {
85 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) },
86 { }
87};
88MODULE_DEVICE_TABLE(hid, sony_devices);
89
90static struct hid_driver sony_driver = {
91 .name = "sony",
92 .id_table = sony_devices,
93 .probe = sony_probe,
94};
95
96static int sony_init(void)
97{
98 return hid_register_driver(&sony_driver);
99}
100
101static void sony_exit(void)
102{
103 hid_unregister_driver(&sony_driver);
104}
105
106module_init(sony_init);
107module_exit(sony_exit);
108MODULE_LICENSE("GPL");
109
110HID_COMPAT_LOAD_DRIVER(sony);
diff --git a/drivers/hid/hid-sunplus.c b/drivers/hid/hid-sunplus.c
new file mode 100644
index 000000000000..5ba68f7dbb78
--- /dev/null
+++ b/drivers/hid/hid-sunplus.c
@@ -0,0 +1,82 @@
1/*
2 * HID driver for some sunplus "special" devices
3 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 */
18
19#include <linux/device.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25static void sp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
26 unsigned int rsize)
27{
28 if (rsize >= 107 && rdesc[104] == 0x26 && rdesc[105] == 0x80 &&
29 rdesc[106] == 0x03) {
30 dev_info(&hdev->dev, "fixing up Sunplus Wireless Desktop "
31 "report descriptor\n");
32 rdesc[105] = rdesc[110] = 0x03;
33 rdesc[106] = rdesc[111] = 0x21;
34 }
35}
36
37#define sp_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
38 EV_KEY, (c))
39static int sp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
40 struct hid_field *field, struct hid_usage *usage,
41 unsigned long **bit, int *max)
42{
43 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
44 return 0;
45
46 switch (usage->hid & HID_USAGE) {
47 case 0x2003: sp_map_key_clear(KEY_ZOOMIN); break;
48 case 0x2103: sp_map_key_clear(KEY_ZOOMOUT); break;
49 default:
50 return 0;
51 }
52 return 1;
53}
54
55static const struct hid_device_id sp_devices[] = {
56 { HID_USB_DEVICE(USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP) },
57 { }
58};
59MODULE_DEVICE_TABLE(hid, sp_devices);
60
61static struct hid_driver sp_driver = {
62 .name = "sunplus",
63 .id_table = sp_devices,
64 .report_fixup = sp_report_fixup,
65 .input_mapping = sp_input_mapping,
66};
67
68static int sp_init(void)
69{
70 return hid_register_driver(&sp_driver);
71}
72
73static void sp_exit(void)
74{
75 hid_unregister_driver(&sp_driver);
76}
77
78module_init(sp_init);
79module_exit(sp_exit);
80MODULE_LICENSE("GPL");
81
82HID_COMPAT_LOAD_DRIVER(sunplus);
diff --git a/drivers/hid/usbhid/hid-tmff.c b/drivers/hid/hid-tmff.c
index 144578b1a00c..1b7cba0f7e1f 100644
--- a/drivers/hid/usbhid/hid-tmff.c
+++ b/drivers/hid/hid-tmff.c
@@ -27,23 +27,17 @@
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */ 28 */
29 29
30#include <linux/hid.h>
30#include <linux/input.h> 31#include <linux/input.h>
31
32#undef DEBUG
33#include <linux/usb.h> 32#include <linux/usb.h>
34 33
35#include <linux/hid.h> 34#include "hid-ids.h"
36#include "usbhid.h" 35
36#include "usbhid/usbhid.h"
37 37
38/* Usages for thrustmaster devices I know about */ 38/* Usages for thrustmaster devices I know about */
39#define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb) 39#define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
40 40
41struct dev_type {
42 u16 idVendor;
43 u16 idProduct;
44 const signed short *ff;
45};
46
47static const signed short ff_rumble[] = { 41static const signed short ff_rumble[] = {
48 FF_RUMBLE, 42 FF_RUMBLE,
49 -1 43 -1
@@ -54,21 +48,13 @@ static const signed short ff_joystick[] = {
54 -1 48 -1
55}; 49};
56 50
57static const struct dev_type devices[] = {
58 { 0x44f, 0xb300, ff_rumble },
59 { 0x44f, 0xb304, ff_rumble },
60 { 0x44f, 0xb651, ff_rumble }, /* FGT Rumble Force Wheel */
61 { 0x44f, 0xb654, ff_joystick }, /* FGT Force Feedback Wheel */
62};
63
64struct tmff_device { 51struct tmff_device {
65 struct hid_report *report; 52 struct hid_report *report;
66 struct hid_field *ff_field; 53 struct hid_field *ff_field;
67}; 54};
68 55
69/* Changes values from 0 to 0xffff into values from minimum to maximum */ 56/* Changes values from 0 to 0xffff into values from minimum to maximum */
70static inline int hid_tmff_scale_u16(unsigned int in, 57static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
71 int minimum, int maximum)
72{ 58{
73 int ret; 59 int ret;
74 60
@@ -81,8 +67,7 @@ static inline int hid_tmff_scale_u16(unsigned int in,
81} 67}
82 68
83/* Changes values from -0x80 to 0x7f into values from minimum to maximum */ 69/* Changes values from -0x80 to 0x7f into values from minimum to maximum */
84static inline int hid_tmff_scale_s8(int in, 70static inline int tmff_scale_s8(int in, int minimum, int maximum)
85 int minimum, int maximum)
86{ 71{
87 int ret; 72 int ret;
88 73
@@ -94,7 +79,8 @@ static inline int hid_tmff_scale_s8(int in,
94 return ret; 79 return ret;
95} 80}
96 81
97static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 82static int tmff_play(struct input_dev *dev, void *data,
83 struct ff_effect *effect)
98{ 84{
99 struct hid_device *hid = input_get_drvdata(dev); 85 struct hid_device *hid = input_get_drvdata(dev);
100 struct tmff_device *tmff = data; 86 struct tmff_device *tmff = data;
@@ -104,10 +90,10 @@ static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *ef
104 90
105 switch (effect->type) { 91 switch (effect->type) {
106 case FF_CONSTANT: 92 case FF_CONSTANT:
107 x = hid_tmff_scale_s8(effect->u.ramp.start_level, 93 x = tmff_scale_s8(effect->u.ramp.start_level,
108 ff_field->logical_minimum, 94 ff_field->logical_minimum,
109 ff_field->logical_maximum); 95 ff_field->logical_maximum);
110 y = hid_tmff_scale_s8(effect->u.ramp.end_level, 96 y = tmff_scale_s8(effect->u.ramp.end_level,
111 ff_field->logical_minimum, 97 ff_field->logical_minimum,
112 ff_field->logical_maximum); 98 ff_field->logical_maximum);
113 99
@@ -118,10 +104,10 @@ static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *ef
118 break; 104 break;
119 105
120 case FF_RUMBLE: 106 case FF_RUMBLE:
121 left = hid_tmff_scale_u16(effect->u.rumble.weak_magnitude, 107 left = tmff_scale_u16(effect->u.rumble.weak_magnitude,
122 ff_field->logical_minimum, 108 ff_field->logical_minimum,
123 ff_field->logical_maximum); 109 ff_field->logical_maximum);
124 right = hid_tmff_scale_u16(effect->u.rumble.strong_magnitude, 110 right = tmff_scale_u16(effect->u.rumble.strong_magnitude,
125 ff_field->logical_minimum, 111 ff_field->logical_minimum,
126 ff_field->logical_maximum); 112 ff_field->logical_maximum);
127 113
@@ -134,14 +120,14 @@ static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *ef
134 return 0; 120 return 0;
135} 121}
136 122
137int hid_tmff_init(struct hid_device *hid) 123static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
138{ 124{
139 struct tmff_device *tmff; 125 struct tmff_device *tmff;
140 struct hid_report *report; 126 struct hid_report *report;
141 struct list_head *report_list; 127 struct list_head *report_list;
142 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 128 struct hid_input *hidinput = list_entry(hid->inputs.next,
129 struct hid_input, list);
143 struct input_dev *input_dev = hidinput->input; 130 struct input_dev *input_dev = hidinput->input;
144 const signed short *ff_bits = ff_joystick;
145 int error; 131 int error;
146 int i; 132 int i;
147 133
@@ -163,63 +149,121 @@ int hid_tmff_init(struct hid_device *hid)
163 switch (field->usage[0].hid) { 149 switch (field->usage[0].hid) {
164 case THRUSTMASTER_USAGE_FF: 150 case THRUSTMASTER_USAGE_FF:
165 if (field->report_count < 2) { 151 if (field->report_count < 2) {
166 warn("ignoring FF field with report_count < 2"); 152 dev_warn(&hid->dev, "ignoring FF field "
153 "with report_count < 2\n");
167 continue; 154 continue;
168 } 155 }
169 156
170 if (field->logical_maximum == field->logical_minimum) { 157 if (field->logical_maximum ==
171 warn("ignoring FF field with logical_maximum == logical_minimum"); 158 field->logical_minimum) {
159 dev_warn(&hid->dev, "ignoring FF field "
160 "with logical_maximum "
161 "== logical_minimum\n");
172 continue; 162 continue;
173 } 163 }
174 164
175 if (tmff->report && tmff->report != report) { 165 if (tmff->report && tmff->report != report) {
176 warn("ignoring FF field in other report"); 166 dev_warn(&hid->dev, "ignoring FF field "
167 "in other report\n");
177 continue; 168 continue;
178 } 169 }
179 170
180 if (tmff->ff_field && tmff->ff_field != field) { 171 if (tmff->ff_field && tmff->ff_field != field) {
181 warn("ignoring duplicate FF field"); 172 dev_warn(&hid->dev, "ignoring "
173 "duplicate FF field\n");
182 continue; 174 continue;
183 } 175 }
184 176
185 tmff->report = report; 177 tmff->report = report;
186 tmff->ff_field = field; 178 tmff->ff_field = field;
187 179
188 for (i = 0; i < ARRAY_SIZE(devices); i++) {
189 if (input_dev->id.vendor == devices[i].idVendor &&
190 input_dev->id.product == devices[i].idProduct) {
191 ff_bits = devices[i].ff;
192 break;
193 }
194 }
195
196 for (i = 0; ff_bits[i] >= 0; i++) 180 for (i = 0; ff_bits[i] >= 0; i++)
197 set_bit(ff_bits[i], input_dev->ffbit); 181 set_bit(ff_bits[i], input_dev->ffbit);
198 182
199 break; 183 break;
200 184
201 default: 185 default:
202 warn("ignoring unknown output usage %08x", field->usage[0].hid); 186 dev_warn(&hid->dev, "ignoring unknown output "
187 "usage %08x\n",
188 field->usage[0].hid);
203 continue; 189 continue;
204 } 190 }
205 } 191 }
206 } 192 }
207 193
208 if (!tmff->report) { 194 if (!tmff->report) {
209 err("cant find FF field in output reports\n"); 195 dev_err(&hid->dev, "can't find FF field in output reports\n");
210 error = -ENODEV; 196 error = -ENODEV;
211 goto fail; 197 goto fail;
212 } 198 }
213 199
214 error = input_ff_create_memless(input_dev, tmff, hid_tmff_play); 200 error = input_ff_create_memless(input_dev, tmff, tmff_play);
215 if (error) 201 if (error)
216 goto fail; 202 goto fail;
217 203
218 info("Force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>"); 204 dev_info(&hid->dev, "force feedback for ThrustMaster devices by Zinx "
205 "Verituse <zinx@epicsol.org>");
219 return 0; 206 return 0;
220 207
221 fail: 208fail:
222 kfree(tmff); 209 kfree(tmff);
223 return error; 210 return error;
224} 211}
225 212
213static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
214{
215 int ret;
216
217 ret = hid_parse(hdev);
218 if (ret) {
219 dev_err(&hdev->dev, "parse failed\n");
220 goto err;
221 }
222
223 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
224 if (ret) {
225 dev_err(&hdev->dev, "hw start failed\n");
226 goto err;
227 }
228
229 tmff_init(hdev, (void *)id->driver_data);
230
231 return 0;
232err:
233 return ret;
234}
235
236static const struct hid_device_id tm_devices[] = {
237 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
238 .driver_data = (unsigned long)ff_rumble },
239 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304),
240 .driver_data = (unsigned long)ff_rumble },
241 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651), /* FGT Rumble Force Wheel */
242 .driver_data = (unsigned long)ff_rumble },
243 { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654), /* FGT Force Feedback Wheel */
244 .driver_data = (unsigned long)ff_joystick },
245 { }
246};
247MODULE_DEVICE_TABLE(hid, tm_devices);
248
249static struct hid_driver tm_driver = {
250 .name = "thrustmaster",
251 .id_table = tm_devices,
252 .probe = tm_probe,
253};
254
255static int tm_init(void)
256{
257 return hid_register_driver(&tm_driver);
258}
259
260static void tm_exit(void)
261{
262 hid_unregister_driver(&tm_driver);
263}
264
265module_init(tm_init);
266module_exit(tm_exit);
267MODULE_LICENSE("GPL");
268
269HID_COMPAT_LOAD_DRIVER(thrustmaster);
diff --git a/drivers/hid/usbhid/hid-zpff.c b/drivers/hid/hid-zpff.c
index 5a688274f6a3..ea82f3718b21 100644
--- a/drivers/hid/usbhid/hid-zpff.c
+++ b/drivers/hid/hid-zpff.c
@@ -21,16 +21,19 @@
21 */ 21 */
22 22
23 23
24#include <linux/hid.h>
24#include <linux/input.h> 25#include <linux/input.h>
25#include <linux/usb.h> 26#include <linux/usb.h>
26#include <linux/hid.h> 27
27#include "usbhid.h" 28#include "hid-ids.h"
29
30#include "usbhid/usbhid.h"
28 31
29struct zpff_device { 32struct zpff_device {
30 struct hid_report *report; 33 struct hid_report *report;
31}; 34};
32 35
33static int hid_zpff_play(struct input_dev *dev, void *data, 36static int zpff_play(struct input_dev *dev, void *data,
34 struct ff_effect *effect) 37 struct ff_effect *effect)
35{ 38{
36 struct hid_device *hid = input_get_drvdata(dev); 39 struct hid_device *hid = input_get_drvdata(dev);
@@ -58,7 +61,7 @@ static int hid_zpff_play(struct input_dev *dev, void *data,
58 return 0; 61 return 0;
59} 62}
60 63
61int hid_zpff_init(struct hid_device *hid) 64static int zpff_init(struct hid_device *hid)
62{ 65{
63 struct zpff_device *zpff; 66 struct zpff_device *zpff;
64 struct hid_report *report; 67 struct hid_report *report;
@@ -70,14 +73,14 @@ int hid_zpff_init(struct hid_device *hid)
70 int error; 73 int error;
71 74
72 if (list_empty(report_list)) { 75 if (list_empty(report_list)) {
73 printk(KERN_ERR "hid-zpff: no output report found\n"); 76 dev_err(&hid->dev, "no output report found\n");
74 return -ENODEV; 77 return -ENODEV;
75 } 78 }
76 79
77 report = list_entry(report_list->next, struct hid_report, list); 80 report = list_entry(report_list->next, struct hid_report, list);
78 81
79 if (report->maxfield < 4) { 82 if (report->maxfield < 4) {
80 printk(KERN_ERR "hid-zpff: not enough fields in report\n"); 83 dev_err(&hid->dev, "not enough fields in report\n");
81 return -ENODEV; 84 return -ENODEV;
82 } 85 }
83 86
@@ -87,7 +90,7 @@ int hid_zpff_init(struct hid_device *hid)
87 90
88 set_bit(FF_RUMBLE, dev->ffbit); 91 set_bit(FF_RUMBLE, dev->ffbit);
89 92
90 error = input_ff_create_memless(dev, zpff, hid_zpff_play); 93 error = input_ff_create_memless(dev, zpff, zpff_play);
91 if (error) { 94 if (error) {
92 kfree(zpff); 95 kfree(zpff);
93 return error; 96 return error;
@@ -100,8 +103,60 @@ int hid_zpff_init(struct hid_device *hid)
100 zpff->report->field[3]->value[0] = 0x00; 103 zpff->report->field[3]->value[0] = 0x00;
101 usbhid_submit_report(hid, zpff->report, USB_DIR_OUT); 104 usbhid_submit_report(hid, zpff->report, USB_DIR_OUT);
102 105
103 printk(KERN_INFO "Force feedback for Zeroplus based devices by " 106 dev_info(&hid->dev, "force feedback for Zeroplus based devices by "
104 "Anssi Hannula <anssi.hannula@gmail.com>\n"); 107 "Anssi Hannula <anssi.hannula@gmail.com>\n");
105 108
106 return 0; 109 return 0;
107} 110}
111
112static int zp_probe(struct hid_device *hdev, const struct hid_device_id *id)
113{
114 int ret;
115
116 ret = hid_parse(hdev);
117 if (ret) {
118 dev_err(&hdev->dev, "parse failed\n");
119 goto err;
120 }
121
122 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
123 if (ret) {
124 dev_err(&hdev->dev, "hw start failed\n");
125 goto err;
126 }
127
128 zpff_init(hdev);
129
130 return 0;
131err:
132 return ret;
133}
134
135static const struct hid_device_id zp_devices[] = {
136 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
137 { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
138 { }
139};
140MODULE_DEVICE_TABLE(hid, zp_devices);
141
142static struct hid_driver zp_driver = {
143 .name = "zeroplus",
144 .id_table = zp_devices,
145 .probe = zp_probe,
146};
147
148static int zp_init(void)
149{
150 return hid_register_driver(&zp_driver);
151}
152
153static void zp_exit(void)
154{
155 hid_unregister_driver(&zp_driver);
156}
157
158module_init(zp_init);
159module_exit(zp_exit);
160MODULE_LICENSE("GPL");
161
162HID_COMPAT_LOAD_DRIVER(zeroplus);
diff --git a/drivers/hid/hidraw.c b/drivers/hid/hidraw.c
index c40f0403edaf..497e0d1dd3c3 100644
--- a/drivers/hid/hidraw.c
+++ b/drivers/hid/hidraw.c
@@ -113,7 +113,7 @@ static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t
113 if (!dev->hid_output_raw_report) 113 if (!dev->hid_output_raw_report)
114 return -ENODEV; 114 return -ENODEV;
115 115
116 if (count > HID_MIN_BUFFER_SIZE) { 116 if (count > HID_MAX_BUFFER_SIZE) {
117 printk(KERN_WARNING "hidraw: pid %d passed too large report\n", 117 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
118 task_pid_nr(current)); 118 task_pid_nr(current));
119 return -EINVAL; 119 return -EINVAL;
@@ -181,7 +181,7 @@ static int hidraw_open(struct inode *inode, struct file *file)
181 181
182 dev = hidraw_table[minor]; 182 dev = hidraw_table[minor];
183 if (!dev->open++) 183 if (!dev->open++)
184 dev->hid->hid_open(dev->hid); 184 dev->hid->ll_driver->open(dev->hid);
185 185
186out_unlock: 186out_unlock:
187 spin_unlock(&minors_lock); 187 spin_unlock(&minors_lock);
@@ -207,7 +207,7 @@ static int hidraw_release(struct inode * inode, struct file * file)
207 dev = hidraw_table[minor]; 207 dev = hidraw_table[minor];
208 if (!dev->open--) { 208 if (!dev->open--) {
209 if (list->hidraw->exist) 209 if (list->hidraw->exist)
210 dev->hid->hid_close(dev->hid); 210 dev->hid->ll_driver->close(dev->hid);
211 else 211 else
212 kfree(list->hidraw); 212 kfree(list->hidraw);
213 } 213 }
@@ -367,7 +367,7 @@ void hidraw_disconnect(struct hid_device *hid)
367 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor)); 367 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
368 368
369 if (hidraw->open) { 369 if (hidraw->open) {
370 hid->hid_close(hid); 370 hid->ll_driver->close(hid);
371 wake_up_interruptible(&hidraw->wait); 371 wake_up_interruptible(&hidraw->wait);
372 } else { 372 } else {
373 kfree(hidraw); 373 kfree(hidraw);
diff --git a/drivers/hid/usbhid/Kconfig b/drivers/hid/usbhid/Kconfig
index 18f09104765c..5d9aa95fc3ef 100644
--- a/drivers/hid/usbhid/Kconfig
+++ b/drivers/hid/usbhid/Kconfig
@@ -24,88 +24,13 @@ config USB_HID
24comment "Input core support is needed for USB HID input layer or HIDBP support" 24comment "Input core support is needed for USB HID input layer or HIDBP support"
25 depends on USB_HID && INPUT=n 25 depends on USB_HID && INPUT=n
26 26
27config USB_HIDINPUT_POWERBOOK
28 bool "Enable support for Apple laptop/aluminum USB special keys"
29 default n
30 depends on USB_HID
31 help
32 Say Y here if you want support for the special keys (Fn, Numlock) on
33 Apple iBooks, PowerBooks, MacBooks, MacBook Pros and aluminum USB
34 keyboards.
35
36 If unsure, say N.
37
38config HID_FF
39 bool "Force feedback support (EXPERIMENTAL)"
40 depends on USB_HID && EXPERIMENTAL
41 help
42 Say Y here is you want force feedback support for a few HID devices.
43 See below for a list of supported devices.
44
45 See <file:Documentation/input/ff.txt> for a description of the force
46 feedback API.
47
48 If unsure, say N.
49
50config HID_PID 27config HID_PID
51 bool "PID device support" 28 bool "PID device support"
52 depends on HID_FF
53 help 29 help
54 Say Y here if you have a PID-compliant device and wish to enable force 30 Say Y here if you have a PID-compliant device and wish to enable force
55 feedback for it. Microsoft Sidewinder Force Feedback 2 is one of such 31 feedback for it. Microsoft Sidewinder Force Feedback 2 is one of such
56 devices. 32 devices.
57 33
58config LOGITECH_FF
59 bool "Logitech devices support"
60 depends on HID_FF
61 select INPUT_FF_MEMLESS if USB_HID
62 help
63 Say Y here if you have one of these devices:
64 - Logitech WingMan Cordless RumblePad
65 - Logitech WingMan Cordless RumblePad 2
66 - Logitech WingMan Force 3D
67 - Logitech Formula Force EX
68 - Logitech MOMO Force wheel
69
70 and if you want to enable force feedback for them.
71 Note: if you say N here, this device will still be supported, but without
72 force feedback.
73
74config LOGIRUMBLEPAD2_FF
75 bool "Logitech Rumblepad 2 support"
76 depends on HID_FF
77 select INPUT_FF_MEMLESS if USB_HID
78 help
79 Say Y here if you want to enable force feedback support for Logitech
80 Rumblepad 2 devices.
81
82config PANTHERLORD_FF
83 bool "PantherLord/GreenAsia based device support"
84 depends on HID_FF
85 select INPUT_FF_MEMLESS if USB_HID
86 help
87 Say Y here if you have a PantherLord/GreenAsia based game controller
88 or adapter and want to enable force feedback support for it.
89
90config THRUSTMASTER_FF
91 bool "ThrustMaster devices support"
92 depends on HID_FF
93 select INPUT_FF_MEMLESS if USB_HID
94 help
95 Say Y here if you have a THRUSTMASTER FireStore Dual Power 2 or
96 a THRUSTMASTER Ferrari GT Rumble Force or Force Feedback Wheel,
97 and want to enable force feedback support for it.
98 Note: if you say N here, this device will still be supported, but without
99 force feedback.
100
101config ZEROPLUS_FF
102 bool "Zeroplus based game controller support"
103 depends on HID_FF
104 select INPUT_FF_MEMLESS if USB_HID
105 help
106 Say Y here if you have a Zeroplus based game controller and want to
107 enable force feedback for it.
108
109config USB_HIDDEV 34config USB_HIDDEV
110 bool "/dev/hiddev raw HID device support" 35 bool "/dev/hiddev raw HID device support"
111 depends on USB_HID 36 depends on USB_HID
diff --git a/drivers/hid/usbhid/Makefile b/drivers/hid/usbhid/Makefile
index 00a7b7090192..1329ecb37a1c 100644
--- a/drivers/hid/usbhid/Makefile
+++ b/drivers/hid/usbhid/Makefile
@@ -13,24 +13,6 @@ endif
13ifeq ($(CONFIG_HID_PID),y) 13ifeq ($(CONFIG_HID_PID),y)
14 usbhid-objs += hid-pidff.o 14 usbhid-objs += hid-pidff.o
15endif 15endif
16ifeq ($(CONFIG_LOGITECH_FF),y)
17 usbhid-objs += hid-lgff.o
18endif
19ifeq ($(CONFIG_LOGIRUMBLEPAD2_FF),y)
20 usbhid-objs += hid-lg2ff.o
21endif
22ifeq ($(CONFIG_PANTHERLORD_FF),y)
23 usbhid-objs += hid-plff.o
24endif
25ifeq ($(CONFIG_THRUSTMASTER_FF),y)
26 usbhid-objs += hid-tmff.o
27endif
28ifeq ($(CONFIG_ZEROPLUS_FF),y)
29 usbhid-objs += hid-zpff.o
30endif
31ifeq ($(CONFIG_HID_FF),y)
32 usbhid-objs += hid-ff.o
33endif
34 16
35obj-$(CONFIG_USB_HID) += usbhid.o 17obj-$(CONFIG_USB_HID) += usbhid.o
36obj-$(CONFIG_USB_KBD) += usbkbd.o 18obj-$(CONFIG_USB_KBD) += usbkbd.o
diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
index 27fe4d8912cb..1d3b8a394d46 100644
--- a/drivers/hid/usbhid/hid-core.c
+++ b/drivers/hid/usbhid/hid-core.c
@@ -44,8 +44,6 @@
44#define DRIVER_DESC "USB HID core driver" 44#define DRIVER_DESC "USB HID core driver"
45#define DRIVER_LICENSE "GPL" 45#define DRIVER_LICENSE "GPL"
46 46
47static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
48 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
49/* 47/*
50 * Module parameters. 48 * Module parameters.
51 */ 49 */
@@ -61,12 +59,6 @@ MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
61 " quirks=vendorID:productID:quirks" 59 " quirks=vendorID:productID:quirks"
62 " where vendorID, productID, and quirks are all in" 60 " where vendorID, productID, and quirks are all in"
63 " 0x-prefixed hex"); 61 " 0x-prefixed hex");
64static char *rdesc_quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
65module_param_array_named(rdesc_quirks, rdesc_quirks_param, charp, NULL, 0444);
66MODULE_PARM_DESC(rdesc_quirks, "Add/modify report descriptor quirks by specifying "
67 " rdesc_quirks=vendorID:productID:rdesc_quirks"
68 " where vendorID, productID, and rdesc_quirks are all in"
69 " 0x-prefixed hex");
70/* 62/*
71 * Input submission and I/O error handler. 63 * Input submission and I/O error handler.
72 */ 64 */
@@ -197,31 +189,32 @@ static void hid_irq_in(struct urb *urb)
197 int status; 189 int status;
198 190
199 switch (urb->status) { 191 switch (urb->status) {
200 case 0: /* success */ 192 case 0: /* success */
201 usbhid->retry_delay = 0; 193 usbhid->retry_delay = 0;
202 hid_input_report(urb->context, HID_INPUT_REPORT, 194 hid_input_report(urb->context, HID_INPUT_REPORT,
203 urb->transfer_buffer, 195 urb->transfer_buffer,
204 urb->actual_length, 1); 196 urb->actual_length, 1);
205 break; 197 break;
206 case -EPIPE: /* stall */ 198 case -EPIPE: /* stall */
207 clear_bit(HID_IN_RUNNING, &usbhid->iofl); 199 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
208 set_bit(HID_CLEAR_HALT, &usbhid->iofl); 200 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
209 schedule_work(&usbhid->reset_work); 201 schedule_work(&usbhid->reset_work);
210 return; 202 return;
211 case -ECONNRESET: /* unlink */ 203 case -ECONNRESET: /* unlink */
212 case -ENOENT: 204 case -ENOENT:
213 case -ESHUTDOWN: /* unplug */ 205 case -ESHUTDOWN: /* unplug */
214 clear_bit(HID_IN_RUNNING, &usbhid->iofl); 206 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
215 return; 207 return;
216 case -EILSEQ: /* protocol error or unplug */ 208 case -EILSEQ: /* protocol error or unplug */
217 case -EPROTO: /* protocol error or unplug */ 209 case -EPROTO: /* protocol error or unplug */
218 case -ETIME: /* protocol error or unplug */ 210 case -ETIME: /* protocol error or unplug */
219 case -ETIMEDOUT: /* Should never happen, but... */ 211 case -ETIMEDOUT: /* Should never happen, but... */
220 clear_bit(HID_IN_RUNNING, &usbhid->iofl); 212 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
221 hid_io_error(hid); 213 hid_io_error(hid);
222 return; 214 return;
223 default: /* error */ 215 default: /* error */
224 warn("input irq status %d received", urb->status); 216 dev_warn(&urb->dev->dev, "input irq status %d "
217 "received\n", urb->status);
225 } 218 }
226 219
227 status = usb_submit_urb(urb, GFP_ATOMIC); 220 status = usb_submit_urb(urb, GFP_ATOMIC);
@@ -240,13 +233,16 @@ static void hid_irq_in(struct urb *urb)
240static int hid_submit_out(struct hid_device *hid) 233static int hid_submit_out(struct hid_device *hid)
241{ 234{
242 struct hid_report *report; 235 struct hid_report *report;
236 char *raw_report;
243 struct usbhid_device *usbhid = hid->driver_data; 237 struct usbhid_device *usbhid = hid->driver_data;
244 238
245 report = usbhid->out[usbhid->outtail]; 239 report = usbhid->out[usbhid->outtail].report;
240 raw_report = usbhid->out[usbhid->outtail].raw_report;
246 241
247 hid_output_report(report, usbhid->outbuf);
248 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0); 242 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
249 usbhid->urbout->dev = hid_to_usb_dev(hid); 243 usbhid->urbout->dev = hid_to_usb_dev(hid);
244 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
245 kfree(raw_report);
250 246
251 dbg_hid("submitting out urb\n"); 247 dbg_hid("submitting out urb\n");
252 248
@@ -262,17 +258,20 @@ static int hid_submit_ctrl(struct hid_device *hid)
262{ 258{
263 struct hid_report *report; 259 struct hid_report *report;
264 unsigned char dir; 260 unsigned char dir;
261 char *raw_report;
265 int len; 262 int len;
266 struct usbhid_device *usbhid = hid->driver_data; 263 struct usbhid_device *usbhid = hid->driver_data;
267 264
268 report = usbhid->ctrl[usbhid->ctrltail].report; 265 report = usbhid->ctrl[usbhid->ctrltail].report;
266 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
269 dir = usbhid->ctrl[usbhid->ctrltail].dir; 267 dir = usbhid->ctrl[usbhid->ctrltail].dir;
270 268
271 len = ((report->size - 1) >> 3) + 1 + (report->id > 0); 269 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
272 if (dir == USB_DIR_OUT) { 270 if (dir == USB_DIR_OUT) {
273 hid_output_report(report, usbhid->ctrlbuf);
274 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0); 271 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
275 usbhid->urbctrl->transfer_buffer_length = len; 272 usbhid->urbctrl->transfer_buffer_length = len;
273 memcpy(usbhid->ctrlbuf, raw_report, len);
274 kfree(raw_report);
276 } else { 275 } else {
277 int maxpacket, padlen; 276 int maxpacket, padlen;
278 277
@@ -319,17 +318,18 @@ static void hid_irq_out(struct urb *urb)
319 int unplug = 0; 318 int unplug = 0;
320 319
321 switch (urb->status) { 320 switch (urb->status) {
322 case 0: /* success */ 321 case 0: /* success */
323 break; 322 break;
324 case -ESHUTDOWN: /* unplug */ 323 case -ESHUTDOWN: /* unplug */
325 unplug = 1; 324 unplug = 1;
326 case -EILSEQ: /* protocol error or unplug */ 325 case -EILSEQ: /* protocol error or unplug */
327 case -EPROTO: /* protocol error or unplug */ 326 case -EPROTO: /* protocol error or unplug */
328 case -ECONNRESET: /* unlink */ 327 case -ECONNRESET: /* unlink */
329 case -ENOENT: 328 case -ENOENT:
330 break; 329 break;
331 default: /* error */ 330 default: /* error */
332 warn("output irq status %d received", urb->status); 331 dev_warn(&urb->dev->dev, "output irq status %d "
332 "received\n", urb->status);
333 } 333 }
334 334
335 spin_lock_irqsave(&usbhid->outlock, flags); 335 spin_lock_irqsave(&usbhid->outlock, flags);
@@ -367,21 +367,23 @@ static void hid_ctrl(struct urb *urb)
367 spin_lock_irqsave(&usbhid->ctrllock, flags); 367 spin_lock_irqsave(&usbhid->ctrllock, flags);
368 368
369 switch (urb->status) { 369 switch (urb->status) {
370 case 0: /* success */ 370 case 0: /* success */
371 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN) 371 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
372 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type, 372 hid_input_report(urb->context,
373 urb->transfer_buffer, urb->actual_length, 0); 373 usbhid->ctrl[usbhid->ctrltail].report->type,
374 break; 374 urb->transfer_buffer, urb->actual_length, 0);
375 case -ESHUTDOWN: /* unplug */ 375 break;
376 unplug = 1; 376 case -ESHUTDOWN: /* unplug */
377 case -EILSEQ: /* protocol error or unplug */ 377 unplug = 1;
378 case -EPROTO: /* protocol error or unplug */ 378 case -EILSEQ: /* protocol error or unplug */
379 case -ECONNRESET: /* unlink */ 379 case -EPROTO: /* protocol error or unplug */
380 case -ENOENT: 380 case -ECONNRESET: /* unlink */
381 case -EPIPE: /* report not available */ 381 case -ENOENT:
382 break; 382 case -EPIPE: /* report not available */
383 default: /* error */ 383 break;
384 warn("ctrl urb status %d received", urb->status); 384 default: /* error */
385 dev_warn(&urb->dev->dev, "ctrl urb status %d "
386 "received\n", urb->status);
385 } 387 }
386 388
387 if (unplug) 389 if (unplug)
@@ -408,6 +410,7 @@ void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, uns
408 int head; 410 int head;
409 unsigned long flags; 411 unsigned long flags;
410 struct usbhid_device *usbhid = hid->driver_data; 412 struct usbhid_device *usbhid = hid->driver_data;
413 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
411 414
412 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) 415 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
413 return; 416 return;
@@ -418,11 +421,18 @@ void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, uns
418 421
419 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) { 422 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
420 spin_unlock_irqrestore(&usbhid->outlock, flags); 423 spin_unlock_irqrestore(&usbhid->outlock, flags);
421 warn("output queue full"); 424 dev_warn(&hid->dev, "output queue full\n");
422 return; 425 return;
423 } 426 }
424 427
425 usbhid->out[usbhid->outhead] = report; 428 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
429 if (!usbhid->out[usbhid->outhead].raw_report) {
430 spin_unlock_irqrestore(&usbhid->outlock, flags);
431 warn("output queueing failed");
432 return;
433 }
434 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
435 usbhid->out[usbhid->outhead].report = report;
426 usbhid->outhead = head; 436 usbhid->outhead = head;
427 437
428 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) 438 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
@@ -437,10 +447,19 @@ void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, uns
437 447
438 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) { 448 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
439 spin_unlock_irqrestore(&usbhid->ctrllock, flags); 449 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
440 warn("control queue full"); 450 dev_warn(&hid->dev, "control queue full\n");
441 return; 451 return;
442 } 452 }
443 453
454 if (dir == USB_DIR_OUT) {
455 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
456 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
457 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
458 warn("control queueing failed");
459 return;
460 }
461 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
462 }
444 usbhid->ctrl[usbhid->ctrlhead].report = report; 463 usbhid->ctrl[usbhid->ctrlhead].report = report;
445 usbhid->ctrl[usbhid->ctrlhead].dir = dir; 464 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
446 usbhid->ctrlhead = head; 465 usbhid->ctrlhead = head;
@@ -451,6 +470,7 @@ void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, uns
451 470
452 spin_unlock_irqrestore(&usbhid->ctrllock, flags); 471 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
453} 472}
473EXPORT_SYMBOL_GPL(usbhid_submit_report);
454 474
455static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) 475static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
456{ 476{
@@ -465,7 +485,7 @@ static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, un
465 return -1; 485 return -1;
466 486
467 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) { 487 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
468 warn("event field not found"); 488 dev_warn(&dev->dev, "event field not found\n");
469 return -1; 489 return -1;
470 } 490 }
471 491
@@ -568,7 +588,7 @@ void usbhid_init_reports(struct hid_device *hid)
568 } 588 }
569 589
570 if (err) 590 if (err)
571 warn("timeout initializing reports"); 591 dev_warn(&hid->dev, "timeout initializing reports\n");
572} 592}
573 593
574/* 594/*
@@ -598,7 +618,7 @@ static int hid_find_field_early(struct hid_device *hid, unsigned int page,
598 return -1; 618 return -1;
599} 619}
600 620
601static void usbhid_set_leds(struct hid_device *hid) 621void usbhid_set_leds(struct hid_device *hid)
602{ 622{
603 struct hid_field *field; 623 struct hid_field *field;
604 int offset; 624 int offset;
@@ -608,6 +628,7 @@ static void usbhid_set_leds(struct hid_device *hid)
608 usbhid_submit_report(hid, field->report, USB_DIR_OUT); 628 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
609 } 629 }
610} 630}
631EXPORT_SYMBOL_GPL(usbhid_set_leds);
611 632
612/* 633/*
613 * Traverse the supplied list of reports and find the longest 634 * Traverse the supplied list of reports and find the longest
@@ -675,43 +696,16 @@ static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
675 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma); 696 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
676} 697}
677 698
678/* 699static int usbhid_parse(struct hid_device *hid)
679 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
680 * to "operational". Without this, the ps3 controller will not report any
681 * events.
682 */
683static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
684{
685 int result;
686 char *buf = kmalloc(18, GFP_KERNEL);
687
688 if (!buf)
689 return;
690
691 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
692 HID_REQ_GET_REPORT,
693 USB_DIR_IN | USB_TYPE_CLASS |
694 USB_RECIP_INTERFACE,
695 (3 << 8) | 0xf2, ifnum, buf, 17,
696 USB_CTRL_GET_TIMEOUT);
697
698 if (result < 0)
699 err_hid("%s failed: %d\n", __func__, result);
700
701 kfree(buf);
702}
703
704static struct hid_device *usb_hid_configure(struct usb_interface *intf)
705{ 700{
701 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
706 struct usb_host_interface *interface = intf->cur_altsetting; 702 struct usb_host_interface *interface = intf->cur_altsetting;
707 struct usb_device *dev = interface_to_usbdev (intf); 703 struct usb_device *dev = interface_to_usbdev (intf);
708 struct hid_descriptor *hdesc; 704 struct hid_descriptor *hdesc;
709 struct hid_device *hid;
710 u32 quirks = 0; 705 u32 quirks = 0;
711 unsigned int insize = 0, rsize = 0; 706 unsigned int rsize = 0;
712 char *rdesc; 707 char *rdesc;
713 int n, len; 708 int ret, n;
714 struct usbhid_device *usbhid;
715 709
716 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), 710 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
717 le16_to_cpu(dev->descriptor.idProduct)); 711 le16_to_cpu(dev->descriptor.idProduct));
@@ -724,63 +718,75 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
724 quirks |= HID_QUIRK_NOGET; 718 quirks |= HID_QUIRK_NOGET;
725 } 719 }
726 720
727 if (quirks & HID_QUIRK_IGNORE)
728 return NULL;
729
730 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
731 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
732 return NULL;
733
734
735 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && 721 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
736 (!interface->desc.bNumEndpoints || 722 (!interface->desc.bNumEndpoints ||
737 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) { 723 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
738 dbg_hid("class descriptor not present\n"); 724 dbg_hid("class descriptor not present\n");
739 return NULL; 725 return -ENODEV;
740 } 726 }
741 727
728 hid->version = le16_to_cpu(hdesc->bcdHID);
729 hid->country = hdesc->bCountryCode;
730
742 for (n = 0; n < hdesc->bNumDescriptors; n++) 731 for (n = 0; n < hdesc->bNumDescriptors; n++)
743 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT) 732 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
744 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength); 733 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
745 734
746 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 735 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
747 dbg_hid("weird size of report descriptor (%u)\n", rsize); 736 dbg_hid("weird size of report descriptor (%u)\n", rsize);
748 return NULL; 737 return -EINVAL;
749 } 738 }
750 739
751 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) { 740 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
752 dbg_hid("couldn't allocate rdesc memory\n"); 741 dbg_hid("couldn't allocate rdesc memory\n");
753 return NULL; 742 return -ENOMEM;
754 } 743 }
755 744
756 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0); 745 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
757 746
758 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) { 747 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
748 HID_DT_REPORT, rdesc, rsize);
749 if (ret < 0) {
759 dbg_hid("reading report descriptor failed\n"); 750 dbg_hid("reading report descriptor failed\n");
760 kfree(rdesc); 751 kfree(rdesc);
761 return NULL; 752 goto err;
762 } 753 }
763 754
764 usbhid_fixup_report_descriptor(le16_to_cpu(dev->descriptor.idVendor),
765 le16_to_cpu(dev->descriptor.idProduct), rdesc,
766 rsize, rdesc_quirks_param);
767
768 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n); 755 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
769 for (n = 0; n < rsize; n++) 756 for (n = 0; n < rsize; n++)
770 dbg_hid_line(" %02x", (unsigned char) rdesc[n]); 757 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
771 dbg_hid_line("\n"); 758 dbg_hid_line("\n");
772 759
773 if (!(hid = hid_parse_report(rdesc, n))) { 760 ret = hid_parse_report(hid, rdesc, rsize);
761 kfree(rdesc);
762 if (ret) {
774 dbg_hid("parsing report descriptor failed\n"); 763 dbg_hid("parsing report descriptor failed\n");
775 kfree(rdesc); 764 goto err;
776 return NULL;
777 } 765 }
778 766
779 kfree(rdesc);
780 hid->quirks = quirks; 767 hid->quirks = quirks;
781 768
782 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL))) 769 return 0;
783 goto fail_no_usbhid; 770err:
771 return ret;
772}
773
774static int usbhid_start(struct hid_device *hid)
775{
776 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
777 struct usb_host_interface *interface = intf->cur_altsetting;
778 struct usb_device *dev = interface_to_usbdev(intf);
779 struct usbhid_device *usbhid;
780 unsigned int n, insize = 0;
781 int ret;
782
783 WARN_ON(hid->driver_data);
784
785 usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL);
786 if (usbhid == NULL) {
787 ret = -ENOMEM;
788 goto err;
789 }
784 790
785 hid->driver_data = usbhid; 791 hid->driver_data = usbhid;
786 usbhid->hid = hid; 792 usbhid->hid = hid;
@@ -799,28 +805,11 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
799 insize = HID_MAX_BUFFER_SIZE; 805 insize = HID_MAX_BUFFER_SIZE;
800 806
801 if (hid_alloc_buffers(dev, hid)) { 807 if (hid_alloc_buffers(dev, hid)) {
802 hid_free_buffers(dev, hid); 808 ret = -ENOMEM;
803 goto fail; 809 goto fail;
804 } 810 }
805 811
806 hid->name[0] = 0;
807
808 if (dev->manufacturer)
809 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
810
811 if (dev->product) {
812 if (dev->manufacturer)
813 strlcat(hid->name, " ", sizeof(hid->name));
814 strlcat(hid->name, dev->product, sizeof(hid->name));
815 }
816
817 if (!strlen(hid->name))
818 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
819 le16_to_cpu(dev->descriptor.idVendor),
820 le16_to_cpu(dev->descriptor.idProduct));
821
822 for (n = 0; n < interface->desc.bNumEndpoints; n++) { 812 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
823
824 struct usb_endpoint_descriptor *endpoint; 813 struct usb_endpoint_descriptor *endpoint;
825 int pipe; 814 int pipe;
826 int interval; 815 int interval;
@@ -832,7 +821,7 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
832 interval = endpoint->bInterval; 821 interval = endpoint->bInterval;
833 822
834 /* Some vendors give fullspeed interval on highspeed devides */ 823 /* Some vendors give fullspeed interval on highspeed devides */
835 if (quirks & HID_QUIRK_FULLSPEED_INTERVAL && 824 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
836 dev->speed == USB_SPEED_HIGH) { 825 dev->speed == USB_SPEED_HIGH) {
837 interval = fls(endpoint->bInterval*8); 826 interval = fls(endpoint->bInterval*8);
838 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n", 827 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
@@ -843,6 +832,7 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
843 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0) 832 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
844 interval = hid_mousepoll_interval; 833 interval = hid_mousepoll_interval;
845 834
835 ret = -ENOMEM;
846 if (usb_endpoint_dir_in(endpoint)) { 836 if (usb_endpoint_dir_in(endpoint)) {
847 if (usbhid->urbin) 837 if (usbhid->urbin)
848 continue; 838 continue;
@@ -868,6 +858,7 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
868 858
869 if (!usbhid->urbin) { 859 if (!usbhid->urbin) {
870 err_hid("couldn't find an input interrupt endpoint"); 860 err_hid("couldn't find an input interrupt endpoint");
861 ret = -ENODEV;
871 goto fail; 862 goto fail;
872 } 863 }
873 864
@@ -879,44 +870,25 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
879 spin_lock_init(&usbhid->outlock); 870 spin_lock_init(&usbhid->outlock);
880 spin_lock_init(&usbhid->ctrllock); 871 spin_lock_init(&usbhid->ctrllock);
881 872
882 hid->version = le16_to_cpu(hdesc->bcdHID);
883 hid->country = hdesc->bCountryCode;
884 hid->dev = &intf->dev;
885 usbhid->intf = intf; 873 usbhid->intf = intf;
886 usbhid->ifnum = interface->desc.bInterfaceNumber; 874 usbhid->ifnum = interface->desc.bInterfaceNumber;
887 875
888 hid->bus = BUS_USB;
889 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
890 hid->product = le16_to_cpu(dev->descriptor.idProduct);
891
892 usb_make_path(dev, hid->phys, sizeof(hid->phys));
893 strlcat(hid->phys, "/input", sizeof(hid->phys));
894 len = strlen(hid->phys);
895 if (len < sizeof(hid->phys) - 1)
896 snprintf(hid->phys + len, sizeof(hid->phys) - len,
897 "%d", intf->altsetting[0].desc.bInterfaceNumber);
898
899 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
900 hid->uniq[0] = 0;
901
902 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL); 876 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
903 if (!usbhid->urbctrl) 877 if (!usbhid->urbctrl) {
878 ret = -ENOMEM;
904 goto fail; 879 goto fail;
880 }
905 881
906 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr, 882 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
907 usbhid->ctrlbuf, 1, hid_ctrl, hid); 883 usbhid->ctrlbuf, 1, hid_ctrl, hid);
908 usbhid->urbctrl->setup_dma = usbhid->cr_dma; 884 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
909 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma; 885 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
910 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); 886 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
911 hid->hidinput_input_event = usb_hidinput_input_event; 887
912 hid->hid_open = usbhid_open; 888 usbhid_init_reports(hid);
913 hid->hid_close = usbhid_close; 889 hid_dump_device(hid);
914#ifdef CONFIG_USB_HIDDEV 890
915 hid->hiddev_hid_event = hiddev_hid_event; 891 return 0;
916 hid->hiddev_report_event = hiddev_report_event;
917#endif
918 hid->hid_output_raw_report = usbhid_output_raw_report;
919 return hid;
920 892
921fail: 893fail:
922 usb_free_urb(usbhid->urbin); 894 usb_free_urb(usbhid->urbin);
@@ -924,24 +896,18 @@ fail:
924 usb_free_urb(usbhid->urbctrl); 896 usb_free_urb(usbhid->urbctrl);
925 hid_free_buffers(dev, hid); 897 hid_free_buffers(dev, hid);
926 kfree(usbhid); 898 kfree(usbhid);
927fail_no_usbhid: 899err:
928 hid_free_device(hid); 900 return ret;
929
930 return NULL;
931} 901}
932 902
933static void hid_disconnect(struct usb_interface *intf) 903static void usbhid_stop(struct hid_device *hid)
934{ 904{
935 struct hid_device *hid = usb_get_intfdata (intf); 905 struct usbhid_device *usbhid = hid->driver_data;
936 struct usbhid_device *usbhid;
937 906
938 if (!hid) 907 if (WARN_ON(!usbhid))
939 return; 908 return;
940 909
941 usbhid = hid->driver_data;
942
943 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */ 910 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
944 usb_set_intfdata(intf, NULL);
945 set_bit(HID_DISCONNECTED, &usbhid->iofl); 911 set_bit(HID_DISCONNECTED, &usbhid->iofl);
946 spin_unlock_irq(&usbhid->inlock); 912 spin_unlock_irq(&usbhid->inlock);
947 usb_kill_urb(usbhid->urbin); 913 usb_kill_urb(usbhid->urbin);
@@ -958,86 +924,100 @@ static void hid_disconnect(struct usb_interface *intf)
958 if (hid->claimed & HID_CLAIMED_HIDRAW) 924 if (hid->claimed & HID_CLAIMED_HIDRAW)
959 hidraw_disconnect(hid); 925 hidraw_disconnect(hid);
960 926
927 hid->claimed = 0;
928
961 usb_free_urb(usbhid->urbin); 929 usb_free_urb(usbhid->urbin);
962 usb_free_urb(usbhid->urbctrl); 930 usb_free_urb(usbhid->urbctrl);
963 usb_free_urb(usbhid->urbout); 931 usb_free_urb(usbhid->urbout);
964 932
965 hid_free_buffers(hid_to_usb_dev(hid), hid); 933 hid_free_buffers(hid_to_usb_dev(hid), hid);
966 kfree(usbhid); 934 kfree(usbhid);
967 hid_free_device(hid); 935 hid->driver_data = NULL;
968} 936}
969 937
938static struct hid_ll_driver usb_hid_driver = {
939 .parse = usbhid_parse,
940 .start = usbhid_start,
941 .stop = usbhid_stop,
942 .open = usbhid_open,
943 .close = usbhid_close,
944 .hidinput_input_event = usb_hidinput_input_event,
945};
946
970static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id) 947static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
971{ 948{
949 struct usb_device *dev = interface_to_usbdev(intf);
972 struct hid_device *hid; 950 struct hid_device *hid;
973 char path[64]; 951 size_t len;
974 int i; 952 int ret;
975 char *c;
976 953
977 dbg_hid("HID probe called for ifnum %d\n", 954 dbg_hid("HID probe called for ifnum %d\n",
978 intf->altsetting->desc.bInterfaceNumber); 955 intf->altsetting->desc.bInterfaceNumber);
979 956
980 if (!(hid = usb_hid_configure(intf))) 957 hid = hid_allocate_device();
981 return -ENODEV; 958 if (IS_ERR(hid))
982 959 return PTR_ERR(hid);
983 usbhid_init_reports(hid);
984 hid_dump_device(hid);
985 if (hid->quirks & HID_QUIRK_RESET_LEDS)
986 usbhid_set_leds(hid);
987
988 if (!hidinput_connect(hid))
989 hid->claimed |= HID_CLAIMED_INPUT;
990 if (!hiddev_connect(hid))
991 hid->claimed |= HID_CLAIMED_HIDDEV;
992 if (!hidraw_connect(hid))
993 hid->claimed |= HID_CLAIMED_HIDRAW;
994 960
995 usb_set_intfdata(intf, hid); 961 usb_set_intfdata(intf, hid);
962 hid->ll_driver = &usb_hid_driver;
963 hid->hid_output_raw_report = usbhid_output_raw_report;
964 hid->ff_init = hid_pidff_init;
965#ifdef CONFIG_USB_HIDDEV
966 hid->hiddev_connect = hiddev_connect;
967 hid->hiddev_hid_event = hiddev_hid_event;
968 hid->hiddev_report_event = hiddev_report_event;
969#endif
970 hid->dev.parent = &intf->dev;
971 hid->bus = BUS_USB;
972 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
973 hid->product = le16_to_cpu(dev->descriptor.idProduct);
974 hid->name[0] = 0;
996 975
997 if (!hid->claimed) { 976 if (dev->manufacturer)
998 printk ("HID device claimed by neither input, hiddev nor hidraw\n"); 977 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
999 hid_disconnect(intf); 978
1000 return -ENODEV; 979 if (dev->product) {
980 if (dev->manufacturer)
981 strlcat(hid->name, " ", sizeof(hid->name));
982 strlcat(hid->name, dev->product, sizeof(hid->name));
1001 } 983 }
1002 984
1003 if ((hid->claimed & HID_CLAIMED_INPUT)) 985 if (!strlen(hid->name))
1004 hid_ff_init(hid); 986 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
987 le16_to_cpu(dev->descriptor.idVendor),
988 le16_to_cpu(dev->descriptor.idProduct));
1005 989
1006 if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER) 990 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1007 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf), 991 strlcat(hid->phys, "/input", sizeof(hid->phys));
1008 intf->cur_altsetting->desc.bInterfaceNumber); 992 len = strlen(hid->phys);
993 if (len < sizeof(hid->phys) - 1)
994 snprintf(hid->phys + len, sizeof(hid->phys) - len,
995 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1009 996
1010 printk(KERN_INFO); 997 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
998 hid->uniq[0] = 0;
1011 999
1012 if (hid->claimed & HID_CLAIMED_INPUT) 1000 ret = hid_add_device(hid);
1013 printk("input"); 1001 if (ret) {
1014 if ((hid->claimed & HID_CLAIMED_INPUT) && ((hid->claimed & HID_CLAIMED_HIDDEV) || 1002 if (ret != -ENODEV)
1015 hid->claimed & HID_CLAIMED_HIDRAW)) 1003 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
1016 printk(","); 1004 goto err;
1017 if (hid->claimed & HID_CLAIMED_HIDDEV)
1018 printk("hiddev%d", hid->minor);
1019 if ((hid->claimed & HID_CLAIMED_INPUT) && (hid->claimed & HID_CLAIMED_HIDDEV) &&
1020 (hid->claimed & HID_CLAIMED_HIDRAW))
1021 printk(",");
1022 if (hid->claimed & HID_CLAIMED_HIDRAW)
1023 printk("hidraw%d", ((struct hidraw*)hid->hidraw)->minor);
1024
1025 c = "Device";
1026 for (i = 0; i < hid->maxcollection; i++) {
1027 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
1028 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
1029 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
1030 c = hid_types[hid->collection[i].usage & 0xffff];
1031 break;
1032 }
1033 } 1005 }
1034 1006
1035 usb_make_path(interface_to_usbdev(intf), path, 63); 1007 return 0;
1008err:
1009 hid_destroy_device(hid);
1010 return ret;
1011}
1036 1012
1037 printk(": USB HID v%x.%02x %s [%s] on %s\n", 1013static void hid_disconnect(struct usb_interface *intf)
1038 hid->version >> 8, hid->version & 0xff, c, hid->name, path); 1014{
1015 struct hid_device *hid = usb_get_intfdata(intf);
1039 1016
1040 return 0; 1017 if (WARN_ON(!hid))
1018 return;
1019
1020 hid_destroy_device(hid);
1041} 1021}
1042 1022
1043static int hid_suspend(struct usb_interface *intf, pm_message_t message) 1023static int hid_suspend(struct usb_interface *intf, pm_message_t message)
@@ -1107,9 +1087,22 @@ static struct usb_driver hid_driver = {
1107 .supports_autosuspend = 1, 1087 .supports_autosuspend = 1,
1108}; 1088};
1109 1089
1090static const struct hid_device_id hid_usb_table[] = {
1091 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1092 { }
1093};
1094
1095static struct hid_driver hid_usb_driver = {
1096 .name = "generic-usb",
1097 .id_table = hid_usb_table,
1098};
1099
1110static int __init hid_init(void) 1100static int __init hid_init(void)
1111{ 1101{
1112 int retval; 1102 int retval;
1103 retval = hid_register_driver(&hid_usb_driver);
1104 if (retval)
1105 goto hid_register_fail;
1113 retval = usbhid_quirks_init(quirks_param); 1106 retval = usbhid_quirks_init(quirks_param);
1114 if (retval) 1107 if (retval)
1115 goto usbhid_quirks_init_fail; 1108 goto usbhid_quirks_init_fail;
@@ -1119,7 +1112,8 @@ static int __init hid_init(void)
1119 retval = usb_register(&hid_driver); 1112 retval = usb_register(&hid_driver);
1120 if (retval) 1113 if (retval)
1121 goto usb_register_fail; 1114 goto usb_register_fail;
1122 info(DRIVER_VERSION ":" DRIVER_DESC); 1115 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1116 DRIVER_DESC "\n");
1123 1117
1124 return 0; 1118 return 0;
1125usb_register_fail: 1119usb_register_fail:
@@ -1127,6 +1121,8 @@ usb_register_fail:
1127hiddev_init_fail: 1121hiddev_init_fail:
1128 usbhid_quirks_exit(); 1122 usbhid_quirks_exit();
1129usbhid_quirks_init_fail: 1123usbhid_quirks_init_fail:
1124 hid_unregister_driver(&hid_usb_driver);
1125hid_register_fail:
1130 return retval; 1126 return retval;
1131} 1127}
1132 1128
@@ -1135,6 +1131,7 @@ static void __exit hid_exit(void)
1135 usb_deregister(&hid_driver); 1131 usb_deregister(&hid_driver);
1136 hiddev_exit(); 1132 hiddev_exit();
1137 usbhid_quirks_exit(); 1133 usbhid_quirks_exit();
1134 hid_unregister_driver(&hid_usb_driver);
1138} 1135}
1139 1136
1140module_init(hid_init); 1137module_init(hid_init);
diff --git a/drivers/hid/usbhid/hid-ff.c b/drivers/hid/usbhid/hid-ff.c
deleted file mode 100644
index 1d0dac52f166..000000000000
--- a/drivers/hid/usbhid/hid-ff.c
+++ /dev/null
@@ -1,95 +0,0 @@
1/*
2 * Force feedback support for hid devices.
3 * Not all hid devices use the same protocol. For example, some use PID,
4 * other use their own proprietary procotol.
5 *
6 * Copyright (c) 2002-2004 Johann Deneux
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Should you need to contact me, the author, you can do so by
25 * e-mail - mail your message to <johann.deneux@it.uu.se>
26 */
27
28#include <linux/input.h>
29
30#undef DEBUG
31#include <linux/usb.h>
32
33#include <linux/hid.h>
34#include "usbhid.h"
35
36/*
37 * This table contains pointers to initializers. To add support for new
38 * devices, you need to add the USB vendor and product ids here.
39 */
40struct hid_ff_initializer {
41 u16 idVendor;
42 u16 idProduct;
43 int (*init)(struct hid_device*);
44};
45
46/*
47 * We try pidff when no other driver is found because PID is the
48 * standards compliant way of implementing force feedback in HID.
49 * pidff_init() will quickly abort if the device doesn't appear to
50 * be a PID device
51 */
52static struct hid_ff_initializer inits[] = {
53#ifdef CONFIG_LOGITECH_FF
54 { 0x46d, 0xc211, hid_lgff_init }, /* Logitech Cordless rumble pad */
55 { 0x46d, 0xc219, hid_lgff_init }, /* Logitech Cordless rumble pad 2 */
56 { 0x46d, 0xc283, hid_lgff_init }, /* Logitech Wingman Force 3d */
57 { 0x46d, 0xc286, hid_lgff_init }, /* Logitech Force 3D Pro Joystick */
58 { 0x46d, 0xc294, hid_lgff_init }, /* Logitech Formula Force EX */
59 { 0x46d, 0xc295, hid_lgff_init }, /* Logitech MOMO force wheel */
60 { 0x46d, 0xca03, hid_lgff_init }, /* Logitech MOMO force wheel */
61#endif
62#ifdef CONFIG_LOGIRUMBLEPAD2_FF
63 { 0x46d, 0xc218, hid_lg2ff_init }, /* Logitech Rumblepad 2 */
64#endif
65#ifdef CONFIG_PANTHERLORD_FF
66 { 0x810, 0x0001, hid_plff_init }, /* "Twin USB Joystick" */
67 { 0xe8f, 0x0003, hid_plff_init }, /* "GreenAsia Inc. USB Joystick " */
68#endif
69#ifdef CONFIG_THRUSTMASTER_FF
70 { 0x44f, 0xb300, hid_tmff_init },
71 { 0x44f, 0xb304, hid_tmff_init },
72 { 0x44f, 0xb651, hid_tmff_init }, /* FGT Rumble Force Wheel */
73 { 0x44f, 0xb654, hid_tmff_init }, /* FGT Force Feedback Wheel */
74#endif
75#ifdef CONFIG_ZEROPLUS_FF
76 { 0xc12, 0x0005, hid_zpff_init },
77 { 0xc12, 0x0030, hid_zpff_init },
78#endif
79 { 0, 0, hid_pidff_init} /* Matches anything */
80};
81
82int hid_ff_init(struct hid_device* hid)
83{
84 struct hid_ff_initializer *init;
85 int vendor = le16_to_cpu(hid_to_usb_dev(hid)->descriptor.idVendor);
86 int product = le16_to_cpu(hid_to_usb_dev(hid)->descriptor.idProduct);
87
88 for (init = inits; init->idVendor; init++)
89 if (init->idVendor == vendor && init->idProduct == product)
90 break;
91
92 return init->init(hid);
93}
94EXPORT_SYMBOL_GPL(hid_ff_init);
95
diff --git a/drivers/hid/usbhid/hid-pidff.c b/drivers/hid/usbhid/hid-pidff.c
index 011326178c06..484e3eec2f88 100644
--- a/drivers/hid/usbhid/hid-pidff.c
+++ b/drivers/hid/usbhid/hid-pidff.c
@@ -397,7 +397,6 @@ static void pidff_set_condition_report(struct pidff_device *pidff,
397 effect->u.condition[i].left_saturation); 397 effect->u.condition[i].left_saturation);
398 pidff_set(&pidff->set_condition[PID_DEAD_BAND], 398 pidff_set(&pidff->set_condition[PID_DEAD_BAND],
399 effect->u.condition[i].deadband); 399 effect->u.condition[i].deadband);
400 usbhid_wait_io(pidff->hid);
401 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION], 400 usbhid_submit_report(pidff->hid, pidff->reports[PID_SET_CONDITION],
402 USB_DIR_OUT); 401 USB_DIR_OUT);
403 } 402 }
@@ -512,7 +511,6 @@ static void pidff_playback_pid(struct pidff_device *pidff, int pid_id, int n)
512 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n; 511 pidff->effect_operation[PID_LOOP_COUNT].value[0] = n;
513 } 512 }
514 513
515 usbhid_wait_io(pidff->hid);
516 usbhid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION], 514 usbhid_submit_report(pidff->hid, pidff->reports[PID_EFFECT_OPERATION],
517 USB_DIR_OUT); 515 USB_DIR_OUT);
518} 516}
@@ -548,6 +546,9 @@ static int pidff_erase_effect(struct input_dev *dev, int effect_id)
548 int pid_id = pidff->pid_id[effect_id]; 546 int pid_id = pidff->pid_id[effect_id];
549 547
550 debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]); 548 debug("starting to erase %d/%d", effect_id, pidff->pid_id[effect_id]);
549 /* Wait for the queue to clear. We do not want a full fifo to
550 prevent the effect removal. */
551 usbhid_wait_io(pidff->hid);
551 pidff_playback_pid(pidff, pid_id, 0); 552 pidff_playback_pid(pidff, pid_id, 0);
552 pidff_erase_pid(pidff, pid_id); 553 pidff_erase_pid(pidff, pid_id);
553 554
diff --git a/drivers/hid/usbhid/hid-quirks.c b/drivers/hid/usbhid/hid-quirks.c
index b15f88249639..47ebe045f9b5 100644
--- a/drivers/hid/usbhid/hid-quirks.c
+++ b/drivers/hid/usbhid/hid-quirks.c
@@ -17,412 +17,7 @@
17 17
18#include <linux/hid.h> 18#include <linux/hid.h>
19 19
20#define USB_VENDOR_ID_A4TECH 0x09da 20#include "../hid-ids.h"
21#define USB_DEVICE_ID_A4TECH_WCP32PU 0x0006
22#define USB_DEVICE_ID_A4TECH_X5_005D 0x000a
23
24#define USB_VENDOR_ID_AASHIMA 0x06d6
25#define USB_DEVICE_ID_AASHIMA_GAMEPAD 0x0025
26#define USB_DEVICE_ID_AASHIMA_PREDATOR 0x0026
27
28#define USB_VENDOR_ID_ACECAD 0x0460
29#define USB_DEVICE_ID_ACECAD_FLAIR 0x0004
30#define USB_DEVICE_ID_ACECAD_302 0x0008
31
32#define USB_VENDOR_ID_ADS_TECH 0x06e1
33#define USB_DEVICE_ID_ADS_TECH_RADIO_SI470X 0xa155
34
35#define USB_VENDOR_ID_AFATECH 0x15a4
36#define USB_DEVICE_ID_AFATECH_AF9016 0x9016
37
38#define USB_VENDOR_ID_AIPTEK 0x08ca
39#define USB_DEVICE_ID_AIPTEK_01 0x0001
40#define USB_DEVICE_ID_AIPTEK_10 0x0010
41#define USB_DEVICE_ID_AIPTEK_20 0x0020
42#define USB_DEVICE_ID_AIPTEK_21 0x0021
43#define USB_DEVICE_ID_AIPTEK_22 0x0022
44#define USB_DEVICE_ID_AIPTEK_23 0x0023
45#define USB_DEVICE_ID_AIPTEK_24 0x0024
46
47#define USB_VENDOR_ID_AIRCABLE 0x16CA
48#define USB_DEVICE_ID_AIRCABLE1 0x1502
49
50#define USB_VENDOR_ID_ALCOR 0x058f
51#define USB_DEVICE_ID_ALCOR_USBRS232 0x9720
52
53#define USB_VENDOR_ID_ALPS 0x0433
54#define USB_DEVICE_ID_IBM_GAMEPAD 0x1101
55
56#define USB_VENDOR_ID_APPLE 0x05ac
57#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
58#define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
59#define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
60#define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
61#define USB_DEVICE_ID_APPLE_GEYSER_ISO 0x0215
62#define USB_DEVICE_ID_APPLE_GEYSER_JIS 0x0216
63#define USB_DEVICE_ID_APPLE_GEYSER3_ANSI 0x0217
64#define USB_DEVICE_ID_APPLE_GEYSER3_ISO 0x0218
65#define USB_DEVICE_ID_APPLE_GEYSER3_JIS 0x0219
66#define USB_DEVICE_ID_APPLE_GEYSER4_ANSI 0x021a
67#define USB_DEVICE_ID_APPLE_GEYSER4_ISO 0x021b
68#define USB_DEVICE_ID_APPLE_GEYSER4_JIS 0x021c
69#define USB_DEVICE_ID_APPLE_ALU_ANSI 0x0220
70#define USB_DEVICE_ID_APPLE_ALU_ISO 0x0221
71#define USB_DEVICE_ID_APPLE_ALU_JIS 0x0222
72#define USB_DEVICE_ID_APPLE_WELLSPRING_ANSI 0x0223
73#define USB_DEVICE_ID_APPLE_WELLSPRING_ISO 0x0224
74#define USB_DEVICE_ID_APPLE_WELLSPRING_JIS 0x0225
75#define USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI 0x0229
76#define USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO 0x022a
77#define USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS 0x022b
78#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI 0x022c
79#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO 0x022d
80#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS 0x022e
81#define USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI 0x0230
82#define USB_DEVICE_ID_APPLE_WELLSPRING2_ISO 0x0231
83#define USB_DEVICE_ID_APPLE_WELLSPRING2_JIS 0x0232
84#define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY 0x030a
85#define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY 0x030b
86#define USB_DEVICE_ID_APPLE_IRCONTROL4 0x8242
87
88#define USB_VENDOR_ID_ASUS 0x0b05
89#define USB_DEVICE_ID_ASUS_LCM 0x1726
90
91#define USB_VENDOR_ID_ATEN 0x0557
92#define USB_DEVICE_ID_ATEN_UC100KM 0x2004
93#define USB_DEVICE_ID_ATEN_CS124U 0x2202
94#define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
95#define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
96#define USB_DEVICE_ID_ATEN_4PORTKVMC 0x2208
97
98#define USB_VENDOR_ID_BELKIN 0x050d
99#define USB_DEVICE_ID_FLIP_KVM 0x3201
100
101#define USB_VENDOR_ID_BERKSHIRE 0x0c98
102#define USB_DEVICE_ID_BERKSHIRE_PCWD 0x1140
103
104#define USB_VENDOR_ID_CHERRY 0x046a
105#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
106
107#define USB_VENDOR_ID_CHIC 0x05fe
108#define USB_DEVICE_ID_CHIC_GAMEPAD 0x0014
109
110#define USB_VENDOR_ID_CIDC 0x1677
111
112#define USB_VENDOR_ID_CMEDIA 0x0d8c
113#define USB_DEVICE_ID_CM109 0x000e
114
115#define USB_VENDOR_ID_CODEMERCS 0x07c0
116#define USB_DEVICE_ID_CODEMERCS_IOW_FIRST 0x1500
117#define USB_DEVICE_ID_CODEMERCS_IOW_LAST 0x15ff
118
119#define USB_VENDOR_ID_CYGNAL 0x10c4
120#define USB_DEVICE_ID_CYGNAL_RADIO_SI470X 0x818a
121
122#define USB_VENDOR_ID_CYPRESS 0x04b4
123#define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
124#define USB_DEVICE_ID_CYPRESS_HIDCOM 0x5500
125#define USB_DEVICE_ID_CYPRESS_ULTRAMOUSE 0x7417
126#define USB_DEVICE_ID_CYPRESS_BARCODE_1 0xde61
127#define USB_DEVICE_ID_CYPRESS_BARCODE_2 0xde64
128
129#define USB_VENDOR_ID_DELL 0x413c
130#define USB_DEVICE_ID_DELL_W7658 0x2005
131
132#define USB_VENDOR_ID_DELORME 0x1163
133#define USB_DEVICE_ID_DELORME_EARTHMATE 0x0100
134#define USB_DEVICE_ID_DELORME_EM_LT20 0x0200
135
136#define USB_VENDOR_ID_DMI 0x0c0b
137#define USB_DEVICE_ID_DMI_ENC 0x5fab
138
139#define USB_VENDOR_ID_ELO 0x04E7
140#define USB_DEVICE_ID_ELO_TS2700 0x0020
141
142#define USB_VENDOR_ID_ESSENTIAL_REALITY 0x0d7f
143#define USB_DEVICE_ID_ESSENTIAL_REALITY_P5 0x0100
144
145#define USB_VENDOR_ID_EZKEY 0x0518
146#define USB_DEVICE_ID_BTC_8193 0x0002
147
148#define USB_VENDOR_ID_GAMERON 0x0810
149#define USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR 0x0001
150
151#define USB_VENDOR_ID_GENERAL_TOUCH 0x0dfc
152
153#define USB_VENDOR_ID_GLAB 0x06c2
154#define USB_DEVICE_ID_4_PHIDGETSERVO_30 0x0038
155#define USB_DEVICE_ID_1_PHIDGETSERVO_30 0x0039
156#define USB_DEVICE_ID_0_0_4_IF_KIT 0x0040
157#define USB_DEVICE_ID_0_16_16_IF_KIT 0x0044
158#define USB_DEVICE_ID_8_8_8_IF_KIT 0x0045
159#define USB_DEVICE_ID_0_8_7_IF_KIT 0x0051
160#define USB_DEVICE_ID_0_8_8_IF_KIT 0x0053
161#define USB_DEVICE_ID_PHIDGET_MOTORCONTROL 0x0058
162
163#define USB_VENDOR_ID_GOTOP 0x08f2
164#define USB_DEVICE_ID_SUPER_Q2 0x007f
165#define USB_DEVICE_ID_GOGOPEN 0x00ce
166#define USB_DEVICE_ID_PENPOWER 0x00f4
167
168#define USB_VENDOR_ID_GRETAGMACBETH 0x0971
169#define USB_DEVICE_ID_GRETAGMACBETH_HUEY 0x2005
170
171#define USB_VENDOR_ID_GRIFFIN 0x077d
172#define USB_DEVICE_ID_POWERMATE 0x0410
173#define USB_DEVICE_ID_SOUNDKNOB 0x04AA
174
175#define USB_VENDOR_ID_GTCO 0x078c
176#define USB_DEVICE_ID_GTCO_90 0x0090
177#define USB_DEVICE_ID_GTCO_100 0x0100
178#define USB_DEVICE_ID_GTCO_101 0x0101
179#define USB_DEVICE_ID_GTCO_103 0x0103
180#define USB_DEVICE_ID_GTCO_104 0x0104
181#define USB_DEVICE_ID_GTCO_105 0x0105
182#define USB_DEVICE_ID_GTCO_106 0x0106
183#define USB_DEVICE_ID_GTCO_107 0x0107
184#define USB_DEVICE_ID_GTCO_108 0x0108
185#define USB_DEVICE_ID_GTCO_200 0x0200
186#define USB_DEVICE_ID_GTCO_201 0x0201
187#define USB_DEVICE_ID_GTCO_202 0x0202
188#define USB_DEVICE_ID_GTCO_203 0x0203
189#define USB_DEVICE_ID_GTCO_204 0x0204
190#define USB_DEVICE_ID_GTCO_205 0x0205
191#define USB_DEVICE_ID_GTCO_206 0x0206
192#define USB_DEVICE_ID_GTCO_207 0x0207
193#define USB_DEVICE_ID_GTCO_300 0x0300
194#define USB_DEVICE_ID_GTCO_301 0x0301
195#define USB_DEVICE_ID_GTCO_302 0x0302
196#define USB_DEVICE_ID_GTCO_303 0x0303
197#define USB_DEVICE_ID_GTCO_304 0x0304
198#define USB_DEVICE_ID_GTCO_305 0x0305
199#define USB_DEVICE_ID_GTCO_306 0x0306
200#define USB_DEVICE_ID_GTCO_307 0x0307
201#define USB_DEVICE_ID_GTCO_308 0x0308
202#define USB_DEVICE_ID_GTCO_309 0x0309
203#define USB_DEVICE_ID_GTCO_400 0x0400
204#define USB_DEVICE_ID_GTCO_401 0x0401
205#define USB_DEVICE_ID_GTCO_402 0x0402
206#define USB_DEVICE_ID_GTCO_403 0x0403
207#define USB_DEVICE_ID_GTCO_404 0x0404
208#define USB_DEVICE_ID_GTCO_405 0x0405
209#define USB_DEVICE_ID_GTCO_500 0x0500
210#define USB_DEVICE_ID_GTCO_501 0x0501
211#define USB_DEVICE_ID_GTCO_502 0x0502
212#define USB_DEVICE_ID_GTCO_503 0x0503
213#define USB_DEVICE_ID_GTCO_504 0x0504
214#define USB_DEVICE_ID_GTCO_1000 0x1000
215#define USB_DEVICE_ID_GTCO_1001 0x1001
216#define USB_DEVICE_ID_GTCO_1002 0x1002
217#define USB_DEVICE_ID_GTCO_1003 0x1003
218#define USB_DEVICE_ID_GTCO_1004 0x1004
219#define USB_DEVICE_ID_GTCO_1005 0x1005
220#define USB_DEVICE_ID_GTCO_1006 0x1006
221#define USB_DEVICE_ID_GTCO_1007 0x1007
222#define USB_VENDOR_ID_HAPP 0x078b
223#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
224#define USB_DEVICE_ID_UGCI_FLYING 0x0020
225#define USB_DEVICE_ID_UGCI_FIGHTING 0x0030
226
227#define USB_VENDOR_ID_IMATION 0x0718
228#define USB_DEVICE_ID_DISC_STAKKA 0xd000
229
230#define USB_VENDOR_ID_KBGEAR 0x084e
231#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001
232
233#define USB_VENDOR_ID_LD 0x0f11
234#define USB_DEVICE_ID_LD_CASSY 0x1000
235#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
236#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
237#define USB_DEVICE_ID_LD_JWM 0x1080
238#define USB_DEVICE_ID_LD_DMMP 0x1081
239#define USB_DEVICE_ID_LD_UMIP 0x1090
240#define USB_DEVICE_ID_LD_XRAY1 0x1100
241#define USB_DEVICE_ID_LD_XRAY2 0x1101
242#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
243#define USB_DEVICE_ID_LD_COM3LAB 0x2000
244#define USB_DEVICE_ID_LD_TELEPORT 0x2010
245#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
246#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
247#define USB_DEVICE_ID_LD_MACHINETEST 0x2040
248
249#define USB_VENDOR_ID_LOGITECH 0x046d
250#define USB_DEVICE_ID_LOGITECH_LX3 0xc044
251#define USB_DEVICE_ID_LOGITECH_V150 0xc047
252#define USB_DEVICE_ID_LOGITECH_RECEIVER 0xc101
253#define USB_DEVICE_ID_LOGITECH_HARMONY 0xc110
254#define USB_DEVICE_ID_LOGITECH_HARMONY_2 0xc111
255#define USB_DEVICE_ID_LOGITECH_HARMONY_3 0xc112
256#define USB_DEVICE_ID_LOGITECH_HARMONY_4 0xc113
257#define USB_DEVICE_ID_LOGITECH_HARMONY_5 0xc114
258#define USB_DEVICE_ID_LOGITECH_HARMONY_6 0xc115
259#define USB_DEVICE_ID_LOGITECH_HARMONY_7 0xc116
260#define USB_DEVICE_ID_LOGITECH_HARMONY_8 0xc117
261#define USB_DEVICE_ID_LOGITECH_HARMONY_9 0xc118
262#define USB_DEVICE_ID_LOGITECH_HARMONY_10 0xc119
263#define USB_DEVICE_ID_LOGITECH_HARMONY_11 0xc11a
264#define USB_DEVICE_ID_LOGITECH_HARMONY_12 0xc11b
265#define USB_DEVICE_ID_LOGITECH_HARMONY_13 0xc11c
266#define USB_DEVICE_ID_LOGITECH_HARMONY_14 0xc11d
267#define USB_DEVICE_ID_LOGITECH_HARMONY_15 0xc11e
268#define USB_DEVICE_ID_LOGITECH_HARMONY_16 0xc11f
269#define USB_DEVICE_ID_LOGITECH_HARMONY_17 0xc120
270#define USB_DEVICE_ID_LOGITECH_HARMONY_18 0xc121
271#define USB_DEVICE_ID_LOGITECH_HARMONY_19 0xc122
272#define USB_DEVICE_ID_LOGITECH_HARMONY_20 0xc123
273#define USB_DEVICE_ID_LOGITECH_HARMONY_21 0xc124
274#define USB_DEVICE_ID_LOGITECH_HARMONY_22 0xc125
275#define USB_DEVICE_ID_LOGITECH_HARMONY_23 0xc126
276#define USB_DEVICE_ID_LOGITECH_HARMONY_24 0xc127
277#define USB_DEVICE_ID_LOGITECH_HARMONY_25 0xc128
278#define USB_DEVICE_ID_LOGITECH_HARMONY_26 0xc129
279#define USB_DEVICE_ID_LOGITECH_HARMONY_27 0xc12a
280#define USB_DEVICE_ID_LOGITECH_HARMONY_28 0xc12b
281#define USB_DEVICE_ID_LOGITECH_HARMONY_29 0xc12c
282#define USB_DEVICE_ID_LOGITECH_HARMONY_30 0xc12d
283#define USB_DEVICE_ID_LOGITECH_HARMONY_31 0xc12e
284#define USB_DEVICE_ID_LOGITECH_HARMONY_32 0xc12f
285#define USB_DEVICE_ID_LOGITECH_HARMONY_33 0xc130
286#define USB_DEVICE_ID_LOGITECH_HARMONY_34 0xc131
287#define USB_DEVICE_ID_LOGITECH_HARMONY_35 0xc132
288#define USB_DEVICE_ID_LOGITECH_HARMONY_36 0xc133
289#define USB_DEVICE_ID_LOGITECH_HARMONY_37 0xc134
290#define USB_DEVICE_ID_LOGITECH_HARMONY_38 0xc135
291#define USB_DEVICE_ID_LOGITECH_HARMONY_39 0xc136
292#define USB_DEVICE_ID_LOGITECH_HARMONY_40 0xc137
293#define USB_DEVICE_ID_LOGITECH_HARMONY_41 0xc138
294#define USB_DEVICE_ID_LOGITECH_HARMONY_42 0xc139
295#define USB_DEVICE_ID_LOGITECH_HARMONY_43 0xc13a
296#define USB_DEVICE_ID_LOGITECH_HARMONY_44 0xc13b
297#define USB_DEVICE_ID_LOGITECH_HARMONY_45 0xc13c
298#define USB_DEVICE_ID_LOGITECH_HARMONY_46 0xc13d
299#define USB_DEVICE_ID_LOGITECH_HARMONY_47 0xc13e
300#define USB_DEVICE_ID_LOGITECH_HARMONY_48 0xc13f
301#define USB_DEVICE_ID_LOGITECH_HARMONY_49 0xc140
302#define USB_DEVICE_ID_LOGITECH_HARMONY_50 0xc141
303#define USB_DEVICE_ID_LOGITECH_HARMONY_51 0xc142
304#define USB_DEVICE_ID_LOGITECH_HARMONY_52 0xc143
305#define USB_DEVICE_ID_LOGITECH_HARMONY_53 0xc144
306#define USB_DEVICE_ID_LOGITECH_HARMONY_54 0xc145
307#define USB_DEVICE_ID_LOGITECH_HARMONY_55 0xc146
308#define USB_DEVICE_ID_LOGITECH_HARMONY_56 0xc147
309#define USB_DEVICE_ID_LOGITECH_HARMONY_57 0xc148
310#define USB_DEVICE_ID_LOGITECH_HARMONY_58 0xc149
311#define USB_DEVICE_ID_LOGITECH_HARMONY_59 0xc14a
312#define USB_DEVICE_ID_LOGITECH_HARMONY_60 0xc14b
313#define USB_DEVICE_ID_LOGITECH_HARMONY_61 0xc14c
314#define USB_DEVICE_ID_LOGITECH_HARMONY_62 0xc14d
315#define USB_DEVICE_ID_LOGITECH_HARMONY_63 0xc14e
316#define USB_DEVICE_ID_LOGITECH_HARMONY_64 0xc14f
317#define USB_DEVICE_ID_LOGITECH_EXTREME_3D 0xc215
318#define USB_DEVICE_ID_LOGITECH_WHEEL 0xc294
319#define USB_DEVICE_ID_LOGITECH_ELITE_KBD 0xc30a
320#define USB_DEVICE_ID_LOGITECH_KBD 0xc311
321#define USB_DEVICE_ID_S510_RECEIVER 0xc50c
322#define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
323#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
324#define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
325#define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704
326#define USB_DEVICE_ID_DINOVO_EDGE 0xc714
327#define USB_DEVICE_ID_DINOVO_MINI 0xc71f
328
329#define USB_VENDOR_ID_MCC 0x09db
330#define USB_DEVICE_ID_MCC_PMD1024LS 0x0076
331#define USB_DEVICE_ID_MCC_PMD1208LS 0x007a
332
333#define USB_VENDOR_ID_MGE 0x0463
334#define USB_DEVICE_ID_MGE_UPS 0xffff
335#define USB_DEVICE_ID_MGE_UPS1 0x0001
336
337#define USB_VENDOR_ID_MICROCHIP 0x04d8
338#define USB_DEVICE_ID_PICKIT1 0x0032
339#define USB_DEVICE_ID_PICKIT2 0x0033
340
341#define USB_VENDOR_ID_MICROSOFT 0x045e
342#define USB_DEVICE_ID_SIDEWINDER_GV 0x003b
343#define USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0 0x009d
344#define USB_DEVICE_ID_DESKTOP_RECV_1028 0x00f9
345#define USB_DEVICE_ID_MS_NE4K 0x00db
346#define USB_DEVICE_ID_MS_LK6K 0x00f9
347
348#define USB_VENDOR_ID_MONTEREY 0x0566
349#define USB_DEVICE_ID_GENIUS_KB29E 0x3004
350
351#define USB_VENDOR_ID_NCR 0x0404
352#define USB_DEVICE_ID_NCR_FIRST 0x0300
353#define USB_DEVICE_ID_NCR_LAST 0x03ff
354
355#define USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR 0x0400
356#define USB_DEVICE_ID_N_S_HARMONY 0xc359
357
358#define USB_VENDOR_ID_NATSU 0x08b7
359#define USB_DEVICE_ID_NATSU_GAMEPAD 0x0001
360
361#define USB_VENDOR_ID_NEC 0x073e
362#define USB_DEVICE_ID_NEC_USB_GAME_PAD 0x0301
363
364#define USB_VENDOR_ID_ONTRAK 0x0a07
365#define USB_DEVICE_ID_ONTRAK_ADU100 0x0064
366
367#define USB_VENDOR_ID_PANJIT 0x134c
368
369#define USB_VENDOR_ID_PANTHERLORD 0x0810
370#define USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK 0x0001
371
372#define USB_VENDOR_ID_PETALYNX 0x18b1
373#define USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE 0x0037
374
375#define USB_VENDOR_ID_PLAYDOTCOM 0x0b43
376#define USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII 0x0003
377
378#define USB_VENDOR_ID_SAITEK 0x06a3
379#define USB_DEVICE_ID_SAITEK_RUMBLEPAD 0xff17
380
381#define USB_VENDOR_ID_SAMSUNG 0x0419
382#define USB_DEVICE_ID_SAMSUNG_IR_REMOTE 0x0001
383
384#define USB_VENDOR_ID_SONY 0x054c
385#define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268
386
387#define USB_VENDOR_ID_SOUNDGRAPH 0x15c2
388#define USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD 0x0038
389
390#define USB_VENDOR_ID_SUN 0x0430
391#define USB_DEVICE_ID_RARITAN_KVM_DONGLE 0xcdab
392
393#define USB_VENDOR_ID_SUNPLUS 0x04fc
394#define USB_DEVICE_ID_SUNPLUS_WDESKTOP 0x05d8
395
396#define USB_VENDOR_ID_TOPMAX 0x0663
397#define USB_DEVICE_ID_TOPMAX_COBRAPAD 0x0103
398
399#define USB_VENDOR_ID_TURBOX 0x062a
400#define USB_DEVICE_ID_TURBOX_KEYBOARD 0x0201
401
402#define USB_VENDOR_ID_VERNIER 0x08f7
403#define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
404#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
405#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
406#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
407#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
408
409#define USB_VENDOR_ID_WACOM 0x056a
410
411#define USB_VENDOR_ID_WISEGROUP 0x0925
412#define USB_DEVICE_ID_1_PHIDGETSERVO_20 0x8101
413#define USB_DEVICE_ID_4_PHIDGETSERVO_20 0x8104
414#define USB_DEVICE_ID_8_8_4_IF_KIT 0x8201
415#define USB_DEVICE_ID_QUAD_USB_JOYPAD 0x8800
416#define USB_DEVICE_ID_DUAL_USB_JOYPAD 0x8866
417
418#define USB_VENDOR_ID_WISEGROUP_LTD 0x6677
419#define USB_DEVICE_ID_SMARTJOY_DUAL_PLUS 0x8802
420
421#define USB_VENDOR_ID_YEALINK 0x6993
422#define USB_DEVICE_ID_YEALINK_P1K_P4K_B2K 0xb001
423
424#define USB_VENDOR_ID_KYE 0x0458
425#define USB_DEVICE_ID_KYE_GPEN_560 0x5003
426 21
427/* 22/*
428 * Alphabetically sorted blacklist by quirk type. 23 * Alphabetically sorted blacklist by quirk type.
@@ -433,18 +28,10 @@ static const struct hid_blacklist {
433 __u16 idProduct; 28 __u16 idProduct;
434 __u32 quirks; 29 __u32 quirks;
435} hid_blacklist[] = { 30} hid_blacklist[] = {
436
437 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU, HID_QUIRK_2WHEEL_MOUSE_HACK_7 },
438 { USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D, HID_QUIRK_2WHEEL_MOUSE_HACK_B8 },
439 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE, HID_QUIRK_2WHEEL_MOUSE_HACK_5 },
440
441 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER, HID_QUIRK_BAD_RELATIVE_KEYS },
442
443 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD }, 31 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_GAMEPAD, HID_QUIRK_BADPAD },
444 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD }, 32 { USB_VENDOR_ID_AASHIMA, USB_DEVICE_ID_AASHIMA_PREDATOR, HID_QUIRK_BADPAD },
445 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD }, 33 { USB_VENDOR_ID_ALPS, USB_DEVICE_ID_IBM_GAMEPAD, HID_QUIRK_BADPAD },
446 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD }, 34 { USB_VENDOR_ID_CHIC, USB_DEVICE_ID_CHIC_GAMEPAD, HID_QUIRK_BADPAD },
447 { USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR, HID_QUIRK_MULTI_INPUT },
448 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT }, 35 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_DRIVING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
449 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT }, 36 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FLYING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
450 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT }, 37 { USB_VENDOR_ID_HAPP, USB_DEVICE_ID_UGCI_FIGHTING, HID_QUIRK_BADPAD | HID_QUIRK_MULTI_INPUT },
@@ -453,169 +40,11 @@ static const struct hid_blacklist {
453 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD }, 40 { USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RUMBLEPAD, HID_QUIRK_BADPAD },
454 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD }, 41 { USB_VENDOR_ID_TOPMAX, USB_DEVICE_ID_TOPMAX_COBRAPAD, HID_QUIRK_BADPAD },
455 42
456 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP, HID_QUIRK_DUPLICATE_USAGES },
457 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE, HID_QUIRK_DUPLICATE_USAGES },
458 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI, HID_QUIRK_DUPLICATE_USAGES },
459
460 { USB_VENDOR_ID_AFATECH, USB_DEVICE_ID_AFATECH_AF9016, HID_QUIRK_FULLSPEED_INTERVAL }, 43 { USB_VENDOR_ID_AFATECH, USB_DEVICE_ID_AFATECH_AF9016, HID_QUIRK_FULLSPEED_INTERVAL },
461 44
462 { USB_VENDOR_ID_BELKIN, USB_DEVICE_ID_FLIP_KVM, HID_QUIRK_HIDDEV },
463 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4, HID_QUIRK_HIDDEV | HID_QUIRK_IGNORE_HIDINPUT },
464 { USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE, HID_QUIRK_HIDDEV | HID_QUIRK_IGNORE_HIDINPUT },
465 { USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV, HID_QUIRK_HIDINPUT },
466
467 { USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193, HID_QUIRK_HWHEEL_WHEEL_INVERT },
468
469 { USB_VENDOR_ID_ADS_TECH, USB_DEVICE_ID_ADS_TECH_RADIO_SI470X, HID_QUIRK_IGNORE },
470 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_01, HID_QUIRK_IGNORE },
471 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_10, HID_QUIRK_IGNORE },
472 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_20, HID_QUIRK_IGNORE },
473 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_21, HID_QUIRK_IGNORE },
474 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_22, HID_QUIRK_IGNORE },
475 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_23, HID_QUIRK_IGNORE },
476 { USB_VENDOR_ID_AIPTEK, USB_DEVICE_ID_AIPTEK_24, HID_QUIRK_IGNORE },
477 { USB_VENDOR_ID_AIRCABLE, USB_DEVICE_ID_AIRCABLE1, HID_QUIRK_IGNORE },
478 { USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_USBRS232, HID_QUIRK_IGNORE },
479 { USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM, HID_QUIRK_IGNORE},
480 { USB_VENDOR_ID_BERKSHIRE, USB_DEVICE_ID_BERKSHIRE_PCWD, HID_QUIRK_IGNORE },
481 { USB_VENDOR_ID_CIDC, 0x0103, HID_QUIRK_IGNORE },
482 { USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_RADIO_SI470X, HID_QUIRK_IGNORE },
483 { USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM109, HID_QUIRK_IGNORE },
484 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_HIDCOM, HID_QUIRK_IGNORE },
485 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_ULTRAMOUSE, HID_QUIRK_IGNORE },
486 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EARTHMATE, HID_QUIRK_IGNORE },
487 { USB_VENDOR_ID_DELORME, USB_DEVICE_ID_DELORME_EM_LT20, HID_QUIRK_IGNORE },
488 { USB_VENDOR_ID_ESSENTIAL_REALITY, USB_DEVICE_ID_ESSENTIAL_REALITY_P5, HID_QUIRK_IGNORE },
489 { USB_VENDOR_ID_GENERAL_TOUCH, 0x0001, HID_QUIRK_IGNORE },
490 { USB_VENDOR_ID_GENERAL_TOUCH, 0x0002, HID_QUIRK_IGNORE },
491 { USB_VENDOR_ID_GENERAL_TOUCH, 0x0003, HID_QUIRK_IGNORE },
492 { USB_VENDOR_ID_GENERAL_TOUCH, 0x0004, HID_QUIRK_IGNORE },
493 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_4_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
494 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_1_PHIDGETSERVO_30, HID_QUIRK_IGNORE },
495 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_0_4_IF_KIT, HID_QUIRK_IGNORE },
496 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_16_16_IF_KIT, HID_QUIRK_IGNORE },
497 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_8_8_8_IF_KIT, HID_QUIRK_IGNORE },
498 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_7_IF_KIT, HID_QUIRK_IGNORE },
499 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_0_8_8_IF_KIT, HID_QUIRK_IGNORE },
500 { USB_VENDOR_ID_GLAB, USB_DEVICE_ID_PHIDGET_MOTORCONTROL, HID_QUIRK_IGNORE },
501 { USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_SUPER_Q2, HID_QUIRK_IGNORE },
502 { USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_GOGOPEN, HID_QUIRK_IGNORE },
503 { USB_VENDOR_ID_GOTOP, USB_DEVICE_ID_PENPOWER, HID_QUIRK_IGNORE },
504 { USB_VENDOR_ID_GRETAGMACBETH, USB_DEVICE_ID_GRETAGMACBETH_HUEY, HID_QUIRK_IGNORE },
505 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_POWERMATE, HID_QUIRK_IGNORE },
506 { USB_VENDOR_ID_GRIFFIN, USB_DEVICE_ID_SOUNDKNOB, HID_QUIRK_IGNORE },
507 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_90, HID_QUIRK_IGNORE },
508 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_100, HID_QUIRK_IGNORE },
509 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_101, HID_QUIRK_IGNORE },
510 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_103, HID_QUIRK_IGNORE },
511 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_104, HID_QUIRK_IGNORE },
512 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_105, HID_QUIRK_IGNORE },
513 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_106, HID_QUIRK_IGNORE },
514 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_107, HID_QUIRK_IGNORE },
515 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_108, HID_QUIRK_IGNORE },
516 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_200, HID_QUIRK_IGNORE },
517 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_201, HID_QUIRK_IGNORE },
518 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_202, HID_QUIRK_IGNORE },
519 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_203, HID_QUIRK_IGNORE },
520 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_204, HID_QUIRK_IGNORE },
521 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_205, HID_QUIRK_IGNORE },
522 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_206, HID_QUIRK_IGNORE },
523 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_207, HID_QUIRK_IGNORE },
524 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_300, HID_QUIRK_IGNORE },
525 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_301, HID_QUIRK_IGNORE },
526 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_302, HID_QUIRK_IGNORE },
527 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_303, HID_QUIRK_IGNORE },
528 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_304, HID_QUIRK_IGNORE },
529 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_305, HID_QUIRK_IGNORE },
530 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_306, HID_QUIRK_IGNORE },
531 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_307, HID_QUIRK_IGNORE },
532 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_308, HID_QUIRK_IGNORE },
533 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_309, HID_QUIRK_IGNORE },
534 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_400, HID_QUIRK_IGNORE },
535 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_401, HID_QUIRK_IGNORE },
536 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_402, HID_QUIRK_IGNORE },
537 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_403, HID_QUIRK_IGNORE },
538 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_404, HID_QUIRK_IGNORE },
539 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_405, HID_QUIRK_IGNORE },
540 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_500, HID_QUIRK_IGNORE },
541 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_501, HID_QUIRK_IGNORE },
542 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_502, HID_QUIRK_IGNORE },
543 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_503, HID_QUIRK_IGNORE },
544 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_504, HID_QUIRK_IGNORE },
545 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1000, HID_QUIRK_IGNORE },
546 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1001, HID_QUIRK_IGNORE },
547 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1002, HID_QUIRK_IGNORE },
548 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1003, HID_QUIRK_IGNORE },
549 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1004, HID_QUIRK_IGNORE },
550 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1005, HID_QUIRK_IGNORE },
551 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006, HID_QUIRK_IGNORE },
552 { USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007, HID_QUIRK_IGNORE },
553 { USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA, HID_QUIRK_IGNORE },
554 { USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO, HID_QUIRK_IGNORE },
555 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY, HID_QUIRK_IGNORE },
556 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY, HID_QUIRK_IGNORE },
557 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY, HID_QUIRK_IGNORE },
558 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM, HID_QUIRK_IGNORE },
559 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP, HID_QUIRK_IGNORE },
560 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP, HID_QUIRK_IGNORE },
561 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1, HID_QUIRK_IGNORE },
562 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2, HID_QUIRK_IGNORE },
563 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM, HID_QUIRK_IGNORE },
564 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB, HID_QUIRK_IGNORE },
565 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT, HID_QUIRK_IGNORE },
566 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER, HID_QUIRK_IGNORE },
567 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL, HID_QUIRK_IGNORE },
568 { USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST, HID_QUIRK_IGNORE },
569 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1024LS, HID_QUIRK_IGNORE },
570 { USB_VENDOR_ID_MCC, USB_DEVICE_ID_MCC_PMD1208LS, HID_QUIRK_IGNORE },
571 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS, HID_QUIRK_IGNORE },
572 { USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1, HID_QUIRK_IGNORE },
573 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100, HID_QUIRK_IGNORE },
574 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 20, HID_QUIRK_IGNORE },
575 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 30, HID_QUIRK_IGNORE },
576 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 100, HID_QUIRK_IGNORE },
577 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 108, HID_QUIRK_IGNORE },
578 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 118, HID_QUIRK_IGNORE },
579 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 200, HID_QUIRK_IGNORE },
580 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 300, HID_QUIRK_IGNORE },
581 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 400, HID_QUIRK_IGNORE },
582 { USB_VENDOR_ID_ONTRAK, USB_DEVICE_ID_ONTRAK_ADU100 + 500, HID_QUIRK_IGNORE },
583 { USB_VENDOR_ID_PANJIT, 0x0001, HID_QUIRK_IGNORE },
584 { USB_VENDOR_ID_PANJIT, 0x0002, HID_QUIRK_IGNORE },
585 { USB_VENDOR_ID_PANJIT, 0x0003, HID_QUIRK_IGNORE },
586 { USB_VENDOR_ID_PANJIT, 0x0004, HID_QUIRK_IGNORE },
587 { USB_VENDOR_ID_SOUNDGRAPH, USB_DEVICE_ID_SOUNDGRAPH_IMON_LCD, HID_QUIRK_IGNORE },
588 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO, HID_QUIRK_IGNORE },
589 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP, HID_QUIRK_IGNORE },
590 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP, HID_QUIRK_IGNORE },
591 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS, HID_QUIRK_IGNORE },
592 { USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC, HID_QUIRK_IGNORE },
593 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_4_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
594 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_1_PHIDGETSERVO_20, HID_QUIRK_IGNORE },
595 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_8_8_4_IF_KIT, HID_QUIRK_IGNORE },
596 { USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K, HID_QUIRK_IGNORE },
597
598 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_FLAIR, HID_QUIRK_IGNORE },
599 { USB_VENDOR_ID_ACECAD, USB_DEVICE_ID_ACECAD_302, HID_QUIRK_IGNORE },
600
601 { USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT1, HID_QUIRK_IGNORE },
602 { USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICKIT2, HID_QUIRK_IGNORE },
603
604 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD, HID_QUIRK_LOGITECH_IGNORE_DOUBLED_WHEEL | HID_QUIRK_LOGITECH_EXPANDED_KEYMAP },
605 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500, HID_QUIRK_LOGITECH_IGNORE_DOUBLED_WHEEL | HID_QUIRK_LOGITECH_EXPANDED_KEYMAP },
606 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_LX3, HID_QUIRK_INVERT_HWHEEL },
607 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_V150, HID_QUIRK_INVERT_HWHEEL },
608
609 { USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K, HID_QUIRK_MICROSOFT_KEYS },
610 { USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K, HID_QUIRK_MICROSOFT_KEYS },
611
612 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
613
614 { USB_VENDOR_ID_PANTHERLORD, USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK, HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS }, 45 { USB_VENDOR_ID_PANTHERLORD, USB_DEVICE_ID_PANTHERLORD_TWIN_USB_JOYSTICK, HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS },
615 { USB_VENDOR_ID_PLAYDOTCOM, USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII, HID_QUIRK_MULTI_INPUT }, 46 { USB_VENDOR_ID_PLAYDOTCOM, USB_DEVICE_ID_PLAYDOTCOM_EMS_USBII, HID_QUIRK_MULTI_INPUT },
616 47
617 { USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER, HID_QUIRK_SONY_PS3_CONTROLLER | HID_QUIRK_HIDDEV },
618
619 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET }, 48 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
620 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET }, 49 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
621 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET }, 50 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
@@ -623,144 +52,13 @@ static const struct hid_blacklist {
623 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET }, 52 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
624 { USB_VENDOR_ID_DMI, USB_DEVICE_ID_DMI_ENC, HID_QUIRK_NOGET }, 53 { USB_VENDOR_ID_DMI, USB_DEVICE_ID_DMI_ENC, HID_QUIRK_NOGET },
625 { USB_VENDOR_ID_ELO, USB_DEVICE_ID_ELO_TS2700, HID_QUIRK_NOGET }, 54 { USB_VENDOR_ID_ELO, USB_DEVICE_ID_ELO_TS2700, HID_QUIRK_NOGET },
626 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D, HID_QUIRK_NOGET },
627 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL, HID_QUIRK_NOGET },
628 { USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0, HID_QUIRK_NOGET },
629 { USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE, HID_QUIRK_NOGET },
630 { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET }, 55 { USB_VENDOR_ID_SUN, USB_DEVICE_ID_RARITAN_KVM_DONGLE, HID_QUIRK_NOGET },
631 { USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_KEYBOARD, HID_QUIRK_NOGET }, 56 { USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_TURBOX_KEYBOARD, HID_QUIRK_NOGET },
632 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS }, 57 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT | HID_QUIRK_SKIP_OUTPUT_REPORTS },
633 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_QUAD_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT }, 58 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_QUAD_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
634 59
635 { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT }, 60 { USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
636 61 { USB_VENDOR_ID_WISEGROUP_LTD2, USB_DEVICE_ID_SMARTJOY_DUAL_PLUS, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
637 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
638 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
639 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
640 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_APPLE_ISO_KEYBOARD},
641 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER_JIS, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
642 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
643 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_APPLE_ISO_KEYBOARD},
644 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER3_JIS, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
645 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
646 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE | HID_QUIRK_APPLE_ISO_KEYBOARD},
647 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
648 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI, HID_QUIRK_APPLE_HAS_FN },
649 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_APPLE_ISO_KEYBOARD },
650 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS, HID_QUIRK_APPLE_HAS_FN },
651 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
652 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
653 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_HF_JIS, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
654 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN },
655 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_APPLE_ISO_KEYBOARD },
656 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN },
657 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
658 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_APPLE_ISO_KEYBOARD | HID_QUIRK_IGNORE_MOUSE},
659 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_JIS, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE},
660 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ANSI, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE},
661 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_ISO, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_APPLE_ISO_KEYBOARD | HID_QUIRK_IGNORE_MOUSE },
662 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING2_JIS, HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
663 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
664 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY, HID_QUIRK_APPLE_NUMLOCK_EMULATION | HID_QUIRK_APPLE_HAS_FN | HID_QUIRK_IGNORE_MOUSE },
665
666 { USB_VENDOR_ID_DELL, USB_DEVICE_ID_DELL_W7658, HID_QUIRK_RESET_LEDS },
667 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_KBD, HID_QUIRK_RESET_LEDS },
668 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY, HID_QUIRK_IGNORE },
669 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_2, HID_QUIRK_IGNORE },
670 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_3, HID_QUIRK_IGNORE },
671 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_4, HID_QUIRK_IGNORE },
672 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_5, HID_QUIRK_IGNORE },
673 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_6, HID_QUIRK_IGNORE },
674 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_7, HID_QUIRK_IGNORE },
675 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_8, HID_QUIRK_IGNORE },
676 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_9, HID_QUIRK_IGNORE },
677 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_10, HID_QUIRK_IGNORE },
678 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_11, HID_QUIRK_IGNORE },
679 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_12, HID_QUIRK_IGNORE },
680 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_13, HID_QUIRK_IGNORE },
681 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_14, HID_QUIRK_IGNORE },
682 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_15, HID_QUIRK_IGNORE },
683 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_16, HID_QUIRK_IGNORE },
684 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_17, HID_QUIRK_IGNORE },
685 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_18, HID_QUIRK_IGNORE },
686 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_19, HID_QUIRK_IGNORE },
687 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_20, HID_QUIRK_IGNORE },
688 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_21, HID_QUIRK_IGNORE },
689 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_22, HID_QUIRK_IGNORE },
690 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_23, HID_QUIRK_IGNORE },
691 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_24, HID_QUIRK_IGNORE },
692 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_25, HID_QUIRK_IGNORE },
693 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_26, HID_QUIRK_IGNORE },
694 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_27, HID_QUIRK_IGNORE },
695 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_28, HID_QUIRK_IGNORE },
696 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_29, HID_QUIRK_IGNORE },
697 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_30, HID_QUIRK_IGNORE },
698 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_31, HID_QUIRK_IGNORE },
699 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_32, HID_QUIRK_IGNORE },
700 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_33, HID_QUIRK_IGNORE },
701 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_34, HID_QUIRK_IGNORE },
702 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_35, HID_QUIRK_IGNORE },
703 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_36, HID_QUIRK_IGNORE },
704 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_37, HID_QUIRK_IGNORE },
705 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_38, HID_QUIRK_IGNORE },
706 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_39, HID_QUIRK_IGNORE },
707 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_40, HID_QUIRK_IGNORE },
708 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_41, HID_QUIRK_IGNORE },
709 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_42, HID_QUIRK_IGNORE },
710 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_43, HID_QUIRK_IGNORE },
711 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_44, HID_QUIRK_IGNORE },
712 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_45, HID_QUIRK_IGNORE },
713 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_46, HID_QUIRK_IGNORE },
714 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_47, HID_QUIRK_IGNORE },
715 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_48, HID_QUIRK_IGNORE },
716 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_49, HID_QUIRK_IGNORE },
717 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_50, HID_QUIRK_IGNORE },
718 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_51, HID_QUIRK_IGNORE },
719 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_52, HID_QUIRK_IGNORE },
720 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_53, HID_QUIRK_IGNORE },
721 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_54, HID_QUIRK_IGNORE },
722 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_55, HID_QUIRK_IGNORE },
723 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_56, HID_QUIRK_IGNORE },
724 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_57, HID_QUIRK_IGNORE },
725 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_58, HID_QUIRK_IGNORE },
726 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_59, HID_QUIRK_IGNORE },
727 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_60, HID_QUIRK_IGNORE },
728 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_61, HID_QUIRK_IGNORE },
729 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_62, HID_QUIRK_IGNORE },
730 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_63, HID_QUIRK_IGNORE },
731 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_64, HID_QUIRK_IGNORE },
732 { USB_VENDOR_ID_NATIONAL_SEMICONDUCTOR, USB_DEVICE_ID_N_S_HARMONY, HID_QUIRK_IGNORE },
733 { USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560, HID_QUIRK_IGNORE },
734
735 { 0, 0 }
736};
737
738/* Quirks for devices which require report descriptor fixup go here */
739static const struct hid_rdesc_blacklist {
740 __u16 idVendor;
741 __u16 idProduct;
742 __u32 quirks;
743} hid_rdesc_blacklist[] = {
744
745 { USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION, HID_QUIRK_RDESC_CYMOTION },
746
747 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER, HID_QUIRK_RDESC_LOGITECH },
748 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER, HID_QUIRK_RDESC_LOGITECH },
749 { USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2, HID_QUIRK_RDESC_LOGITECH },
750 { USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_DESKTOP_RECV_1028, HID_QUIRK_RDESC_MICROSOFT_RECV_1028 },
751
752 { USB_VENDOR_ID_MONTEREY, USB_DEVICE_ID_GENIUS_KB29E, HID_QUIRK_RDESC_BUTTON_CONSUMER },
753
754 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER4_JIS, HID_QUIRK_RDESC_MACBOOK_JIS },
755
756 { USB_VENDOR_ID_PETALYNX, USB_DEVICE_ID_PETALYNX_MAXTER_REMOTE, HID_QUIRK_RDESC_PETALYNX },
757
758 { USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE, HID_QUIRK_RDESC_SAMSUNG_REMOTE },
759
760 { USB_VENDOR_ID_SUNPLUS, USB_DEVICE_ID_SUNPLUS_WDESKTOP, HID_QUIRK_RDESC_SUNPLUS_WDESKTOP },
761
762 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1, HID_QUIRK_RDESC_SWAPPED_MIN_MAX },
763 { USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2, HID_QUIRK_RDESC_SWAPPED_MIN_MAX },
764 62
765 { 0, 0 } 63 { 0, 0 }
766}; 64};
@@ -974,16 +272,6 @@ u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct)
974 u32 quirks = 0; 272 u32 quirks = 0;
975 const struct hid_blacklist *bl_entry = NULL; 273 const struct hid_blacklist *bl_entry = NULL;
976 274
977 /* Ignore all Wacom devices */
978 if (idVendor == USB_VENDOR_ID_WACOM)
979 return HID_QUIRK_IGNORE;
980
981 /* ignore all Code Mercenaries IOWarrior devices */
982 if (idVendor == USB_VENDOR_ID_CODEMERCS)
983 if (idProduct >= USB_DEVICE_ID_CODEMERCS_IOW_FIRST &&
984 idProduct <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
985 return HID_QUIRK_IGNORE;
986
987 /* NCR devices must not be queried for reports */ 275 /* NCR devices must not be queried for reports */
988 if (idVendor == USB_VENDOR_ID_NCR && 276 if (idVendor == USB_VENDOR_ID_NCR &&
989 idProduct >= USB_DEVICE_ID_NCR_FIRST && 277 idProduct >= USB_DEVICE_ID_NCR_FIRST &&
@@ -1002,221 +290,3 @@ u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct)
1002} 290}
1003 291
1004EXPORT_SYMBOL_GPL(usbhid_lookup_quirk); 292EXPORT_SYMBOL_GPL(usbhid_lookup_quirk);
1005
1006/*
1007 * Cherry Cymotion keyboard have an invalid HID report descriptor,
1008 * that needs fixing before we can parse it.
1009 */
1010static void usbhid_fixup_cymotion_descriptor(char *rdesc, int rsize)
1011{
1012 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
1013 printk(KERN_INFO "Fixing up Cherry Cymotion report descriptor\n");
1014 rdesc[11] = rdesc[16] = 0xff;
1015 rdesc[12] = rdesc[17] = 0x03;
1016 }
1017}
1018
1019
1020/*
1021 * Certain Logitech keyboards send in report #3 keys which are far
1022 * above the logical maximum described in descriptor. This extends
1023 * the original value of 0x28c of logical maximum to 0x104d
1024 */
1025static void usbhid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
1026{
1027 if (rsize >= 90 && rdesc[83] == 0x26
1028 && rdesc[84] == 0x8c
1029 && rdesc[85] == 0x02) {
1030 printk(KERN_INFO "Fixing up Logitech keyboard report descriptor\n");
1031 rdesc[84] = rdesc[89] = 0x4d;
1032 rdesc[85] = rdesc[90] = 0x10;
1033 }
1034}
1035
1036static void usbhid_fixup_sunplus_wdesktop(unsigned char *rdesc, int rsize)
1037{
1038 if (rsize >= 107 && rdesc[104] == 0x26
1039 && rdesc[105] == 0x80
1040 && rdesc[106] == 0x03) {
1041 printk(KERN_INFO "Fixing up Sunplus Wireless Desktop report descriptor\n");
1042 rdesc[105] = rdesc[110] = 0x03;
1043 rdesc[106] = rdesc[111] = 0x21;
1044 }
1045}
1046
1047/*
1048 * Samsung IrDA remote controller (reports as Cypress USB Mouse).
1049 *
1050 * Vendor specific report #4 has a size of 48 bit,
1051 * and therefore is not accepted when inspecting the descriptors.
1052 * As a workaround we reinterpret the report as:
1053 * Variable type, count 6, size 8 bit, log. maximum 255
1054 * The burden to reconstruct the data is moved into user space.
1055 */
1056static void usbhid_fixup_samsung_irda_descriptor(unsigned char *rdesc,
1057 int rsize)
1058{
1059 if (rsize >= 182 && rdesc[175] == 0x25
1060 && rdesc[176] == 0x40
1061 && rdesc[177] == 0x75
1062 && rdesc[178] == 0x30
1063 && rdesc[179] == 0x95
1064 && rdesc[180] == 0x01
1065 && rdesc[182] == 0x40) {
1066 printk(KERN_INFO "Fixing up Samsung IrDA report descriptor\n");
1067 rdesc[176] = 0xff;
1068 rdesc[178] = 0x08;
1069 rdesc[180] = 0x06;
1070 rdesc[182] = 0x42;
1071 }
1072}
1073
1074/* Petalynx Maxter Remote has maximum for consumer page set too low */
1075static void usbhid_fixup_petalynx_descriptor(unsigned char *rdesc, int rsize)
1076{
1077 if (rsize >= 60 && rdesc[39] == 0x2a
1078 && rdesc[40] == 0xf5
1079 && rdesc[41] == 0x00
1080 && rdesc[59] == 0x26
1081 && rdesc[60] == 0xf9
1082 && rdesc[61] == 0x00) {
1083 printk(KERN_INFO "Fixing up Petalynx Maxter Remote report descriptor\n");
1084 rdesc[60] = 0xfa;
1085 rdesc[40] = 0xfa;
1086 }
1087}
1088
1089/*
1090 * Some USB barcode readers from cypress have usage min and usage max in
1091 * the wrong order
1092 */
1093static void usbhid_fixup_cypress_descriptor(unsigned char *rdesc, int rsize)
1094{
1095 short fixed = 0;
1096 int i;
1097
1098 for (i = 0; i < rsize - 4; i++) {
1099 if (rdesc[i] == 0x29 && rdesc [i+2] == 0x19) {
1100 unsigned char tmp;
1101
1102 rdesc[i] = 0x19; rdesc[i+2] = 0x29;
1103 tmp = rdesc[i+3];
1104 rdesc[i+3] = rdesc[i+1];
1105 rdesc[i+1] = tmp;
1106 }
1107 }
1108
1109 if (fixed)
1110 printk(KERN_INFO "Fixing up Cypress report descriptor\n");
1111}
1112
1113/*
1114 * MacBook JIS keyboard has wrong logical maximum
1115 */
1116static void usbhid_fixup_macbook_descriptor(unsigned char *rdesc, int rsize)
1117{
1118 if (rsize >= 60 && rdesc[53] == 0x65
1119 && rdesc[59] == 0x65) {
1120 printk(KERN_INFO "Fixing up MacBook JIS keyboard report descriptor\n");
1121 rdesc[53] = rdesc[59] = 0xe7;
1122 }
1123}
1124
1125static void usbhid_fixup_button_consumer_descriptor(unsigned char *rdesc, int rsize)
1126{
1127 if (rsize >= 30 && rdesc[29] == 0x05
1128 && rdesc[30] == 0x09) {
1129 printk(KERN_INFO "Fixing up button/consumer in HID report descriptor\n");
1130 rdesc[30] = 0x0c;
1131 }
1132}
1133
1134/*
1135 * Microsoft Wireless Desktop Receiver (Model 1028) has several
1136 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
1137 */
1138static void usbhid_fixup_microsoft_descriptor(unsigned char *rdesc, int rsize)
1139{
1140 if (rsize == 571 && rdesc[284] == 0x19
1141 && rdesc[286] == 0x2a
1142 && rdesc[304] == 0x19
1143 && rdesc[306] == 0x29
1144 && rdesc[352] == 0x1a
1145 && rdesc[355] == 0x2a
1146 && rdesc[557] == 0x19
1147 && rdesc[559] == 0x29) {
1148 printk(KERN_INFO "Fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
1149 rdesc[284] = rdesc[304] = rdesc[557] = 0x35;
1150 rdesc[352] = 0x36;
1151 rdesc[286] = rdesc[355] = 0x46;
1152 rdesc[306] = rdesc[559] = 0x45;
1153 }
1154}
1155
1156static void __usbhid_fixup_report_descriptor(__u32 quirks, char *rdesc, unsigned rsize)
1157{
1158 if ((quirks & HID_QUIRK_RDESC_CYMOTION))
1159 usbhid_fixup_cymotion_descriptor(rdesc, rsize);
1160
1161 if (quirks & HID_QUIRK_RDESC_LOGITECH)
1162 usbhid_fixup_logitech_descriptor(rdesc, rsize);
1163
1164 if (quirks & HID_QUIRK_RDESC_SWAPPED_MIN_MAX)
1165 usbhid_fixup_cypress_descriptor(rdesc, rsize);
1166
1167 if (quirks & HID_QUIRK_RDESC_PETALYNX)
1168 usbhid_fixup_petalynx_descriptor(rdesc, rsize);
1169
1170 if (quirks & HID_QUIRK_RDESC_MACBOOK_JIS)
1171 usbhid_fixup_macbook_descriptor(rdesc, rsize);
1172
1173 if (quirks & HID_QUIRK_RDESC_BUTTON_CONSUMER)
1174 usbhid_fixup_button_consumer_descriptor(rdesc, rsize);
1175
1176 if (quirks & HID_QUIRK_RDESC_SAMSUNG_REMOTE)
1177 usbhid_fixup_samsung_irda_descriptor(rdesc, rsize);
1178
1179 if (quirks & HID_QUIRK_RDESC_MICROSOFT_RECV_1028)
1180 usbhid_fixup_microsoft_descriptor(rdesc, rsize);
1181
1182 if (quirks & HID_QUIRK_RDESC_SUNPLUS_WDESKTOP)
1183 usbhid_fixup_sunplus_wdesktop(rdesc, rsize);
1184}
1185
1186/**
1187 * usbhid_fixup_report_descriptor: check if report descriptor needs fixup
1188 *
1189 * Description:
1190 * Walks the hid_rdesc_blacklist[] array and checks whether the device
1191 * is known to have broken report descriptor that needs to be fixed up
1192 * prior to entering the HID parser
1193 *
1194 * Returns: nothing
1195 */
1196void usbhid_fixup_report_descriptor(const u16 idVendor, const u16 idProduct,
1197 char *rdesc, unsigned rsize, char **quirks_param)
1198{
1199 int n, m;
1200 u16 paramVendor, paramProduct;
1201 u32 quirks;
1202
1203 /* static rdesc quirk entries */
1204 for (n = 0; hid_rdesc_blacklist[n].idVendor; n++)
1205 if (hid_rdesc_blacklist[n].idVendor == idVendor &&
1206 hid_rdesc_blacklist[n].idProduct == idProduct)
1207 __usbhid_fixup_report_descriptor(hid_rdesc_blacklist[n].quirks,
1208 rdesc, rsize);
1209
1210 /* runtime rdesc quirk entries handling */
1211 for (n = 0; quirks_param[n] && n < MAX_USBHID_BOOT_QUIRKS; n++) {
1212 m = sscanf(quirks_param[n], "0x%hx:0x%hx:0x%x",
1213 &paramVendor, &paramProduct, &quirks);
1214
1215 if (m != 3)
1216 printk(KERN_WARNING
1217 "Could not parse HID quirk module param %s\n",
1218 quirks_param[n]);
1219 else if (paramVendor == idVendor && paramProduct == idProduct)
1220 __usbhid_fixup_report_descriptor(quirks, rdesc, rsize);
1221 }
1222}
diff --git a/drivers/hid/usbhid/hiddev.c b/drivers/hid/usbhid/hiddev.c
index 842e9edb888e..babd65dd46ad 100644
--- a/drivers/hid/usbhid/hiddev.c
+++ b/drivers/hid/usbhid/hiddev.c
@@ -790,21 +790,23 @@ static struct usb_class_driver hiddev_class = {
790/* 790/*
791 * This is where hid.c calls us to connect a hid device to the hiddev driver 791 * This is where hid.c calls us to connect a hid device to the hiddev driver
792 */ 792 */
793int hiddev_connect(struct hid_device *hid) 793int hiddev_connect(struct hid_device *hid, unsigned int force)
794{ 794{
795 struct hiddev *hiddev; 795 struct hiddev *hiddev;
796 struct usbhid_device *usbhid = hid->driver_data; 796 struct usbhid_device *usbhid = hid->driver_data;
797 int i;
798 int retval; 797 int retval;
799 798
800 for (i = 0; i < hid->maxcollection; i++) 799 if (!force) {
801 if (hid->collection[i].type == 800 unsigned int i;
802 HID_COLLECTION_APPLICATION && 801 for (i = 0; i < hid->maxcollection; i++)
803 !IS_INPUT_APPLICATION(hid->collection[i].usage)) 802 if (hid->collection[i].type ==
804 break; 803 HID_COLLECTION_APPLICATION &&
804 !IS_INPUT_APPLICATION(hid->collection[i].usage))
805 break;
805 806
806 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0) 807 if (i == hid->maxcollection)
807 return -1; 808 return -1;
809 }
808 810
809 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL))) 811 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
810 return -1; 812 return -1;
diff --git a/drivers/hid/usbhid/usbhid.h b/drivers/hid/usbhid/usbhid.h
index 62d2d7c925bd..abedb13c623e 100644
--- a/drivers/hid/usbhid/usbhid.h
+++ b/drivers/hid/usbhid/usbhid.h
@@ -67,7 +67,7 @@ struct usbhid_device {
67 spinlock_t ctrllock; /* Control fifo spinlock */ 67 spinlock_t ctrllock; /* Control fifo spinlock */
68 68
69 struct urb *urbout; /* Output URB */ 69 struct urb *urbout; /* Output URB */
70 struct hid_report *out[HID_CONTROL_FIFO_SIZE]; /* Output pipe fifo */ 70 struct hid_output_fifo out[HID_CONTROL_FIFO_SIZE]; /* Output pipe fifo */
71 unsigned char outhead, outtail; /* Output pipe fifo head & tail */ 71 unsigned char outhead, outtail; /* Output pipe fifo head & tail */
72 char *outbuf; /* Output buffer */ 72 char *outbuf; /* Output buffer */
73 dma_addr_t outbuf_dma; /* Output buffer dma */ 73 dma_addr_t outbuf_dma; /* Output buffer dma */
@@ -82,7 +82,7 @@ struct usbhid_device {
82}; 82};
83 83
84#define hid_to_usb_dev(hid_dev) \ 84#define hid_to_usb_dev(hid_dev) \
85 container_of(hid_dev->dev->parent, struct usb_device, dev) 85 container_of(hid_dev->dev.parent->parent, struct usb_device, dev)
86 86
87#endif 87#endif
88 88
diff --git a/drivers/hid/usbhid/usbkbd.c b/drivers/hid/usbhid/usbkbd.c
index 0caaafe01843..b342926dd7fc 100644
--- a/drivers/hid/usbhid/usbkbd.c
+++ b/drivers/hid/usbhid/usbkbd.c
@@ -105,14 +105,16 @@ static void usb_kbd_irq(struct urb *urb)
105 if (usb_kbd_keycode[kbd->old[i]]) 105 if (usb_kbd_keycode[kbd->old[i]])
106 input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0); 106 input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
107 else 107 else
108 info("Unknown key (scancode %#x) released.", kbd->old[i]); 108 dev_info(&urb->dev->dev,
109 "Unknown key (scancode %#x) released.\n", kbd->old[i]);
109 } 110 }
110 111
111 if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) { 112 if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
112 if (usb_kbd_keycode[kbd->new[i]]) 113 if (usb_kbd_keycode[kbd->new[i]])
113 input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1); 114 input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
114 else 115 else
115 info("Unknown key (scancode %#x) pressed.", kbd->new[i]); 116 dev_info(&urb->dev->dev,
117 "Unknown key (scancode %#x) released.\n", kbd->new[i]);
116 } 118 }
117 } 119 }
118 120
@@ -159,7 +161,8 @@ static void usb_kbd_led(struct urb *urb)
159 struct usb_kbd *kbd = urb->context; 161 struct usb_kbd *kbd = urb->context;
160 162
161 if (urb->status) 163 if (urb->status)
162 warn("led urb status %d received", urb->status); 164 dev_warn(&urb->dev->dev, "led urb status %d received\n",
165 urb->status);
163 166
164 if (*(kbd->leds) == kbd->newleds) 167 if (*(kbd->leds) == kbd->newleds)
165 return; 168 return;
@@ -352,7 +355,8 @@ static int __init usb_kbd_init(void)
352{ 355{
353 int result = usb_register(&usb_kbd_driver); 356 int result = usb_register(&usb_kbd_driver);
354 if (result == 0) 357 if (result == 0)
355 info(DRIVER_VERSION ":" DRIVER_DESC); 358 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
359 DRIVER_DESC "\n");
356 return result; 360 return result;
357} 361}
358 362
diff --git a/drivers/hid/usbhid/usbmouse.c b/drivers/hid/usbhid/usbmouse.c
index 35689ef172cc..72ab4b268096 100644
--- a/drivers/hid/usbhid/usbmouse.c
+++ b/drivers/hid/usbhid/usbmouse.c
@@ -31,6 +31,11 @@
31#include <linux/usb/input.h> 31#include <linux/usb/input.h>
32#include <linux/hid.h> 32#include <linux/hid.h>
33 33
34/* for apple IDs */
35#ifdef CONFIG_USB_HID_MODULE
36#include "../hid-ids.h"
37#endif
38
34/* 39/*
35 * Version Information 40 * Version Information
36 */ 41 */
@@ -240,7 +245,8 @@ static int __init usb_mouse_init(void)
240{ 245{
241 int retval = usb_register(&usb_mouse_driver); 246 int retval = usb_register(&usb_mouse_driver);
242 if (retval == 0) 247 if (retval == 0)
243 info(DRIVER_VERSION ":" DRIVER_DESC); 248 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
249 DRIVER_DESC "\n");
244 return retval; 250 return retval;
245} 251}
246 252
diff --git a/drivers/hwmon/dme1737.c b/drivers/hwmon/dme1737.c
index cdb8311e4ef7..27a5d397f9a1 100644
--- a/drivers/hwmon/dme1737.c
+++ b/drivers/hwmon/dme1737.c
@@ -175,11 +175,11 @@ static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
175 * Data structures and manipulation thereof 175 * Data structures and manipulation thereof
176 * --------------------------------------------------------------------- */ 176 * --------------------------------------------------------------------- */
177 177
178/* For ISA chips, we abuse the i2c_client addr and name fields. We also use
179 the driver field to differentiate between I2C and ISA chips. */
180struct dme1737_data { 178struct dme1737_data {
181 struct i2c_client client; 179 struct i2c_client *client; /* for I2C devices only */
182 struct device *hwmon_dev; 180 struct device *hwmon_dev;
181 const char *name;
182 unsigned int addr; /* for ISA devices only */
183 183
184 struct mutex update_lock; 184 struct mutex update_lock;
185 int valid; /* !=0 if following fields are valid */ 185 int valid; /* !=0 if following fields are valid */
@@ -512,11 +512,12 @@ static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
512 * before calling dme1737_read or dme1737_write. 512 * before calling dme1737_read or dme1737_write.
513 * --------------------------------------------------------------------- */ 513 * --------------------------------------------------------------------- */
514 514
515static u8 dme1737_read(struct i2c_client *client, u8 reg) 515static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
516{ 516{
517 struct i2c_client *client = data->client;
517 s32 val; 518 s32 val;
518 519
519 if (client->driver) { /* I2C device */ 520 if (client) { /* I2C device */
520 val = i2c_smbus_read_byte_data(client, reg); 521 val = i2c_smbus_read_byte_data(client, reg);
521 522
522 if (val < 0) { 523 if (val < 0) {
@@ -525,18 +526,19 @@ static u8 dme1737_read(struct i2c_client *client, u8 reg)
525 "maintainer.\n", reg); 526 "maintainer.\n", reg);
526 } 527 }
527 } else { /* ISA device */ 528 } else { /* ISA device */
528 outb(reg, client->addr); 529 outb(reg, data->addr);
529 val = inb(client->addr + 1); 530 val = inb(data->addr + 1);
530 } 531 }
531 532
532 return val; 533 return val;
533} 534}
534 535
535static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val) 536static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
536{ 537{
538 struct i2c_client *client = data->client;
537 s32 res = 0; 539 s32 res = 0;
538 540
539 if (client->driver) { /* I2C device */ 541 if (client) { /* I2C device */
540 res = i2c_smbus_write_byte_data(client, reg, val); 542 res = i2c_smbus_write_byte_data(client, reg, val);
541 543
542 if (res < 0) { 544 if (res < 0) {
@@ -545,8 +547,8 @@ static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
545 "maintainer.\n", reg); 547 "maintainer.\n", reg);
546 } 548 }
547 } else { /* ISA device */ 549 } else { /* ISA device */
548 outb(reg, client->addr); 550 outb(reg, data->addr);
549 outb(val, client->addr + 1); 551 outb(val, data->addr + 1);
550 } 552 }
551 553
552 return res; 554 return res;
@@ -555,7 +557,6 @@ static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
555static struct dme1737_data *dme1737_update_device(struct device *dev) 557static struct dme1737_data *dme1737_update_device(struct device *dev)
556{ 558{
557 struct dme1737_data *data = dev_get_drvdata(dev); 559 struct dme1737_data *data = dev_get_drvdata(dev);
558 struct i2c_client *client = &data->client;
559 int ix; 560 int ix;
560 u8 lsb[5]; 561 u8 lsb[5];
561 562
@@ -563,7 +564,7 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
563 564
564 /* Enable a Vbat monitoring cycle every 10 mins */ 565 /* Enable a Vbat monitoring cycle every 10 mins */
565 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) { 566 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
566 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client, 567 dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
567 DME1737_REG_CONFIG) | 0x10); 568 DME1737_REG_CONFIG) | 0x10);
568 data->last_vbat = jiffies; 569 data->last_vbat = jiffies;
569 } 570 }
@@ -571,7 +572,7 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
571 /* Sample register contents every 1 sec */ 572 /* Sample register contents every 1 sec */
572 if (time_after(jiffies, data->last_update + HZ) || !data->valid) { 573 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
573 if (data->type != sch5027) { 574 if (data->type != sch5027) {
574 data->vid = dme1737_read(client, DME1737_REG_VID) & 575 data->vid = dme1737_read(data, DME1737_REG_VID) &
575 0x3f; 576 0x3f;
576 } 577 }
577 578
@@ -580,11 +581,11 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
580 /* Voltage inputs are stored as 16 bit values even 581 /* Voltage inputs are stored as 16 bit values even
581 * though they have only 12 bits resolution. This is 582 * though they have only 12 bits resolution. This is
582 * to make it consistent with the temp inputs. */ 583 * to make it consistent with the temp inputs. */
583 data->in[ix] = dme1737_read(client, 584 data->in[ix] = dme1737_read(data,
584 DME1737_REG_IN(ix)) << 8; 585 DME1737_REG_IN(ix)) << 8;
585 data->in_min[ix] = dme1737_read(client, 586 data->in_min[ix] = dme1737_read(data,
586 DME1737_REG_IN_MIN(ix)); 587 DME1737_REG_IN_MIN(ix));
587 data->in_max[ix] = dme1737_read(client, 588 data->in_max[ix] = dme1737_read(data,
588 DME1737_REG_IN_MAX(ix)); 589 DME1737_REG_IN_MAX(ix));
589 } 590 }
590 591
@@ -595,14 +596,14 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
595 * to take advantage of implicit conversions between 596 * to take advantage of implicit conversions between
596 * register values (2's complement) and temp values 597 * register values (2's complement) and temp values
597 * (signed decimal). */ 598 * (signed decimal). */
598 data->temp[ix] = dme1737_read(client, 599 data->temp[ix] = dme1737_read(data,
599 DME1737_REG_TEMP(ix)) << 8; 600 DME1737_REG_TEMP(ix)) << 8;
600 data->temp_min[ix] = dme1737_read(client, 601 data->temp_min[ix] = dme1737_read(data,
601 DME1737_REG_TEMP_MIN(ix)); 602 DME1737_REG_TEMP_MIN(ix));
602 data->temp_max[ix] = dme1737_read(client, 603 data->temp_max[ix] = dme1737_read(data,
603 DME1737_REG_TEMP_MAX(ix)); 604 DME1737_REG_TEMP_MAX(ix));
604 if (data->type != sch5027) { 605 if (data->type != sch5027) {
605 data->temp_offset[ix] = dme1737_read(client, 606 data->temp_offset[ix] = dme1737_read(data,
606 DME1737_REG_TEMP_OFFSET(ix)); 607 DME1737_REG_TEMP_OFFSET(ix));
607 } 608 }
608 } 609 }
@@ -612,7 +613,7 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
612 * which the registers are read (MSB first, then LSB) is 613 * which the registers are read (MSB first, then LSB) is
613 * important! */ 614 * important! */
614 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) { 615 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
615 lsb[ix] = dme1737_read(client, 616 lsb[ix] = dme1737_read(data,
616 DME1737_REG_IN_TEMP_LSB(ix)); 617 DME1737_REG_IN_TEMP_LSB(ix));
617 } 618 }
618 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { 619 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
@@ -631,19 +632,19 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
631 if (!(data->has_fan & (1 << ix))) { 632 if (!(data->has_fan & (1 << ix))) {
632 continue; 633 continue;
633 } 634 }
634 data->fan[ix] = dme1737_read(client, 635 data->fan[ix] = dme1737_read(data,
635 DME1737_REG_FAN(ix)); 636 DME1737_REG_FAN(ix));
636 data->fan[ix] |= dme1737_read(client, 637 data->fan[ix] |= dme1737_read(data,
637 DME1737_REG_FAN(ix) + 1) << 8; 638 DME1737_REG_FAN(ix) + 1) << 8;
638 data->fan_min[ix] = dme1737_read(client, 639 data->fan_min[ix] = dme1737_read(data,
639 DME1737_REG_FAN_MIN(ix)); 640 DME1737_REG_FAN_MIN(ix));
640 data->fan_min[ix] |= dme1737_read(client, 641 data->fan_min[ix] |= dme1737_read(data,
641 DME1737_REG_FAN_MIN(ix) + 1) << 8; 642 DME1737_REG_FAN_MIN(ix) + 1) << 8;
642 data->fan_opt[ix] = dme1737_read(client, 643 data->fan_opt[ix] = dme1737_read(data,
643 DME1737_REG_FAN_OPT(ix)); 644 DME1737_REG_FAN_OPT(ix));
644 /* fan_max exists only for fan[5-6] */ 645 /* fan_max exists only for fan[5-6] */
645 if (ix > 3) { 646 if (ix > 3) {
646 data->fan_max[ix - 4] = dme1737_read(client, 647 data->fan_max[ix - 4] = dme1737_read(data,
647 DME1737_REG_FAN_MAX(ix)); 648 DME1737_REG_FAN_MAX(ix));
648 } 649 }
649 } 650 }
@@ -655,63 +656,63 @@ static struct dme1737_data *dme1737_update_device(struct device *dev)
655 if (!(data->has_pwm & (1 << ix))) { 656 if (!(data->has_pwm & (1 << ix))) {
656 continue; 657 continue;
657 } 658 }
658 data->pwm[ix] = dme1737_read(client, 659 data->pwm[ix] = dme1737_read(data,
659 DME1737_REG_PWM(ix)); 660 DME1737_REG_PWM(ix));
660 data->pwm_freq[ix] = dme1737_read(client, 661 data->pwm_freq[ix] = dme1737_read(data,
661 DME1737_REG_PWM_FREQ(ix)); 662 DME1737_REG_PWM_FREQ(ix));
662 /* pwm_config and pwm_min exist only for pwm[1-3] */ 663 /* pwm_config and pwm_min exist only for pwm[1-3] */
663 if (ix < 3) { 664 if (ix < 3) {
664 data->pwm_config[ix] = dme1737_read(client, 665 data->pwm_config[ix] = dme1737_read(data,
665 DME1737_REG_PWM_CONFIG(ix)); 666 DME1737_REG_PWM_CONFIG(ix));
666 data->pwm_min[ix] = dme1737_read(client, 667 data->pwm_min[ix] = dme1737_read(data,
667 DME1737_REG_PWM_MIN(ix)); 668 DME1737_REG_PWM_MIN(ix));
668 } 669 }
669 } 670 }
670 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) { 671 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
671 data->pwm_rr[ix] = dme1737_read(client, 672 data->pwm_rr[ix] = dme1737_read(data,
672 DME1737_REG_PWM_RR(ix)); 673 DME1737_REG_PWM_RR(ix));
673 } 674 }
674 675
675 /* Thermal zone registers */ 676 /* Thermal zone registers */
676 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) { 677 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
677 data->zone_low[ix] = dme1737_read(client, 678 data->zone_low[ix] = dme1737_read(data,
678 DME1737_REG_ZONE_LOW(ix)); 679 DME1737_REG_ZONE_LOW(ix));
679 data->zone_abs[ix] = dme1737_read(client, 680 data->zone_abs[ix] = dme1737_read(data,
680 DME1737_REG_ZONE_ABS(ix)); 681 DME1737_REG_ZONE_ABS(ix));
681 } 682 }
682 if (data->type != sch5027) { 683 if (data->type != sch5027) {
683 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) { 684 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
684 data->zone_hyst[ix] = dme1737_read(client, 685 data->zone_hyst[ix] = dme1737_read(data,
685 DME1737_REG_ZONE_HYST(ix)); 686 DME1737_REG_ZONE_HYST(ix));
686 } 687 }
687 } 688 }
688 689
689 /* Alarm registers */ 690 /* Alarm registers */
690 data->alarms = dme1737_read(client, 691 data->alarms = dme1737_read(data,
691 DME1737_REG_ALARM1); 692 DME1737_REG_ALARM1);
692 /* Bit 7 tells us if the other alarm registers are non-zero and 693 /* Bit 7 tells us if the other alarm registers are non-zero and
693 * therefore also need to be read */ 694 * therefore also need to be read */
694 if (data->alarms & 0x80) { 695 if (data->alarms & 0x80) {
695 data->alarms |= dme1737_read(client, 696 data->alarms |= dme1737_read(data,
696 DME1737_REG_ALARM2) << 8; 697 DME1737_REG_ALARM2) << 8;
697 data->alarms |= dme1737_read(client, 698 data->alarms |= dme1737_read(data,
698 DME1737_REG_ALARM3) << 16; 699 DME1737_REG_ALARM3) << 16;
699 } 700 }
700 701
701 /* The ISA chips require explicit clearing of alarm bits. 702 /* The ISA chips require explicit clearing of alarm bits.
702 * Don't worry, an alarm will come back if the condition 703 * Don't worry, an alarm will come back if the condition
703 * that causes it still exists */ 704 * that causes it still exists */
704 if (!client->driver) { 705 if (!data->client) {
705 if (data->alarms & 0xff0000) { 706 if (data->alarms & 0xff0000) {
706 dme1737_write(client, DME1737_REG_ALARM3, 707 dme1737_write(data, DME1737_REG_ALARM3,
707 0xff); 708 0xff);
708 } 709 }
709 if (data->alarms & 0xff00) { 710 if (data->alarms & 0xff00) {
710 dme1737_write(client, DME1737_REG_ALARM2, 711 dme1737_write(data, DME1737_REG_ALARM2,
711 0xff); 712 0xff);
712 } 713 }
713 if (data->alarms & 0xff) { 714 if (data->alarms & 0xff) {
714 dme1737_write(client, DME1737_REG_ALARM1, 715 dme1737_write(data, DME1737_REG_ALARM1,
715 0xff); 716 0xff);
716 } 717 }
717 } 718 }
@@ -770,7 +771,6 @@ static ssize_t set_in(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count) 771 const char *buf, size_t count)
771{ 772{
772 struct dme1737_data *data = dev_get_drvdata(dev); 773 struct dme1737_data *data = dev_get_drvdata(dev);
773 struct i2c_client *client = &data->client;
774 struct sensor_device_attribute_2 774 struct sensor_device_attribute_2
775 *sensor_attr_2 = to_sensor_dev_attr_2(attr); 775 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
776 int ix = sensor_attr_2->index; 776 int ix = sensor_attr_2->index;
@@ -781,12 +781,12 @@ static ssize_t set_in(struct device *dev, struct device_attribute *attr,
781 switch (fn) { 781 switch (fn) {
782 case SYS_IN_MIN: 782 case SYS_IN_MIN:
783 data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]); 783 data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
784 dme1737_write(client, DME1737_REG_IN_MIN(ix), 784 dme1737_write(data, DME1737_REG_IN_MIN(ix),
785 data->in_min[ix]); 785 data->in_min[ix]);
786 break; 786 break;
787 case SYS_IN_MAX: 787 case SYS_IN_MAX:
788 data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]); 788 data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
789 dme1737_write(client, DME1737_REG_IN_MAX(ix), 789 dme1737_write(data, DME1737_REG_IN_MAX(ix),
790 data->in_max[ix]); 790 data->in_max[ix]);
791 break; 791 break;
792 default: 792 default:
@@ -850,7 +850,6 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
850 const char *buf, size_t count) 850 const char *buf, size_t count)
851{ 851{
852 struct dme1737_data *data = dev_get_drvdata(dev); 852 struct dme1737_data *data = dev_get_drvdata(dev);
853 struct i2c_client *client = &data->client;
854 struct sensor_device_attribute_2 853 struct sensor_device_attribute_2
855 *sensor_attr_2 = to_sensor_dev_attr_2(attr); 854 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
856 int ix = sensor_attr_2->index; 855 int ix = sensor_attr_2->index;
@@ -861,17 +860,17 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
861 switch (fn) { 860 switch (fn) {
862 case SYS_TEMP_MIN: 861 case SYS_TEMP_MIN:
863 data->temp_min[ix] = TEMP_TO_REG(val); 862 data->temp_min[ix] = TEMP_TO_REG(val);
864 dme1737_write(client, DME1737_REG_TEMP_MIN(ix), 863 dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
865 data->temp_min[ix]); 864 data->temp_min[ix]);
866 break; 865 break;
867 case SYS_TEMP_MAX: 866 case SYS_TEMP_MAX:
868 data->temp_max[ix] = TEMP_TO_REG(val); 867 data->temp_max[ix] = TEMP_TO_REG(val);
869 dme1737_write(client, DME1737_REG_TEMP_MAX(ix), 868 dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
870 data->temp_max[ix]); 869 data->temp_max[ix]);
871 break; 870 break;
872 case SYS_TEMP_OFFSET: 871 case SYS_TEMP_OFFSET:
873 data->temp_offset[ix] = TEMP_TO_REG(val); 872 data->temp_offset[ix] = TEMP_TO_REG(val);
874 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix), 873 dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
875 data->temp_offset[ix]); 874 data->temp_offset[ix]);
876 break; 875 break;
877 default: 876 default:
@@ -939,7 +938,6 @@ static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
939 const char *buf, size_t count) 938 const char *buf, size_t count)
940{ 939{
941 struct dme1737_data *data = dev_get_drvdata(dev); 940 struct dme1737_data *data = dev_get_drvdata(dev);
942 struct i2c_client *client = &data->client;
943 struct sensor_device_attribute_2 941 struct sensor_device_attribute_2
944 *sensor_attr_2 = to_sensor_dev_attr_2(attr); 942 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
945 int ix = sensor_attr_2->index; 943 int ix = sensor_attr_2->index;
@@ -950,37 +948,37 @@ static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
950 switch (fn) { 948 switch (fn) {
951 case SYS_ZONE_AUTO_POINT1_TEMP_HYST: 949 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
952 /* Refresh the cache */ 950 /* Refresh the cache */
953 data->zone_low[ix] = dme1737_read(client, 951 data->zone_low[ix] = dme1737_read(data,
954 DME1737_REG_ZONE_LOW(ix)); 952 DME1737_REG_ZONE_LOW(ix));
955 /* Modify the temp hyst value */ 953 /* Modify the temp hyst value */
956 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG( 954 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
957 TEMP_FROM_REG(data->zone_low[ix], 8) - 955 TEMP_FROM_REG(data->zone_low[ix], 8) -
958 val, ix, dme1737_read(client, 956 val, ix, dme1737_read(data,
959 DME1737_REG_ZONE_HYST(ix == 2))); 957 DME1737_REG_ZONE_HYST(ix == 2)));
960 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2), 958 dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
961 data->zone_hyst[ix == 2]); 959 data->zone_hyst[ix == 2]);
962 break; 960 break;
963 case SYS_ZONE_AUTO_POINT1_TEMP: 961 case SYS_ZONE_AUTO_POINT1_TEMP:
964 data->zone_low[ix] = TEMP_TO_REG(val); 962 data->zone_low[ix] = TEMP_TO_REG(val);
965 dme1737_write(client, DME1737_REG_ZONE_LOW(ix), 963 dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
966 data->zone_low[ix]); 964 data->zone_low[ix]);
967 break; 965 break;
968 case SYS_ZONE_AUTO_POINT2_TEMP: 966 case SYS_ZONE_AUTO_POINT2_TEMP:
969 /* Refresh the cache */ 967 /* Refresh the cache */
970 data->zone_low[ix] = dme1737_read(client, 968 data->zone_low[ix] = dme1737_read(data,
971 DME1737_REG_ZONE_LOW(ix)); 969 DME1737_REG_ZONE_LOW(ix));
972 /* Modify the temp range value (which is stored in the upper 970 /* Modify the temp range value (which is stored in the upper
973 * nibble of the pwm_freq register) */ 971 * nibble of the pwm_freq register) */
974 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val - 972 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
975 TEMP_FROM_REG(data->zone_low[ix], 8), 973 TEMP_FROM_REG(data->zone_low[ix], 8),
976 dme1737_read(client, 974 dme1737_read(data,
977 DME1737_REG_PWM_FREQ(ix))); 975 DME1737_REG_PWM_FREQ(ix)));
978 dme1737_write(client, DME1737_REG_PWM_FREQ(ix), 976 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
979 data->pwm_freq[ix]); 977 data->pwm_freq[ix]);
980 break; 978 break;
981 case SYS_ZONE_AUTO_POINT3_TEMP: 979 case SYS_ZONE_AUTO_POINT3_TEMP:
982 data->zone_abs[ix] = TEMP_TO_REG(val); 980 data->zone_abs[ix] = TEMP_TO_REG(val);
983 dme1737_write(client, DME1737_REG_ZONE_ABS(ix), 981 dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
984 data->zone_abs[ix]); 982 data->zone_abs[ix]);
985 break; 983 break;
986 default: 984 default:
@@ -1046,7 +1044,6 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1046 const char *buf, size_t count) 1044 const char *buf, size_t count)
1047{ 1045{
1048 struct dme1737_data *data = dev_get_drvdata(dev); 1046 struct dme1737_data *data = dev_get_drvdata(dev);
1049 struct i2c_client *client = &data->client;
1050 struct sensor_device_attribute_2 1047 struct sensor_device_attribute_2
1051 *sensor_attr_2 = to_sensor_dev_attr_2(attr); 1048 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1052 int ix = sensor_attr_2->index; 1049 int ix = sensor_attr_2->index;
@@ -1060,21 +1057,21 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1060 data->fan_min[ix] = FAN_TO_REG(val, 0); 1057 data->fan_min[ix] = FAN_TO_REG(val, 0);
1061 } else { 1058 } else {
1062 /* Refresh the cache */ 1059 /* Refresh the cache */
1063 data->fan_opt[ix] = dme1737_read(client, 1060 data->fan_opt[ix] = dme1737_read(data,
1064 DME1737_REG_FAN_OPT(ix)); 1061 DME1737_REG_FAN_OPT(ix));
1065 /* Modify the fan min value */ 1062 /* Modify the fan min value */
1066 data->fan_min[ix] = FAN_TO_REG(val, 1063 data->fan_min[ix] = FAN_TO_REG(val,
1067 FAN_TPC_FROM_REG(data->fan_opt[ix])); 1064 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1068 } 1065 }
1069 dme1737_write(client, DME1737_REG_FAN_MIN(ix), 1066 dme1737_write(data, DME1737_REG_FAN_MIN(ix),
1070 data->fan_min[ix] & 0xff); 1067 data->fan_min[ix] & 0xff);
1071 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1, 1068 dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
1072 data->fan_min[ix] >> 8); 1069 data->fan_min[ix] >> 8);
1073 break; 1070 break;
1074 case SYS_FAN_MAX: 1071 case SYS_FAN_MAX:
1075 /* Only valid for fan[5-6] */ 1072 /* Only valid for fan[5-6] */
1076 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val); 1073 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1077 dme1737_write(client, DME1737_REG_FAN_MAX(ix), 1074 dme1737_write(data, DME1737_REG_FAN_MAX(ix),
1078 data->fan_max[ix - 4]); 1075 data->fan_max[ix - 4]);
1079 break; 1076 break;
1080 case SYS_FAN_TYPE: 1077 case SYS_FAN_TYPE:
@@ -1086,9 +1083,9 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1086 val); 1083 val);
1087 goto exit; 1084 goto exit;
1088 } 1085 }
1089 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client, 1086 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
1090 DME1737_REG_FAN_OPT(ix))); 1087 DME1737_REG_FAN_OPT(ix)));
1091 dme1737_write(client, DME1737_REG_FAN_OPT(ix), 1088 dme1737_write(data, DME1737_REG_FAN_OPT(ix),
1092 data->fan_opt[ix]); 1089 data->fan_opt[ix]);
1093 break; 1090 break;
1094 default: 1091 default:
@@ -1185,7 +1182,6 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1185 const char *buf, size_t count) 1182 const char *buf, size_t count)
1186{ 1183{
1187 struct dme1737_data *data = dev_get_drvdata(dev); 1184 struct dme1737_data *data = dev_get_drvdata(dev);
1188 struct i2c_client *client = &data->client;
1189 struct sensor_device_attribute_2 1185 struct sensor_device_attribute_2
1190 *sensor_attr_2 = to_sensor_dev_attr_2(attr); 1186 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1191 int ix = sensor_attr_2->index; 1187 int ix = sensor_attr_2->index;
@@ -1196,12 +1192,12 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1196 switch (fn) { 1192 switch (fn) {
1197 case SYS_PWM: 1193 case SYS_PWM:
1198 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255); 1194 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1199 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]); 1195 dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
1200 break; 1196 break;
1201 case SYS_PWM_FREQ: 1197 case SYS_PWM_FREQ:
1202 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client, 1198 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
1203 DME1737_REG_PWM_FREQ(ix))); 1199 DME1737_REG_PWM_FREQ(ix)));
1204 dme1737_write(client, DME1737_REG_PWM_FREQ(ix), 1200 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
1205 data->pwm_freq[ix]); 1201 data->pwm_freq[ix]);
1206 break; 1202 break;
1207 case SYS_PWM_ENABLE: 1203 case SYS_PWM_ENABLE:
@@ -1214,7 +1210,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1214 goto exit; 1210 goto exit;
1215 } 1211 }
1216 /* Refresh the cache */ 1212 /* Refresh the cache */
1217 data->pwm_config[ix] = dme1737_read(client, 1213 data->pwm_config[ix] = dme1737_read(data,
1218 DME1737_REG_PWM_CONFIG(ix)); 1214 DME1737_REG_PWM_CONFIG(ix));
1219 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) { 1215 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1220 /* Bail out if no change */ 1216 /* Bail out if no change */
@@ -1226,14 +1222,14 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1226 data->pwm_acz[ix] = PWM_ACZ_FROM_REG( 1222 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1227 data->pwm_config[ix]); 1223 data->pwm_config[ix]);
1228 /* Save the current ramp rate state and disable it */ 1224 /* Save the current ramp rate state and disable it */
1229 data->pwm_rr[ix > 0] = dme1737_read(client, 1225 data->pwm_rr[ix > 0] = dme1737_read(data,
1230 DME1737_REG_PWM_RR(ix > 0)); 1226 DME1737_REG_PWM_RR(ix > 0));
1231 data->pwm_rr_en &= ~(1 << ix); 1227 data->pwm_rr_en &= ~(1 << ix);
1232 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) { 1228 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1233 data->pwm_rr_en |= (1 << ix); 1229 data->pwm_rr_en |= (1 << ix);
1234 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix, 1230 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1235 data->pwm_rr[ix > 0]); 1231 data->pwm_rr[ix > 0]);
1236 dme1737_write(client, 1232 dme1737_write(data,
1237 DME1737_REG_PWM_RR(ix > 0), 1233 DME1737_REG_PWM_RR(ix > 0),
1238 data->pwm_rr[ix > 0]); 1234 data->pwm_rr[ix > 0]);
1239 } 1235 }
@@ -1247,14 +1243,14 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1247 /* Turn fan fully on */ 1243 /* Turn fan fully on */
1248 data->pwm_config[ix] = PWM_EN_TO_REG(0, 1244 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1249 data->pwm_config[ix]); 1245 data->pwm_config[ix]);
1250 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), 1246 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
1251 data->pwm_config[ix]); 1247 data->pwm_config[ix]);
1252 break; 1248 break;
1253 case 1: 1249 case 1:
1254 /* Turn on manual mode */ 1250 /* Turn on manual mode */
1255 data->pwm_config[ix] = PWM_EN_TO_REG(1, 1251 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1256 data->pwm_config[ix]); 1252 data->pwm_config[ix]);
1257 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), 1253 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
1258 data->pwm_config[ix]); 1254 data->pwm_config[ix]);
1259 /* Change permissions of pwm[ix] to read-writeable */ 1255 /* Change permissions of pwm[ix] to read-writeable */
1260 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix], 1256 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
@@ -1269,14 +1265,14 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1269 data->pwm_config[ix] = PWM_ACZ_TO_REG( 1265 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1270 data->pwm_acz[ix], 1266 data->pwm_acz[ix],
1271 data->pwm_config[ix]); 1267 data->pwm_config[ix]);
1272 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), 1268 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
1273 data->pwm_config[ix]); 1269 data->pwm_config[ix]);
1274 /* Enable PWM ramp rate if previously enabled */ 1270 /* Enable PWM ramp rate if previously enabled */
1275 if (data->pwm_rr_en & (1 << ix)) { 1271 if (data->pwm_rr_en & (1 << ix)) {
1276 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix, 1272 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1277 dme1737_read(client, 1273 dme1737_read(data,
1278 DME1737_REG_PWM_RR(ix > 0))); 1274 DME1737_REG_PWM_RR(ix > 0)));
1279 dme1737_write(client, 1275 dme1737_write(data,
1280 DME1737_REG_PWM_RR(ix > 0), 1276 DME1737_REG_PWM_RR(ix > 0),
1281 data->pwm_rr[ix > 0]); 1277 data->pwm_rr[ix > 0]);
1282 } 1278 }
@@ -1286,9 +1282,9 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1286 case SYS_PWM_RAMP_RATE: 1282 case SYS_PWM_RAMP_RATE:
1287 /* Only valid for pwm[1-3] */ 1283 /* Only valid for pwm[1-3] */
1288 /* Refresh the cache */ 1284 /* Refresh the cache */
1289 data->pwm_config[ix] = dme1737_read(client, 1285 data->pwm_config[ix] = dme1737_read(data,
1290 DME1737_REG_PWM_CONFIG(ix)); 1286 DME1737_REG_PWM_CONFIG(ix));
1291 data->pwm_rr[ix > 0] = dme1737_read(client, 1287 data->pwm_rr[ix > 0] = dme1737_read(data,
1292 DME1737_REG_PWM_RR(ix > 0)); 1288 DME1737_REG_PWM_RR(ix > 0));
1293 /* Set the ramp rate value */ 1289 /* Set the ramp rate value */
1294 if (val > 0) { 1290 if (val > 0) {
@@ -1301,7 +1297,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1301 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix, 1297 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1302 data->pwm_rr[ix > 0]); 1298 data->pwm_rr[ix > 0]);
1303 } 1299 }
1304 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0), 1300 dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
1305 data->pwm_rr[ix > 0]); 1301 data->pwm_rr[ix > 0]);
1306 break; 1302 break;
1307 case SYS_PWM_AUTO_CHANNELS_ZONE: 1303 case SYS_PWM_AUTO_CHANNELS_ZONE:
@@ -1315,14 +1311,14 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1315 goto exit; 1311 goto exit;
1316 } 1312 }
1317 /* Refresh the cache */ 1313 /* Refresh the cache */
1318 data->pwm_config[ix] = dme1737_read(client, 1314 data->pwm_config[ix] = dme1737_read(data,
1319 DME1737_REG_PWM_CONFIG(ix)); 1315 DME1737_REG_PWM_CONFIG(ix));
1320 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { 1316 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1321 /* PWM is already in auto mode so update the temp 1317 /* PWM is already in auto mode so update the temp
1322 * channel assignment */ 1318 * channel assignment */
1323 data->pwm_config[ix] = PWM_ACZ_TO_REG(val, 1319 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1324 data->pwm_config[ix]); 1320 data->pwm_config[ix]);
1325 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), 1321 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
1326 data->pwm_config[ix]); 1322 data->pwm_config[ix]);
1327 } else { 1323 } else {
1328 /* PWM is not in auto mode so we save the temp 1324 /* PWM is not in auto mode so we save the temp
@@ -1333,7 +1329,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1333 case SYS_PWM_AUTO_PWM_MIN: 1329 case SYS_PWM_AUTO_PWM_MIN:
1334 /* Only valid for pwm[1-3] */ 1330 /* Only valid for pwm[1-3] */
1335 /* Refresh the cache */ 1331 /* Refresh the cache */
1336 data->pwm_min[ix] = dme1737_read(client, 1332 data->pwm_min[ix] = dme1737_read(data,
1337 DME1737_REG_PWM_MIN(ix)); 1333 DME1737_REG_PWM_MIN(ix));
1338 /* There are only 2 values supported for the auto_pwm_min 1334 /* There are only 2 values supported for the auto_pwm_min
1339 * value: 0 or auto_point1_pwm. So if the temperature drops 1335 * value: 0 or auto_point1_pwm. So if the temperature drops
@@ -1341,20 +1337,20 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1341 * off or runs at auto_point1_pwm duty-cycle. */ 1337 * off or runs at auto_point1_pwm duty-cycle. */
1342 if (val > ((data->pwm_min[ix] + 1) / 2)) { 1338 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1343 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix, 1339 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1344 dme1737_read(client, 1340 dme1737_read(data,
1345 DME1737_REG_PWM_RR(0))); 1341 DME1737_REG_PWM_RR(0)));
1346 } else { 1342 } else {
1347 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix, 1343 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1348 dme1737_read(client, 1344 dme1737_read(data,
1349 DME1737_REG_PWM_RR(0))); 1345 DME1737_REG_PWM_RR(0)));
1350 } 1346 }
1351 dme1737_write(client, DME1737_REG_PWM_RR(0), 1347 dme1737_write(data, DME1737_REG_PWM_RR(0),
1352 data->pwm_rr[0]); 1348 data->pwm_rr[0]);
1353 break; 1349 break;
1354 case SYS_PWM_AUTO_POINT1_PWM: 1350 case SYS_PWM_AUTO_POINT1_PWM:
1355 /* Only valid for pwm[1-3] */ 1351 /* Only valid for pwm[1-3] */
1356 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255); 1352 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1357 dme1737_write(client, DME1737_REG_PWM_MIN(ix), 1353 dme1737_write(data, DME1737_REG_PWM_MIN(ix),
1358 data->pwm_min[ix]); 1354 data->pwm_min[ix]);
1359 break; 1355 break;
1360 default: 1356 default:
@@ -1402,7 +1398,7 @@ static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1402{ 1398{
1403 struct dme1737_data *data = dev_get_drvdata(dev); 1399 struct dme1737_data *data = dev_get_drvdata(dev);
1404 1400
1405 return sprintf(buf, "%s\n", data->client.name); 1401 return sprintf(buf, "%s\n", data->name);
1406} 1402}
1407 1403
1408/* --------------------------------------------------------------------- 1404/* ---------------------------------------------------------------------
@@ -1908,7 +1904,7 @@ static void dme1737_remove_files(struct device *dev)
1908 1904
1909 sysfs_remove_group(&dev->kobj, &dme1737_group); 1905 sysfs_remove_group(&dev->kobj, &dme1737_group);
1910 1906
1911 if (!data->client.driver) { 1907 if (!data->client) {
1912 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr); 1908 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1913 } 1909 }
1914} 1910}
@@ -1919,7 +1915,7 @@ static int dme1737_create_files(struct device *dev)
1919 int err, ix; 1915 int err, ix;
1920 1916
1921 /* Create a name attribute for ISA devices */ 1917 /* Create a name attribute for ISA devices */
1922 if (!data->client.driver && 1918 if (!data->client &&
1923 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) { 1919 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1924 goto exit; 1920 goto exit;
1925 } 1921 }
@@ -2013,14 +2009,14 @@ exit:
2013static int dme1737_init_device(struct device *dev) 2009static int dme1737_init_device(struct device *dev)
2014{ 2010{
2015 struct dme1737_data *data = dev_get_drvdata(dev); 2011 struct dme1737_data *data = dev_get_drvdata(dev);
2016 struct i2c_client *client = &data->client; 2012 struct i2c_client *client = data->client;
2017 int ix; 2013 int ix;
2018 u8 reg; 2014 u8 reg;
2019 2015
2020 /* Point to the right nominal voltages array */ 2016 /* Point to the right nominal voltages array */
2021 data->in_nominal = IN_NOMINAL(data->type); 2017 data->in_nominal = IN_NOMINAL(data->type);
2022 2018
2023 data->config = dme1737_read(client, DME1737_REG_CONFIG); 2019 data->config = dme1737_read(data, DME1737_REG_CONFIG);
2024 /* Inform if part is not monitoring/started */ 2020 /* Inform if part is not monitoring/started */
2025 if (!(data->config & 0x01)) { 2021 if (!(data->config & 0x01)) {
2026 if (!force_start) { 2022 if (!force_start) {
@@ -2032,7 +2028,7 @@ static int dme1737_init_device(struct device *dev)
2032 2028
2033 /* Force monitoring */ 2029 /* Force monitoring */
2034 data->config |= 0x01; 2030 data->config |= 0x01;
2035 dme1737_write(client, DME1737_REG_CONFIG, data->config); 2031 dme1737_write(data, DME1737_REG_CONFIG, data->config);
2036 } 2032 }
2037 /* Inform if part is not ready */ 2033 /* Inform if part is not ready */
2038 if (!(data->config & 0x04)) { 2034 if (!(data->config & 0x04)) {
@@ -2041,8 +2037,8 @@ static int dme1737_init_device(struct device *dev)
2041 } 2037 }
2042 2038
2043 /* Determine which optional fan and pwm features are enabled/present */ 2039 /* Determine which optional fan and pwm features are enabled/present */
2044 if (client->driver) { /* I2C chip */ 2040 if (client) { /* I2C chip */
2045 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2); 2041 data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
2046 /* Check if optional fan3 input is enabled */ 2042 /* Check if optional fan3 input is enabled */
2047 if (data->config2 & 0x04) { 2043 if (data->config2 & 0x04) {
2048 data->has_fan |= (1 << 2); 2044 data->has_fan |= (1 << 2);
@@ -2051,7 +2047,7 @@ static int dme1737_init_device(struct device *dev)
2051 /* Fan4 and pwm3 are only available if the client's I2C address 2047 /* Fan4 and pwm3 are only available if the client's I2C address
2052 * is the default 0x2e. Otherwise the I/Os associated with 2048 * is the default 0x2e. Otherwise the I/Os associated with
2053 * these functions are used for addr enable/select. */ 2049 * these functions are used for addr enable/select. */
2054 if (data->client.addr == 0x2e) { 2050 if (client->addr == 0x2e) {
2055 data->has_fan |= (1 << 3); 2051 data->has_fan |= (1 << 3);
2056 data->has_pwm |= (1 << 2); 2052 data->has_pwm |= (1 << 2);
2057 } 2053 }
@@ -2086,16 +2082,16 @@ static int dme1737_init_device(struct device *dev)
2086 (data->has_fan & (1 << 4)) ? "yes" : "no", 2082 (data->has_fan & (1 << 4)) ? "yes" : "no",
2087 (data->has_fan & (1 << 5)) ? "yes" : "no"); 2083 (data->has_fan & (1 << 5)) ? "yes" : "no");
2088 2084
2089 reg = dme1737_read(client, DME1737_REG_TACH_PWM); 2085 reg = dme1737_read(data, DME1737_REG_TACH_PWM);
2090 /* Inform if fan-to-pwm mapping differs from the default */ 2086 /* Inform if fan-to-pwm mapping differs from the default */
2091 if (client->driver && reg != 0xa4) { /* I2C chip */ 2087 if (client && reg != 0xa4) { /* I2C chip */
2092 dev_warn(dev, "Non-standard fan to pwm mapping: " 2088 dev_warn(dev, "Non-standard fan to pwm mapping: "
2093 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, " 2089 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2094 "fan4->pwm%d. Please report to the driver " 2090 "fan4->pwm%d. Please report to the driver "
2095 "maintainer.\n", 2091 "maintainer.\n",
2096 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1, 2092 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2097 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1); 2093 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
2098 } else if (!client->driver && reg != 0x24) { /* ISA chip */ 2094 } else if (!client && reg != 0x24) { /* ISA chip */
2099 dev_warn(dev, "Non-standard fan to pwm mapping: " 2095 dev_warn(dev, "Non-standard fan to pwm mapping: "
2100 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. " 2096 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2101 "Please report to the driver maintainer.\n", 2097 "Please report to the driver maintainer.\n",
@@ -2108,7 +2104,7 @@ static int dme1737_init_device(struct device *dev)
2108 * disabled). */ 2104 * disabled). */
2109 if (!(data->config & 0x02)) { 2105 if (!(data->config & 0x02)) {
2110 for (ix = 0; ix < 3; ix++) { 2106 for (ix = 0; ix < 3; ix++) {
2111 data->pwm_config[ix] = dme1737_read(client, 2107 data->pwm_config[ix] = dme1737_read(data,
2112 DME1737_REG_PWM_CONFIG(ix)); 2108 DME1737_REG_PWM_CONFIG(ix));
2113 if ((data->has_pwm & (1 << ix)) && 2109 if ((data->has_pwm & (1 << ix)) &&
2114 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) { 2110 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
@@ -2116,8 +2112,8 @@ static int dme1737_init_device(struct device *dev)
2116 "manual mode.\n", ix + 1); 2112 "manual mode.\n", ix + 1);
2117 data->pwm_config[ix] = PWM_EN_TO_REG(1, 2113 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2118 data->pwm_config[ix]); 2114 data->pwm_config[ix]);
2119 dme1737_write(client, DME1737_REG_PWM(ix), 0); 2115 dme1737_write(data, DME1737_REG_PWM(ix), 0);
2120 dme1737_write(client, 2116 dme1737_write(data,
2121 DME1737_REG_PWM_CONFIG(ix), 2117 DME1737_REG_PWM_CONFIG(ix),
2122 data->pwm_config[ix]); 2118 data->pwm_config[ix]);
2123 } 2119 }
@@ -2191,37 +2187,24 @@ exit:
2191 return err; 2187 return err;
2192} 2188}
2193 2189
2194static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address, 2190/* Return 0 if detection is successful, -ENODEV otherwise */
2195 int kind) 2191static int dme1737_i2c_detect(struct i2c_client *client, int kind,
2192 struct i2c_board_info *info)
2196{ 2193{
2194 struct i2c_adapter *adapter = client->adapter;
2195 struct device *dev = &adapter->dev;
2197 u8 company, verstep = 0; 2196 u8 company, verstep = 0;
2198 struct i2c_client *client;
2199 struct dme1737_data *data;
2200 struct device *dev;
2201 int err = 0;
2202 const char *name; 2197 const char *name;
2203 2198
2204 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 2199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2205 goto exit; 2200 return -ENODEV;
2206 }
2207
2208 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2209 err = -ENOMEM;
2210 goto exit;
2211 } 2201 }
2212 2202
2213 client = &data->client;
2214 i2c_set_clientdata(client, data);
2215 client->addr = address;
2216 client->adapter = adapter;
2217 client->driver = &dme1737_i2c_driver;
2218 dev = &client->dev;
2219
2220 /* A negative kind means that the driver was loaded with no force 2203 /* A negative kind means that the driver was loaded with no force
2221 * parameter (default), so we must identify the chip. */ 2204 * parameter (default), so we must identify the chip. */
2222 if (kind < 0) { 2205 if (kind < 0) {
2223 company = dme1737_read(client, DME1737_REG_COMPANY); 2206 company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
2224 verstep = dme1737_read(client, DME1737_REG_VERSTEP); 2207 verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);
2225 2208
2226 if (company == DME1737_COMPANY_SMSC && 2209 if (company == DME1737_COMPANY_SMSC &&
2227 (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) { 2210 (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
@@ -2230,8 +2213,7 @@ static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2230 verstep == SCH5027_VERSTEP) { 2213 verstep == SCH5027_VERSTEP) {
2231 kind = sch5027; 2214 kind = sch5027;
2232 } else { 2215 } else {
2233 err = -ENODEV; 2216 return -ENODEV;
2234 goto exit_kfree;
2235 } 2217 }
2236 } 2218 }
2237 2219
@@ -2241,32 +2223,44 @@ static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2241 kind = dme1737; 2223 kind = dme1737;
2242 name = "dme1737"; 2224 name = "dme1737";
2243 } 2225 }
2244 data->type = kind;
2245
2246 /* Fill in the remaining client fields and put it into the global
2247 * list */
2248 strlcpy(client->name, name, I2C_NAME_SIZE);
2249 mutex_init(&data->update_lock);
2250
2251 /* Tell the I2C layer a new client has arrived */
2252 if ((err = i2c_attach_client(client))) {
2253 goto exit_kfree;
2254 }
2255 2226
2256 dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n", 2227 dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
2257 kind == sch5027 ? "SCH5027" : "DME1737", client->addr, 2228 kind == sch5027 ? "SCH5027" : "DME1737", client->addr,
2258 verstep); 2229 verstep);
2230 strlcpy(info->type, name, I2C_NAME_SIZE);
2231
2232 return 0;
2233}
2234
2235static int dme1737_i2c_probe(struct i2c_client *client,
2236 const struct i2c_device_id *id)
2237{
2238 struct dme1737_data *data;
2239 struct device *dev = &client->dev;
2240 int err;
2241
2242 data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
2243 if (!data) {
2244 err = -ENOMEM;
2245 goto exit;
2246 }
2247
2248 i2c_set_clientdata(client, data);
2249 data->type = id->driver_data;
2250 data->client = client;
2251 data->name = client->name;
2252 mutex_init(&data->update_lock);
2259 2253
2260 /* Initialize the DME1737 chip */ 2254 /* Initialize the DME1737 chip */
2261 if ((err = dme1737_init_device(dev))) { 2255 if ((err = dme1737_init_device(dev))) {
2262 dev_err(dev, "Failed to initialize device.\n"); 2256 dev_err(dev, "Failed to initialize device.\n");
2263 goto exit_detach; 2257 goto exit_kfree;
2264 } 2258 }
2265 2259
2266 /* Create sysfs files */ 2260 /* Create sysfs files */
2267 if ((err = dme1737_create_files(dev))) { 2261 if ((err = dme1737_create_files(dev))) {
2268 dev_err(dev, "Failed to create sysfs files.\n"); 2262 dev_err(dev, "Failed to create sysfs files.\n");
2269 goto exit_detach; 2263 goto exit_kfree;
2270 } 2264 }
2271 2265
2272 /* Register device */ 2266 /* Register device */
@@ -2281,45 +2275,40 @@ static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2281 2275
2282exit_remove: 2276exit_remove:
2283 dme1737_remove_files(dev); 2277 dme1737_remove_files(dev);
2284exit_detach:
2285 i2c_detach_client(client);
2286exit_kfree: 2278exit_kfree:
2287 kfree(data); 2279 kfree(data);
2288exit: 2280exit:
2289 return err; 2281 return err;
2290} 2282}
2291 2283
2292static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter) 2284static int dme1737_i2c_remove(struct i2c_client *client)
2293{
2294 if (!(adapter->class & I2C_CLASS_HWMON)) {
2295 return 0;
2296 }
2297
2298 return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
2299}
2300
2301static int dme1737_i2c_detach_client(struct i2c_client *client)
2302{ 2285{
2303 struct dme1737_data *data = i2c_get_clientdata(client); 2286 struct dme1737_data *data = i2c_get_clientdata(client);
2304 int err;
2305 2287
2306 hwmon_device_unregister(data->hwmon_dev); 2288 hwmon_device_unregister(data->hwmon_dev);
2307 dme1737_remove_files(&client->dev); 2289 dme1737_remove_files(&client->dev);
2308 2290
2309 if ((err = i2c_detach_client(client))) {
2310 return err;
2311 }
2312
2313 kfree(data); 2291 kfree(data);
2314 return 0; 2292 return 0;
2315} 2293}
2316 2294
2295static const struct i2c_device_id dme1737_id[] = {
2296 { "dme1737", dme1737 },
2297 { "sch5027", sch5027 },
2298 { }
2299};
2300MODULE_DEVICE_TABLE(i2c, dme1737_id);
2301
2317static struct i2c_driver dme1737_i2c_driver = { 2302static struct i2c_driver dme1737_i2c_driver = {
2303 .class = I2C_CLASS_HWMON,
2318 .driver = { 2304 .driver = {
2319 .name = "dme1737", 2305 .name = "dme1737",
2320 }, 2306 },
2321 .attach_adapter = dme1737_i2c_attach_adapter, 2307 .probe = dme1737_i2c_probe,
2322 .detach_client = dme1737_i2c_detach_client, 2308 .remove = dme1737_i2c_remove,
2309 .id_table = dme1737_id,
2310 .detect = dme1737_i2c_detect,
2311 .address_data = &addr_data,
2323}; 2312};
2324 2313
2325/* --------------------------------------------------------------------- 2314/* ---------------------------------------------------------------------
@@ -2403,7 +2392,6 @@ static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2403{ 2392{
2404 u8 company, device; 2393 u8 company, device;
2405 struct resource *res; 2394 struct resource *res;
2406 struct i2c_client *client;
2407 struct dme1737_data *data; 2395 struct dme1737_data *data;
2408 struct device *dev = &pdev->dev; 2396 struct device *dev = &pdev->dev;
2409 int err; 2397 int err;
@@ -2422,15 +2410,13 @@ static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2422 goto exit_release_region; 2410 goto exit_release_region;
2423 } 2411 }
2424 2412
2425 client = &data->client; 2413 data->addr = res->start;
2426 i2c_set_clientdata(client, data);
2427 client->addr = res->start;
2428 platform_set_drvdata(pdev, data); 2414 platform_set_drvdata(pdev, data);
2429 2415
2430 /* Skip chip detection if module is loaded with force_id parameter */ 2416 /* Skip chip detection if module is loaded with force_id parameter */
2431 if (!force_id) { 2417 if (!force_id) {
2432 company = dme1737_read(client, DME1737_REG_COMPANY); 2418 company = dme1737_read(data, DME1737_REG_COMPANY);
2433 device = dme1737_read(client, DME1737_REG_DEVICE); 2419 device = dme1737_read(data, DME1737_REG_DEVICE);
2434 2420
2435 if (!((company == DME1737_COMPANY_SMSC) && 2421 if (!((company == DME1737_COMPANY_SMSC) &&
2436 (device == SCH311X_DEVICE))) { 2422 (device == SCH311X_DEVICE))) {
@@ -2441,10 +2427,10 @@ static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2441 data->type = sch311x; 2427 data->type = sch311x;
2442 2428
2443 /* Fill in the remaining client fields and initialize the mutex */ 2429 /* Fill in the remaining client fields and initialize the mutex */
2444 strlcpy(client->name, "sch311x", I2C_NAME_SIZE); 2430 data->name = "sch311x";
2445 mutex_init(&data->update_lock); 2431 mutex_init(&data->update_lock);
2446 2432
2447 dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr); 2433 dev_info(dev, "Found a SCH311x chip at 0x%04x\n", data->addr);
2448 2434
2449 /* Initialize the chip */ 2435 /* Initialize the chip */
2450 if ((err = dme1737_init_device(dev))) { 2436 if ((err = dme1737_init_device(dev))) {
@@ -2485,7 +2471,7 @@ static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2485 2471
2486 hwmon_device_unregister(data->hwmon_dev); 2472 hwmon_device_unregister(data->hwmon_dev);
2487 dme1737_remove_files(&pdev->dev); 2473 dme1737_remove_files(&pdev->dev);
2488 release_region(data->client.addr, DME1737_EXTENT); 2474 release_region(data->addr, DME1737_EXTENT);
2489 platform_set_drvdata(pdev, NULL); 2475 platform_set_drvdata(pdev, NULL);
2490 kfree(data); 2476 kfree(data);
2491 2477
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 6ee997b2817c..acadbc51fc0f 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -55,7 +55,7 @@ config I2C_AMD756
55 55
56config I2C_AMD756_S4882 56config I2C_AMD756_S4882
57 tristate "SMBus multiplexing on the Tyan S4882" 57 tristate "SMBus multiplexing on the Tyan S4882"
58 depends on I2C_AMD756 && EXPERIMENTAL 58 depends on I2C_AMD756 && X86 && EXPERIMENTAL
59 help 59 help
60 Enabling this option will add specific SMBus support for the Tyan 60 Enabling this option will add specific SMBus support for the Tyan
61 S4882 motherboard. On this 4-CPU board, the SMBus is multiplexed 61 S4882 motherboard. On this 4-CPU board, the SMBus is multiplexed
@@ -148,7 +148,7 @@ config I2C_NFORCE2
148 148
149config I2C_NFORCE2_S4985 149config I2C_NFORCE2_S4985
150 tristate "SMBus multiplexing on the Tyan S4985" 150 tristate "SMBus multiplexing on the Tyan S4985"
151 depends on I2C_NFORCE2 && EXPERIMENTAL 151 depends on I2C_NFORCE2 && X86 && EXPERIMENTAL
152 help 152 help
153 Enabling this option will add specific SMBus support for the Tyan 153 Enabling this option will add specific SMBus support for the Tyan
154 S4985 motherboard. On this 4-CPU board, the SMBus is multiplexed 154 S4985 motherboard. On this 4-CPU board, the SMBus is multiplexed
@@ -209,7 +209,7 @@ config I2C_VIA
209 will be called i2c-via. 209 will be called i2c-via.
210 210
211config I2C_VIAPRO 211config I2C_VIAPRO
212 tristate "VIA VT82C596/82C686/82xx and CX700" 212 tristate "VIA VT82C596/82C686/82xx and CX700/VX800/VX820"
213 depends on PCI 213 depends on PCI
214 help 214 help
215 If you say yes to this option, support will be included for the VIA 215 If you say yes to this option, support will be included for the VIA
@@ -223,6 +223,8 @@ config I2C_VIAPRO
223 VT8237R/A/S 223 VT8237R/A/S
224 VT8251 224 VT8251
225 CX700 225 CX700
226 VX800
227 VX820
226 228
227 This driver can also be built as a module. If so, the module 229 This driver can also be built as a module. If so, the module
228 will be called i2c-viapro. 230 will be called i2c-viapro.
@@ -330,6 +332,18 @@ config I2C_GPIO
330 This is a very simple bitbanging I2C driver utilizing the 332 This is a very simple bitbanging I2C driver utilizing the
331 arch-neutral GPIO API to control the SCL and SDA lines. 333 arch-neutral GPIO API to control the SCL and SDA lines.
332 334
335config I2C_HIGHLANDER
336 tristate "Highlander FPGA SMBus interface"
337 depends on SH_HIGHLANDER
338 help
339 If you say yes to this option, support will be included for
340 the SMBus interface located in the FPGA on various Highlander
341 boards, particularly the R0P7780LC0011RL and R0P7785LC0011RL
342 FPGAs. This is wholly unrelated to the SoC I2C.
343
344 This driver can also be built as a module. If so, the module
345 will be called i2c-highlander.
346
333config I2C_IBM_IIC 347config I2C_IBM_IIC
334 tristate "IBM PPC 4xx on-chip I2C interface" 348 tristate "IBM PPC 4xx on-chip I2C interface"
335 depends on 4xx 349 depends on 4xx
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 97dbfa2107fe..0c2c4b26cdf1 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
31obj-$(CONFIG_I2C_CPM) += i2c-cpm.o 31obj-$(CONFIG_I2C_CPM) += i2c-cpm.o
32obj-$(CONFIG_I2C_DAVINCI) += i2c-davinci.o 32obj-$(CONFIG_I2C_DAVINCI) += i2c-davinci.o
33obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o 33obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o
34obj-$(CONFIG_I2C_HIGHLANDER) += i2c-highlander.o
34obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o 35obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o
35obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o 36obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o
36obj-$(CONFIG_I2C_IXP2000) += i2c-ixp2000.o 37obj-$(CONFIG_I2C_IXP2000) += i2c-ixp2000.o
diff --git a/drivers/i2c/busses/i2c-highlander.c b/drivers/i2c/busses/i2c-highlander.c
new file mode 100644
index 000000000000..f4d22ae9d294
--- /dev/null
+++ b/drivers/i2c/busses/i2c-highlander.c
@@ -0,0 +1,498 @@
1/*
2 * Renesas Solutions Highlander FPGA I2C/SMBus support.
3 *
4 * Supported devices: R0P7780LC0011RL, R0P7785LC0011RL
5 *
6 * Copyright (C) 2008 Paul Mundt
7 * Copyright (C) 2008 Renesas Solutions Corp.
8 * Copyright (C) 2008 Atom Create Engineering Co., Ltd.
9 *
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License version 2. See the file "COPYING" in the main directory
12 * of this archive for more details.
13 */
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/i2c.h>
18#include <linux/platform_device.h>
19#include <linux/completion.h>
20#include <linux/io.h>
21#include <linux/delay.h>
22
23#define SMCR 0x00
24#define SMCR_START (1 << 0)
25#define SMCR_IRIC (1 << 1)
26#define SMCR_BBSY (1 << 2)
27#define SMCR_ACKE (1 << 3)
28#define SMCR_RST (1 << 4)
29#define SMCR_IEIC (1 << 6)
30
31#define SMSMADR 0x02
32
33#define SMMR 0x04
34#define SMMR_MODE0 (1 << 0)
35#define SMMR_MODE1 (1 << 1)
36#define SMMR_CAP (1 << 3)
37#define SMMR_TMMD (1 << 4)
38#define SMMR_SP (1 << 7)
39
40#define SMSADR 0x06
41#define SMTRDR 0x46
42
43struct highlander_i2c_dev {
44 struct device *dev;
45 void __iomem *base;
46 struct i2c_adapter adapter;
47 struct completion cmd_complete;
48 unsigned long last_read_time;
49 int irq;
50 u8 *buf;
51 size_t buf_len;
52};
53
54static int iic_force_poll, iic_force_normal;
55static int iic_timeout = 1000, iic_read_delay;
56
57static inline void highlander_i2c_irq_enable(struct highlander_i2c_dev *dev)
58{
59 iowrite16(ioread16(dev->base + SMCR) | SMCR_IEIC, dev->base + SMCR);
60}
61
62static inline void highlander_i2c_irq_disable(struct highlander_i2c_dev *dev)
63{
64 iowrite16(ioread16(dev->base + SMCR) & ~SMCR_IEIC, dev->base + SMCR);
65}
66
67static inline void highlander_i2c_start(struct highlander_i2c_dev *dev)
68{
69 iowrite16(ioread16(dev->base + SMCR) | SMCR_START, dev->base + SMCR);
70}
71
72static inline void highlander_i2c_done(struct highlander_i2c_dev *dev)
73{
74 iowrite16(ioread16(dev->base + SMCR) | SMCR_IRIC, dev->base + SMCR);
75}
76
77static void highlander_i2c_setup(struct highlander_i2c_dev *dev)
78{
79 u16 smmr;
80
81 smmr = ioread16(dev->base + SMMR);
82 smmr |= SMMR_TMMD;
83
84 if (iic_force_normal)
85 smmr &= ~SMMR_SP;
86 else
87 smmr |= SMMR_SP;
88
89 iowrite16(smmr, dev->base + SMMR);
90}
91
92static void smbus_write_data(u8 *src, u16 *dst, int len)
93{
94 for (; len > 1; len -= 2) {
95 *dst++ = be16_to_cpup((u16 *)src);
96 src += 2;
97 }
98
99 if (len)
100 *dst = *src << 8;
101}
102
103static void smbus_read_data(u16 *src, u8 *dst, int len)
104{
105 for (; len > 1; len -= 2) {
106 *(u16 *)dst = cpu_to_be16p(src++);
107 dst += 2;
108 }
109
110 if (len)
111 *dst = *src >> 8;
112}
113
114static void highlander_i2c_command(struct highlander_i2c_dev *dev,
115 u8 command, int len)
116{
117 unsigned int i;
118 u16 cmd = (command << 8) | command;
119
120 for (i = 0; i < len; i += 2) {
121 if (len - i == 1)
122 cmd = command << 8;
123 iowrite16(cmd, dev->base + SMSADR + i);
124 dev_dbg(dev->dev, "command data[%x] 0x%04x\n", i/2, cmd);
125 }
126}
127
128static int highlander_i2c_wait_for_bbsy(struct highlander_i2c_dev *dev)
129{
130 unsigned long timeout;
131
132 timeout = jiffies + msecs_to_jiffies(iic_timeout);
133 while (ioread16(dev->base + SMCR) & SMCR_BBSY) {
134 if (time_after(jiffies, timeout)) {
135 dev_warn(dev->dev, "timeout waiting for bus ready\n");
136 return -ETIMEDOUT;
137 }
138
139 msleep(1);
140 }
141
142 return 0;
143}
144
145static int highlander_i2c_reset(struct highlander_i2c_dev *dev)
146{
147 iowrite16(ioread16(dev->base + SMCR) | SMCR_RST, dev->base + SMCR);
148 return highlander_i2c_wait_for_bbsy(dev);
149}
150
151static int highlander_i2c_wait_for_ack(struct highlander_i2c_dev *dev)
152{
153 u16 tmp = ioread16(dev->base + SMCR);
154
155 if ((tmp & (SMCR_IRIC | SMCR_ACKE)) == SMCR_ACKE) {
156 dev_warn(dev->dev, "ack abnormality\n");
157 return highlander_i2c_reset(dev);
158 }
159
160 return 0;
161}
162
163static irqreturn_t highlander_i2c_irq(int irq, void *dev_id)
164{
165 struct highlander_i2c_dev *dev = dev_id;
166
167 highlander_i2c_done(dev);
168 complete(&dev->cmd_complete);
169
170 return IRQ_HANDLED;
171}
172
173static void highlander_i2c_poll(struct highlander_i2c_dev *dev)
174{
175 unsigned long timeout;
176 u16 smcr;
177
178 timeout = jiffies + msecs_to_jiffies(iic_timeout);
179 for (;;) {
180 smcr = ioread16(dev->base + SMCR);
181
182 /*
183 * Don't bother checking ACKE here, this and the reset
184 * are handled in highlander_i2c_wait_xfer_done() when
185 * waiting for the ACK.
186 */
187
188 if (smcr & SMCR_IRIC)
189 return;
190 if (time_after(jiffies, timeout))
191 break;
192
193 cpu_relax();
194 cond_resched();
195 }
196
197 dev_err(dev->dev, "polling timed out\n");
198}
199
200static inline int highlander_i2c_wait_xfer_done(struct highlander_i2c_dev *dev)
201{
202 if (dev->irq)
203 wait_for_completion_timeout(&dev->cmd_complete,
204 msecs_to_jiffies(iic_timeout));
205 else
206 /* busy looping, the IRQ of champions */
207 highlander_i2c_poll(dev);
208
209 return highlander_i2c_wait_for_ack(dev);
210}
211
212static int highlander_i2c_read(struct highlander_i2c_dev *dev)
213{
214 int i, cnt;
215 u16 data[16];
216
217 if (highlander_i2c_wait_for_bbsy(dev))
218 return -EAGAIN;
219
220 highlander_i2c_start(dev);
221
222 if (highlander_i2c_wait_xfer_done(dev)) {
223 dev_err(dev->dev, "Arbitration loss\n");
224 return -EAGAIN;
225 }
226
227 /*
228 * The R0P7780LC0011RL FPGA needs a significant delay between
229 * data read cycles, otherwise the transciever gets confused and
230 * garbage is returned when the read is subsequently aborted.
231 *
232 * It is not sufficient to wait for BBSY.
233 *
234 * While this generally only applies to the older SH7780-based
235 * Highlanders, the same issue can be observed on SH7785 ones,
236 * albeit less frequently. SH7780-based Highlanders may need
237 * this to be as high as 1000 ms.
238 */
239 if (iic_read_delay && time_before(jiffies, dev->last_read_time +
240 msecs_to_jiffies(iic_read_delay)))
241 msleep(jiffies_to_msecs((dev->last_read_time +
242 msecs_to_jiffies(iic_read_delay)) - jiffies));
243
244 cnt = (dev->buf_len + 1) >> 1;
245 for (i = 0; i < cnt; i++) {
246 data[i] = ioread16(dev->base + SMTRDR + (i * sizeof(u16)));
247 dev_dbg(dev->dev, "read data[%x] 0x%04x\n", i, data[i]);
248 }
249
250 smbus_read_data(data, dev->buf, dev->buf_len);
251
252 dev->last_read_time = jiffies;
253
254 return 0;
255}
256
257static int highlander_i2c_write(struct highlander_i2c_dev *dev)
258{
259 int i, cnt;
260 u16 data[16];
261
262 smbus_write_data(dev->buf, data, dev->buf_len);
263
264 cnt = (dev->buf_len + 1) >> 1;
265 for (i = 0; i < cnt; i++) {
266 iowrite16(data[i], dev->base + SMTRDR + (i * sizeof(u16)));
267 dev_dbg(dev->dev, "write data[%x] 0x%04x\n", i, data[i]);
268 }
269
270 if (highlander_i2c_wait_for_bbsy(dev))
271 return -EAGAIN;
272
273 highlander_i2c_start(dev);
274
275 return highlander_i2c_wait_xfer_done(dev);
276}
277
278static int highlander_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
279 unsigned short flags, char read_write,
280 u8 command, int size,
281 union i2c_smbus_data *data)
282{
283 struct highlander_i2c_dev *dev = i2c_get_adapdata(adap);
284 int read = read_write & I2C_SMBUS_READ;
285 u16 tmp;
286
287 init_completion(&dev->cmd_complete);
288
289 dev_dbg(dev->dev, "addr %04x, command %02x, read_write %d, size %d\n",
290 addr, command, read_write, size);
291
292 /*
293 * Set up the buffer and transfer size
294 */
295 switch (size) {
296 case I2C_SMBUS_BYTE_DATA:
297 dev->buf = &data->byte;
298 dev->buf_len = 1;
299 break;
300 case I2C_SMBUS_I2C_BLOCK_DATA:
301 dev->buf = &data->block[1];
302 dev->buf_len = data->block[0];
303 break;
304 default:
305 dev_err(dev->dev, "unsupported command %d\n", size);
306 return -EINVAL;
307 }
308
309 /*
310 * Encode the mode setting
311 */
312 tmp = ioread16(dev->base + SMMR);
313 tmp &= ~(SMMR_MODE0 | SMMR_MODE1);
314
315 switch (dev->buf_len) {
316 case 1:
317 /* default */
318 break;
319 case 8:
320 tmp |= SMMR_MODE0;
321 break;
322 case 16:
323 tmp |= SMMR_MODE1;
324 break;
325 case 32:
326 tmp |= (SMMR_MODE0 | SMMR_MODE1);
327 break;
328 default:
329 dev_err(dev->dev, "unsupported xfer size %d\n", dev->buf_len);
330 return -EINVAL;
331 }
332
333 iowrite16(tmp, dev->base + SMMR);
334
335 /* Ensure we're in a sane state */
336 highlander_i2c_done(dev);
337
338 /* Set slave address */
339 iowrite16((addr << 1) | read, dev->base + SMSMADR);
340
341 highlander_i2c_command(dev, command, dev->buf_len);
342
343 if (read)
344 return highlander_i2c_read(dev);
345 else
346 return highlander_i2c_write(dev);
347}
348
349static u32 highlander_i2c_func(struct i2c_adapter *adapter)
350{
351 return I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
352}
353
354static const struct i2c_algorithm highlander_i2c_algo = {
355 .smbus_xfer = highlander_i2c_smbus_xfer,
356 .functionality = highlander_i2c_func,
357};
358
359static int __devinit highlander_i2c_probe(struct platform_device *pdev)
360{
361 struct highlander_i2c_dev *dev;
362 struct i2c_adapter *adap;
363 struct resource *res;
364 int ret;
365
366 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
367 if (unlikely(!res)) {
368 dev_err(&pdev->dev, "no mem resource\n");
369 return -ENODEV;
370 }
371
372 dev = kzalloc(sizeof(struct highlander_i2c_dev), GFP_KERNEL);
373 if (unlikely(!dev))
374 return -ENOMEM;
375
376 dev->base = ioremap_nocache(res->start, res->end - res->start + 1);
377 if (unlikely(!dev->base)) {
378 ret = -ENXIO;
379 goto err;
380 }
381
382 dev->dev = &pdev->dev;
383 platform_set_drvdata(pdev, dev);
384
385 dev->irq = platform_get_irq(pdev, 0);
386 if (iic_force_poll)
387 dev->irq = 0;
388
389 if (dev->irq) {
390 ret = request_irq(dev->irq, highlander_i2c_irq, IRQF_DISABLED,
391 pdev->name, dev);
392 if (unlikely(ret))
393 goto err_unmap;
394
395 highlander_i2c_irq_enable(dev);
396 } else {
397 dev_notice(&pdev->dev, "no IRQ, using polling mode\n");
398 highlander_i2c_irq_disable(dev);
399 }
400
401 dev->last_read_time = jiffies; /* initial read jiffies */
402
403 highlander_i2c_setup(dev);
404
405 adap = &dev->adapter;
406 i2c_set_adapdata(adap, dev);
407 adap->owner = THIS_MODULE;
408 adap->class = I2C_CLASS_HWMON;
409 strlcpy(adap->name, "HL FPGA I2C adapter", sizeof(adap->name));
410 adap->algo = &highlander_i2c_algo;
411 adap->dev.parent = &pdev->dev;
412 adap->nr = pdev->id;
413
414 /*
415 * Reset the adapter
416 */
417 ret = highlander_i2c_reset(dev);
418 if (unlikely(ret)) {
419 dev_err(&pdev->dev, "controller didn't come up\n");
420 goto err_free_irq;
421 }
422
423 ret = i2c_add_numbered_adapter(adap);
424 if (unlikely(ret)) {
425 dev_err(&pdev->dev, "failure adding adapter\n");
426 goto err_free_irq;
427 }
428
429 return 0;
430
431err_free_irq:
432 if (dev->irq)
433 free_irq(dev->irq, dev);
434err_unmap:
435 iounmap(dev->base);
436err:
437 kfree(dev);
438
439 platform_set_drvdata(pdev, NULL);
440
441 return ret;
442}
443
444static int __devexit highlander_i2c_remove(struct platform_device *pdev)
445{
446 struct highlander_i2c_dev *dev = platform_get_drvdata(pdev);
447
448 i2c_del_adapter(&dev->adapter);
449
450 if (dev->irq)
451 free_irq(dev->irq, dev);
452
453 iounmap(dev->base);
454 kfree(dev);
455
456 platform_set_drvdata(pdev, NULL);
457
458 return 0;
459}
460
461static struct platform_driver highlander_i2c_driver = {
462 .driver = {
463 .name = "i2c-highlander",
464 .owner = THIS_MODULE,
465 },
466
467 .probe = highlander_i2c_probe,
468 .remove = __devexit_p(highlander_i2c_remove),
469};
470
471static int __init highlander_i2c_init(void)
472{
473 return platform_driver_register(&highlander_i2c_driver);
474}
475
476static void __exit highlander_i2c_exit(void)
477{
478 platform_driver_unregister(&highlander_i2c_driver);
479}
480
481module_init(highlander_i2c_init);
482module_exit(highlander_i2c_exit);
483
484MODULE_AUTHOR("Paul Mundt");
485MODULE_DESCRIPTION("Renesas Highlander FPGA I2C/SMBus adapter");
486MODULE_LICENSE("GPL v2");
487
488module_param(iic_force_poll, bool, 0);
489module_param(iic_force_normal, bool, 0);
490module_param(iic_timeout, int, 0);
491module_param(iic_read_delay, int, 0);
492
493MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
494MODULE_PARM_DESC(iic_force_normal,
495 "Force normal mode (100 kHz), default is fast mode (400 kHz)");
496MODULE_PARM_DESC(iic_timeout, "Set timeout value in msecs (default 1000 ms)");
497MODULE_PARM_DESC(iic_read_delay,
498 "Delay between data read cycles (default 0 ms)");
diff --git a/drivers/i2c/busses/i2c-parport-light.c b/drivers/i2c/busses/i2c-parport-light.c
index c6faf9bdad18..b2b8380f6602 100644
--- a/drivers/i2c/busses/i2c-parport-light.c
+++ b/drivers/i2c/busses/i2c-parport-light.c
@@ -123,11 +123,6 @@ static struct i2c_adapter parport_adapter = {
123static int __devinit i2c_parport_probe(struct platform_device *pdev) 123static int __devinit i2c_parport_probe(struct platform_device *pdev)
124{ 124{
125 int err; 125 int err;
126 struct resource *res;
127
128 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
129 if (!request_region(res->start, res->end - res->start + 1, DRVNAME))
130 return -EBUSY;
131 126
132 /* Reset hardware to a sane state (SCL and SDA high) */ 127 /* Reset hardware to a sane state (SCL and SDA high) */
133 parport_setsda(NULL, 1); 128 parport_setsda(NULL, 1);
@@ -138,29 +133,19 @@ static int __devinit i2c_parport_probe(struct platform_device *pdev)
138 133
139 parport_adapter.dev.parent = &pdev->dev; 134 parport_adapter.dev.parent = &pdev->dev;
140 err = i2c_bit_add_bus(&parport_adapter); 135 err = i2c_bit_add_bus(&parport_adapter);
141 if (err) { 136 if (err)
142 dev_err(&pdev->dev, "Unable to register with I2C\n"); 137 dev_err(&pdev->dev, "Unable to register with I2C\n");
143 goto exit_region;
144 }
145 return 0;
146
147exit_region:
148 release_region(res->start, res->end - res->start + 1);
149 return err; 138 return err;
150} 139}
151 140
152static int __devexit i2c_parport_remove(struct platform_device *pdev) 141static int __devexit i2c_parport_remove(struct platform_device *pdev)
153{ 142{
154 struct resource *res;
155
156 i2c_del_adapter(&parport_adapter); 143 i2c_del_adapter(&parport_adapter);
157 144
158 /* Un-init if needed (power off...) */ 145 /* Un-init if needed (power off...) */
159 if (adapter_parm[type].init.val) 146 if (adapter_parm[type].init.val)
160 line_set(0, &adapter_parm[type].init); 147 line_set(0, &adapter_parm[type].init);
161 148
162 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
163 release_region(res->start, res->end - res->start + 1);
164 return 0; 149 return 0;
165} 150}
166 151
@@ -175,12 +160,6 @@ static struct platform_driver i2c_parport_driver = {
175 160
176static int __init i2c_parport_device_add(u16 address) 161static int __init i2c_parport_device_add(u16 address)
177{ 162{
178 struct resource res = {
179 .start = address,
180 .end = address + 2,
181 .name = DRVNAME,
182 .flags = IORESOURCE_IO,
183 };
184 int err; 163 int err;
185 164
186 pdev = platform_device_alloc(DRVNAME, -1); 165 pdev = platform_device_alloc(DRVNAME, -1);
@@ -190,13 +169,6 @@ static int __init i2c_parport_device_add(u16 address)
190 goto exit; 169 goto exit;
191 } 170 }
192 171
193 err = platform_device_add_resources(pdev, &res, 1);
194 if (err) {
195 printk(KERN_ERR DRVNAME ": Device resource addition failed "
196 "(%d)\n", err);
197 goto exit_device_put;
198 }
199
200 err = platform_device_add(pdev); 172 err = platform_device_add(pdev);
201 if (err) { 173 if (err) {
202 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", 174 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
@@ -231,13 +203,16 @@ static int __init i2c_parport_init(void)
231 base = DEFAULT_BASE; 203 base = DEFAULT_BASE;
232 } 204 }
233 205
206 if (!request_region(base, 3, DRVNAME))
207 return -EBUSY;
208
234 if (!adapter_parm[type].getscl.val) 209 if (!adapter_parm[type].getscl.val)
235 parport_algo_data.getscl = NULL; 210 parport_algo_data.getscl = NULL;
236 211
237 /* Sets global pdev as a side effect */ 212 /* Sets global pdev as a side effect */
238 err = i2c_parport_device_add(base); 213 err = i2c_parport_device_add(base);
239 if (err) 214 if (err)
240 goto exit; 215 goto exit_release;
241 216
242 err = platform_driver_register(&i2c_parport_driver); 217 err = platform_driver_register(&i2c_parport_driver);
243 if (err) 218 if (err)
@@ -247,7 +222,8 @@ static int __init i2c_parport_init(void)
247 222
248exit_device: 223exit_device:
249 platform_device_unregister(pdev); 224 platform_device_unregister(pdev);
250exit: 225exit_release:
226 release_region(base, 3);
251 return err; 227 return err;
252} 228}
253 229
@@ -255,6 +231,7 @@ static void __exit i2c_parport_exit(void)
255{ 231{
256 platform_driver_unregister(&i2c_parport_driver); 232 platform_driver_unregister(&i2c_parport_driver);
257 platform_device_unregister(pdev); 233 platform_device_unregister(pdev);
234 release_region(base, 3);
258} 235}
259 236
260MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); 237MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
diff --git a/drivers/i2c/busses/i2c-pca-isa.c b/drivers/i2c/busses/i2c-pca-isa.c
index 28d8463b74ad..9eb76268ec78 100644
--- a/drivers/i2c/busses/i2c-pca-isa.c
+++ b/drivers/i2c/busses/i2c-pca-isa.c
@@ -36,8 +36,8 @@
36#define DRIVER "i2c-pca-isa" 36#define DRIVER "i2c-pca-isa"
37#define IO_SIZE 4 37#define IO_SIZE 4
38 38
39static unsigned long base = 0x330; 39static unsigned long base;
40static int irq = 10; 40static int irq = -1;
41 41
42/* Data sheet recommends 59kHz for 100kHz operation due to variation 42/* Data sheet recommends 59kHz for 100kHz operation due to variation
43 * in the actual clock rate */ 43 * in the actual clock rate */
@@ -107,6 +107,19 @@ static struct i2c_adapter pca_isa_ops = {
107 .timeout = 100, 107 .timeout = 100,
108}; 108};
109 109
110static int __devinit pca_isa_match(struct device *dev, unsigned int id)
111{
112 int match = base != 0;
113
114 if (match) {
115 if (irq <= -1)
116 dev_warn(dev, "Using polling mode (specify irq)\n");
117 } else
118 dev_err(dev, "Please specify I/O base\n");
119
120 return match;
121}
122
110static int __devinit pca_isa_probe(struct device *dev, unsigned int id) 123static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
111{ 124{
112 init_waitqueue_head(&pca_wait); 125 init_waitqueue_head(&pca_wait);
@@ -153,7 +166,7 @@ static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
153{ 166{
154 i2c_del_adapter(&pca_isa_ops); 167 i2c_del_adapter(&pca_isa_ops);
155 168
156 if (irq > 0) { 169 if (irq > -1) {
157 disable_irq(irq); 170 disable_irq(irq);
158 free_irq(irq, &pca_isa_ops); 171 free_irq(irq, &pca_isa_ops);
159 } 172 }
@@ -163,6 +176,7 @@ static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
163} 176}
164 177
165static struct isa_driver pca_isa_driver = { 178static struct isa_driver pca_isa_driver = {
179 .match = pca_isa_match,
166 .probe = pca_isa_probe, 180 .probe = pca_isa_probe,
167 .remove = __devexit_p(pca_isa_remove), 181 .remove = __devexit_p(pca_isa_remove),
168 .driver = { 182 .driver = {
diff --git a/drivers/i2c/busses/i2c-viapro.c b/drivers/i2c/busses/i2c-viapro.c
index 862eb352a2d9..73dc52e114eb 100644
--- a/drivers/i2c/busses/i2c-viapro.c
+++ b/drivers/i2c/busses/i2c-viapro.c
@@ -36,6 +36,7 @@
36 VT8237S 0x3372 yes 36 VT8237S 0x3372 yes
37 VT8251 0x3287 yes 37 VT8251 0x3287 yes
38 CX700 0x8324 yes 38 CX700 0x8324 yes
39 VX800/VX820 0x8353 yes
39 40
40 Note: we assume there can only be one device, with one SMBus interface. 41 Note: we assume there can only be one device, with one SMBus interface.
41*/ 42*/
@@ -82,6 +83,7 @@ static unsigned short SMBHSTCFG = 0xD2;
82#define VT596_BYTE 0x04 83#define VT596_BYTE 0x04
83#define VT596_BYTE_DATA 0x08 84#define VT596_BYTE_DATA 0x08
84#define VT596_WORD_DATA 0x0C 85#define VT596_WORD_DATA 0x0C
86#define VT596_PROC_CALL 0x10
85#define VT596_BLOCK_DATA 0x14 87#define VT596_BLOCK_DATA 0x14
86#define VT596_I2C_BLOCK_DATA 0x34 88#define VT596_I2C_BLOCK_DATA 0x34
87 89
@@ -232,6 +234,12 @@ static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
232 } 234 }
233 size = VT596_WORD_DATA; 235 size = VT596_WORD_DATA;
234 break; 236 break;
237 case I2C_SMBUS_PROC_CALL:
238 outb_p(command, SMBHSTCMD);
239 outb_p(data->word & 0xff, SMBHSTDAT0);
240 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
241 size = VT596_PROC_CALL;
242 break;
235 case I2C_SMBUS_I2C_BLOCK_DATA: 243 case I2C_SMBUS_I2C_BLOCK_DATA:
236 if (!(vt596_features & FEATURE_I2CBLOCK)) 244 if (!(vt596_features & FEATURE_I2CBLOCK))
237 goto exit_unsupported; 245 goto exit_unsupported;
@@ -262,6 +270,9 @@ static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
262 if (status) 270 if (status)
263 return status; 271 return status;
264 272
273 if (size == VT596_PROC_CALL)
274 read_write = I2C_SMBUS_READ;
275
265 if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK)) 276 if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
266 return 0; 277 return 0;
267 278
@@ -271,6 +282,7 @@ static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
271 data->byte = inb_p(SMBHSTDAT0); 282 data->byte = inb_p(SMBHSTDAT0);
272 break; 283 break;
273 case VT596_WORD_DATA: 284 case VT596_WORD_DATA:
285 case VT596_PROC_CALL:
274 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); 286 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
275 break; 287 break;
276 case VT596_I2C_BLOCK_DATA: 288 case VT596_I2C_BLOCK_DATA:
@@ -295,7 +307,7 @@ static u32 vt596_func(struct i2c_adapter *adapter)
295{ 307{
296 u32 func = I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 308 u32 func = I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
297 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 309 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
298 I2C_FUNC_SMBUS_BLOCK_DATA; 310 I2C_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
299 311
300 if (vt596_features & FEATURE_I2CBLOCK) 312 if (vt596_features & FEATURE_I2CBLOCK)
301 func |= I2C_FUNC_SMBUS_I2C_BLOCK; 313 func |= I2C_FUNC_SMBUS_I2C_BLOCK;
@@ -396,6 +408,7 @@ found:
396 408
397 switch (pdev->device) { 409 switch (pdev->device) {
398 case PCI_DEVICE_ID_VIA_CX700: 410 case PCI_DEVICE_ID_VIA_CX700:
411 case PCI_DEVICE_ID_VIA_VX800:
399 case PCI_DEVICE_ID_VIA_8251: 412 case PCI_DEVICE_ID_VIA_8251:
400 case PCI_DEVICE_ID_VIA_8237: 413 case PCI_DEVICE_ID_VIA_8237:
401 case PCI_DEVICE_ID_VIA_8237A: 414 case PCI_DEVICE_ID_VIA_8237A:
@@ -459,6 +472,8 @@ static struct pci_device_id vt596_ids[] = {
459 .driver_data = SMBBA3 }, 472 .driver_data = SMBBA3 },
460 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_CX700), 473 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_CX700),
461 .driver_data = SMBBA3 }, 474 .driver_data = SMBBA3 },
475 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX800),
476 .driver_data = SMBBA3 },
462 { 0, } 477 { 0, }
463}; 478};
464 479
diff --git a/drivers/i2c/chips/isp1301_omap.c b/drivers/i2c/chips/isp1301_omap.c
index 4655b794ebe3..28902ebd5539 100644
--- a/drivers/i2c/chips/isp1301_omap.c
+++ b/drivers/i2c/chips/isp1301_omap.c
@@ -49,10 +49,9 @@ MODULE_LICENSE("GPL");
49 49
50struct isp1301 { 50struct isp1301 {
51 struct otg_transceiver otg; 51 struct otg_transceiver otg;
52 struct i2c_client client; 52 struct i2c_client *client;
53 void (*i2c_release)(struct device *dev); 53 void (*i2c_release)(struct device *dev);
54 54
55 int irq;
56 int irq_type; 55 int irq_type;
57 56
58 u32 last_otg_ctrl; 57 u32 last_otg_ctrl;
@@ -138,14 +137,6 @@ static inline void notresponding(struct isp1301 *isp)
138 137
139/*-------------------------------------------------------------------------*/ 138/*-------------------------------------------------------------------------*/
140 139
141/* only two addresses possible */
142#define ISP_BASE 0x2c
143static unsigned short normal_i2c[] = {
144 ISP_BASE, ISP_BASE + 1,
145 I2C_CLIENT_END };
146
147I2C_CLIENT_INSMOD;
148
149static struct i2c_driver isp1301_driver; 140static struct i2c_driver isp1301_driver;
150 141
151/* smbus apis are used for portability */ 142/* smbus apis are used for portability */
@@ -153,25 +144,25 @@ static struct i2c_driver isp1301_driver;
153static inline u8 144static inline u8
154isp1301_get_u8(struct isp1301 *isp, u8 reg) 145isp1301_get_u8(struct isp1301 *isp, u8 reg)
155{ 146{
156 return i2c_smbus_read_byte_data(&isp->client, reg + 0); 147 return i2c_smbus_read_byte_data(isp->client, reg + 0);
157} 148}
158 149
159static inline int 150static inline int
160isp1301_get_u16(struct isp1301 *isp, u8 reg) 151isp1301_get_u16(struct isp1301 *isp, u8 reg)
161{ 152{
162 return i2c_smbus_read_word_data(&isp->client, reg); 153 return i2c_smbus_read_word_data(isp->client, reg);
163} 154}
164 155
165static inline int 156static inline int
166isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits) 157isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
167{ 158{
168 return i2c_smbus_write_byte_data(&isp->client, reg + 0, bits); 159 return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
169} 160}
170 161
171static inline int 162static inline int
172isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits) 163isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
173{ 164{
174 return i2c_smbus_write_byte_data(&isp->client, reg + 1, bits); 165 return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
175} 166}
176 167
177/*-------------------------------------------------------------------------*/ 168/*-------------------------------------------------------------------------*/
@@ -349,10 +340,10 @@ isp1301_defer_work(struct isp1301 *isp, int work)
349 int status; 340 int status;
350 341
351 if (isp && !test_and_set_bit(work, &isp->todo)) { 342 if (isp && !test_and_set_bit(work, &isp->todo)) {
352 (void) get_device(&isp->client.dev); 343 (void) get_device(&isp->client->dev);
353 status = schedule_work(&isp->work); 344 status = schedule_work(&isp->work);
354 if (!status && !isp->working) 345 if (!status && !isp->working)
355 dev_vdbg(&isp->client.dev, 346 dev_vdbg(&isp->client->dev,
356 "work item %d may be lost\n", work); 347 "work item %d may be lost\n", work);
357 } 348 }
358} 349}
@@ -1135,7 +1126,7 @@ isp1301_work(struct work_struct *work)
1135 /* transfer state from otg engine to isp1301 */ 1126 /* transfer state from otg engine to isp1301 */
1136 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) { 1127 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1137 otg_update_isp(isp); 1128 otg_update_isp(isp);
1138 put_device(&isp->client.dev); 1129 put_device(&isp->client->dev);
1139 } 1130 }
1140#endif 1131#endif
1141 /* transfer state from isp1301 to otg engine */ 1132 /* transfer state from isp1301 to otg engine */
@@ -1143,7 +1134,7 @@ isp1301_work(struct work_struct *work)
1143 u8 stat = isp1301_clear_latch(isp); 1134 u8 stat = isp1301_clear_latch(isp);
1144 1135
1145 isp_update_otg(isp, stat); 1136 isp_update_otg(isp, stat);
1146 put_device(&isp->client.dev); 1137 put_device(&isp->client->dev);
1147 } 1138 }
1148 1139
1149 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) { 1140 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
@@ -1178,7 +1169,7 @@ isp1301_work(struct work_struct *work)
1178 } 1169 }
1179 host_resume(isp); 1170 host_resume(isp);
1180 // mdelay(10); 1171 // mdelay(10);
1181 put_device(&isp->client.dev); 1172 put_device(&isp->client->dev);
1182 } 1173 }
1183 1174
1184 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) { 1175 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
@@ -1187,15 +1178,15 @@ isp1301_work(struct work_struct *work)
1187 if (!stop) 1178 if (!stop)
1188 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES); 1179 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1189#endif 1180#endif
1190 put_device(&isp->client.dev); 1181 put_device(&isp->client->dev);
1191 } 1182 }
1192 1183
1193 if (isp->todo) 1184 if (isp->todo)
1194 dev_vdbg(&isp->client.dev, 1185 dev_vdbg(&isp->client->dev,
1195 "work done, todo = 0x%lx\n", 1186 "work done, todo = 0x%lx\n",
1196 isp->todo); 1187 isp->todo);
1197 if (stop) { 1188 if (stop) {
1198 dev_dbg(&isp->client.dev, "stop\n"); 1189 dev_dbg(&isp->client->dev, "stop\n");
1199 break; 1190 break;
1200 } 1191 }
1201 } while (isp->todo); 1192 } while (isp->todo);
@@ -1219,7 +1210,7 @@ static void isp1301_release(struct device *dev)
1219{ 1210{
1220 struct isp1301 *isp; 1211 struct isp1301 *isp;
1221 1212
1222 isp = container_of(dev, struct isp1301, client.dev); 1213 isp = dev_get_drvdata(dev);
1223 1214
1224 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */ 1215 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1225 if (isp->i2c_release) 1216 if (isp->i2c_release)
@@ -1229,15 +1220,15 @@ static void isp1301_release(struct device *dev)
1229 1220
1230static struct isp1301 *the_transceiver; 1221static struct isp1301 *the_transceiver;
1231 1222
1232static int isp1301_detach_client(struct i2c_client *i2c) 1223static int __exit isp1301_remove(struct i2c_client *i2c)
1233{ 1224{
1234 struct isp1301 *isp; 1225 struct isp1301 *isp;
1235 1226
1236 isp = container_of(i2c, struct isp1301, client); 1227 isp = i2c_get_clientdata(i2c);
1237 1228
1238 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0); 1229 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1239 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0); 1230 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1240 free_irq(isp->irq, isp); 1231 free_irq(i2c->irq, isp);
1241#ifdef CONFIG_USB_OTG 1232#ifdef CONFIG_USB_OTG
1242 otg_unbind(isp); 1233 otg_unbind(isp);
1243#endif 1234#endif
@@ -1252,7 +1243,7 @@ static int isp1301_detach_client(struct i2c_client *i2c)
1252 put_device(&i2c->dev); 1243 put_device(&i2c->dev);
1253 the_transceiver = 0; 1244 the_transceiver = 0;
1254 1245
1255 return i2c_detach_client(i2c); 1246 return 0;
1256} 1247}
1257 1248
1258/*-------------------------------------------------------------------------*/ 1249/*-------------------------------------------------------------------------*/
@@ -1285,7 +1276,7 @@ static int isp1301_otg_enable(struct isp1301 *isp)
1285 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING, 1276 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1286 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND); 1277 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1287 1278
1288 dev_info(&isp->client.dev, "ready for dual-role USB ...\n"); 1279 dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
1289 1280
1290 return 0; 1281 return 0;
1291} 1282}
@@ -1310,7 +1301,7 @@ isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1310 1301
1311#ifdef CONFIG_USB_OTG 1302#ifdef CONFIG_USB_OTG
1312 isp->otg.host = host; 1303 isp->otg.host = host;
1313 dev_dbg(&isp->client.dev, "registered host\n"); 1304 dev_dbg(&isp->client->dev, "registered host\n");
1314 host_suspend(isp); 1305 host_suspend(isp);
1315 if (isp->otg.gadget) 1306 if (isp->otg.gadget)
1316 return isp1301_otg_enable(isp); 1307 return isp1301_otg_enable(isp);
@@ -1325,7 +1316,7 @@ isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1325 if (machine_is_omap_h2()) 1316 if (machine_is_omap_h2())
1326 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0); 1317 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1327 1318
1328 dev_info(&isp->client.dev, "A-Host sessions ok\n"); 1319 dev_info(&isp->client->dev, "A-Host sessions ok\n");
1329 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING, 1320 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1330 INTR_ID_GND); 1321 INTR_ID_GND);
1331 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING, 1322 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
@@ -1343,7 +1334,7 @@ isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1343 return 0; 1334 return 0;
1344 1335
1345#else 1336#else
1346 dev_dbg(&isp->client.dev, "host sessions not allowed\n"); 1337 dev_dbg(&isp->client->dev, "host sessions not allowed\n");
1347 return -EINVAL; 1338 return -EINVAL;
1348#endif 1339#endif
1349 1340
@@ -1370,7 +1361,7 @@ isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1370 1361
1371#ifdef CONFIG_USB_OTG 1362#ifdef CONFIG_USB_OTG
1372 isp->otg.gadget = gadget; 1363 isp->otg.gadget = gadget;
1373 dev_dbg(&isp->client.dev, "registered gadget\n"); 1364 dev_dbg(&isp->client->dev, "registered gadget\n");
1374 /* gadget driver may be suspended until vbus_connect () */ 1365 /* gadget driver may be suspended until vbus_connect () */
1375 if (isp->otg.host) 1366 if (isp->otg.host)
1376 return isp1301_otg_enable(isp); 1367 return isp1301_otg_enable(isp);
@@ -1395,7 +1386,7 @@ isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1395 INTR_SESS_VLD); 1386 INTR_SESS_VLD);
1396 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING, 1387 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1397 INTR_VBUS_VLD); 1388 INTR_VBUS_VLD);
1398 dev_info(&isp->client.dev, "B-Peripheral sessions ok\n"); 1389 dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
1399 dump_regs(isp, __func__); 1390 dump_regs(isp, __func__);
1400 1391
1401 /* If this has a Mini-AB connector, this mode is highly 1392 /* If this has a Mini-AB connector, this mode is highly
@@ -1408,7 +1399,7 @@ isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1408 return 0; 1399 return 0;
1409 1400
1410#else 1401#else
1411 dev_dbg(&isp->client.dev, "peripheral sessions not allowed\n"); 1402 dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
1412 return -EINVAL; 1403 return -EINVAL;
1413#endif 1404#endif
1414} 1405}
@@ -1508,12 +1499,10 @@ isp1301_start_hnp(struct otg_transceiver *dev)
1508 1499
1509/*-------------------------------------------------------------------------*/ 1500/*-------------------------------------------------------------------------*/
1510 1501
1511/* no error returns, they'd just make bus scanning stop */ 1502static int __init isp1301_probe(struct i2c_client *i2c)
1512static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
1513{ 1503{
1514 int status; 1504 int status;
1515 struct isp1301 *isp; 1505 struct isp1301 *isp;
1516 struct i2c_client *i2c;
1517 1506
1518 if (the_transceiver) 1507 if (the_transceiver)
1519 return 0; 1508 return 0;
@@ -1527,37 +1516,19 @@ static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
1527 isp->timer.function = isp1301_timer; 1516 isp->timer.function = isp1301_timer;
1528 isp->timer.data = (unsigned long) isp; 1517 isp->timer.data = (unsigned long) isp;
1529 1518
1530 isp->irq = -1; 1519 i2c_set_clientdata(i2c, isp);
1531 isp->client.addr = address; 1520 isp->client = i2c;
1532 i2c_set_clientdata(&isp->client, isp);
1533 isp->client.adapter = bus;
1534 isp->client.driver = &isp1301_driver;
1535 strlcpy(isp->client.name, DRIVER_NAME, I2C_NAME_SIZE);
1536 i2c = &isp->client;
1537
1538 /* if this is a true probe, verify the chip ... */
1539 if (kind < 0) {
1540 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1541 if (status != I2C_VENDOR_ID_PHILIPS) {
1542 dev_dbg(&bus->dev, "addr %d not philips id: %d\n",
1543 address, status);
1544 goto fail1;
1545 }
1546 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1547 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1548 dev_dbg(&bus->dev, "%d not isp1301, %d\n",
1549 address, status);
1550 goto fail1;
1551 }
1552 }
1553 1521
1554 status = i2c_attach_client(i2c); 1522 /* verify the chip (shouldn't be necesary) */
1555 if (status < 0) { 1523 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1556 dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n", 1524 if (status != I2C_VENDOR_ID_PHILIPS) {
1557 DRIVER_NAME, address, status); 1525 dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1558fail1: 1526 goto fail;
1559 kfree(isp); 1527 }
1560 return 0; 1528 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1529 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1530 dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1531 goto fail;
1561 } 1532 }
1562 isp->i2c_release = i2c->dev.release; 1533 isp->i2c_release = i2c->dev.release;
1563 i2c->dev.release = isp1301_release; 1534 i2c->dev.release = isp1301_release;
@@ -1586,7 +1557,7 @@ fail1:
1586 status = otg_bind(isp); 1557 status = otg_bind(isp);
1587 if (status < 0) { 1558 if (status < 0) {
1588 dev_dbg(&i2c->dev, "can't bind OTG\n"); 1559 dev_dbg(&i2c->dev, "can't bind OTG\n");
1589 goto fail2; 1560 goto fail;
1590 } 1561 }
1591#endif 1562#endif
1592 1563
@@ -1599,26 +1570,21 @@ fail1:
1599 1570
1600 /* IRQ wired at M14 */ 1571 /* IRQ wired at M14 */
1601 omap_cfg_reg(M14_1510_GPIO2); 1572 omap_cfg_reg(M14_1510_GPIO2);
1602 isp->irq = OMAP_GPIO_IRQ(2);
1603 if (gpio_request(2, "isp1301") == 0) 1573 if (gpio_request(2, "isp1301") == 0)
1604 gpio_direction_input(2); 1574 gpio_direction_input(2);
1605 isp->irq_type = IRQF_TRIGGER_FALLING; 1575 isp->irq_type = IRQF_TRIGGER_FALLING;
1606 } 1576 }
1607 1577
1608 isp->irq_type |= IRQF_SAMPLE_RANDOM; 1578 isp->irq_type |= IRQF_SAMPLE_RANDOM;
1609 status = request_irq(isp->irq, isp1301_irq, 1579 status = request_irq(i2c->irq, isp1301_irq,
1610 isp->irq_type, DRIVER_NAME, isp); 1580 isp->irq_type, DRIVER_NAME, isp);
1611 if (status < 0) { 1581 if (status < 0) {
1612 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n", 1582 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1613 isp->irq, status); 1583 i2c->irq, status);
1614#ifdef CONFIG_USB_OTG 1584 goto fail;
1615fail2:
1616#endif
1617 i2c_detach_client(i2c);
1618 goto fail1;
1619 } 1585 }
1620 1586
1621 isp->otg.dev = &isp->client.dev; 1587 isp->otg.dev = &i2c->dev;
1622 isp->otg.label = DRIVER_NAME; 1588 isp->otg.label = DRIVER_NAME;
1623 1589
1624 isp->otg.set_host = isp1301_set_host, 1590 isp->otg.set_host = isp1301_set_host,
@@ -1649,22 +1615,25 @@ fail2:
1649 status); 1615 status);
1650 1616
1651 return 0; 1617 return 0;
1652}
1653 1618
1654static int isp1301_scan_bus(struct i2c_adapter *bus) 1619fail:
1655{ 1620 kfree(isp);
1656 if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA 1621 return -ENODEV;
1657 | I2C_FUNC_SMBUS_READ_WORD_DATA))
1658 return -EINVAL;
1659 return i2c_probe(bus, &addr_data, isp1301_probe);
1660} 1622}
1661 1623
1624static const struct i2c_device_id isp1301_id[] = {
1625 { "isp1301_omap", 0 },
1626 { }
1627};
1628MODULE_DEVICE_TABLE(i2c, isp1301_id);
1629
1662static struct i2c_driver isp1301_driver = { 1630static struct i2c_driver isp1301_driver = {
1663 .driver = { 1631 .driver = {
1664 .name = "isp1301_omap", 1632 .name = "isp1301_omap",
1665 }, 1633 },
1666 .attach_adapter = isp1301_scan_bus, 1634 .probe = isp1301_probe,
1667 .detach_client = isp1301_detach_client, 1635 .remove = __exit_p(isp1301_remove),
1636 .id_table = isp1301_id,
1668}; 1637};
1669 1638
1670/*-------------------------------------------------------------------------*/ 1639/*-------------------------------------------------------------------------*/
diff --git a/drivers/i2c/chips/tps65010.c b/drivers/i2c/chips/tps65010.c
index cf02e8fceb42..acf8b9d5f575 100644
--- a/drivers/i2c/chips/tps65010.c
+++ b/drivers/i2c/chips/tps65010.c
@@ -456,14 +456,17 @@ static irqreturn_t tps65010_irq(int irq, void *_tps)
456 456
457/* offsets 0..3 == GPIO1..GPIO4 457/* offsets 0..3 == GPIO1..GPIO4
458 * offsets 4..5 == LED1/nPG, LED2 (we set one of the non-BLINK modes) 458 * offsets 4..5 == LED1/nPG, LED2 (we set one of the non-BLINK modes)
459 * offset 6 == vibrator motor driver
459 */ 460 */
460static void 461static void
461tps65010_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 462tps65010_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
462{ 463{
463 if (offset < 4) 464 if (offset < 4)
464 tps65010_set_gpio_out_value(offset + 1, value); 465 tps65010_set_gpio_out_value(offset + 1, value);
465 else 466 else if (offset < 6)
466 tps65010_set_led(offset - 3, value ? ON : OFF); 467 tps65010_set_led(offset - 3, value ? ON : OFF);
468 else
469 tps65010_set_vib(value);
467} 470}
468 471
469static int 472static int
@@ -477,8 +480,10 @@ tps65010_output(struct gpio_chip *chip, unsigned offset, int value)
477 if (!(tps->outmask & (1 << offset))) 480 if (!(tps->outmask & (1 << offset)))
478 return -EINVAL; 481 return -EINVAL;
479 tps65010_set_gpio_out_value(offset + 1, value); 482 tps65010_set_gpio_out_value(offset + 1, value);
480 } else 483 } else if (offset < 6)
481 tps65010_set_led(offset - 3, value ? ON : OFF); 484 tps65010_set_led(offset - 3, value ? ON : OFF);
485 else
486 tps65010_set_vib(value);
482 487
483 return 0; 488 return 0;
484} 489}
@@ -646,7 +651,7 @@ static int tps65010_probe(struct i2c_client *client,
646 tps->chip.get = tps65010_gpio_get; 651 tps->chip.get = tps65010_gpio_get;
647 652
648 tps->chip.base = board->base; 653 tps->chip.base = board->base;
649 tps->chip.ngpio = 6; 654 tps->chip.ngpio = 7;
650 tps->chip.can_sleep = 1; 655 tps->chip.can_sleep = 1;
651 656
652 status = gpiochip_add(&tps->chip); 657 status = gpiochip_add(&tps->chip);
@@ -675,6 +680,7 @@ static const struct i2c_device_id tps65010_id[] = {
675 { "tps65011", TPS65011 }, 680 { "tps65011", TPS65011 },
676 { "tps65012", TPS65012 }, 681 { "tps65012", TPS65012 },
677 { "tps65013", TPS65013 }, 682 { "tps65013", TPS65013 },
683 { "tps65014", TPS65011 }, /* tps65011 charging at 6.5V max */
678 { } 684 { }
679}; 685};
680MODULE_DEVICE_TABLE(i2c, tps65010_id); 686MODULE_DEVICE_TABLE(i2c, tps65010_id);
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index b346a687ab59..42e852d79ffa 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -437,6 +437,10 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
437{ 437{
438 int res = 0, dummy; 438 int res = 0, dummy;
439 439
440 /* Can't register until after driver model init */
441 if (unlikely(WARN_ON(!i2c_bus_type.p)))
442 return -EAGAIN;
443
440 mutex_init(&adap->bus_lock); 444 mutex_init(&adap->bus_lock);
441 mutex_init(&adap->clist_lock); 445 mutex_init(&adap->clist_lock);
442 INIT_LIST_HEAD(&adap->clients); 446 INIT_LIST_HEAD(&adap->clients);
@@ -696,6 +700,10 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
696{ 700{
697 int res; 701 int res;
698 702
703 /* Can't register until after driver model init */
704 if (unlikely(WARN_ON(!i2c_bus_type.p)))
705 return -EAGAIN;
706
699 /* new style driver methods can't mix with legacy ones */ 707 /* new style driver methods can't mix with legacy ones */
700 if (is_newstyle_driver(driver)) { 708 if (is_newstyle_driver(driver)) {
701 if (driver->attach_adapter || driver->detach_adapter 709 if (driver->attach_adapter || driver->detach_adapter
@@ -978,7 +986,10 @@ static void __exit i2c_exit(void)
978 bus_unregister(&i2c_bus_type); 986 bus_unregister(&i2c_bus_type);
979} 987}
980 988
981subsys_initcall(i2c_init); 989/* We must initialize early, because some subsystems register i2c drivers
990 * in subsys_initcall() code, but are linked (and initialized) before i2c.
991 */
992postcore_initcall(i2c_init);
982module_exit(i2c_exit); 993module_exit(i2c_exit);
983 994
984/* ---------------------------------------------------- 995/* ----------------------------------------------------
@@ -1677,6 +1688,28 @@ s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1677EXPORT_SYMBOL(i2c_smbus_write_word_data); 1688EXPORT_SYMBOL(i2c_smbus_write_word_data);
1678 1689
1679/** 1690/**
1691 * i2c_smbus_process_call - SMBus "process call" protocol
1692 * @client: Handle to slave device
1693 * @command: Byte interpreted by slave
1694 * @value: 16-bit "word" being written
1695 *
1696 * This executes the SMBus "process call" protocol, returning negative errno
1697 * else a 16-bit unsigned "word" received from the device.
1698 */
1699s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1700{
1701 union i2c_smbus_data data;
1702 int status;
1703 data.word = value;
1704
1705 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1706 I2C_SMBUS_WRITE, command,
1707 I2C_SMBUS_PROC_CALL, &data);
1708 return (status < 0) ? status : data.word;
1709}
1710EXPORT_SYMBOL(i2c_smbus_process_call);
1711
1712/**
1680 * i2c_smbus_read_block_data - SMBus "block read" protocol 1713 * i2c_smbus_read_block_data - SMBus "block read" protocol
1681 * @client: Handle to slave device 1714 * @client: Handle to slave device
1682 * @command: Byte interpreted by slave 1715 * @command: Byte interpreted by slave
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index b23af0c2a869..963ad0b6a4e9 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -913,6 +913,92 @@ static inline int cmos_poweroff(struct device *dev)
913 * predate even PNPBIOS should set up platform_bus devices. 913 * predate even PNPBIOS should set up platform_bus devices.
914 */ 914 */
915 915
916#ifdef CONFIG_ACPI
917
918#include <linux/acpi.h>
919
920#ifdef CONFIG_PM
921static u32 rtc_handler(void *context)
922{
923 acpi_clear_event(ACPI_EVENT_RTC);
924 acpi_disable_event(ACPI_EVENT_RTC, 0);
925 return ACPI_INTERRUPT_HANDLED;
926}
927
928static inline void rtc_wake_setup(void)
929{
930 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
931 /*
932 * After the RTC handler is installed, the Fixed_RTC event should
933 * be disabled. Only when the RTC alarm is set will it be enabled.
934 */
935 acpi_clear_event(ACPI_EVENT_RTC);
936 acpi_disable_event(ACPI_EVENT_RTC, 0);
937}
938
939static void rtc_wake_on(struct device *dev)
940{
941 acpi_clear_event(ACPI_EVENT_RTC);
942 acpi_enable_event(ACPI_EVENT_RTC, 0);
943}
944
945static void rtc_wake_off(struct device *dev)
946{
947 acpi_disable_event(ACPI_EVENT_RTC, 0);
948}
949#else
950#define rtc_wake_setup() do{}while(0)
951#define rtc_wake_on NULL
952#define rtc_wake_off NULL
953#endif
954
955/* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
956 * its device node and pass extra config data. This helps its driver use
957 * capabilities that the now-obsolete mc146818 didn't have, and informs it
958 * that this board's RTC is wakeup-capable (per ACPI spec).
959 */
960static struct cmos_rtc_board_info acpi_rtc_info;
961
962static void __devinit
963cmos_wake_setup(struct device *dev)
964{
965 if (acpi_disabled)
966 return;
967
968 rtc_wake_setup();
969 acpi_rtc_info.wake_on = rtc_wake_on;
970 acpi_rtc_info.wake_off = rtc_wake_off;
971
972 /* workaround bug in some ACPI tables */
973 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
974 dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
975 acpi_gbl_FADT.month_alarm);
976 acpi_gbl_FADT.month_alarm = 0;
977 }
978
979 acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
980 acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
981 acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
982
983 /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
984 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
985 dev_info(dev, "RTC can wake from S4\n");
986
987 dev->platform_data = &acpi_rtc_info;
988
989 /* RTC always wakes from S1/S2/S3, and often S4/STD */
990 device_init_wakeup(dev, 1);
991}
992
993#else
994
995static void __devinit
996cmos_wake_setup(struct device *dev)
997{
998}
999
1000#endif
1001
916#ifdef CONFIG_PNP 1002#ifdef CONFIG_PNP
917 1003
918#include <linux/pnp.h> 1004#include <linux/pnp.h>
@@ -920,6 +1006,8 @@ static inline int cmos_poweroff(struct device *dev)
920static int __devinit 1006static int __devinit
921cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) 1007cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
922{ 1008{
1009 cmos_wake_setup(&pnp->dev);
1010
923 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0)) 1011 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
924 /* Some machines contain a PNP entry for the RTC, but 1012 /* Some machines contain a PNP entry for the RTC, but
925 * don't define the IRQ. It should always be safe to 1013 * don't define the IRQ. It should always be safe to
@@ -997,6 +1085,7 @@ static struct pnp_driver cmos_pnp_driver = {
997 1085
998static int __init cmos_platform_probe(struct platform_device *pdev) 1086static int __init cmos_platform_probe(struct platform_device *pdev)
999{ 1087{
1088 cmos_wake_setup(&pdev->dev);
1000 return cmos_do_probe(&pdev->dev, 1089 return cmos_do_probe(&pdev->dev,
1001 platform_get_resource(pdev, IORESOURCE_IO, 0), 1090 platform_get_resource(pdev, IORESOURCE_IO, 0),
1002 platform_get_irq(pdev, 0)); 1091 platform_get_irq(pdev, 0));
@@ -1031,29 +1120,32 @@ static struct platform_driver cmos_platform_driver = {
1031 1120
1032static int __init cmos_init(void) 1121static int __init cmos_init(void)
1033{ 1122{
1123 int retval = 0;
1124
1034#ifdef CONFIG_PNP 1125#ifdef CONFIG_PNP
1035 if (pnp_platform_devices) 1126 pnp_register_driver(&cmos_pnp_driver);
1036 return pnp_register_driver(&cmos_pnp_driver); 1127#endif
1037 else 1128
1038 return platform_driver_probe(&cmos_platform_driver, 1129 if (!cmos_rtc.dev)
1039 cmos_platform_probe); 1130 retval = platform_driver_probe(&cmos_platform_driver,
1040#else 1131 cmos_platform_probe);
1041 return platform_driver_probe(&cmos_platform_driver, 1132
1042 cmos_platform_probe); 1133 if (retval == 0)
1043#endif /* CONFIG_PNP */ 1134 return 0;
1135
1136#ifdef CONFIG_PNP
1137 pnp_unregister_driver(&cmos_pnp_driver);
1138#endif
1139 return retval;
1044} 1140}
1045module_init(cmos_init); 1141module_init(cmos_init);
1046 1142
1047static void __exit cmos_exit(void) 1143static void __exit cmos_exit(void)
1048{ 1144{
1049#ifdef CONFIG_PNP 1145#ifdef CONFIG_PNP
1050 if (pnp_platform_devices) 1146 pnp_unregister_driver(&cmos_pnp_driver);
1051 pnp_unregister_driver(&cmos_pnp_driver); 1147#endif
1052 else
1053 platform_driver_unregister(&cmos_platform_driver);
1054#else
1055 platform_driver_unregister(&cmos_platform_driver); 1148 platform_driver_unregister(&cmos_platform_driver);
1056#endif /* CONFIG_PNP */
1057} 1149}
1058module_exit(cmos_exit); 1150module_exit(cmos_exit);
1059 1151
diff --git a/fs/Kconfig b/fs/Kconfig
index 501f012e0c6f..9e9d70c02a07 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -220,17 +220,16 @@ config JBD
220 tristate 220 tristate
221 help 221 help
222 This is a generic journalling layer for block devices. It is 222 This is a generic journalling layer for block devices. It is
223 currently used by the ext3 and OCFS2 file systems, but it could 223 currently used by the ext3 file system, but it could also be
224 also be used to add journal support to other file systems or block 224 used to add journal support to other file systems or block
225 devices such as RAID or LVM. 225 devices such as RAID or LVM.
226 226
227 If you are using the ext3 or OCFS2 file systems, you need to 227 If you are using the ext3 file system, you need to say Y here.
228 say Y here. If you are not using ext3 OCFS2 then you will probably 228 If you are not using ext3 then you will probably want to say N.
229 want to say N.
230 229
231 To compile this device as a module, choose M here: the module will be 230 To compile this device as a module, choose M here: the module will be
232 called jbd. If you are compiling ext3 or OCFS2 into the kernel, 231 called jbd. If you are compiling ext3 into the kernel, you
233 you cannot compile this code as a module. 232 cannot compile this code as a module.
234 233
235config JBD_DEBUG 234config JBD_DEBUG
236 bool "JBD (ext3) debugging support" 235 bool "JBD (ext3) debugging support"
@@ -254,15 +253,16 @@ config JBD2
254 help 253 help
255 This is a generic journaling layer for block devices that support 254 This is a generic journaling layer for block devices that support
256 both 32-bit and 64-bit block numbers. It is currently used by 255 both 32-bit and 64-bit block numbers. It is currently used by
257 the ext4 filesystem, but it could also be used to add 256 the ext4 and OCFS2 filesystems, but it could also be used to add
258 journal support to other file systems or block devices such 257 journal support to other file systems or block devices such
259 as RAID or LVM. 258 as RAID or LVM.
260 259
261 If you are using ext4, you need to say Y here. If you are not 260 If you are using ext4 or OCFS2, you need to say Y here.
262 using ext4 then you will probably want to say N. 261 If you are not using ext4 or OCFS2 then you will
262 probably want to say N.
263 263
264 To compile this device as a module, choose M here. The module will be 264 To compile this device as a module, choose M here. The module will be
265 called jbd2. If you are compiling ext4 into the kernel, 265 called jbd2. If you are compiling ext4 or OCFS2 into the kernel,
266 you cannot compile this code as a module. 266 you cannot compile this code as a module.
267 267
268config JBD2_DEBUG 268config JBD2_DEBUG
@@ -448,7 +448,7 @@ config OCFS2_FS
448 tristate "OCFS2 file system support" 448 tristate "OCFS2 file system support"
449 depends on NET && SYSFS 449 depends on NET && SYSFS
450 select CONFIGFS_FS 450 select CONFIGFS_FS
451 select JBD 451 select JBD2
452 select CRC32 452 select CRC32
453 help 453 help
454 OCFS2 is a general purpose extent based shared disk cluster file 454 OCFS2 is a general purpose extent based shared disk cluster file
@@ -519,6 +519,16 @@ config OCFS2_DEBUG_FS
519 this option for debugging only as it is likely to decrease 519 this option for debugging only as it is likely to decrease
520 performance of the filesystem. 520 performance of the filesystem.
521 521
522config OCFS2_COMPAT_JBD
523 bool "Use JBD for compatibility"
524 depends on OCFS2_FS
525 default n
526 select JBD
527 help
528 The ocfs2 filesystem now uses JBD2 for its journalling. JBD2
529 is backwards compatible with JBD. It is safe to say N here.
530 However, if you really want to use the original JBD, say Y here.
531
522endif # BLOCK 532endif # BLOCK
523 533
524config DNOTIFY 534config DNOTIFY
diff --git a/fs/ocfs2/Makefile b/fs/ocfs2/Makefile
index f6956de56fdb..589dcdfdfe3c 100644
--- a/fs/ocfs2/Makefile
+++ b/fs/ocfs2/Makefile
@@ -34,7 +34,8 @@ ocfs2-objs := \
34 symlink.o \ 34 symlink.o \
35 sysfile.o \ 35 sysfile.o \
36 uptodate.o \ 36 uptodate.o \
37 ver.o 37 ver.o \
38 xattr.o
38 39
39ocfs2_stackglue-objs := stackglue.o 40ocfs2_stackglue-objs := stackglue.o
40ocfs2_stack_o2cb-objs := stack_o2cb.o 41ocfs2_stack_o2cb-objs := stack_o2cb.o
diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
index 29ff57ec5d1f..0cc2deb9394c 100644
--- a/fs/ocfs2/alloc.c
+++ b/fs/ocfs2/alloc.c
@@ -49,6 +49,340 @@
49 49
50#include "buffer_head_io.h" 50#include "buffer_head_io.h"
51 51
52
53/*
54 * Operations for a specific extent tree type.
55 *
56 * To implement an on-disk btree (extent tree) type in ocfs2, add
57 * an ocfs2_extent_tree_operations structure and the matching
58 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
59 * for the allocation portion of the extent tree.
60 */
61struct ocfs2_extent_tree_operations {
62 /*
63 * last_eb_blk is the block number of the right most leaf extent
64 * block. Most on-disk structures containing an extent tree store
65 * this value for fast access. The ->eo_set_last_eb_blk() and
66 * ->eo_get_last_eb_blk() operations access this value. They are
67 * both required.
68 */
69 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
70 u64 blkno);
71 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
72
73 /*
74 * The on-disk structure usually keeps track of how many total
75 * clusters are stored in this extent tree. This function updates
76 * that value. new_clusters is the delta, and must be
77 * added to the total. Required.
78 */
79 void (*eo_update_clusters)(struct inode *inode,
80 struct ocfs2_extent_tree *et,
81 u32 new_clusters);
82
83 /*
84 * If ->eo_insert_check() exists, it is called before rec is
85 * inserted into the extent tree. It is optional.
86 */
87 int (*eo_insert_check)(struct inode *inode,
88 struct ocfs2_extent_tree *et,
89 struct ocfs2_extent_rec *rec);
90 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
91
92 /*
93 * --------------------------------------------------------------
94 * The remaining are internal to ocfs2_extent_tree and don't have
95 * accessor functions
96 */
97
98 /*
99 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
100 * It is required.
101 */
102 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
103
104 /*
105 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
106 * it exists. If it does not, et->et_max_leaf_clusters is set
107 * to 0 (unlimited). Optional.
108 */
109 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
110 struct ocfs2_extent_tree *et);
111};
112
113
114/*
115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
116 * in the methods.
117 */
118static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
119static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
120 u64 blkno);
121static void ocfs2_dinode_update_clusters(struct inode *inode,
122 struct ocfs2_extent_tree *et,
123 u32 clusters);
124static int ocfs2_dinode_insert_check(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 struct ocfs2_extent_rec *rec);
127static int ocfs2_dinode_sanity_check(struct inode *inode,
128 struct ocfs2_extent_tree *et);
129static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
130static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
131 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
132 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
133 .eo_update_clusters = ocfs2_dinode_update_clusters,
134 .eo_insert_check = ocfs2_dinode_insert_check,
135 .eo_sanity_check = ocfs2_dinode_sanity_check,
136 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
137};
138
139static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
140 u64 blkno)
141{
142 struct ocfs2_dinode *di = et->et_object;
143
144 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
145 di->i_last_eb_blk = cpu_to_le64(blkno);
146}
147
148static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
149{
150 struct ocfs2_dinode *di = et->et_object;
151
152 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
153 return le64_to_cpu(di->i_last_eb_blk);
154}
155
156static void ocfs2_dinode_update_clusters(struct inode *inode,
157 struct ocfs2_extent_tree *et,
158 u32 clusters)
159{
160 struct ocfs2_dinode *di = et->et_object;
161
162 le32_add_cpu(&di->i_clusters, clusters);
163 spin_lock(&OCFS2_I(inode)->ip_lock);
164 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
165 spin_unlock(&OCFS2_I(inode)->ip_lock);
166}
167
168static int ocfs2_dinode_insert_check(struct inode *inode,
169 struct ocfs2_extent_tree *et,
170 struct ocfs2_extent_rec *rec)
171{
172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
173
174 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
175 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
176 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
177 "Device %s, asking for sparse allocation: inode %llu, "
178 "cpos %u, clusters %u\n",
179 osb->dev_str,
180 (unsigned long long)OCFS2_I(inode)->ip_blkno,
181 rec->e_cpos,
182 OCFS2_I(inode)->ip_clusters);
183
184 return 0;
185}
186
187static int ocfs2_dinode_sanity_check(struct inode *inode,
188 struct ocfs2_extent_tree *et)
189{
190 int ret = 0;
191 struct ocfs2_dinode *di;
192
193 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
194
195 di = et->et_object;
196 if (!OCFS2_IS_VALID_DINODE(di)) {
197 ret = -EIO;
198 ocfs2_error(inode->i_sb,
199 "Inode %llu has invalid path root",
200 (unsigned long long)OCFS2_I(inode)->ip_blkno);
201 }
202
203 return ret;
204}
205
206static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
207{
208 struct ocfs2_dinode *di = et->et_object;
209
210 et->et_root_el = &di->id2.i_list;
211}
212
213
214static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
215{
216 struct ocfs2_xattr_value_root *xv = et->et_object;
217
218 et->et_root_el = &xv->xr_list;
219}
220
221static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
222 u64 blkno)
223{
224 struct ocfs2_xattr_value_root *xv =
225 (struct ocfs2_xattr_value_root *)et->et_object;
226
227 xv->xr_last_eb_blk = cpu_to_le64(blkno);
228}
229
230static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
231{
232 struct ocfs2_xattr_value_root *xv =
233 (struct ocfs2_xattr_value_root *) et->et_object;
234
235 return le64_to_cpu(xv->xr_last_eb_blk);
236}
237
238static void ocfs2_xattr_value_update_clusters(struct inode *inode,
239 struct ocfs2_extent_tree *et,
240 u32 clusters)
241{
242 struct ocfs2_xattr_value_root *xv =
243 (struct ocfs2_xattr_value_root *)et->et_object;
244
245 le32_add_cpu(&xv->xr_clusters, clusters);
246}
247
248static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
249 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
250 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
251 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
252 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
253};
254
255static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
256{
257 struct ocfs2_xattr_block *xb = et->et_object;
258
259 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
260}
261
262static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
263 struct ocfs2_extent_tree *et)
264{
265 et->et_max_leaf_clusters =
266 ocfs2_clusters_for_bytes(inode->i_sb,
267 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
268}
269
270static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
271 u64 blkno)
272{
273 struct ocfs2_xattr_block *xb = et->et_object;
274 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
275
276 xt->xt_last_eb_blk = cpu_to_le64(blkno);
277}
278
279static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
280{
281 struct ocfs2_xattr_block *xb = et->et_object;
282 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
283
284 return le64_to_cpu(xt->xt_last_eb_blk);
285}
286
287static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
288 struct ocfs2_extent_tree *et,
289 u32 clusters)
290{
291 struct ocfs2_xattr_block *xb = et->et_object;
292
293 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
294}
295
296static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
297 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
298 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
299 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
300 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
301 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
302};
303
304static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
305 struct inode *inode,
306 struct buffer_head *bh,
307 void *obj,
308 struct ocfs2_extent_tree_operations *ops)
309{
310 et->et_ops = ops;
311 et->et_root_bh = bh;
312 if (!obj)
313 obj = (void *)bh->b_data;
314 et->et_object = obj;
315
316 et->et_ops->eo_fill_root_el(et);
317 if (!et->et_ops->eo_fill_max_leaf_clusters)
318 et->et_max_leaf_clusters = 0;
319 else
320 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
321}
322
323void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
324 struct inode *inode,
325 struct buffer_head *bh)
326{
327 __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
328}
329
330void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
331 struct inode *inode,
332 struct buffer_head *bh)
333{
334 __ocfs2_init_extent_tree(et, inode, bh, NULL,
335 &ocfs2_xattr_tree_et_ops);
336}
337
338void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
339 struct inode *inode,
340 struct buffer_head *bh,
341 struct ocfs2_xattr_value_root *xv)
342{
343 __ocfs2_init_extent_tree(et, inode, bh, xv,
344 &ocfs2_xattr_value_et_ops);
345}
346
347static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
348 u64 new_last_eb_blk)
349{
350 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
351}
352
353static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
354{
355 return et->et_ops->eo_get_last_eb_blk(et);
356}
357
358static inline void ocfs2_et_update_clusters(struct inode *inode,
359 struct ocfs2_extent_tree *et,
360 u32 clusters)
361{
362 et->et_ops->eo_update_clusters(inode, et, clusters);
363}
364
365static inline int ocfs2_et_insert_check(struct inode *inode,
366 struct ocfs2_extent_tree *et,
367 struct ocfs2_extent_rec *rec)
368{
369 int ret = 0;
370
371 if (et->et_ops->eo_insert_check)
372 ret = et->et_ops->eo_insert_check(inode, et, rec);
373 return ret;
374}
375
376static inline int ocfs2_et_sanity_check(struct inode *inode,
377 struct ocfs2_extent_tree *et)
378{
379 int ret = 0;
380
381 if (et->et_ops->eo_sanity_check)
382 ret = et->et_ops->eo_sanity_check(inode, et);
383 return ret;
384}
385
52static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); 386static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
53static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, 387static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
54 struct ocfs2_extent_block *eb); 388 struct ocfs2_extent_block *eb);
@@ -205,17 +539,6 @@ static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
205} 539}
206 540
207/* 541/*
208 * Allocate and initialize a new path based on a disk inode tree.
209 */
210static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
211{
212 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
213 struct ocfs2_extent_list *el = &di->id2.i_list;
214
215 return ocfs2_new_path(di_bh, el);
216}
217
218/*
219 * Convenience function to journal all components in a path. 542 * Convenience function to journal all components in a path.
220 */ 543 */
221static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle, 544static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
@@ -368,39 +691,35 @@ struct ocfs2_merge_ctxt {
368 */ 691 */
369int ocfs2_num_free_extents(struct ocfs2_super *osb, 692int ocfs2_num_free_extents(struct ocfs2_super *osb,
370 struct inode *inode, 693 struct inode *inode,
371 struct ocfs2_dinode *fe) 694 struct ocfs2_extent_tree *et)
372{ 695{
373 int retval; 696 int retval;
374 struct ocfs2_extent_list *el; 697 struct ocfs2_extent_list *el = NULL;
375 struct ocfs2_extent_block *eb; 698 struct ocfs2_extent_block *eb;
376 struct buffer_head *eb_bh = NULL; 699 struct buffer_head *eb_bh = NULL;
700 u64 last_eb_blk = 0;
377 701
378 mlog_entry_void(); 702 mlog_entry_void();
379 703
380 if (!OCFS2_IS_VALID_DINODE(fe)) { 704 el = et->et_root_el;
381 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); 705 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
382 retval = -EIO;
383 goto bail;
384 }
385 706
386 if (fe->i_last_eb_blk) { 707 if (last_eb_blk) {
387 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), 708 retval = ocfs2_read_block(inode, last_eb_blk,
388 &eb_bh, OCFS2_BH_CACHED, inode); 709 &eb_bh);
389 if (retval < 0) { 710 if (retval < 0) {
390 mlog_errno(retval); 711 mlog_errno(retval);
391 goto bail; 712 goto bail;
392 } 713 }
393 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 714 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
394 el = &eb->h_list; 715 el = &eb->h_list;
395 } else 716 }
396 el = &fe->id2.i_list;
397 717
398 BUG_ON(el->l_tree_depth != 0); 718 BUG_ON(el->l_tree_depth != 0);
399 719
400 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); 720 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
401bail: 721bail:
402 if (eb_bh) 722 brelse(eb_bh);
403 brelse(eb_bh);
404 723
405 mlog_exit(retval); 724 mlog_exit(retval);
406 return retval; 725 return retval;
@@ -486,8 +805,7 @@ static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
486bail: 805bail:
487 if (status < 0) { 806 if (status < 0) {
488 for(i = 0; i < wanted; i++) { 807 for(i = 0; i < wanted; i++) {
489 if (bhs[i]) 808 brelse(bhs[i]);
490 brelse(bhs[i]);
491 bhs[i] = NULL; 809 bhs[i] = NULL;
492 } 810 }
493 } 811 }
@@ -531,7 +849,7 @@ static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
531static int ocfs2_add_branch(struct ocfs2_super *osb, 849static int ocfs2_add_branch(struct ocfs2_super *osb,
532 handle_t *handle, 850 handle_t *handle,
533 struct inode *inode, 851 struct inode *inode,
534 struct buffer_head *fe_bh, 852 struct ocfs2_extent_tree *et,
535 struct buffer_head *eb_bh, 853 struct buffer_head *eb_bh,
536 struct buffer_head **last_eb_bh, 854 struct buffer_head **last_eb_bh,
537 struct ocfs2_alloc_context *meta_ac) 855 struct ocfs2_alloc_context *meta_ac)
@@ -540,7 +858,6 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
540 u64 next_blkno, new_last_eb_blk; 858 u64 next_blkno, new_last_eb_blk;
541 struct buffer_head *bh; 859 struct buffer_head *bh;
542 struct buffer_head **new_eb_bhs = NULL; 860 struct buffer_head **new_eb_bhs = NULL;
543 struct ocfs2_dinode *fe;
544 struct ocfs2_extent_block *eb; 861 struct ocfs2_extent_block *eb;
545 struct ocfs2_extent_list *eb_el; 862 struct ocfs2_extent_list *eb_el;
546 struct ocfs2_extent_list *el; 863 struct ocfs2_extent_list *el;
@@ -550,13 +867,11 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
550 867
551 BUG_ON(!last_eb_bh || !*last_eb_bh); 868 BUG_ON(!last_eb_bh || !*last_eb_bh);
552 869
553 fe = (struct ocfs2_dinode *) fe_bh->b_data;
554
555 if (eb_bh) { 870 if (eb_bh) {
556 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 871 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
557 el = &eb->h_list; 872 el = &eb->h_list;
558 } else 873 } else
559 el = &fe->id2.i_list; 874 el = et->et_root_el;
560 875
561 /* we never add a branch to a leaf. */ 876 /* we never add a branch to a leaf. */
562 BUG_ON(!el->l_tree_depth); 877 BUG_ON(!el->l_tree_depth);
@@ -646,7 +961,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
646 mlog_errno(status); 961 mlog_errno(status);
647 goto bail; 962 goto bail;
648 } 963 }
649 status = ocfs2_journal_access(handle, inode, fe_bh, 964 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
650 OCFS2_JOURNAL_ACCESS_WRITE); 965 OCFS2_JOURNAL_ACCESS_WRITE);
651 if (status < 0) { 966 if (status < 0) {
652 mlog_errno(status); 967 mlog_errno(status);
@@ -662,7 +977,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
662 } 977 }
663 978
664 /* Link the new branch into the rest of the tree (el will 979 /* Link the new branch into the rest of the tree (el will
665 * either be on the fe, or the extent block passed in. */ 980 * either be on the root_bh, or the extent block passed in. */
666 i = le16_to_cpu(el->l_next_free_rec); 981 i = le16_to_cpu(el->l_next_free_rec);
667 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); 982 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
668 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); 983 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
@@ -671,7 +986,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
671 986
672 /* fe needs a new last extent block pointer, as does the 987 /* fe needs a new last extent block pointer, as does the
673 * next_leaf on the previously last-extent-block. */ 988 * next_leaf on the previously last-extent-block. */
674 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk); 989 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
675 990
676 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; 991 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
677 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); 992 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
@@ -679,7 +994,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
679 status = ocfs2_journal_dirty(handle, *last_eb_bh); 994 status = ocfs2_journal_dirty(handle, *last_eb_bh);
680 if (status < 0) 995 if (status < 0)
681 mlog_errno(status); 996 mlog_errno(status);
682 status = ocfs2_journal_dirty(handle, fe_bh); 997 status = ocfs2_journal_dirty(handle, et->et_root_bh);
683 if (status < 0) 998 if (status < 0)
684 mlog_errno(status); 999 mlog_errno(status);
685 if (eb_bh) { 1000 if (eb_bh) {
@@ -700,8 +1015,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
700bail: 1015bail:
701 if (new_eb_bhs) { 1016 if (new_eb_bhs) {
702 for (i = 0; i < new_blocks; i++) 1017 for (i = 0; i < new_blocks; i++)
703 if (new_eb_bhs[i]) 1018 brelse(new_eb_bhs[i]);
704 brelse(new_eb_bhs[i]);
705 kfree(new_eb_bhs); 1019 kfree(new_eb_bhs);
706 } 1020 }
707 1021
@@ -717,16 +1031,15 @@ bail:
717static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, 1031static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
718 handle_t *handle, 1032 handle_t *handle,
719 struct inode *inode, 1033 struct inode *inode,
720 struct buffer_head *fe_bh, 1034 struct ocfs2_extent_tree *et,
721 struct ocfs2_alloc_context *meta_ac, 1035 struct ocfs2_alloc_context *meta_ac,
722 struct buffer_head **ret_new_eb_bh) 1036 struct buffer_head **ret_new_eb_bh)
723{ 1037{
724 int status, i; 1038 int status, i;
725 u32 new_clusters; 1039 u32 new_clusters;
726 struct buffer_head *new_eb_bh = NULL; 1040 struct buffer_head *new_eb_bh = NULL;
727 struct ocfs2_dinode *fe;
728 struct ocfs2_extent_block *eb; 1041 struct ocfs2_extent_block *eb;
729 struct ocfs2_extent_list *fe_el; 1042 struct ocfs2_extent_list *root_el;
730 struct ocfs2_extent_list *eb_el; 1043 struct ocfs2_extent_list *eb_el;
731 1044
732 mlog_entry_void(); 1045 mlog_entry_void();
@@ -746,8 +1059,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
746 } 1059 }
747 1060
748 eb_el = &eb->h_list; 1061 eb_el = &eb->h_list;
749 fe = (struct ocfs2_dinode *) fe_bh->b_data; 1062 root_el = et->et_root_el;
750 fe_el = &fe->id2.i_list;
751 1063
752 status = ocfs2_journal_access(handle, inode, new_eb_bh, 1064 status = ocfs2_journal_access(handle, inode, new_eb_bh,
753 OCFS2_JOURNAL_ACCESS_CREATE); 1065 OCFS2_JOURNAL_ACCESS_CREATE);
@@ -756,11 +1068,11 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
756 goto bail; 1068 goto bail;
757 } 1069 }
758 1070
759 /* copy the fe data into the new extent block */ 1071 /* copy the root extent list data into the new extent block */
760 eb_el->l_tree_depth = fe_el->l_tree_depth; 1072 eb_el->l_tree_depth = root_el->l_tree_depth;
761 eb_el->l_next_free_rec = fe_el->l_next_free_rec; 1073 eb_el->l_next_free_rec = root_el->l_next_free_rec;
762 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) 1074 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
763 eb_el->l_recs[i] = fe_el->l_recs[i]; 1075 eb_el->l_recs[i] = root_el->l_recs[i];
764 1076
765 status = ocfs2_journal_dirty(handle, new_eb_bh); 1077 status = ocfs2_journal_dirty(handle, new_eb_bh);
766 if (status < 0) { 1078 if (status < 0) {
@@ -768,7 +1080,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
768 goto bail; 1080 goto bail;
769 } 1081 }
770 1082
771 status = ocfs2_journal_access(handle, inode, fe_bh, 1083 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
772 OCFS2_JOURNAL_ACCESS_WRITE); 1084 OCFS2_JOURNAL_ACCESS_WRITE);
773 if (status < 0) { 1085 if (status < 0) {
774 mlog_errno(status); 1086 mlog_errno(status);
@@ -777,21 +1089,21 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
777 1089
778 new_clusters = ocfs2_sum_rightmost_rec(eb_el); 1090 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
779 1091
780 /* update fe now */ 1092 /* update root_bh now */
781 le16_add_cpu(&fe_el->l_tree_depth, 1); 1093 le16_add_cpu(&root_el->l_tree_depth, 1);
782 fe_el->l_recs[0].e_cpos = 0; 1094 root_el->l_recs[0].e_cpos = 0;
783 fe_el->l_recs[0].e_blkno = eb->h_blkno; 1095 root_el->l_recs[0].e_blkno = eb->h_blkno;
784 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); 1096 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
785 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) 1097 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
786 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); 1098 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
787 fe_el->l_next_free_rec = cpu_to_le16(1); 1099 root_el->l_next_free_rec = cpu_to_le16(1);
788 1100
789 /* If this is our 1st tree depth shift, then last_eb_blk 1101 /* If this is our 1st tree depth shift, then last_eb_blk
790 * becomes the allocated extent block */ 1102 * becomes the allocated extent block */
791 if (fe_el->l_tree_depth == cpu_to_le16(1)) 1103 if (root_el->l_tree_depth == cpu_to_le16(1))
792 fe->i_last_eb_blk = eb->h_blkno; 1104 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
793 1105
794 status = ocfs2_journal_dirty(handle, fe_bh); 1106 status = ocfs2_journal_dirty(handle, et->et_root_bh);
795 if (status < 0) { 1107 if (status < 0) {
796 mlog_errno(status); 1108 mlog_errno(status);
797 goto bail; 1109 goto bail;
@@ -801,8 +1113,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
801 new_eb_bh = NULL; 1113 new_eb_bh = NULL;
802 status = 0; 1114 status = 0;
803bail: 1115bail:
804 if (new_eb_bh) 1116 brelse(new_eb_bh);
805 brelse(new_eb_bh);
806 1117
807 mlog_exit(status); 1118 mlog_exit(status);
808 return status; 1119 return status;
@@ -817,22 +1128,21 @@ bail:
817 * 1) a lowest extent block is found, then we pass it back in 1128 * 1) a lowest extent block is found, then we pass it back in
818 * *lowest_eb_bh and return '0' 1129 * *lowest_eb_bh and return '0'
819 * 1130 *
820 * 2) the search fails to find anything, but the dinode has room. We 1131 * 2) the search fails to find anything, but the root_el has room. We
821 * pass NULL back in *lowest_eb_bh, but still return '0' 1132 * pass NULL back in *lowest_eb_bh, but still return '0'
822 * 1133 *
823 * 3) the search fails to find anything AND the dinode is full, in 1134 * 3) the search fails to find anything AND the root_el is full, in
824 * which case we return > 0 1135 * which case we return > 0
825 * 1136 *
826 * return status < 0 indicates an error. 1137 * return status < 0 indicates an error.
827 */ 1138 */
828static int ocfs2_find_branch_target(struct ocfs2_super *osb, 1139static int ocfs2_find_branch_target(struct ocfs2_super *osb,
829 struct inode *inode, 1140 struct inode *inode,
830 struct buffer_head *fe_bh, 1141 struct ocfs2_extent_tree *et,
831 struct buffer_head **target_bh) 1142 struct buffer_head **target_bh)
832{ 1143{
833 int status = 0, i; 1144 int status = 0, i;
834 u64 blkno; 1145 u64 blkno;
835 struct ocfs2_dinode *fe;
836 struct ocfs2_extent_block *eb; 1146 struct ocfs2_extent_block *eb;
837 struct ocfs2_extent_list *el; 1147 struct ocfs2_extent_list *el;
838 struct buffer_head *bh = NULL; 1148 struct buffer_head *bh = NULL;
@@ -842,8 +1152,7 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
842 1152
843 *target_bh = NULL; 1153 *target_bh = NULL;
844 1154
845 fe = (struct ocfs2_dinode *) fe_bh->b_data; 1155 el = et->et_root_el;
846 el = &fe->id2.i_list;
847 1156
848 while(le16_to_cpu(el->l_tree_depth) > 1) { 1157 while(le16_to_cpu(el->l_tree_depth) > 1) {
849 if (le16_to_cpu(el->l_next_free_rec) == 0) { 1158 if (le16_to_cpu(el->l_next_free_rec) == 0) {
@@ -864,13 +1173,10 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
864 goto bail; 1173 goto bail;
865 } 1174 }
866 1175
867 if (bh) { 1176 brelse(bh);
868 brelse(bh); 1177 bh = NULL;
869 bh = NULL;
870 }
871 1178
872 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED, 1179 status = ocfs2_read_block(inode, blkno, &bh);
873 inode);
874 if (status < 0) { 1180 if (status < 0) {
875 mlog_errno(status); 1181 mlog_errno(status);
876 goto bail; 1182 goto bail;
@@ -886,8 +1192,7 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
886 1192
887 if (le16_to_cpu(el->l_next_free_rec) < 1193 if (le16_to_cpu(el->l_next_free_rec) <
888 le16_to_cpu(el->l_count)) { 1194 le16_to_cpu(el->l_count)) {
889 if (lowest_bh) 1195 brelse(lowest_bh);
890 brelse(lowest_bh);
891 lowest_bh = bh; 1196 lowest_bh = bh;
892 get_bh(lowest_bh); 1197 get_bh(lowest_bh);
893 } 1198 }
@@ -895,14 +1200,13 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
895 1200
896 /* If we didn't find one and the fe doesn't have any room, 1201 /* If we didn't find one and the fe doesn't have any room,
897 * then return '1' */ 1202 * then return '1' */
898 if (!lowest_bh 1203 el = et->et_root_el;
899 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count)) 1204 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
900 status = 1; 1205 status = 1;
901 1206
902 *target_bh = lowest_bh; 1207 *target_bh = lowest_bh;
903bail: 1208bail:
904 if (bh) 1209 brelse(bh);
905 brelse(bh);
906 1210
907 mlog_exit(status); 1211 mlog_exit(status);
908 return status; 1212 return status;
@@ -919,19 +1223,19 @@ bail:
919 * *last_eb_bh will be updated by ocfs2_add_branch(). 1223 * *last_eb_bh will be updated by ocfs2_add_branch().
920 */ 1224 */
921static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, 1225static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
922 struct buffer_head *di_bh, int *final_depth, 1226 struct ocfs2_extent_tree *et, int *final_depth,
923 struct buffer_head **last_eb_bh, 1227 struct buffer_head **last_eb_bh,
924 struct ocfs2_alloc_context *meta_ac) 1228 struct ocfs2_alloc_context *meta_ac)
925{ 1229{
926 int ret, shift; 1230 int ret, shift;
927 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 1231 struct ocfs2_extent_list *el = et->et_root_el;
928 int depth = le16_to_cpu(di->id2.i_list.l_tree_depth); 1232 int depth = le16_to_cpu(el->l_tree_depth);
929 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1233 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
930 struct buffer_head *bh = NULL; 1234 struct buffer_head *bh = NULL;
931 1235
932 BUG_ON(meta_ac == NULL); 1236 BUG_ON(meta_ac == NULL);
933 1237
934 shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh); 1238 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
935 if (shift < 0) { 1239 if (shift < 0) {
936 ret = shift; 1240 ret = shift;
937 mlog_errno(ret); 1241 mlog_errno(ret);
@@ -948,7 +1252,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
948 /* ocfs2_shift_tree_depth will return us a buffer with 1252 /* ocfs2_shift_tree_depth will return us a buffer with
949 * the new extent block (so we can pass that to 1253 * the new extent block (so we can pass that to
950 * ocfs2_add_branch). */ 1254 * ocfs2_add_branch). */
951 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh, 1255 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
952 meta_ac, &bh); 1256 meta_ac, &bh);
953 if (ret < 0) { 1257 if (ret < 0) {
954 mlog_errno(ret); 1258 mlog_errno(ret);
@@ -975,7 +1279,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
975 /* call ocfs2_add_branch to add the final part of the tree with 1279 /* call ocfs2_add_branch to add the final part of the tree with
976 * the new data. */ 1280 * the new data. */
977 mlog(0, "add branch. bh = %p\n", bh); 1281 mlog(0, "add branch. bh = %p\n", bh);
978 ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh, 1282 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
979 meta_ac); 1283 meta_ac);
980 if (ret < 0) { 1284 if (ret < 0) {
981 mlog_errno(ret); 1285 mlog_errno(ret);
@@ -1236,8 +1540,7 @@ static int __ocfs2_find_path(struct inode *inode,
1236 1540
1237 brelse(bh); 1541 brelse(bh);
1238 bh = NULL; 1542 bh = NULL;
1239 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, 1543 ret = ocfs2_read_block(inode, blkno, &bh);
1240 &bh, OCFS2_BH_CACHED, inode);
1241 if (ret) { 1544 if (ret) {
1242 mlog_errno(ret); 1545 mlog_errno(ret);
1243 goto out; 1546 goto out;
@@ -2058,11 +2361,11 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2058 struct ocfs2_path *right_path, 2361 struct ocfs2_path *right_path,
2059 int subtree_index, 2362 int subtree_index,
2060 struct ocfs2_cached_dealloc_ctxt *dealloc, 2363 struct ocfs2_cached_dealloc_ctxt *dealloc,
2061 int *deleted) 2364 int *deleted,
2365 struct ocfs2_extent_tree *et)
2062{ 2366{
2063 int ret, i, del_right_subtree = 0, right_has_empty = 0; 2367 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2064 struct buffer_head *root_bh, *di_bh = path_root_bh(right_path); 2368 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2065 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2066 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; 2369 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2067 struct ocfs2_extent_block *eb; 2370 struct ocfs2_extent_block *eb;
2068 2371
@@ -2114,7 +2417,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2114 * We have to update i_last_eb_blk during the meta 2417 * We have to update i_last_eb_blk during the meta
2115 * data delete. 2418 * data delete.
2116 */ 2419 */
2117 ret = ocfs2_journal_access(handle, inode, di_bh, 2420 ret = ocfs2_journal_access(handle, inode, et_root_bh,
2118 OCFS2_JOURNAL_ACCESS_WRITE); 2421 OCFS2_JOURNAL_ACCESS_WRITE);
2119 if (ret) { 2422 if (ret) {
2120 mlog_errno(ret); 2423 mlog_errno(ret);
@@ -2189,7 +2492,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2189 ocfs2_update_edge_lengths(inode, handle, left_path); 2492 ocfs2_update_edge_lengths(inode, handle, left_path);
2190 2493
2191 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2494 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2192 di->i_last_eb_blk = eb->h_blkno; 2495 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2193 2496
2194 /* 2497 /*
2195 * Removal of the extent in the left leaf was skipped 2498 * Removal of the extent in the left leaf was skipped
@@ -2199,7 +2502,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2199 if (right_has_empty) 2502 if (right_has_empty)
2200 ocfs2_remove_empty_extent(left_leaf_el); 2503 ocfs2_remove_empty_extent(left_leaf_el);
2201 2504
2202 ret = ocfs2_journal_dirty(handle, di_bh); 2505 ret = ocfs2_journal_dirty(handle, et_root_bh);
2203 if (ret) 2506 if (ret)
2204 mlog_errno(ret); 2507 mlog_errno(ret);
2205 2508
@@ -2322,7 +2625,8 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
2322 handle_t *handle, int orig_credits, 2625 handle_t *handle, int orig_credits,
2323 struct ocfs2_path *path, 2626 struct ocfs2_path *path,
2324 struct ocfs2_cached_dealloc_ctxt *dealloc, 2627 struct ocfs2_cached_dealloc_ctxt *dealloc,
2325 struct ocfs2_path **empty_extent_path) 2628 struct ocfs2_path **empty_extent_path,
2629 struct ocfs2_extent_tree *et)
2326{ 2630{
2327 int ret, subtree_root, deleted; 2631 int ret, subtree_root, deleted;
2328 u32 right_cpos; 2632 u32 right_cpos;
@@ -2395,7 +2699,7 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
2395 2699
2396 ret = ocfs2_rotate_subtree_left(inode, handle, left_path, 2700 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2397 right_path, subtree_root, 2701 right_path, subtree_root,
2398 dealloc, &deleted); 2702 dealloc, &deleted, et);
2399 if (ret == -EAGAIN) { 2703 if (ret == -EAGAIN) {
2400 /* 2704 /*
2401 * The rotation has to temporarily stop due to 2705 * The rotation has to temporarily stop due to
@@ -2438,29 +2742,20 @@ out:
2438} 2742}
2439 2743
2440static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, 2744static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2441 struct ocfs2_path *path, 2745 struct ocfs2_path *path,
2442 struct ocfs2_cached_dealloc_ctxt *dealloc) 2746 struct ocfs2_cached_dealloc_ctxt *dealloc,
2747 struct ocfs2_extent_tree *et)
2443{ 2748{
2444 int ret, subtree_index; 2749 int ret, subtree_index;
2445 u32 cpos; 2750 u32 cpos;
2446 struct ocfs2_path *left_path = NULL; 2751 struct ocfs2_path *left_path = NULL;
2447 struct ocfs2_dinode *di;
2448 struct ocfs2_extent_block *eb; 2752 struct ocfs2_extent_block *eb;
2449 struct ocfs2_extent_list *el; 2753 struct ocfs2_extent_list *el;
2450 2754
2451 /*
2452 * XXX: This code assumes that the root is an inode, which is
2453 * true for now but may change as tree code gets generic.
2454 */
2455 di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
2456 if (!OCFS2_IS_VALID_DINODE(di)) {
2457 ret = -EIO;
2458 ocfs2_error(inode->i_sb,
2459 "Inode %llu has invalid path root",
2460 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2461 goto out;
2462 }
2463 2755
2756 ret = ocfs2_et_sanity_check(inode, et);
2757 if (ret)
2758 goto out;
2464 /* 2759 /*
2465 * There's two ways we handle this depending on 2760 * There's two ways we handle this depending on
2466 * whether path is the only existing one. 2761 * whether path is the only existing one.
@@ -2517,7 +2812,7 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2517 ocfs2_update_edge_lengths(inode, handle, left_path); 2812 ocfs2_update_edge_lengths(inode, handle, left_path);
2518 2813
2519 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2814 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2520 di->i_last_eb_blk = eb->h_blkno; 2815 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2521 } else { 2816 } else {
2522 /* 2817 /*
2523 * 'path' is also the leftmost path which 2818 * 'path' is also the leftmost path which
@@ -2528,12 +2823,12 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2528 */ 2823 */
2529 ocfs2_unlink_path(inode, handle, dealloc, path, 1); 2824 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2530 2825
2531 el = &di->id2.i_list; 2826 el = et->et_root_el;
2532 el->l_tree_depth = 0; 2827 el->l_tree_depth = 0;
2533 el->l_next_free_rec = 0; 2828 el->l_next_free_rec = 0;
2534 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2829 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2535 2830
2536 di->i_last_eb_blk = 0; 2831 ocfs2_et_set_last_eb_blk(et, 0);
2537 } 2832 }
2538 2833
2539 ocfs2_journal_dirty(handle, path_root_bh(path)); 2834 ocfs2_journal_dirty(handle, path_root_bh(path));
@@ -2561,7 +2856,8 @@ out:
2561 */ 2856 */
2562static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, 2857static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2563 struct ocfs2_path *path, 2858 struct ocfs2_path *path,
2564 struct ocfs2_cached_dealloc_ctxt *dealloc) 2859 struct ocfs2_cached_dealloc_ctxt *dealloc,
2860 struct ocfs2_extent_tree *et)
2565{ 2861{
2566 int ret, orig_credits = handle->h_buffer_credits; 2862 int ret, orig_credits = handle->h_buffer_credits;
2567 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; 2863 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
@@ -2575,7 +2871,7 @@ static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2575 if (path->p_tree_depth == 0) { 2871 if (path->p_tree_depth == 0) {
2576rightmost_no_delete: 2872rightmost_no_delete:
2577 /* 2873 /*
2578 * In-inode extents. This is trivially handled, so do 2874 * Inline extents. This is trivially handled, so do
2579 * it up front. 2875 * it up front.
2580 */ 2876 */
2581 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle, 2877 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
@@ -2629,7 +2925,7 @@ rightmost_no_delete:
2629 */ 2925 */
2630 2926
2631 ret = ocfs2_remove_rightmost_path(inode, handle, path, 2927 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2632 dealloc); 2928 dealloc, et);
2633 if (ret) 2929 if (ret)
2634 mlog_errno(ret); 2930 mlog_errno(ret);
2635 goto out; 2931 goto out;
@@ -2641,7 +2937,7 @@ rightmost_no_delete:
2641 */ 2937 */
2642try_rotate: 2938try_rotate:
2643 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path, 2939 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2644 dealloc, &restart_path); 2940 dealloc, &restart_path, et);
2645 if (ret && ret != -EAGAIN) { 2941 if (ret && ret != -EAGAIN) {
2646 mlog_errno(ret); 2942 mlog_errno(ret);
2647 goto out; 2943 goto out;
@@ -2653,7 +2949,7 @@ try_rotate:
2653 2949
2654 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, 2950 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2655 tmp_path, dealloc, 2951 tmp_path, dealloc,
2656 &restart_path); 2952 &restart_path, et);
2657 if (ret && ret != -EAGAIN) { 2953 if (ret && ret != -EAGAIN) {
2658 mlog_errno(ret); 2954 mlog_errno(ret);
2659 goto out; 2955 goto out;
@@ -2939,6 +3235,7 @@ static int ocfs2_merge_rec_left(struct inode *inode,
2939 handle_t *handle, 3235 handle_t *handle,
2940 struct ocfs2_extent_rec *split_rec, 3236 struct ocfs2_extent_rec *split_rec,
2941 struct ocfs2_cached_dealloc_ctxt *dealloc, 3237 struct ocfs2_cached_dealloc_ctxt *dealloc,
3238 struct ocfs2_extent_tree *et,
2942 int index) 3239 int index)
2943{ 3240{
2944 int ret, i, subtree_index = 0, has_empty_extent = 0; 3241 int ret, i, subtree_index = 0, has_empty_extent = 0;
@@ -3059,7 +3356,8 @@ static int ocfs2_merge_rec_left(struct inode *inode,
3059 le16_to_cpu(el->l_next_free_rec) == 1) { 3356 le16_to_cpu(el->l_next_free_rec) == 1) {
3060 3357
3061 ret = ocfs2_remove_rightmost_path(inode, handle, 3358 ret = ocfs2_remove_rightmost_path(inode, handle,
3062 right_path, dealloc); 3359 right_path,
3360 dealloc, et);
3063 if (ret) { 3361 if (ret) {
3064 mlog_errno(ret); 3362 mlog_errno(ret);
3065 goto out; 3363 goto out;
@@ -3086,7 +3384,8 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3086 int split_index, 3384 int split_index,
3087 struct ocfs2_extent_rec *split_rec, 3385 struct ocfs2_extent_rec *split_rec,
3088 struct ocfs2_cached_dealloc_ctxt *dealloc, 3386 struct ocfs2_cached_dealloc_ctxt *dealloc,
3089 struct ocfs2_merge_ctxt *ctxt) 3387 struct ocfs2_merge_ctxt *ctxt,
3388 struct ocfs2_extent_tree *et)
3090 3389
3091{ 3390{
3092 int ret = 0; 3391 int ret = 0;
@@ -3104,7 +3403,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3104 * illegal. 3403 * illegal.
3105 */ 3404 */
3106 ret = ocfs2_rotate_tree_left(inode, handle, path, 3405 ret = ocfs2_rotate_tree_left(inode, handle, path,
3107 dealloc); 3406 dealloc, et);
3108 if (ret) { 3407 if (ret) {
3109 mlog_errno(ret); 3408 mlog_errno(ret);
3110 goto out; 3409 goto out;
@@ -3147,7 +3446,8 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3147 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); 3446 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3148 3447
3149 /* The merge left us with an empty extent, remove it. */ 3448 /* The merge left us with an empty extent, remove it. */
3150 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); 3449 ret = ocfs2_rotate_tree_left(inode, handle, path,
3450 dealloc, et);
3151 if (ret) { 3451 if (ret) {
3152 mlog_errno(ret); 3452 mlog_errno(ret);
3153 goto out; 3453 goto out;
@@ -3161,7 +3461,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3161 */ 3461 */
3162 ret = ocfs2_merge_rec_left(inode, path, 3462 ret = ocfs2_merge_rec_left(inode, path,
3163 handle, rec, 3463 handle, rec,
3164 dealloc, 3464 dealloc, et,
3165 split_index); 3465 split_index);
3166 3466
3167 if (ret) { 3467 if (ret) {
@@ -3170,7 +3470,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3170 } 3470 }
3171 3471
3172 ret = ocfs2_rotate_tree_left(inode, handle, path, 3472 ret = ocfs2_rotate_tree_left(inode, handle, path,
3173 dealloc); 3473 dealloc, et);
3174 /* 3474 /*
3175 * Error from this last rotate is not critical, so 3475 * Error from this last rotate is not critical, so
3176 * print but don't bubble it up. 3476 * print but don't bubble it up.
@@ -3190,7 +3490,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3190 ret = ocfs2_merge_rec_left(inode, 3490 ret = ocfs2_merge_rec_left(inode,
3191 path, 3491 path,
3192 handle, split_rec, 3492 handle, split_rec,
3193 dealloc, 3493 dealloc, et,
3194 split_index); 3494 split_index);
3195 if (ret) { 3495 if (ret) {
3196 mlog_errno(ret); 3496 mlog_errno(ret);
@@ -3213,7 +3513,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
3213 * our leaf. Try to rotate it away. 3513 * our leaf. Try to rotate it away.
3214 */ 3514 */
3215 ret = ocfs2_rotate_tree_left(inode, handle, path, 3515 ret = ocfs2_rotate_tree_left(inode, handle, path,
3216 dealloc); 3516 dealloc, et);
3217 if (ret) 3517 if (ret)
3218 mlog_errno(ret); 3518 mlog_errno(ret);
3219 ret = 0; 3519 ret = 0;
@@ -3347,16 +3647,6 @@ rotate:
3347 ocfs2_rotate_leaf(el, insert_rec); 3647 ocfs2_rotate_leaf(el, insert_rec);
3348} 3648}
3349 3649
3350static inline void ocfs2_update_dinode_clusters(struct inode *inode,
3351 struct ocfs2_dinode *di,
3352 u32 clusters)
3353{
3354 le32_add_cpu(&di->i_clusters, clusters);
3355 spin_lock(&OCFS2_I(inode)->ip_lock);
3356 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
3357 spin_unlock(&OCFS2_I(inode)->ip_lock);
3358}
3359
3360static void ocfs2_adjust_rightmost_records(struct inode *inode, 3650static void ocfs2_adjust_rightmost_records(struct inode *inode,
3361 handle_t *handle, 3651 handle_t *handle,
3362 struct ocfs2_path *path, 3652 struct ocfs2_path *path,
@@ -3558,8 +3848,8 @@ static void ocfs2_split_record(struct inode *inode,
3558} 3848}
3559 3849
3560/* 3850/*
3561 * This function only does inserts on an allocation b-tree. For dinode 3851 * This function only does inserts on an allocation b-tree. For tree
3562 * lists, ocfs2_insert_at_leaf() is called directly. 3852 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3563 * 3853 *
3564 * right_path is the path we want to do the actual insert 3854 * right_path is the path we want to do the actual insert
3565 * in. left_path should only be passed in if we need to update that 3855 * in. left_path should only be passed in if we need to update that
@@ -3656,7 +3946,7 @@ out:
3656 3946
3657static int ocfs2_do_insert_extent(struct inode *inode, 3947static int ocfs2_do_insert_extent(struct inode *inode,
3658 handle_t *handle, 3948 handle_t *handle,
3659 struct buffer_head *di_bh, 3949 struct ocfs2_extent_tree *et,
3660 struct ocfs2_extent_rec *insert_rec, 3950 struct ocfs2_extent_rec *insert_rec,
3661 struct ocfs2_insert_type *type) 3951 struct ocfs2_insert_type *type)
3662{ 3952{
@@ -3664,13 +3954,11 @@ static int ocfs2_do_insert_extent(struct inode *inode,
3664 u32 cpos; 3954 u32 cpos;
3665 struct ocfs2_path *right_path = NULL; 3955 struct ocfs2_path *right_path = NULL;
3666 struct ocfs2_path *left_path = NULL; 3956 struct ocfs2_path *left_path = NULL;
3667 struct ocfs2_dinode *di;
3668 struct ocfs2_extent_list *el; 3957 struct ocfs2_extent_list *el;
3669 3958
3670 di = (struct ocfs2_dinode *) di_bh->b_data; 3959 el = et->et_root_el;
3671 el = &di->id2.i_list;
3672 3960
3673 ret = ocfs2_journal_access(handle, inode, di_bh, 3961 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3674 OCFS2_JOURNAL_ACCESS_WRITE); 3962 OCFS2_JOURNAL_ACCESS_WRITE);
3675 if (ret) { 3963 if (ret) {
3676 mlog_errno(ret); 3964 mlog_errno(ret);
@@ -3682,7 +3970,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
3682 goto out_update_clusters; 3970 goto out_update_clusters;
3683 } 3971 }
3684 3972
3685 right_path = ocfs2_new_inode_path(di_bh); 3973 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
3686 if (!right_path) { 3974 if (!right_path) {
3687 ret = -ENOMEM; 3975 ret = -ENOMEM;
3688 mlog_errno(ret); 3976 mlog_errno(ret);
@@ -3732,7 +4020,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
3732 * ocfs2_rotate_tree_right() might have extended the 4020 * ocfs2_rotate_tree_right() might have extended the
3733 * transaction without re-journaling our tree root. 4021 * transaction without re-journaling our tree root.
3734 */ 4022 */
3735 ret = ocfs2_journal_access(handle, inode, di_bh, 4023 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
3736 OCFS2_JOURNAL_ACCESS_WRITE); 4024 OCFS2_JOURNAL_ACCESS_WRITE);
3737 if (ret) { 4025 if (ret) {
3738 mlog_errno(ret); 4026 mlog_errno(ret);
@@ -3757,10 +4045,10 @@ static int ocfs2_do_insert_extent(struct inode *inode,
3757 4045
3758out_update_clusters: 4046out_update_clusters:
3759 if (type->ins_split == SPLIT_NONE) 4047 if (type->ins_split == SPLIT_NONE)
3760 ocfs2_update_dinode_clusters(inode, di, 4048 ocfs2_et_update_clusters(inode, et,
3761 le16_to_cpu(insert_rec->e_leaf_clusters)); 4049 le16_to_cpu(insert_rec->e_leaf_clusters));
3762 4050
3763 ret = ocfs2_journal_dirty(handle, di_bh); 4051 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
3764 if (ret) 4052 if (ret)
3765 mlog_errno(ret); 4053 mlog_errno(ret);
3766 4054
@@ -3890,7 +4178,8 @@ out:
3890static void ocfs2_figure_contig_type(struct inode *inode, 4178static void ocfs2_figure_contig_type(struct inode *inode,
3891 struct ocfs2_insert_type *insert, 4179 struct ocfs2_insert_type *insert,
3892 struct ocfs2_extent_list *el, 4180 struct ocfs2_extent_list *el,
3893 struct ocfs2_extent_rec *insert_rec) 4181 struct ocfs2_extent_rec *insert_rec,
4182 struct ocfs2_extent_tree *et)
3894{ 4183{
3895 int i; 4184 int i;
3896 enum ocfs2_contig_type contig_type = CONTIG_NONE; 4185 enum ocfs2_contig_type contig_type = CONTIG_NONE;
@@ -3906,6 +4195,21 @@ static void ocfs2_figure_contig_type(struct inode *inode,
3906 } 4195 }
3907 } 4196 }
3908 insert->ins_contig = contig_type; 4197 insert->ins_contig = contig_type;
4198
4199 if (insert->ins_contig != CONTIG_NONE) {
4200 struct ocfs2_extent_rec *rec =
4201 &el->l_recs[insert->ins_contig_index];
4202 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4203 le16_to_cpu(insert_rec->e_leaf_clusters);
4204
4205 /*
4206 * Caller might want us to limit the size of extents, don't
4207 * calculate contiguousness if we might exceed that limit.
4208 */
4209 if (et->et_max_leaf_clusters &&
4210 (len > et->et_max_leaf_clusters))
4211 insert->ins_contig = CONTIG_NONE;
4212 }
3909} 4213}
3910 4214
3911/* 4215/*
@@ -3914,8 +4218,8 @@ static void ocfs2_figure_contig_type(struct inode *inode,
3914 * ocfs2_figure_appending_type() will figure out whether we'll have to 4218 * ocfs2_figure_appending_type() will figure out whether we'll have to
3915 * insert at the tail of the rightmost leaf. 4219 * insert at the tail of the rightmost leaf.
3916 * 4220 *
3917 * This should also work against the dinode list for tree's with 0 4221 * This should also work against the root extent list for tree's with 0
3918 * depth. If we consider the dinode list to be the rightmost leaf node 4222 * depth. If we consider the root extent list to be the rightmost leaf node
3919 * then the logic here makes sense. 4223 * then the logic here makes sense.
3920 */ 4224 */
3921static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, 4225static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
@@ -3966,14 +4270,13 @@ set_tail_append:
3966 * structure. 4270 * structure.
3967 */ 4271 */
3968static int ocfs2_figure_insert_type(struct inode *inode, 4272static int ocfs2_figure_insert_type(struct inode *inode,
3969 struct buffer_head *di_bh, 4273 struct ocfs2_extent_tree *et,
3970 struct buffer_head **last_eb_bh, 4274 struct buffer_head **last_eb_bh,
3971 struct ocfs2_extent_rec *insert_rec, 4275 struct ocfs2_extent_rec *insert_rec,
3972 int *free_records, 4276 int *free_records,
3973 struct ocfs2_insert_type *insert) 4277 struct ocfs2_insert_type *insert)
3974{ 4278{
3975 int ret; 4279 int ret;
3976 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3977 struct ocfs2_extent_block *eb; 4280 struct ocfs2_extent_block *eb;
3978 struct ocfs2_extent_list *el; 4281 struct ocfs2_extent_list *el;
3979 struct ocfs2_path *path = NULL; 4282 struct ocfs2_path *path = NULL;
@@ -3981,7 +4284,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
3981 4284
3982 insert->ins_split = SPLIT_NONE; 4285 insert->ins_split = SPLIT_NONE;
3983 4286
3984 el = &di->id2.i_list; 4287 el = et->et_root_el;
3985 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); 4288 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
3986 4289
3987 if (el->l_tree_depth) { 4290 if (el->l_tree_depth) {
@@ -3991,9 +4294,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
3991 * ocfs2_figure_insert_type() and ocfs2_add_branch() 4294 * ocfs2_figure_insert_type() and ocfs2_add_branch()
3992 * may want it later. 4295 * may want it later.
3993 */ 4296 */
3994 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 4297 ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et), &bh);
3995 le64_to_cpu(di->i_last_eb_blk), &bh,
3996 OCFS2_BH_CACHED, inode);
3997 if (ret) { 4298 if (ret) {
3998 mlog_exit(ret); 4299 mlog_exit(ret);
3999 goto out; 4300 goto out;
@@ -4014,12 +4315,12 @@ static int ocfs2_figure_insert_type(struct inode *inode,
4014 le16_to_cpu(el->l_next_free_rec); 4315 le16_to_cpu(el->l_next_free_rec);
4015 4316
4016 if (!insert->ins_tree_depth) { 4317 if (!insert->ins_tree_depth) {
4017 ocfs2_figure_contig_type(inode, insert, el, insert_rec); 4318 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4018 ocfs2_figure_appending_type(insert, el, insert_rec); 4319 ocfs2_figure_appending_type(insert, el, insert_rec);
4019 return 0; 4320 return 0;
4020 } 4321 }
4021 4322
4022 path = ocfs2_new_inode_path(di_bh); 4323 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4023 if (!path) { 4324 if (!path) {
4024 ret = -ENOMEM; 4325 ret = -ENOMEM;
4025 mlog_errno(ret); 4326 mlog_errno(ret);
@@ -4048,7 +4349,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
4048 * into two types of appends: simple record append, or a 4349 * into two types of appends: simple record append, or a
4049 * rotate inside the tail leaf. 4350 * rotate inside the tail leaf.
4050 */ 4351 */
4051 ocfs2_figure_contig_type(inode, insert, el, insert_rec); 4352 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4052 4353
4053 /* 4354 /*
4054 * The insert code isn't quite ready to deal with all cases of 4355 * The insert code isn't quite ready to deal with all cases of
@@ -4069,7 +4370,8 @@ static int ocfs2_figure_insert_type(struct inode *inode,
4069 * the case that we're doing a tail append, so maybe we can 4370 * the case that we're doing a tail append, so maybe we can
4070 * take advantage of that information somehow. 4371 * take advantage of that information somehow.
4071 */ 4372 */
4072 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) { 4373 if (ocfs2_et_get_last_eb_blk(et) ==
4374 path_leaf_bh(path)->b_blocknr) {
4073 /* 4375 /*
4074 * Ok, ocfs2_find_path() returned us the rightmost 4376 * Ok, ocfs2_find_path() returned us the rightmost
4075 * tree path. This might be an appending insert. There are 4377 * tree path. This might be an appending insert. There are
@@ -4099,7 +4401,7 @@ out:
4099int ocfs2_insert_extent(struct ocfs2_super *osb, 4401int ocfs2_insert_extent(struct ocfs2_super *osb,
4100 handle_t *handle, 4402 handle_t *handle,
4101 struct inode *inode, 4403 struct inode *inode,
4102 struct buffer_head *fe_bh, 4404 struct ocfs2_extent_tree *et,
4103 u32 cpos, 4405 u32 cpos,
4104 u64 start_blk, 4406 u64 start_blk,
4105 u32 new_clusters, 4407 u32 new_clusters,
@@ -4112,26 +4414,21 @@ int ocfs2_insert_extent(struct ocfs2_super *osb,
4112 struct ocfs2_insert_type insert = {0, }; 4414 struct ocfs2_insert_type insert = {0, };
4113 struct ocfs2_extent_rec rec; 4415 struct ocfs2_extent_rec rec;
4114 4416
4115 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4116
4117 mlog(0, "add %u clusters at position %u to inode %llu\n", 4417 mlog(0, "add %u clusters at position %u to inode %llu\n",
4118 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); 4418 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4119 4419
4120 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4121 (OCFS2_I(inode)->ip_clusters != cpos),
4122 "Device %s, asking for sparse allocation: inode %llu, "
4123 "cpos %u, clusters %u\n",
4124 osb->dev_str,
4125 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4126 OCFS2_I(inode)->ip_clusters);
4127
4128 memset(&rec, 0, sizeof(rec)); 4420 memset(&rec, 0, sizeof(rec));
4129 rec.e_cpos = cpu_to_le32(cpos); 4421 rec.e_cpos = cpu_to_le32(cpos);
4130 rec.e_blkno = cpu_to_le64(start_blk); 4422 rec.e_blkno = cpu_to_le64(start_blk);
4131 rec.e_leaf_clusters = cpu_to_le16(new_clusters); 4423 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4132 rec.e_flags = flags; 4424 rec.e_flags = flags;
4425 status = ocfs2_et_insert_check(inode, et, &rec);
4426 if (status) {
4427 mlog_errno(status);
4428 goto bail;
4429 }
4133 4430
4134 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec, 4431 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4135 &free_records, &insert); 4432 &free_records, &insert);
4136 if (status < 0) { 4433 if (status < 0) {
4137 mlog_errno(status); 4434 mlog_errno(status);
@@ -4145,7 +4442,7 @@ int ocfs2_insert_extent(struct ocfs2_super *osb,
4145 free_records, insert.ins_tree_depth); 4442 free_records, insert.ins_tree_depth);
4146 4443
4147 if (insert.ins_contig == CONTIG_NONE && free_records == 0) { 4444 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4148 status = ocfs2_grow_tree(inode, handle, fe_bh, 4445 status = ocfs2_grow_tree(inode, handle, et,
4149 &insert.ins_tree_depth, &last_eb_bh, 4446 &insert.ins_tree_depth, &last_eb_bh,
4150 meta_ac); 4447 meta_ac);
4151 if (status) { 4448 if (status) {
@@ -4155,17 +4452,124 @@ int ocfs2_insert_extent(struct ocfs2_super *osb,
4155 } 4452 }
4156 4453
4157 /* Finally, we can add clusters. This might rotate the tree for us. */ 4454 /* Finally, we can add clusters. This might rotate the tree for us. */
4158 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert); 4455 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4159 if (status < 0) 4456 if (status < 0)
4160 mlog_errno(status); 4457 mlog_errno(status);
4161 else 4458 else if (et->et_ops == &ocfs2_dinode_et_ops)
4162 ocfs2_extent_map_insert_rec(inode, &rec); 4459 ocfs2_extent_map_insert_rec(inode, &rec);
4163 4460
4164bail: 4461bail:
4165 if (last_eb_bh) 4462 brelse(last_eb_bh);
4166 brelse(last_eb_bh); 4463
4464 mlog_exit(status);
4465 return status;
4466}
4467
4468/*
4469 * Allcate and add clusters into the extent b-tree.
4470 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4471 * The extent b-tree's root is specified by et, and
4472 * it is not limited to the file storage. Any extent tree can use this
4473 * function if it implements the proper ocfs2_extent_tree.
4474 */
4475int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4476 struct inode *inode,
4477 u32 *logical_offset,
4478 u32 clusters_to_add,
4479 int mark_unwritten,
4480 struct ocfs2_extent_tree *et,
4481 handle_t *handle,
4482 struct ocfs2_alloc_context *data_ac,
4483 struct ocfs2_alloc_context *meta_ac,
4484 enum ocfs2_alloc_restarted *reason_ret)
4485{
4486 int status = 0;
4487 int free_extents;
4488 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4489 u32 bit_off, num_bits;
4490 u64 block;
4491 u8 flags = 0;
4492
4493 BUG_ON(!clusters_to_add);
4494
4495 if (mark_unwritten)
4496 flags = OCFS2_EXT_UNWRITTEN;
4497
4498 free_extents = ocfs2_num_free_extents(osb, inode, et);
4499 if (free_extents < 0) {
4500 status = free_extents;
4501 mlog_errno(status);
4502 goto leave;
4503 }
4504
4505 /* there are two cases which could cause us to EAGAIN in the
4506 * we-need-more-metadata case:
4507 * 1) we haven't reserved *any*
4508 * 2) we are so fragmented, we've needed to add metadata too
4509 * many times. */
4510 if (!free_extents && !meta_ac) {
4511 mlog(0, "we haven't reserved any metadata!\n");
4512 status = -EAGAIN;
4513 reason = RESTART_META;
4514 goto leave;
4515 } else if ((!free_extents)
4516 && (ocfs2_alloc_context_bits_left(meta_ac)
4517 < ocfs2_extend_meta_needed(et->et_root_el))) {
4518 mlog(0, "filesystem is really fragmented...\n");
4519 status = -EAGAIN;
4520 reason = RESTART_META;
4521 goto leave;
4522 }
4523
4524 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4525 clusters_to_add, &bit_off, &num_bits);
4526 if (status < 0) {
4527 if (status != -ENOSPC)
4528 mlog_errno(status);
4529 goto leave;
4530 }
4531
4532 BUG_ON(num_bits > clusters_to_add);
4167 4533
4534 /* reserve our write early -- insert_extent may update the inode */
4535 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
4536 OCFS2_JOURNAL_ACCESS_WRITE);
4537 if (status < 0) {
4538 mlog_errno(status);
4539 goto leave;
4540 }
4541
4542 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4543 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4544 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4545 status = ocfs2_insert_extent(osb, handle, inode, et,
4546 *logical_offset, block,
4547 num_bits, flags, meta_ac);
4548 if (status < 0) {
4549 mlog_errno(status);
4550 goto leave;
4551 }
4552
4553 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4554 if (status < 0) {
4555 mlog_errno(status);
4556 goto leave;
4557 }
4558
4559 clusters_to_add -= num_bits;
4560 *logical_offset += num_bits;
4561
4562 if (clusters_to_add) {
4563 mlog(0, "need to alloc once more, wanted = %u\n",
4564 clusters_to_add);
4565 status = -EAGAIN;
4566 reason = RESTART_TRANS;
4567 }
4568
4569leave:
4168 mlog_exit(status); 4570 mlog_exit(status);
4571 if (reason_ret)
4572 *reason_ret = reason;
4169 return status; 4573 return status;
4170} 4574}
4171 4575
@@ -4192,7 +4596,7 @@ static void ocfs2_make_right_split_rec(struct super_block *sb,
4192static int ocfs2_split_and_insert(struct inode *inode, 4596static int ocfs2_split_and_insert(struct inode *inode,
4193 handle_t *handle, 4597 handle_t *handle,
4194 struct ocfs2_path *path, 4598 struct ocfs2_path *path,
4195 struct buffer_head *di_bh, 4599 struct ocfs2_extent_tree *et,
4196 struct buffer_head **last_eb_bh, 4600 struct buffer_head **last_eb_bh,
4197 int split_index, 4601 int split_index,
4198 struct ocfs2_extent_rec *orig_split_rec, 4602 struct ocfs2_extent_rec *orig_split_rec,
@@ -4206,7 +4610,6 @@ static int ocfs2_split_and_insert(struct inode *inode,
4206 struct ocfs2_extent_rec split_rec = *orig_split_rec; 4610 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4207 struct ocfs2_insert_type insert; 4611 struct ocfs2_insert_type insert;
4208 struct ocfs2_extent_block *eb; 4612 struct ocfs2_extent_block *eb;
4209 struct ocfs2_dinode *di;
4210 4613
4211leftright: 4614leftright:
4212 /* 4615 /*
@@ -4215,8 +4618,7 @@ leftright:
4215 */ 4618 */
4216 rec = path_leaf_el(path)->l_recs[split_index]; 4619 rec = path_leaf_el(path)->l_recs[split_index];
4217 4620
4218 di = (struct ocfs2_dinode *)di_bh->b_data; 4621 rightmost_el = et->et_root_el;
4219 rightmost_el = &di->id2.i_list;
4220 4622
4221 depth = le16_to_cpu(rightmost_el->l_tree_depth); 4623 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4222 if (depth) { 4624 if (depth) {
@@ -4227,8 +4629,8 @@ leftright:
4227 4629
4228 if (le16_to_cpu(rightmost_el->l_next_free_rec) == 4630 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4229 le16_to_cpu(rightmost_el->l_count)) { 4631 le16_to_cpu(rightmost_el->l_count)) {
4230 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh, 4632 ret = ocfs2_grow_tree(inode, handle, et,
4231 meta_ac); 4633 &depth, last_eb_bh, meta_ac);
4232 if (ret) { 4634 if (ret) {
4233 mlog_errno(ret); 4635 mlog_errno(ret);
4234 goto out; 4636 goto out;
@@ -4265,8 +4667,7 @@ leftright:
4265 do_leftright = 1; 4667 do_leftright = 1;
4266 } 4668 }
4267 4669
4268 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, 4670 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4269 &insert);
4270 if (ret) { 4671 if (ret) {
4271 mlog_errno(ret); 4672 mlog_errno(ret);
4272 goto out; 4673 goto out;
@@ -4308,8 +4709,9 @@ out:
4308 * of the tree is required. All other cases will degrade into a less 4709 * of the tree is required. All other cases will degrade into a less
4309 * optimal tree layout. 4710 * optimal tree layout.
4310 * 4711 *
4311 * last_eb_bh should be the rightmost leaf block for any inode with a 4712 * last_eb_bh should be the rightmost leaf block for any extent
4312 * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call. 4713 * btree. Since a split may grow the tree or a merge might shrink it,
4714 * the caller cannot trust the contents of that buffer after this call.
4313 * 4715 *
4314 * This code is optimized for readability - several passes might be 4716 * This code is optimized for readability - several passes might be
4315 * made over certain portions of the tree. All of those blocks will 4717 * made over certain portions of the tree. All of those blocks will
@@ -4317,7 +4719,7 @@ out:
4317 * extra overhead is not expressed in terms of disk reads. 4719 * extra overhead is not expressed in terms of disk reads.
4318 */ 4720 */
4319static int __ocfs2_mark_extent_written(struct inode *inode, 4721static int __ocfs2_mark_extent_written(struct inode *inode,
4320 struct buffer_head *di_bh, 4722 struct ocfs2_extent_tree *et,
4321 handle_t *handle, 4723 handle_t *handle,
4322 struct ocfs2_path *path, 4724 struct ocfs2_path *path,
4323 int split_index, 4725 int split_index,
@@ -4357,11 +4759,9 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
4357 */ 4759 */
4358 if (path->p_tree_depth) { 4760 if (path->p_tree_depth) {
4359 struct ocfs2_extent_block *eb; 4761 struct ocfs2_extent_block *eb;
4360 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4361 4762
4362 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 4763 ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et),
4363 le64_to_cpu(di->i_last_eb_blk), 4764 &last_eb_bh);
4364 &last_eb_bh, OCFS2_BH_CACHED, inode);
4365 if (ret) { 4765 if (ret) {
4366 mlog_exit(ret); 4766 mlog_exit(ret);
4367 goto out; 4767 goto out;
@@ -4394,7 +4794,7 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
4394 if (ctxt.c_split_covers_rec) 4794 if (ctxt.c_split_covers_rec)
4395 el->l_recs[split_index] = *split_rec; 4795 el->l_recs[split_index] = *split_rec;
4396 else 4796 else
4397 ret = ocfs2_split_and_insert(inode, handle, path, di_bh, 4797 ret = ocfs2_split_and_insert(inode, handle, path, et,
4398 &last_eb_bh, split_index, 4798 &last_eb_bh, split_index,
4399 split_rec, meta_ac); 4799 split_rec, meta_ac);
4400 if (ret) 4800 if (ret)
@@ -4402,7 +4802,7 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
4402 } else { 4802 } else {
4403 ret = ocfs2_try_to_merge_extent(inode, handle, path, 4803 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4404 split_index, split_rec, 4804 split_index, split_rec,
4405 dealloc, &ctxt); 4805 dealloc, &ctxt, et);
4406 if (ret) 4806 if (ret)
4407 mlog_errno(ret); 4807 mlog_errno(ret);
4408 } 4808 }
@@ -4420,7 +4820,8 @@ out:
4420 * 4820 *
4421 * The caller is responsible for passing down meta_ac if we'll need it. 4821 * The caller is responsible for passing down meta_ac if we'll need it.
4422 */ 4822 */
4423int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, 4823int ocfs2_mark_extent_written(struct inode *inode,
4824 struct ocfs2_extent_tree *et,
4424 handle_t *handle, u32 cpos, u32 len, u32 phys, 4825 handle_t *handle, u32 cpos, u32 len, u32 phys,
4425 struct ocfs2_alloc_context *meta_ac, 4826 struct ocfs2_alloc_context *meta_ac,
4426 struct ocfs2_cached_dealloc_ctxt *dealloc) 4827 struct ocfs2_cached_dealloc_ctxt *dealloc)
@@ -4446,10 +4847,14 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4446 /* 4847 /*
4447 * XXX: This should be fixed up so that we just re-insert the 4848 * XXX: This should be fixed up so that we just re-insert the
4448 * next extent records. 4849 * next extent records.
4850 *
4851 * XXX: This is a hack on the extent tree, maybe it should be
4852 * an op?
4449 */ 4853 */
4450 ocfs2_extent_map_trunc(inode, 0); 4854 if (et->et_ops == &ocfs2_dinode_et_ops)
4855 ocfs2_extent_map_trunc(inode, 0);
4451 4856
4452 left_path = ocfs2_new_inode_path(di_bh); 4857 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4453 if (!left_path) { 4858 if (!left_path) {
4454 ret = -ENOMEM; 4859 ret = -ENOMEM;
4455 mlog_errno(ret); 4860 mlog_errno(ret);
@@ -4480,8 +4885,9 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4480 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; 4885 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4481 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; 4886 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4482 4887
4483 ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path, 4888 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4484 index, &split_rec, meta_ac, dealloc); 4889 index, &split_rec, meta_ac,
4890 dealloc);
4485 if (ret) 4891 if (ret)
4486 mlog_errno(ret); 4892 mlog_errno(ret);
4487 4893
@@ -4490,13 +4896,12 @@ out:
4490 return ret; 4896 return ret;
4491} 4897}
4492 4898
4493static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, 4899static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
4494 handle_t *handle, struct ocfs2_path *path, 4900 handle_t *handle, struct ocfs2_path *path,
4495 int index, u32 new_range, 4901 int index, u32 new_range,
4496 struct ocfs2_alloc_context *meta_ac) 4902 struct ocfs2_alloc_context *meta_ac)
4497{ 4903{
4498 int ret, depth, credits = handle->h_buffer_credits; 4904 int ret, depth, credits = handle->h_buffer_credits;
4499 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4500 struct buffer_head *last_eb_bh = NULL; 4905 struct buffer_head *last_eb_bh = NULL;
4501 struct ocfs2_extent_block *eb; 4906 struct ocfs2_extent_block *eb;
4502 struct ocfs2_extent_list *rightmost_el, *el; 4907 struct ocfs2_extent_list *rightmost_el, *el;
@@ -4513,9 +4918,8 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4513 4918
4514 depth = path->p_tree_depth; 4919 depth = path->p_tree_depth;
4515 if (depth > 0) { 4920 if (depth > 0) {
4516 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 4921 ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et),
4517 le64_to_cpu(di->i_last_eb_blk), 4922 &last_eb_bh);
4518 &last_eb_bh, OCFS2_BH_CACHED, inode);
4519 if (ret < 0) { 4923 if (ret < 0) {
4520 mlog_errno(ret); 4924 mlog_errno(ret);
4521 goto out; 4925 goto out;
@@ -4526,7 +4930,8 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4526 } else 4930 } else
4527 rightmost_el = path_leaf_el(path); 4931 rightmost_el = path_leaf_el(path);
4528 4932
4529 credits += path->p_tree_depth + ocfs2_extend_meta_needed(di); 4933 credits += path->p_tree_depth +
4934 ocfs2_extend_meta_needed(et->et_root_el);
4530 ret = ocfs2_extend_trans(handle, credits); 4935 ret = ocfs2_extend_trans(handle, credits);
4531 if (ret) { 4936 if (ret) {
4532 mlog_errno(ret); 4937 mlog_errno(ret);
@@ -4535,7 +4940,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4535 4940
4536 if (le16_to_cpu(rightmost_el->l_next_free_rec) == 4941 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4537 le16_to_cpu(rightmost_el->l_count)) { 4942 le16_to_cpu(rightmost_el->l_count)) {
4538 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh, 4943 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
4539 meta_ac); 4944 meta_ac);
4540 if (ret) { 4945 if (ret) {
4541 mlog_errno(ret); 4946 mlog_errno(ret);
@@ -4549,7 +4954,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4549 insert.ins_split = SPLIT_RIGHT; 4954 insert.ins_split = SPLIT_RIGHT;
4550 insert.ins_tree_depth = depth; 4955 insert.ins_tree_depth = depth;
4551 4956
4552 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert); 4957 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4553 if (ret) 4958 if (ret)
4554 mlog_errno(ret); 4959 mlog_errno(ret);
4555 4960
@@ -4561,7 +4966,8 @@ out:
4561static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, 4966static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4562 struct ocfs2_path *path, int index, 4967 struct ocfs2_path *path, int index,
4563 struct ocfs2_cached_dealloc_ctxt *dealloc, 4968 struct ocfs2_cached_dealloc_ctxt *dealloc,
4564 u32 cpos, u32 len) 4969 u32 cpos, u32 len,
4970 struct ocfs2_extent_tree *et)
4565{ 4971{
4566 int ret; 4972 int ret;
4567 u32 left_cpos, rec_range, trunc_range; 4973 u32 left_cpos, rec_range, trunc_range;
@@ -4573,7 +4979,7 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4573 struct ocfs2_extent_block *eb; 4979 struct ocfs2_extent_block *eb;
4574 4980
4575 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { 4981 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
4576 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); 4982 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
4577 if (ret) { 4983 if (ret) {
4578 mlog_errno(ret); 4984 mlog_errno(ret);
4579 goto out; 4985 goto out;
@@ -4704,7 +5110,7 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4704 5110
4705 ocfs2_journal_dirty(handle, path_leaf_bh(path)); 5111 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4706 5112
4707 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); 5113 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
4708 if (ret) { 5114 if (ret) {
4709 mlog_errno(ret); 5115 mlog_errno(ret);
4710 goto out; 5116 goto out;
@@ -4715,7 +5121,8 @@ out:
4715 return ret; 5121 return ret;
4716} 5122}
4717 5123
4718int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, 5124int ocfs2_remove_extent(struct inode *inode,
5125 struct ocfs2_extent_tree *et,
4719 u32 cpos, u32 len, handle_t *handle, 5126 u32 cpos, u32 len, handle_t *handle,
4720 struct ocfs2_alloc_context *meta_ac, 5127 struct ocfs2_alloc_context *meta_ac,
4721 struct ocfs2_cached_dealloc_ctxt *dealloc) 5128 struct ocfs2_cached_dealloc_ctxt *dealloc)
@@ -4724,11 +5131,11 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4724 u32 rec_range, trunc_range; 5131 u32 rec_range, trunc_range;
4725 struct ocfs2_extent_rec *rec; 5132 struct ocfs2_extent_rec *rec;
4726 struct ocfs2_extent_list *el; 5133 struct ocfs2_extent_list *el;
4727 struct ocfs2_path *path; 5134 struct ocfs2_path *path = NULL;
4728 5135
4729 ocfs2_extent_map_trunc(inode, 0); 5136 ocfs2_extent_map_trunc(inode, 0);
4730 5137
4731 path = ocfs2_new_inode_path(di_bh); 5138 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
4732 if (!path) { 5139 if (!path) {
4733 ret = -ENOMEM; 5140 ret = -ENOMEM;
4734 mlog_errno(ret); 5141 mlog_errno(ret);
@@ -4781,13 +5188,13 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4781 5188
4782 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { 5189 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
4783 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, 5190 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4784 cpos, len); 5191 cpos, len, et);
4785 if (ret) { 5192 if (ret) {
4786 mlog_errno(ret); 5193 mlog_errno(ret);
4787 goto out; 5194 goto out;
4788 } 5195 }
4789 } else { 5196 } else {
4790 ret = ocfs2_split_tree(inode, di_bh, handle, path, index, 5197 ret = ocfs2_split_tree(inode, et, handle, path, index,
4791 trunc_range, meta_ac); 5198 trunc_range, meta_ac);
4792 if (ret) { 5199 if (ret) {
4793 mlog_errno(ret); 5200 mlog_errno(ret);
@@ -4836,7 +5243,7 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4836 } 5243 }
4837 5244
4838 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, 5245 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4839 cpos, len); 5246 cpos, len, et);
4840 if (ret) { 5247 if (ret) {
4841 mlog_errno(ret); 5248 mlog_errno(ret);
4842 goto out; 5249 goto out;
@@ -5179,8 +5586,7 @@ static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5179 goto bail; 5586 goto bail;
5180 } 5587 }
5181 5588
5182 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, 5589 status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &bh);
5183 OCFS2_BH_CACHED, inode);
5184 if (status < 0) { 5590 if (status < 0) {
5185 iput(inode); 5591 iput(inode);
5186 mlog_errno(status); 5592 mlog_errno(status);
@@ -5255,8 +5661,7 @@ int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5255bail: 5661bail:
5256 if (tl_inode) 5662 if (tl_inode)
5257 iput(tl_inode); 5663 iput(tl_inode);
5258 if (tl_bh) 5664 brelse(tl_bh);
5259 brelse(tl_bh);
5260 5665
5261 if (status < 0 && (*tl_copy)) { 5666 if (status < 0 && (*tl_copy)) {
5262 kfree(*tl_copy); 5667 kfree(*tl_copy);
@@ -5999,20 +6404,13 @@ bail:
5999 return status; 6404 return status;
6000} 6405}
6001 6406
6002static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh) 6407static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6003{ 6408{
6004 set_buffer_uptodate(bh); 6409 set_buffer_uptodate(bh);
6005 mark_buffer_dirty(bh); 6410 mark_buffer_dirty(bh);
6006 return 0; 6411 return 0;
6007} 6412}
6008 6413
6009static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6010{
6011 set_buffer_uptodate(bh);
6012 mark_buffer_dirty(bh);
6013 return ocfs2_journal_dirty_data(handle, bh);
6014}
6015
6016static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, 6414static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6017 unsigned int from, unsigned int to, 6415 unsigned int from, unsigned int to,
6018 struct page *page, int zero, u64 *phys) 6416 struct page *page, int zero, u64 *phys)
@@ -6031,17 +6429,18 @@ static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6031 * here if they aren't - ocfs2_map_page_blocks() 6429 * here if they aren't - ocfs2_map_page_blocks()
6032 * might've skipped some 6430 * might've skipped some
6033 */ 6431 */
6034 if (ocfs2_should_order_data(inode)) { 6432 ret = walk_page_buffers(handle, page_buffers(page),
6035 ret = walk_page_buffers(handle, 6433 from, to, &partial,
6036 page_buffers(page), 6434 ocfs2_zero_func);
6037 from, to, &partial, 6435 if (ret < 0)
6038 ocfs2_ordered_zero_func); 6436 mlog_errno(ret);
6039 if (ret < 0) 6437 else if (ocfs2_should_order_data(inode)) {
6040 mlog_errno(ret); 6438 ret = ocfs2_jbd2_file_inode(handle, inode);
6041 } else { 6439#ifdef CONFIG_OCFS2_COMPAT_JBD
6042 ret = walk_page_buffers(handle, page_buffers(page), 6440 ret = walk_page_buffers(handle, page_buffers(page),
6043 from, to, &partial, 6441 from, to, &partial,
6044 ocfs2_writeback_zero_func); 6442 ocfs2_journal_dirty_data);
6443#endif
6045 if (ret < 0) 6444 if (ret < 0)
6046 mlog_errno(ret); 6445 mlog_errno(ret);
6047 } 6446 }
@@ -6206,20 +6605,29 @@ out:
6206 return ret; 6605 return ret;
6207} 6606}
6208 6607
6209static void ocfs2_zero_dinode_id2(struct inode *inode, struct ocfs2_dinode *di) 6608static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6609 struct ocfs2_dinode *di)
6210{ 6610{
6211 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; 6611 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6612 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6212 6613
6213 memset(&di->id2, 0, blocksize - offsetof(struct ocfs2_dinode, id2)); 6614 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6615 memset(&di->id2, 0, blocksize -
6616 offsetof(struct ocfs2_dinode, id2) -
6617 xattrsize);
6618 else
6619 memset(&di->id2, 0, blocksize -
6620 offsetof(struct ocfs2_dinode, id2));
6214} 6621}
6215 6622
6216void ocfs2_dinode_new_extent_list(struct inode *inode, 6623void ocfs2_dinode_new_extent_list(struct inode *inode,
6217 struct ocfs2_dinode *di) 6624 struct ocfs2_dinode *di)
6218{ 6625{
6219 ocfs2_zero_dinode_id2(inode, di); 6626 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6220 di->id2.i_list.l_tree_depth = 0; 6627 di->id2.i_list.l_tree_depth = 0;
6221 di->id2.i_list.l_next_free_rec = 0; 6628 di->id2.i_list.l_next_free_rec = 0;
6222 di->id2.i_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(inode->i_sb)); 6629 di->id2.i_list.l_count = cpu_to_le16(
6630 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6223} 6631}
6224 6632
6225void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) 6633void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
@@ -6236,9 +6644,10 @@ void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6236 * We clear the entire i_data structure here so that all 6644 * We clear the entire i_data structure here so that all
6237 * fields can be properly initialized. 6645 * fields can be properly initialized.
6238 */ 6646 */
6239 ocfs2_zero_dinode_id2(inode, di); 6647 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6240 6648
6241 idata->id_count = cpu_to_le16(ocfs2_max_inline_data(inode->i_sb)); 6649 idata->id_count = cpu_to_le16(
6650 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6242} 6651}
6243 6652
6244int ocfs2_convert_inline_data_to_extents(struct inode *inode, 6653int ocfs2_convert_inline_data_to_extents(struct inode *inode,
@@ -6253,6 +6662,7 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6253 struct ocfs2_alloc_context *data_ac = NULL; 6662 struct ocfs2_alloc_context *data_ac = NULL;
6254 struct page **pages = NULL; 6663 struct page **pages = NULL;
6255 loff_t end = osb->s_clustersize; 6664 loff_t end = osb->s_clustersize;
6665 struct ocfs2_extent_tree et;
6256 6666
6257 has_data = i_size_read(inode) ? 1 : 0; 6667 has_data = i_size_read(inode) ? 1 : 0;
6258 6668
@@ -6352,7 +6762,8 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6352 * this proves to be false, we could always re-build 6762 * this proves to be false, we could always re-build
6353 * the in-inode data from our pages. 6763 * the in-inode data from our pages.
6354 */ 6764 */
6355 ret = ocfs2_insert_extent(osb, handle, inode, di_bh, 6765 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
6766 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6356 0, block, 1, 0, NULL); 6767 0, block, 1, 0, NULL);
6357 if (ret) { 6768 if (ret) {
6358 mlog_errno(ret); 6769 mlog_errno(ret);
@@ -6395,13 +6806,14 @@ int ocfs2_commit_truncate(struct ocfs2_super *osb,
6395 handle_t *handle = NULL; 6806 handle_t *handle = NULL;
6396 struct inode *tl_inode = osb->osb_tl_inode; 6807 struct inode *tl_inode = osb->osb_tl_inode;
6397 struct ocfs2_path *path = NULL; 6808 struct ocfs2_path *path = NULL;
6809 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6398 6810
6399 mlog_entry_void(); 6811 mlog_entry_void();
6400 6812
6401 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, 6813 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6402 i_size_read(inode)); 6814 i_size_read(inode));
6403 6815
6404 path = ocfs2_new_inode_path(fe_bh); 6816 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
6405 if (!path) { 6817 if (!path) {
6406 status = -ENOMEM; 6818 status = -ENOMEM;
6407 mlog_errno(status); 6819 mlog_errno(status);
@@ -6572,8 +6984,8 @@ int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6572 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); 6984 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
6573 6985
6574 if (fe->id2.i_list.l_tree_depth) { 6986 if (fe->id2.i_list.l_tree_depth) {
6575 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), 6987 status = ocfs2_read_block(inode, le64_to_cpu(fe->i_last_eb_blk),
6576 &last_eb_bh, OCFS2_BH_CACHED, inode); 6988 &last_eb_bh);
6577 if (status < 0) { 6989 if (status < 0) {
6578 mlog_errno(status); 6990 mlog_errno(status);
6579 goto bail; 6991 goto bail;
@@ -6686,8 +7098,7 @@ static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
6686 mlog(ML_NOTICE, 7098 mlog(ML_NOTICE,
6687 "Truncate completion has non-empty dealloc context\n"); 7099 "Truncate completion has non-empty dealloc context\n");
6688 7100
6689 if (tc->tc_last_eb_bh) 7101 brelse(tc->tc_last_eb_bh);
6690 brelse(tc->tc_last_eb_bh);
6691 7102
6692 kfree(tc); 7103 kfree(tc);
6693} 7104}
diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h
index 60cd3d59230c..70257c84cfbe 100644
--- a/fs/ocfs2/alloc.h
+++ b/fs/ocfs2/alloc.h
@@ -26,30 +26,102 @@
26#ifndef OCFS2_ALLOC_H 26#ifndef OCFS2_ALLOC_H
27#define OCFS2_ALLOC_H 27#define OCFS2_ALLOC_H
28 28
29
30/*
31 * For xattr tree leaf, we limit the leaf byte size to be 64K.
32 */
33#define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
34
35/*
36 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
37 * the b-tree operations in ocfs2. Now all the b-tree operations are not
38 * limited to ocfs2_dinode only. Any data which need to allocate clusters
39 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
40 * and operation.
41 *
42 * ocfs2_extent_tree becomes the first-class object for extent tree
43 * manipulation. Callers of the alloc.c code need to fill it via one of
44 * the ocfs2_init_*_extent_tree() operations below.
45 *
46 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
47 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
48 * functions.
49 * ocfs2_extent_tree_operations abstract the normal operations we do for
50 * the root of extent b-tree.
51 */
52struct ocfs2_extent_tree_operations;
53struct ocfs2_extent_tree {
54 struct ocfs2_extent_tree_operations *et_ops;
55 struct buffer_head *et_root_bh;
56 struct ocfs2_extent_list *et_root_el;
57 void *et_object;
58 unsigned int et_max_leaf_clusters;
59};
60
61/*
62 * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the
63 * specified object buffer.
64 */
65void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
66 struct inode *inode,
67 struct buffer_head *bh);
68void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
69 struct inode *inode,
70 struct buffer_head *bh);
71void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
72 struct inode *inode,
73 struct buffer_head *bh,
74 struct ocfs2_xattr_value_root *xv);
75
29struct ocfs2_alloc_context; 76struct ocfs2_alloc_context;
30int ocfs2_insert_extent(struct ocfs2_super *osb, 77int ocfs2_insert_extent(struct ocfs2_super *osb,
31 handle_t *handle, 78 handle_t *handle,
32 struct inode *inode, 79 struct inode *inode,
33 struct buffer_head *fe_bh, 80 struct ocfs2_extent_tree *et,
34 u32 cpos, 81 u32 cpos,
35 u64 start_blk, 82 u64 start_blk,
36 u32 new_clusters, 83 u32 new_clusters,
37 u8 flags, 84 u8 flags,
38 struct ocfs2_alloc_context *meta_ac); 85 struct ocfs2_alloc_context *meta_ac);
86
87enum ocfs2_alloc_restarted {
88 RESTART_NONE = 0,
89 RESTART_TRANS,
90 RESTART_META
91};
92int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
93 struct inode *inode,
94 u32 *logical_offset,
95 u32 clusters_to_add,
96 int mark_unwritten,
97 struct ocfs2_extent_tree *et,
98 handle_t *handle,
99 struct ocfs2_alloc_context *data_ac,
100 struct ocfs2_alloc_context *meta_ac,
101 enum ocfs2_alloc_restarted *reason_ret);
39struct ocfs2_cached_dealloc_ctxt; 102struct ocfs2_cached_dealloc_ctxt;
40int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, 103int ocfs2_mark_extent_written(struct inode *inode,
104 struct ocfs2_extent_tree *et,
41 handle_t *handle, u32 cpos, u32 len, u32 phys, 105 handle_t *handle, u32 cpos, u32 len, u32 phys,
42 struct ocfs2_alloc_context *meta_ac, 106 struct ocfs2_alloc_context *meta_ac,
43 struct ocfs2_cached_dealloc_ctxt *dealloc); 107 struct ocfs2_cached_dealloc_ctxt *dealloc);
44int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, 108int ocfs2_remove_extent(struct inode *inode,
109 struct ocfs2_extent_tree *et,
45 u32 cpos, u32 len, handle_t *handle, 110 u32 cpos, u32 len, handle_t *handle,
46 struct ocfs2_alloc_context *meta_ac, 111 struct ocfs2_alloc_context *meta_ac,
47 struct ocfs2_cached_dealloc_ctxt *dealloc); 112 struct ocfs2_cached_dealloc_ctxt *dealloc);
48int ocfs2_num_free_extents(struct ocfs2_super *osb, 113int ocfs2_num_free_extents(struct ocfs2_super *osb,
49 struct inode *inode, 114 struct inode *inode,
50 struct ocfs2_dinode *fe); 115 struct ocfs2_extent_tree *et);
51/* how many new metadata chunks would an allocation need at maximum? */ 116
52static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe) 117/*
118 * how many new metadata chunks would an allocation need at maximum?
119 *
120 * Please note that the caller must make sure that root_el is the root
121 * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
122 * the result may be wrong.
123 */
124static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
53{ 125{
54 /* 126 /*
55 * Rather than do all the work of determining how much we need 127 * Rather than do all the work of determining how much we need
@@ -59,7 +131,7 @@ static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe)
59 * new tree_depth==0 extent_block, and one block at the new 131 * new tree_depth==0 extent_block, and one block at the new
60 * top-of-the tree. 132 * top-of-the tree.
61 */ 133 */
62 return le16_to_cpu(fe->id2.i_list.l_tree_depth) + 2; 134 return le16_to_cpu(root_el->l_tree_depth) + 2;
63} 135}
64 136
65void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di); 137void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index a53da1466277..c22543b33420 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -68,9 +68,7 @@ static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
68 goto bail; 68 goto bail;
69 } 69 }
70 70
71 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), 71 status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &bh);
72 OCFS2_I(inode)->ip_blkno,
73 &bh, OCFS2_BH_CACHED, inode);
74 if (status < 0) { 72 if (status < 0) {
75 mlog_errno(status); 73 mlog_errno(status);
76 goto bail; 74 goto bail;
@@ -128,8 +126,7 @@ static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
128 err = 0; 126 err = 0;
129 127
130bail: 128bail:
131 if (bh) 129 brelse(bh);
132 brelse(bh);
133 130
134 mlog_exit(err); 131 mlog_exit(err);
135 return err; 132 return err;
@@ -261,13 +258,11 @@ static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
261{ 258{
262 int ret; 259 int ret;
263 struct buffer_head *di_bh = NULL; 260 struct buffer_head *di_bh = NULL;
264 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
265 261
266 BUG_ON(!PageLocked(page)); 262 BUG_ON(!PageLocked(page));
267 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)); 263 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
268 264
269 ret = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &di_bh, 265 ret = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &di_bh);
270 OCFS2_BH_CACHED, inode);
271 if (ret) { 266 if (ret) {
272 mlog_errno(ret); 267 mlog_errno(ret);
273 goto out; 268 goto out;
@@ -485,11 +480,14 @@ handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
485 } 480 }
486 481
487 if (ocfs2_should_order_data(inode)) { 482 if (ocfs2_should_order_data(inode)) {
483 ret = ocfs2_jbd2_file_inode(handle, inode);
484#ifdef CONFIG_OCFS2_COMPAT_JBD
488 ret = walk_page_buffers(handle, 485 ret = walk_page_buffers(handle,
489 page_buffers(page), 486 page_buffers(page),
490 from, to, NULL, 487 from, to, NULL,
491 ocfs2_journal_dirty_data); 488 ocfs2_journal_dirty_data);
492 if (ret < 0) 489#endif
490 if (ret < 0)
493 mlog_errno(ret); 491 mlog_errno(ret);
494 } 492 }
495out: 493out:
@@ -669,7 +667,7 @@ static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
669{ 667{
670 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; 668 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
671 669
672 journal_invalidatepage(journal, page, offset); 670 jbd2_journal_invalidatepage(journal, page, offset);
673} 671}
674 672
675static int ocfs2_releasepage(struct page *page, gfp_t wait) 673static int ocfs2_releasepage(struct page *page, gfp_t wait)
@@ -678,7 +676,7 @@ static int ocfs2_releasepage(struct page *page, gfp_t wait)
678 676
679 if (!page_has_buffers(page)) 677 if (!page_has_buffers(page))
680 return 0; 678 return 0;
681 return journal_try_to_free_buffers(journal, page, wait); 679 return jbd2_journal_try_to_free_buffers(journal, page, wait);
682} 680}
683 681
684static ssize_t ocfs2_direct_IO(int rw, 682static ssize_t ocfs2_direct_IO(int rw,
@@ -1074,11 +1072,15 @@ static void ocfs2_write_failure(struct inode *inode,
1074 tmppage = wc->w_pages[i]; 1072 tmppage = wc->w_pages[i];
1075 1073
1076 if (page_has_buffers(tmppage)) { 1074 if (page_has_buffers(tmppage)) {
1077 if (ocfs2_should_order_data(inode)) 1075 if (ocfs2_should_order_data(inode)) {
1076 ocfs2_jbd2_file_inode(wc->w_handle, inode);
1077#ifdef CONFIG_OCFS2_COMPAT_JBD
1078 walk_page_buffers(wc->w_handle, 1078 walk_page_buffers(wc->w_handle,
1079 page_buffers(tmppage), 1079 page_buffers(tmppage),
1080 from, to, NULL, 1080 from, to, NULL,
1081 ocfs2_journal_dirty_data); 1081 ocfs2_journal_dirty_data);
1082#endif
1083 }
1082 1084
1083 block_commit_write(tmppage, from, to); 1085 block_commit_write(tmppage, from, to);
1084 } 1086 }
@@ -1242,6 +1244,7 @@ static int ocfs2_write_cluster(struct address_space *mapping,
1242 int ret, i, new, should_zero = 0; 1244 int ret, i, new, should_zero = 0;
1243 u64 v_blkno, p_blkno; 1245 u64 v_blkno, p_blkno;
1244 struct inode *inode = mapping->host; 1246 struct inode *inode = mapping->host;
1247 struct ocfs2_extent_tree et;
1245 1248
1246 new = phys == 0 ? 1 : 0; 1249 new = phys == 0 ? 1 : 0;
1247 if (new || unwritten) 1250 if (new || unwritten)
@@ -1255,10 +1258,10 @@ static int ocfs2_write_cluster(struct address_space *mapping,
1255 * any additional semaphores or cluster locks. 1258 * any additional semaphores or cluster locks.
1256 */ 1259 */
1257 tmp_pos = cpos; 1260 tmp_pos = cpos;
1258 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode, 1261 ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
1259 &tmp_pos, 1, 0, wc->w_di_bh, 1262 &tmp_pos, 1, 0, wc->w_di_bh,
1260 wc->w_handle, data_ac, 1263 wc->w_handle, data_ac,
1261 meta_ac, NULL); 1264 meta_ac, NULL);
1262 /* 1265 /*
1263 * This shouldn't happen because we must have already 1266 * This shouldn't happen because we must have already
1264 * calculated the correct meta data allocation required. The 1267 * calculated the correct meta data allocation required. The
@@ -1276,7 +1279,8 @@ static int ocfs2_write_cluster(struct address_space *mapping,
1276 goto out; 1279 goto out;
1277 } 1280 }
1278 } else if (unwritten) { 1281 } else if (unwritten) {
1279 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh, 1282 ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
1283 ret = ocfs2_mark_extent_written(inode, &et,
1280 wc->w_handle, cpos, 1, phys, 1284 wc->w_handle, cpos, 1, phys,
1281 meta_ac, &wc->w_dealloc); 1285 meta_ac, &wc->w_dealloc);
1282 if (ret < 0) { 1286 if (ret < 0) {
@@ -1665,6 +1669,7 @@ int ocfs2_write_begin_nolock(struct address_space *mapping,
1665 struct ocfs2_alloc_context *data_ac = NULL; 1669 struct ocfs2_alloc_context *data_ac = NULL;
1666 struct ocfs2_alloc_context *meta_ac = NULL; 1670 struct ocfs2_alloc_context *meta_ac = NULL;
1667 handle_t *handle; 1671 handle_t *handle;
1672 struct ocfs2_extent_tree et;
1668 1673
1669 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh); 1674 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1670 if (ret) { 1675 if (ret) {
@@ -1712,14 +1717,23 @@ int ocfs2_write_begin_nolock(struct address_space *mapping,
1712 * ocfs2_lock_allocators(). It greatly over-estimates 1717 * ocfs2_lock_allocators(). It greatly over-estimates
1713 * the work to be done. 1718 * the work to be done.
1714 */ 1719 */
1715 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc, 1720 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u,"
1716 extents_to_split, &data_ac, &meta_ac); 1721 " clusters_to_add = %u, extents_to_split = %u\n",
1722 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1723 (long long)i_size_read(inode), le32_to_cpu(di->i_clusters),
1724 clusters_to_alloc, extents_to_split);
1725
1726 ocfs2_init_dinode_extent_tree(&et, inode, wc->w_di_bh);
1727 ret = ocfs2_lock_allocators(inode, &et,
1728 clusters_to_alloc, extents_to_split,
1729 &data_ac, &meta_ac);
1717 if (ret) { 1730 if (ret) {
1718 mlog_errno(ret); 1731 mlog_errno(ret);
1719 goto out; 1732 goto out;
1720 } 1733 }
1721 1734
1722 credits = ocfs2_calc_extend_credits(inode->i_sb, di, 1735 credits = ocfs2_calc_extend_credits(inode->i_sb,
1736 &di->id2.i_list,
1723 clusters_to_alloc); 1737 clusters_to_alloc);
1724 1738
1725 } 1739 }
@@ -1905,11 +1919,15 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
1905 } 1919 }
1906 1920
1907 if (page_has_buffers(tmppage)) { 1921 if (page_has_buffers(tmppage)) {
1908 if (ocfs2_should_order_data(inode)) 1922 if (ocfs2_should_order_data(inode)) {
1923 ocfs2_jbd2_file_inode(wc->w_handle, inode);
1924#ifdef CONFIG_OCFS2_COMPAT_JBD
1909 walk_page_buffers(wc->w_handle, 1925 walk_page_buffers(wc->w_handle,
1910 page_buffers(tmppage), 1926 page_buffers(tmppage),
1911 from, to, NULL, 1927 from, to, NULL,
1912 ocfs2_journal_dirty_data); 1928 ocfs2_journal_dirty_data);
1929#endif
1930 }
1913 block_commit_write(tmppage, from, to); 1931 block_commit_write(tmppage, from, to);
1914 } 1932 }
1915 } 1933 }
diff --git a/fs/ocfs2/buffer_head_io.c b/fs/ocfs2/buffer_head_io.c
index f136639f5b41..7e947c672469 100644
--- a/fs/ocfs2/buffer_head_io.c
+++ b/fs/ocfs2/buffer_head_io.c
@@ -66,7 +66,7 @@ int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
66 /* remove from dirty list before I/O. */ 66 /* remove from dirty list before I/O. */
67 clear_buffer_dirty(bh); 67 clear_buffer_dirty(bh);
68 68
69 get_bh(bh); /* for end_buffer_write_sync() */ 69 get_bh(bh); /* for end_buffer_write_sync() */
70 bh->b_end_io = end_buffer_write_sync; 70 bh->b_end_io = end_buffer_write_sync;
71 submit_bh(WRITE, bh); 71 submit_bh(WRITE, bh);
72 72
@@ -88,22 +88,103 @@ out:
88 return ret; 88 return ret;
89} 89}
90 90
91int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr, 91int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
92 struct buffer_head *bhs[], int flags, 92 unsigned int nr, struct buffer_head *bhs[])
93 struct inode *inode) 93{
94 int status = 0;
95 unsigned int i;
96 struct buffer_head *bh;
97
98 if (!nr) {
99 mlog(ML_BH_IO, "No buffers will be read!\n");
100 goto bail;
101 }
102
103 for (i = 0 ; i < nr ; i++) {
104 if (bhs[i] == NULL) {
105 bhs[i] = sb_getblk(osb->sb, block++);
106 if (bhs[i] == NULL) {
107 status = -EIO;
108 mlog_errno(status);
109 goto bail;
110 }
111 }
112 bh = bhs[i];
113
114 if (buffer_jbd(bh)) {
115 mlog(ML_ERROR,
116 "trying to sync read a jbd "
117 "managed bh (blocknr = %llu), skipping\n",
118 (unsigned long long)bh->b_blocknr);
119 continue;
120 }
121
122 if (buffer_dirty(bh)) {
123 /* This should probably be a BUG, or
124 * at least return an error. */
125 mlog(ML_ERROR,
126 "trying to sync read a dirty "
127 "buffer! (blocknr = %llu), skipping\n",
128 (unsigned long long)bh->b_blocknr);
129 continue;
130 }
131
132 lock_buffer(bh);
133 if (buffer_jbd(bh)) {
134 mlog(ML_ERROR,
135 "block %llu had the JBD bit set "
136 "while I was in lock_buffer!",
137 (unsigned long long)bh->b_blocknr);
138 BUG();
139 }
140
141 clear_buffer_uptodate(bh);
142 get_bh(bh); /* for end_buffer_read_sync() */
143 bh->b_end_io = end_buffer_read_sync;
144 submit_bh(READ, bh);
145 }
146
147 for (i = nr; i > 0; i--) {
148 bh = bhs[i - 1];
149
150 if (buffer_jbd(bh)) {
151 mlog(ML_ERROR,
152 "the journal got the buffer while it was "
153 "locked for io! (blocknr = %llu)\n",
154 (unsigned long long)bh->b_blocknr);
155 BUG();
156 }
157
158 wait_on_buffer(bh);
159 if (!buffer_uptodate(bh)) {
160 /* Status won't be cleared from here on out,
161 * so we can safely record this and loop back
162 * to cleanup the other buffers. */
163 status = -EIO;
164 put_bh(bh);
165 bhs[i - 1] = NULL;
166 }
167 }
168
169bail:
170 return status;
171}
172
173int ocfs2_read_blocks(struct inode *inode, u64 block, int nr,
174 struct buffer_head *bhs[], int flags)
94{ 175{
95 int status = 0; 176 int status = 0;
96 struct super_block *sb;
97 int i, ignore_cache = 0; 177 int i, ignore_cache = 0;
98 struct buffer_head *bh; 178 struct buffer_head *bh;
99 179
100 mlog_entry("(block=(%llu), nr=(%d), flags=%d, inode=%p)\n", 180 mlog_entry("(inode=%p, block=(%llu), nr=(%d), flags=%d)\n",
101 (unsigned long long)block, nr, flags, inode); 181 inode, (unsigned long long)block, nr, flags);
102 182
183 BUG_ON(!inode);
103 BUG_ON((flags & OCFS2_BH_READAHEAD) && 184 BUG_ON((flags & OCFS2_BH_READAHEAD) &&
104 (!inode || !(flags & OCFS2_BH_CACHED))); 185 (flags & OCFS2_BH_IGNORE_CACHE));
105 186
106 if (osb == NULL || osb->sb == NULL || bhs == NULL) { 187 if (bhs == NULL) {
107 status = -EINVAL; 188 status = -EINVAL;
108 mlog_errno(status); 189 mlog_errno(status);
109 goto bail; 190 goto bail;
@@ -122,26 +203,19 @@ int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
122 goto bail; 203 goto bail;
123 } 204 }
124 205
125 sb = osb->sb; 206 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
126
127 if (flags & OCFS2_BH_CACHED && !inode)
128 flags &= ~OCFS2_BH_CACHED;
129
130 if (inode)
131 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
132 for (i = 0 ; i < nr ; i++) { 207 for (i = 0 ; i < nr ; i++) {
133 if (bhs[i] == NULL) { 208 if (bhs[i] == NULL) {
134 bhs[i] = sb_getblk(sb, block++); 209 bhs[i] = sb_getblk(inode->i_sb, block++);
135 if (bhs[i] == NULL) { 210 if (bhs[i] == NULL) {
136 if (inode) 211 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
137 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
138 status = -EIO; 212 status = -EIO;
139 mlog_errno(status); 213 mlog_errno(status);
140 goto bail; 214 goto bail;
141 } 215 }
142 } 216 }
143 bh = bhs[i]; 217 bh = bhs[i];
144 ignore_cache = 0; 218 ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
145 219
146 /* There are three read-ahead cases here which we need to 220 /* There are three read-ahead cases here which we need to
147 * be concerned with. All three assume a buffer has 221 * be concerned with. All three assume a buffer has
@@ -167,26 +241,27 @@ int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
167 * before our is-it-in-flight check. 241 * before our is-it-in-flight check.
168 */ 242 */
169 243
170 if (flags & OCFS2_BH_CACHED && 244 if (!ignore_cache && !ocfs2_buffer_uptodate(inode, bh)) {
171 !ocfs2_buffer_uptodate(inode, bh)) {
172 mlog(ML_UPTODATE, 245 mlog(ML_UPTODATE,
173 "bh (%llu), inode %llu not uptodate\n", 246 "bh (%llu), inode %llu not uptodate\n",
174 (unsigned long long)bh->b_blocknr, 247 (unsigned long long)bh->b_blocknr,
175 (unsigned long long)OCFS2_I(inode)->ip_blkno); 248 (unsigned long long)OCFS2_I(inode)->ip_blkno);
249 /* We're using ignore_cache here to say
250 * "go to disk" */
176 ignore_cache = 1; 251 ignore_cache = 1;
177 } 252 }
178 253
179 /* XXX: Can we ever get this and *not* have the cached 254 /* XXX: Can we ever get this and *not* have the cached
180 * flag set? */ 255 * flag set? */
181 if (buffer_jbd(bh)) { 256 if (buffer_jbd(bh)) {
182 if (!(flags & OCFS2_BH_CACHED) || ignore_cache) 257 if (ignore_cache)
183 mlog(ML_BH_IO, "trying to sync read a jbd " 258 mlog(ML_BH_IO, "trying to sync read a jbd "
184 "managed bh (blocknr = %llu)\n", 259 "managed bh (blocknr = %llu)\n",
185 (unsigned long long)bh->b_blocknr); 260 (unsigned long long)bh->b_blocknr);
186 continue; 261 continue;
187 } 262 }
188 263
189 if (!(flags & OCFS2_BH_CACHED) || ignore_cache) { 264 if (ignore_cache) {
190 if (buffer_dirty(bh)) { 265 if (buffer_dirty(bh)) {
191 /* This should probably be a BUG, or 266 /* This should probably be a BUG, or
192 * at least return an error. */ 267 * at least return an error. */
@@ -221,7 +296,7 @@ int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
221 * previously read-ahead buffer may have 296 * previously read-ahead buffer may have
222 * completed I/O while we were waiting for the 297 * completed I/O while we were waiting for the
223 * buffer lock. */ 298 * buffer lock. */
224 if ((flags & OCFS2_BH_CACHED) 299 if (!(flags & OCFS2_BH_IGNORE_CACHE)
225 && !(flags & OCFS2_BH_READAHEAD) 300 && !(flags & OCFS2_BH_READAHEAD)
226 && ocfs2_buffer_uptodate(inode, bh)) { 301 && ocfs2_buffer_uptodate(inode, bh)) {
227 unlock_buffer(bh); 302 unlock_buffer(bh);
@@ -265,15 +340,14 @@ int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
265 /* Always set the buffer in the cache, even if it was 340 /* Always set the buffer in the cache, even if it was
266 * a forced read, or read-ahead which hasn't yet 341 * a forced read, or read-ahead which hasn't yet
267 * completed. */ 342 * completed. */
268 if (inode) 343 ocfs2_set_buffer_uptodate(inode, bh);
269 ocfs2_set_buffer_uptodate(inode, bh);
270 } 344 }
271 if (inode) 345 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
272 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
273 346
274 mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n", 347 mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n",
275 (unsigned long long)block, nr, 348 (unsigned long long)block, nr,
276 (!(flags & OCFS2_BH_CACHED) || ignore_cache) ? "no" : "yes", flags); 349 ((flags & OCFS2_BH_IGNORE_CACHE) || ignore_cache) ? "no" : "yes",
350 flags);
277 351
278bail: 352bail:
279 353
diff --git a/fs/ocfs2/buffer_head_io.h b/fs/ocfs2/buffer_head_io.h
index c2e78614c3e5..75e1dcb1ade7 100644
--- a/fs/ocfs2/buffer_head_io.h
+++ b/fs/ocfs2/buffer_head_io.h
@@ -31,31 +31,29 @@
31void ocfs2_end_buffer_io_sync(struct buffer_head *bh, 31void ocfs2_end_buffer_io_sync(struct buffer_head *bh,
32 int uptodate); 32 int uptodate);
33 33
34static inline int ocfs2_read_block(struct ocfs2_super *osb, 34static inline int ocfs2_read_block(struct inode *inode,
35 u64 off, 35 u64 off,
36 struct buffer_head **bh, 36 struct buffer_head **bh);
37 int flags,
38 struct inode *inode);
39 37
40int ocfs2_write_block(struct ocfs2_super *osb, 38int ocfs2_write_block(struct ocfs2_super *osb,
41 struct buffer_head *bh, 39 struct buffer_head *bh,
42 struct inode *inode); 40 struct inode *inode);
43int ocfs2_read_blocks(struct ocfs2_super *osb, 41int ocfs2_read_blocks(struct inode *inode,
44 u64 block, 42 u64 block,
45 int nr, 43 int nr,
46 struct buffer_head *bhs[], 44 struct buffer_head *bhs[],
47 int flags, 45 int flags);
48 struct inode *inode); 46int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
47 unsigned int nr, struct buffer_head *bhs[]);
49 48
50int ocfs2_write_super_or_backup(struct ocfs2_super *osb, 49int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
51 struct buffer_head *bh); 50 struct buffer_head *bh);
52 51
53#define OCFS2_BH_CACHED 1 52#define OCFS2_BH_IGNORE_CACHE 1
54#define OCFS2_BH_READAHEAD 8 53#define OCFS2_BH_READAHEAD 8
55 54
56static inline int ocfs2_read_block(struct ocfs2_super * osb, u64 off, 55static inline int ocfs2_read_block(struct inode *inode, u64 off,
57 struct buffer_head **bh, int flags, 56 struct buffer_head **bh)
58 struct inode *inode)
59{ 57{
60 int status = 0; 58 int status = 0;
61 59
@@ -65,8 +63,7 @@ static inline int ocfs2_read_block(struct ocfs2_super * osb, u64 off,
65 goto bail; 63 goto bail;
66 } 64 }
67 65
68 status = ocfs2_read_blocks(osb, off, 1, bh, 66 status = ocfs2_read_blocks(inode, off, 1, bh, 0);
69 flags, inode);
70 67
71bail: 68bail:
72 return status; 69 return status;
diff --git a/fs/ocfs2/cluster/masklog.c b/fs/ocfs2/cluster/masklog.c
index 23c732f27529..d8a0cb92cef6 100644
--- a/fs/ocfs2/cluster/masklog.c
+++ b/fs/ocfs2/cluster/masklog.c
@@ -109,6 +109,7 @@ static struct mlog_attribute mlog_attrs[MLOG_MAX_BITS] = {
109 define_mask(CONN), 109 define_mask(CONN),
110 define_mask(QUORUM), 110 define_mask(QUORUM),
111 define_mask(EXPORT), 111 define_mask(EXPORT),
112 define_mask(XATTR),
112 define_mask(ERROR), 113 define_mask(ERROR),
113 define_mask(NOTICE), 114 define_mask(NOTICE),
114 define_mask(KTHREAD), 115 define_mask(KTHREAD),
diff --git a/fs/ocfs2/cluster/masklog.h b/fs/ocfs2/cluster/masklog.h
index 597e064bb94f..57670c680471 100644
--- a/fs/ocfs2/cluster/masklog.h
+++ b/fs/ocfs2/cluster/masklog.h
@@ -112,6 +112,7 @@
112#define ML_CONN 0x0000000004000000ULL /* net connection management */ 112#define ML_CONN 0x0000000004000000ULL /* net connection management */
113#define ML_QUORUM 0x0000000008000000ULL /* net connection quorum */ 113#define ML_QUORUM 0x0000000008000000ULL /* net connection quorum */
114#define ML_EXPORT 0x0000000010000000ULL /* ocfs2 export operations */ 114#define ML_EXPORT 0x0000000010000000ULL /* ocfs2 export operations */
115#define ML_XATTR 0x0000000020000000ULL /* ocfs2 extended attributes */
115/* bits that are infrequently given and frequently matched in the high word */ 116/* bits that are infrequently given and frequently matched in the high word */
116#define ML_ERROR 0x0000000100000000ULL /* sent to KERN_ERR */ 117#define ML_ERROR 0x0000000100000000ULL /* sent to KERN_ERR */
117#define ML_NOTICE 0x0000000200000000ULL /* setn to KERN_NOTICE */ 118#define ML_NOTICE 0x0000000200000000ULL /* setn to KERN_NOTICE */
diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c
index 9cce563fd627..026e6eb85187 100644
--- a/fs/ocfs2/dir.c
+++ b/fs/ocfs2/dir.c
@@ -82,6 +82,49 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
82 struct ocfs2_alloc_context *meta_ac, 82 struct ocfs2_alloc_context *meta_ac,
83 struct buffer_head **new_bh); 83 struct buffer_head **new_bh);
84 84
85static struct buffer_head *ocfs2_bread(struct inode *inode,
86 int block, int *err, int reada)
87{
88 struct buffer_head *bh = NULL;
89 int tmperr;
90 u64 p_blkno;
91 int readflags = 0;
92
93 if (reada)
94 readflags |= OCFS2_BH_READAHEAD;
95
96 if (((u64)block << inode->i_sb->s_blocksize_bits) >=
97 i_size_read(inode)) {
98 BUG_ON(!reada);
99 return NULL;
100 }
101
102 down_read(&OCFS2_I(inode)->ip_alloc_sem);
103 tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
104 NULL);
105 up_read(&OCFS2_I(inode)->ip_alloc_sem);
106 if (tmperr < 0) {
107 mlog_errno(tmperr);
108 goto fail;
109 }
110
111 tmperr = ocfs2_read_blocks(inode, p_blkno, 1, &bh, readflags);
112 if (tmperr < 0)
113 goto fail;
114
115 tmperr = 0;
116
117 *err = 0;
118 return bh;
119
120fail:
121 brelse(bh);
122 bh = NULL;
123
124 *err = -EIO;
125 return NULL;
126}
127
85/* 128/*
86 * bh passed here can be an inode block or a dir data block, depending 129 * bh passed here can be an inode block or a dir data block, depending
87 * on the inode inline data flag. 130 * on the inode inline data flag.
@@ -188,8 +231,7 @@ static struct buffer_head *ocfs2_find_entry_id(const char *name,
188 struct ocfs2_dinode *di; 231 struct ocfs2_dinode *di;
189 struct ocfs2_inline_data *data; 232 struct ocfs2_inline_data *data;
190 233
191 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno, 234 ret = ocfs2_read_block(dir, OCFS2_I(dir)->ip_blkno, &di_bh);
192 &di_bh, OCFS2_BH_CACHED, dir);
193 if (ret) { 235 if (ret) {
194 mlog_errno(ret); 236 mlog_errno(ret);
195 goto out; 237 goto out;
@@ -260,14 +302,13 @@ restart:
260 } 302 }
261 if ((bh = bh_use[ra_ptr++]) == NULL) 303 if ((bh = bh_use[ra_ptr++]) == NULL)
262 goto next; 304 goto next;
263 wait_on_buffer(bh); 305 if (ocfs2_read_block(dir, block, &bh)) {
264 if (!buffer_uptodate(bh)) { 306 /* read error, skip block & hope for the best.
265 /* read error, skip block & hope for the best */ 307 * ocfs2_read_block() has released the bh. */
266 ocfs2_error(dir->i_sb, "reading directory %llu, " 308 ocfs2_error(dir->i_sb, "reading directory %llu, "
267 "offset %lu\n", 309 "offset %lu\n",
268 (unsigned long long)OCFS2_I(dir)->ip_blkno, 310 (unsigned long long)OCFS2_I(dir)->ip_blkno,
269 block); 311 block);
270 brelse(bh);
271 goto next; 312 goto next;
272 } 313 }
273 i = ocfs2_search_dirblock(bh, dir, name, namelen, 314 i = ocfs2_search_dirblock(bh, dir, name, namelen,
@@ -417,8 +458,7 @@ static inline int ocfs2_delete_entry_id(handle_t *handle,
417 struct ocfs2_dinode *di; 458 struct ocfs2_dinode *di;
418 struct ocfs2_inline_data *data; 459 struct ocfs2_inline_data *data;
419 460
420 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno, 461 ret = ocfs2_read_block(dir, OCFS2_I(dir)->ip_blkno, &di_bh);
421 &di_bh, OCFS2_BH_CACHED, dir);
422 if (ret) { 462 if (ret) {
423 mlog_errno(ret); 463 mlog_errno(ret);
424 goto out; 464 goto out;
@@ -596,8 +636,7 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
596 struct ocfs2_inline_data *data; 636 struct ocfs2_inline_data *data;
597 struct ocfs2_dir_entry *de; 637 struct ocfs2_dir_entry *de;
598 638
599 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno, 639 ret = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &di_bh);
600 &di_bh, OCFS2_BH_CACHED, inode);
601 if (ret) { 640 if (ret) {
602 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", 641 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
603 (unsigned long long)OCFS2_I(inode)->ip_blkno); 642 (unsigned long long)OCFS2_I(inode)->ip_blkno);
@@ -716,8 +755,7 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
716 for (i = ra_sectors >> (sb->s_blocksize_bits - 9); 755 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
717 i > 0; i--) { 756 i > 0; i--) {
718 tmp = ocfs2_bread(inode, ++blk, &err, 1); 757 tmp = ocfs2_bread(inode, ++blk, &err, 1);
719 if (tmp) 758 brelse(tmp);
720 brelse(tmp);
721 } 759 }
722 last_ra_blk = blk; 760 last_ra_blk = blk;
723 ra_sectors = 8; 761 ra_sectors = 8;
@@ -899,10 +937,8 @@ int ocfs2_find_files_on_disk(const char *name,
899leave: 937leave:
900 if (status < 0) { 938 if (status < 0) {
901 *dirent = NULL; 939 *dirent = NULL;
902 if (*dirent_bh) { 940 brelse(*dirent_bh);
903 brelse(*dirent_bh); 941 *dirent_bh = NULL;
904 *dirent_bh = NULL;
905 }
906 } 942 }
907 943
908 mlog_exit(status); 944 mlog_exit(status);
@@ -951,8 +987,7 @@ int ocfs2_check_dir_for_entry(struct inode *dir,
951 987
952 ret = 0; 988 ret = 0;
953bail: 989bail:
954 if (dirent_bh) 990 brelse(dirent_bh);
955 brelse(dirent_bh);
956 991
957 mlog_exit(ret); 992 mlog_exit(ret);
958 return ret; 993 return ret;
@@ -1127,8 +1162,7 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1127 1162
1128 status = 0; 1163 status = 0;
1129bail: 1164bail:
1130 if (new_bh) 1165 brelse(new_bh);
1131 brelse(new_bh);
1132 1166
1133 mlog_exit(status); 1167 mlog_exit(status);
1134 return status; 1168 return status;
@@ -1192,6 +1226,9 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1192 struct buffer_head *dirdata_bh = NULL; 1226 struct buffer_head *dirdata_bh = NULL;
1193 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 1227 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1194 handle_t *handle; 1228 handle_t *handle;
1229 struct ocfs2_extent_tree et;
1230
1231 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1195 1232
1196 alloc = ocfs2_clusters_for_bytes(sb, bytes); 1233 alloc = ocfs2_clusters_for_bytes(sb, bytes);
1197 1234
@@ -1305,8 +1342,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1305 * This should never fail as our extent list is empty and all 1342 * This should never fail as our extent list is empty and all
1306 * related blocks have been journaled already. 1343 * related blocks have been journaled already.
1307 */ 1344 */
1308 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0, 1345 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1309 NULL); 1346 0, NULL);
1310 if (ret) { 1347 if (ret) {
1311 mlog_errno(ret); 1348 mlog_errno(ret);
1312 goto out_commit; 1349 goto out_commit;
@@ -1337,8 +1374,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1337 } 1374 }
1338 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); 1375 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1339 1376
1340 ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 1, blkno, 1377 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1341 len, 0, NULL); 1378 blkno, len, 0, NULL);
1342 if (ret) { 1379 if (ret) {
1343 mlog_errno(ret); 1380 mlog_errno(ret);
1344 goto out_commit; 1381 goto out_commit;
@@ -1383,9 +1420,9 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
1383 if (extend) { 1420 if (extend) {
1384 u32 offset = OCFS2_I(dir)->ip_clusters; 1421 u32 offset = OCFS2_I(dir)->ip_clusters;
1385 1422
1386 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset, 1423 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1387 1, 0, parent_fe_bh, handle, 1424 1, 0, parent_fe_bh, handle,
1388 data_ac, meta_ac, NULL); 1425 data_ac, meta_ac, NULL);
1389 BUG_ON(status == -EAGAIN); 1426 BUG_ON(status == -EAGAIN);
1390 if (status < 0) { 1427 if (status < 0) {
1391 mlog_errno(status); 1428 mlog_errno(status);
@@ -1430,12 +1467,14 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
1430 int credits, num_free_extents, drop_alloc_sem = 0; 1467 int credits, num_free_extents, drop_alloc_sem = 0;
1431 loff_t dir_i_size; 1468 loff_t dir_i_size;
1432 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; 1469 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1470 struct ocfs2_extent_list *el = &fe->id2.i_list;
1433 struct ocfs2_alloc_context *data_ac = NULL; 1471 struct ocfs2_alloc_context *data_ac = NULL;
1434 struct ocfs2_alloc_context *meta_ac = NULL; 1472 struct ocfs2_alloc_context *meta_ac = NULL;
1435 handle_t *handle = NULL; 1473 handle_t *handle = NULL;
1436 struct buffer_head *new_bh = NULL; 1474 struct buffer_head *new_bh = NULL;
1437 struct ocfs2_dir_entry * de; 1475 struct ocfs2_dir_entry * de;
1438 struct super_block *sb = osb->sb; 1476 struct super_block *sb = osb->sb;
1477 struct ocfs2_extent_tree et;
1439 1478
1440 mlog_entry_void(); 1479 mlog_entry_void();
1441 1480
@@ -1479,7 +1518,8 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
1479 spin_lock(&OCFS2_I(dir)->ip_lock); 1518 spin_lock(&OCFS2_I(dir)->ip_lock);
1480 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { 1519 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1481 spin_unlock(&OCFS2_I(dir)->ip_lock); 1520 spin_unlock(&OCFS2_I(dir)->ip_lock);
1482 num_free_extents = ocfs2_num_free_extents(osb, dir, fe); 1521 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1522 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1483 if (num_free_extents < 0) { 1523 if (num_free_extents < 0) {
1484 status = num_free_extents; 1524 status = num_free_extents;
1485 mlog_errno(status); 1525 mlog_errno(status);
@@ -1487,7 +1527,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
1487 } 1527 }
1488 1528
1489 if (!num_free_extents) { 1529 if (!num_free_extents) {
1490 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); 1530 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1491 if (status < 0) { 1531 if (status < 0) {
1492 if (status != -ENOSPC) 1532 if (status != -ENOSPC)
1493 mlog_errno(status); 1533 mlog_errno(status);
@@ -1502,7 +1542,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
1502 goto bail; 1542 goto bail;
1503 } 1543 }
1504 1544
1505 credits = ocfs2_calc_extend_credits(sb, fe, 1); 1545 credits = ocfs2_calc_extend_credits(sb, el, 1);
1506 } else { 1546 } else {
1507 spin_unlock(&OCFS2_I(dir)->ip_lock); 1547 spin_unlock(&OCFS2_I(dir)->ip_lock);
1508 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; 1548 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
@@ -1568,8 +1608,7 @@ bail:
1568 if (meta_ac) 1608 if (meta_ac)
1569 ocfs2_free_alloc_context(meta_ac); 1609 ocfs2_free_alloc_context(meta_ac);
1570 1610
1571 if (new_bh) 1611 brelse(new_bh);
1572 brelse(new_bh);
1573 1612
1574 mlog_exit(status); 1613 mlog_exit(status);
1575 return status; 1614 return status;
@@ -1696,8 +1735,7 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1696 1735
1697 status = 0; 1736 status = 0;
1698bail: 1737bail:
1699 if (bh) 1738 brelse(bh);
1700 brelse(bh);
1701 1739
1702 mlog_exit(status); 1740 mlog_exit(status);
1703 return status; 1741 return status;
@@ -1756,7 +1794,6 @@ int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1756 *ret_de_bh = bh; 1794 *ret_de_bh = bh;
1757 bh = NULL; 1795 bh = NULL;
1758out: 1796out:
1759 if (bh) 1797 brelse(bh);
1760 brelse(bh);
1761 return ret; 1798 return ret;
1762} 1799}
diff --git a/fs/ocfs2/dlmglue.c b/fs/ocfs2/dlmglue.c
index eae3d643a5e4..ec684426034b 100644
--- a/fs/ocfs2/dlmglue.c
+++ b/fs/ocfs2/dlmglue.c
@@ -2024,8 +2024,7 @@ static int ocfs2_inode_lock_update(struct inode *inode,
2024 } else { 2024 } else {
2025 /* Boo, we have to go to disk. */ 2025 /* Boo, we have to go to disk. */
2026 /* read bh, cast, ocfs2_refresh_inode */ 2026 /* read bh, cast, ocfs2_refresh_inode */
2027 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno, 2027 status = ocfs2_read_block(inode, oi->ip_blkno, bh);
2028 bh, OCFS2_BH_CACHED, inode);
2029 if (status < 0) { 2028 if (status < 0) {
2030 mlog_errno(status); 2029 mlog_errno(status);
2031 goto bail_refresh; 2030 goto bail_refresh;
@@ -2086,11 +2085,7 @@ static int ocfs2_assign_bh(struct inode *inode,
2086 return 0; 2085 return 0;
2087 } 2086 }
2088 2087
2089 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), 2088 status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, ret_bh);
2090 OCFS2_I(inode)->ip_blkno,
2091 ret_bh,
2092 OCFS2_BH_CACHED,
2093 inode);
2094 if (status < 0) 2089 if (status < 0)
2095 mlog_errno(status); 2090 mlog_errno(status);
2096 2091
diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c
index aed268e80b49..2baedac58234 100644
--- a/fs/ocfs2/extent_map.c
+++ b/fs/ocfs2/extent_map.c
@@ -293,8 +293,7 @@ static int ocfs2_last_eb_is_empty(struct inode *inode,
293 struct ocfs2_extent_block *eb; 293 struct ocfs2_extent_block *eb;
294 struct ocfs2_extent_list *el; 294 struct ocfs2_extent_list *el;
295 295
296 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), last_eb_blk, 296 ret = ocfs2_read_block(inode, last_eb_blk, &eb_bh);
297 &eb_bh, OCFS2_BH_CACHED, inode);
298 if (ret) { 297 if (ret) {
299 mlog_errno(ret); 298 mlog_errno(ret);
300 goto out; 299 goto out;
@@ -382,9 +381,9 @@ static int ocfs2_figure_hole_clusters(struct inode *inode,
382 if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL) 381 if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
383 goto no_more_extents; 382 goto no_more_extents;
384 383
385 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 384 ret = ocfs2_read_block(inode,
386 le64_to_cpu(eb->h_next_leaf_blk), 385 le64_to_cpu(eb->h_next_leaf_blk),
387 &next_eb_bh, OCFS2_BH_CACHED, inode); 386 &next_eb_bh);
388 if (ret) { 387 if (ret) {
389 mlog_errno(ret); 388 mlog_errno(ret);
390 goto out; 389 goto out;
@@ -551,6 +550,66 @@ static void ocfs2_relative_extent_offsets(struct super_block *sb,
551 *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff; 550 *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
552} 551}
553 552
553int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
554 u32 *p_cluster, u32 *num_clusters,
555 struct ocfs2_extent_list *el)
556{
557 int ret = 0, i;
558 struct buffer_head *eb_bh = NULL;
559 struct ocfs2_extent_block *eb;
560 struct ocfs2_extent_rec *rec;
561 u32 coff;
562
563 if (el->l_tree_depth) {
564 ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
565 if (ret) {
566 mlog_errno(ret);
567 goto out;
568 }
569
570 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
571 el = &eb->h_list;
572
573 if (el->l_tree_depth) {
574 ocfs2_error(inode->i_sb,
575 "Inode %lu has non zero tree depth in "
576 "xattr leaf block %llu\n", inode->i_ino,
577 (unsigned long long)eb_bh->b_blocknr);
578 ret = -EROFS;
579 goto out;
580 }
581 }
582
583 i = ocfs2_search_extent_list(el, v_cluster);
584 if (i == -1) {
585 ret = -EROFS;
586 mlog_errno(ret);
587 goto out;
588 } else {
589 rec = &el->l_recs[i];
590 BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
591
592 if (!rec->e_blkno) {
593 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
594 "record (%u, %u, 0) in xattr", inode->i_ino,
595 le32_to_cpu(rec->e_cpos),
596 ocfs2_rec_clusters(el, rec));
597 ret = -EROFS;
598 goto out;
599 }
600 coff = v_cluster - le32_to_cpu(rec->e_cpos);
601 *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
602 le64_to_cpu(rec->e_blkno));
603 *p_cluster = *p_cluster + coff;
604 if (num_clusters)
605 *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
606 }
607out:
608 if (eb_bh)
609 brelse(eb_bh);
610 return ret;
611}
612
554int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, 613int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
555 u32 *p_cluster, u32 *num_clusters, 614 u32 *p_cluster, u32 *num_clusters,
556 unsigned int *extent_flags) 615 unsigned int *extent_flags)
@@ -571,8 +630,7 @@ int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
571 if (ret == 0) 630 if (ret == 0)
572 goto out; 631 goto out;
573 632
574 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno, 633 ret = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &di_bh);
575 &di_bh, OCFS2_BH_CACHED, inode);
576 if (ret) { 634 if (ret) {
577 mlog_errno(ret); 635 mlog_errno(ret);
578 goto out; 636 goto out;
diff --git a/fs/ocfs2/extent_map.h b/fs/ocfs2/extent_map.h
index 1b97490e1ea8..1c4aa8b06f34 100644
--- a/fs/ocfs2/extent_map.h
+++ b/fs/ocfs2/extent_map.h
@@ -53,4 +53,8 @@ int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
53int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 53int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
54 u64 map_start, u64 map_len); 54 u64 map_start, u64 map_len);
55 55
56int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
57 u32 *p_cluster, u32 *num_clusters,
58 struct ocfs2_extent_list *el);
59
56#endif /* _EXTENT_MAP_H */ 60#endif /* _EXTENT_MAP_H */
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c
index ed38796052d2..8d3225a78073 100644
--- a/fs/ocfs2/file.c
+++ b/fs/ocfs2/file.c
@@ -55,6 +55,7 @@
55#include "mmap.h" 55#include "mmap.h"
56#include "suballoc.h" 56#include "suballoc.h"
57#include "super.h" 57#include "super.h"
58#include "xattr.h"
58 59
59#include "buffer_head_io.h" 60#include "buffer_head_io.h"
60 61
@@ -184,7 +185,7 @@ static int ocfs2_sync_file(struct file *file,
184 goto bail; 185 goto bail;
185 186
186 journal = osb->journal->j_journal; 187 journal = osb->journal->j_journal;
187 err = journal_force_commit(journal); 188 err = jbd2_journal_force_commit(journal);
188 189
189bail: 190bail:
190 mlog_exit(err); 191 mlog_exit(err);
@@ -488,7 +489,7 @@ bail:
488} 489}
489 490
490/* 491/*
491 * extend allocation only here. 492 * extend file allocation only here.
492 * we'll update all the disk stuff, and oip->alloc_size 493 * we'll update all the disk stuff, and oip->alloc_size
493 * 494 *
494 * expect stuff to be locked, a transaction started and enough data / 495 * expect stuff to be locked, a transaction started and enough data /
@@ -497,189 +498,25 @@ bail:
497 * Will return -EAGAIN, and a reason if a restart is needed. 498 * Will return -EAGAIN, and a reason if a restart is needed.
498 * If passed in, *reason will always be set, even in error. 499 * If passed in, *reason will always be set, even in error.
499 */ 500 */
500int ocfs2_do_extend_allocation(struct ocfs2_super *osb, 501int ocfs2_add_inode_data(struct ocfs2_super *osb,
501 struct inode *inode, 502 struct inode *inode,
502 u32 *logical_offset, 503 u32 *logical_offset,
503 u32 clusters_to_add, 504 u32 clusters_to_add,
504 int mark_unwritten, 505 int mark_unwritten,
505 struct buffer_head *fe_bh, 506 struct buffer_head *fe_bh,
506 handle_t *handle, 507 handle_t *handle,
507 struct ocfs2_alloc_context *data_ac, 508 struct ocfs2_alloc_context *data_ac,
508 struct ocfs2_alloc_context *meta_ac, 509 struct ocfs2_alloc_context *meta_ac,
509 enum ocfs2_alloc_restarted *reason_ret) 510 enum ocfs2_alloc_restarted *reason_ret)
510{ 511{
511 int status = 0; 512 int ret;
512 int free_extents; 513 struct ocfs2_extent_tree et;
513 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
514 enum ocfs2_alloc_restarted reason = RESTART_NONE;
515 u32 bit_off, num_bits;
516 u64 block;
517 u8 flags = 0;
518
519 BUG_ON(!clusters_to_add);
520
521 if (mark_unwritten)
522 flags = OCFS2_EXT_UNWRITTEN;
523
524 free_extents = ocfs2_num_free_extents(osb, inode, fe);
525 if (free_extents < 0) {
526 status = free_extents;
527 mlog_errno(status);
528 goto leave;
529 }
530
531 /* there are two cases which could cause us to EAGAIN in the
532 * we-need-more-metadata case:
533 * 1) we haven't reserved *any*
534 * 2) we are so fragmented, we've needed to add metadata too
535 * many times. */
536 if (!free_extents && !meta_ac) {
537 mlog(0, "we haven't reserved any metadata!\n");
538 status = -EAGAIN;
539 reason = RESTART_META;
540 goto leave;
541 } else if ((!free_extents)
542 && (ocfs2_alloc_context_bits_left(meta_ac)
543 < ocfs2_extend_meta_needed(fe))) {
544 mlog(0, "filesystem is really fragmented...\n");
545 status = -EAGAIN;
546 reason = RESTART_META;
547 goto leave;
548 }
549
550 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
551 clusters_to_add, &bit_off, &num_bits);
552 if (status < 0) {
553 if (status != -ENOSPC)
554 mlog_errno(status);
555 goto leave;
556 }
557
558 BUG_ON(num_bits > clusters_to_add);
559
560 /* reserve our write early -- insert_extent may update the inode */
561 status = ocfs2_journal_access(handle, inode, fe_bh,
562 OCFS2_JOURNAL_ACCESS_WRITE);
563 if (status < 0) {
564 mlog_errno(status);
565 goto leave;
566 }
567
568 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
569 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
570 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
571 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
572 *logical_offset, block, num_bits,
573 flags, meta_ac);
574 if (status < 0) {
575 mlog_errno(status);
576 goto leave;
577 }
578
579 status = ocfs2_journal_dirty(handle, fe_bh);
580 if (status < 0) {
581 mlog_errno(status);
582 goto leave;
583 }
584
585 clusters_to_add -= num_bits;
586 *logical_offset += num_bits;
587
588 if (clusters_to_add) {
589 mlog(0, "need to alloc once more, clusters = %u, wanted = "
590 "%u\n", fe->i_clusters, clusters_to_add);
591 status = -EAGAIN;
592 reason = RESTART_TRANS;
593 }
594
595leave:
596 mlog_exit(status);
597 if (reason_ret)
598 *reason_ret = reason;
599 return status;
600}
601
602/*
603 * For a given allocation, determine which allocators will need to be
604 * accessed, and lock them, reserving the appropriate number of bits.
605 *
606 * Sparse file systems call this from ocfs2_write_begin_nolock()
607 * and ocfs2_allocate_unwritten_extents().
608 *
609 * File systems which don't support holes call this from
610 * ocfs2_extend_allocation().
611 */
612int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
613 u32 clusters_to_add, u32 extents_to_split,
614 struct ocfs2_alloc_context **data_ac,
615 struct ocfs2_alloc_context **meta_ac)
616{
617 int ret = 0, num_free_extents;
618 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
619 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
620
621 *meta_ac = NULL;
622 if (data_ac)
623 *data_ac = NULL;
624
625 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
626
627 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
628 "clusters_to_add = %u, extents_to_split = %u\n",
629 (unsigned long long)OCFS2_I(inode)->ip_blkno, (long long)i_size_read(inode),
630 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
631
632 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
633 if (num_free_extents < 0) {
634 ret = num_free_extents;
635 mlog_errno(ret);
636 goto out;
637 }
638
639 /*
640 * Sparse allocation file systems need to be more conservative
641 * with reserving room for expansion - the actual allocation
642 * happens while we've got a journal handle open so re-taking
643 * a cluster lock (because we ran out of room for another
644 * extent) will violate ordering rules.
645 *
646 * Most of the time we'll only be seeing this 1 cluster at a time
647 * anyway.
648 *
649 * Always lock for any unwritten extents - we might want to
650 * add blocks during a split.
651 */
652 if (!num_free_extents ||
653 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
654 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
655 if (ret < 0) {
656 if (ret != -ENOSPC)
657 mlog_errno(ret);
658 goto out;
659 }
660 }
661
662 if (clusters_to_add == 0)
663 goto out;
664
665 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
666 if (ret < 0) {
667 if (ret != -ENOSPC)
668 mlog_errno(ret);
669 goto out;
670 }
671
672out:
673 if (ret) {
674 if (*meta_ac) {
675 ocfs2_free_alloc_context(*meta_ac);
676 *meta_ac = NULL;
677 }
678 514
679 /* 515 ocfs2_init_dinode_extent_tree(&et, inode, fe_bh);
680 * We cannot have an error and a non null *data_ac. 516 ret = ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
681 */ 517 clusters_to_add, mark_unwritten,
682 } 518 &et, handle,
519 data_ac, meta_ac, reason_ret);
683 520
684 return ret; 521 return ret;
685} 522}
@@ -698,6 +535,7 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
698 struct ocfs2_alloc_context *meta_ac = NULL; 535 struct ocfs2_alloc_context *meta_ac = NULL;
699 enum ocfs2_alloc_restarted why; 536 enum ocfs2_alloc_restarted why;
700 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 537 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
538 struct ocfs2_extent_tree et;
701 539
702 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); 540 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
703 541
@@ -707,8 +545,7 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
707 */ 545 */
708 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb)); 546 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
709 547
710 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, 548 status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &bh);
711 OCFS2_BH_CACHED, inode);
712 if (status < 0) { 549 if (status < 0) {
713 mlog_errno(status); 550 mlog_errno(status);
714 goto leave; 551 goto leave;
@@ -724,14 +561,21 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
724restart_all: 561restart_all:
725 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); 562 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
726 563
727 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac, 564 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
728 &meta_ac); 565 "clusters_to_add = %u\n",
566 (unsigned long long)OCFS2_I(inode)->ip_blkno,
567 (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
568 clusters_to_add);
569 ocfs2_init_dinode_extent_tree(&et, inode, bh);
570 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
571 &data_ac, &meta_ac);
729 if (status) { 572 if (status) {
730 mlog_errno(status); 573 mlog_errno(status);
731 goto leave; 574 goto leave;
732 } 575 }
733 576
734 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); 577 credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
578 clusters_to_add);
735 handle = ocfs2_start_trans(osb, credits); 579 handle = ocfs2_start_trans(osb, credits);
736 if (IS_ERR(handle)) { 580 if (IS_ERR(handle)) {
737 status = PTR_ERR(handle); 581 status = PTR_ERR(handle);
@@ -753,16 +597,16 @@ restarted_transaction:
753 597
754 prev_clusters = OCFS2_I(inode)->ip_clusters; 598 prev_clusters = OCFS2_I(inode)->ip_clusters;
755 599
756 status = ocfs2_do_extend_allocation(osb, 600 status = ocfs2_add_inode_data(osb,
757 inode, 601 inode,
758 &logical_start, 602 &logical_start,
759 clusters_to_add, 603 clusters_to_add,
760 mark_unwritten, 604 mark_unwritten,
761 bh, 605 bh,
762 handle, 606 handle,
763 data_ac, 607 data_ac,
764 meta_ac, 608 meta_ac,
765 &why); 609 &why);
766 if ((status < 0) && (status != -EAGAIN)) { 610 if ((status < 0) && (status != -EAGAIN)) {
767 if (status != -ENOSPC) 611 if (status != -ENOSPC)
768 mlog_errno(status); 612 mlog_errno(status);
@@ -789,7 +633,7 @@ restarted_transaction:
789 mlog(0, "restarting transaction.\n"); 633 mlog(0, "restarting transaction.\n");
790 /* TODO: This can be more intelligent. */ 634 /* TODO: This can be more intelligent. */
791 credits = ocfs2_calc_extend_credits(osb->sb, 635 credits = ocfs2_calc_extend_credits(osb->sb,
792 fe, 636 &fe->id2.i_list,
793 clusters_to_add); 637 clusters_to_add);
794 status = ocfs2_extend_trans(handle, credits); 638 status = ocfs2_extend_trans(handle, credits);
795 if (status < 0) { 639 if (status < 0) {
@@ -826,10 +670,8 @@ leave:
826 restart_func = 0; 670 restart_func = 0;
827 goto restart_all; 671 goto restart_all;
828 } 672 }
829 if (bh) { 673 brelse(bh);
830 brelse(bh); 674 bh = NULL;
831 bh = NULL;
832 }
833 675
834 mlog_exit(status); 676 mlog_exit(status);
835 return status; 677 return status;
@@ -1096,9 +938,15 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1096 goto bail_unlock; 938 goto bail_unlock;
1097 } 939 }
1098 940
1099 if (i_size_read(inode) > attr->ia_size) 941 if (i_size_read(inode) > attr->ia_size) {
942 if (ocfs2_should_order_data(inode)) {
943 status = ocfs2_begin_ordered_truncate(inode,
944 attr->ia_size);
945 if (status)
946 goto bail_unlock;
947 }
1100 status = ocfs2_truncate_file(inode, bh, attr->ia_size); 948 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1101 else 949 } else
1102 status = ocfs2_extend_file(inode, bh, attr->ia_size); 950 status = ocfs2_extend_file(inode, bh, attr->ia_size);
1103 if (status < 0) { 951 if (status < 0) {
1104 if (status != -ENOSPC) 952 if (status != -ENOSPC)
@@ -1140,8 +988,7 @@ bail_unlock_rw:
1140 if (size_change) 988 if (size_change)
1141 ocfs2_rw_unlock(inode, 1); 989 ocfs2_rw_unlock(inode, 1);
1142bail: 990bail:
1143 if (bh) 991 brelse(bh);
1144 brelse(bh);
1145 992
1146 mlog_exit(status); 993 mlog_exit(status);
1147 return status; 994 return status;
@@ -1284,8 +1131,7 @@ static int ocfs2_write_remove_suid(struct inode *inode)
1284 struct buffer_head *bh = NULL; 1131 struct buffer_head *bh = NULL;
1285 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1132 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1286 1133
1287 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 1134 ret = ocfs2_read_block(inode, oi->ip_blkno, &bh);
1288 oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1289 if (ret < 0) { 1135 if (ret < 0) {
1290 mlog_errno(ret); 1136 mlog_errno(ret);
1291 goto out; 1137 goto out;
@@ -1311,9 +1157,8 @@ static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1311 struct buffer_head *di_bh = NULL; 1157 struct buffer_head *di_bh = NULL;
1312 1158
1313 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 1159 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1314 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 1160 ret = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno,
1315 OCFS2_I(inode)->ip_blkno, &di_bh, 1161 &di_bh);
1316 OCFS2_BH_CACHED, inode);
1317 if (ret) { 1162 if (ret) {
1318 mlog_errno(ret); 1163 mlog_errno(ret);
1319 goto out; 1164 goto out;
@@ -1394,8 +1239,11 @@ static int __ocfs2_remove_inode_range(struct inode *inode,
1394 handle_t *handle; 1239 handle_t *handle;
1395 struct ocfs2_alloc_context *meta_ac = NULL; 1240 struct ocfs2_alloc_context *meta_ac = NULL;
1396 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 1241 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1242 struct ocfs2_extent_tree et;
1397 1243
1398 ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac); 1244 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
1245
1246 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
1399 if (ret) { 1247 if (ret) {
1400 mlog_errno(ret); 1248 mlog_errno(ret);
1401 return ret; 1249 return ret;
@@ -1425,7 +1273,7 @@ static int __ocfs2_remove_inode_range(struct inode *inode,
1425 goto out; 1273 goto out;
1426 } 1274 }
1427 1275
1428 ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, 1276 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
1429 dealloc); 1277 dealloc);
1430 if (ret) { 1278 if (ret) {
1431 mlog_errno(ret); 1279 mlog_errno(ret);
@@ -2040,7 +1888,7 @@ out_dio:
2040 */ 1888 */
2041 if (old_size != i_size_read(inode) || 1889 if (old_size != i_size_read(inode) ||
2042 old_clusters != OCFS2_I(inode)->ip_clusters) { 1890 old_clusters != OCFS2_I(inode)->ip_clusters) {
2043 ret = journal_force_commit(osb->journal->j_journal); 1891 ret = jbd2_journal_force_commit(osb->journal->j_journal);
2044 if (ret < 0) 1892 if (ret < 0)
2045 written = ret; 1893 written = ret;
2046 } 1894 }
@@ -2227,6 +2075,10 @@ const struct inode_operations ocfs2_file_iops = {
2227 .setattr = ocfs2_setattr, 2075 .setattr = ocfs2_setattr,
2228 .getattr = ocfs2_getattr, 2076 .getattr = ocfs2_getattr,
2229 .permission = ocfs2_permission, 2077 .permission = ocfs2_permission,
2078 .setxattr = generic_setxattr,
2079 .getxattr = generic_getxattr,
2080 .listxattr = ocfs2_listxattr,
2081 .removexattr = generic_removexattr,
2230 .fallocate = ocfs2_fallocate, 2082 .fallocate = ocfs2_fallocate,
2231 .fiemap = ocfs2_fiemap, 2083 .fiemap = ocfs2_fiemap,
2232}; 2084};
@@ -2237,6 +2089,10 @@ const struct inode_operations ocfs2_special_file_iops = {
2237 .permission = ocfs2_permission, 2089 .permission = ocfs2_permission,
2238}; 2090};
2239 2091
2092/*
2093 * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
2094 * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
2095 */
2240const struct file_operations ocfs2_fops = { 2096const struct file_operations ocfs2_fops = {
2241 .llseek = generic_file_llseek, 2097 .llseek = generic_file_llseek,
2242 .read = do_sync_read, 2098 .read = do_sync_read,
@@ -2251,6 +2107,7 @@ const struct file_operations ocfs2_fops = {
2251#ifdef CONFIG_COMPAT 2107#ifdef CONFIG_COMPAT
2252 .compat_ioctl = ocfs2_compat_ioctl, 2108 .compat_ioctl = ocfs2_compat_ioctl,
2253#endif 2109#endif
2110 .lock = ocfs2_lock,
2254 .flock = ocfs2_flock, 2111 .flock = ocfs2_flock,
2255 .splice_read = ocfs2_file_splice_read, 2112 .splice_read = ocfs2_file_splice_read,
2256 .splice_write = ocfs2_file_splice_write, 2113 .splice_write = ocfs2_file_splice_write,
@@ -2267,5 +2124,51 @@ const struct file_operations ocfs2_dops = {
2267#ifdef CONFIG_COMPAT 2124#ifdef CONFIG_COMPAT
2268 .compat_ioctl = ocfs2_compat_ioctl, 2125 .compat_ioctl = ocfs2_compat_ioctl,
2269#endif 2126#endif
2127 .lock = ocfs2_lock,
2128 .flock = ocfs2_flock,
2129};
2130
2131/*
2132 * POSIX-lockless variants of our file_operations.
2133 *
2134 * These will be used if the underlying cluster stack does not support
2135 * posix file locking, if the user passes the "localflocks" mount
2136 * option, or if we have a local-only fs.
2137 *
2138 * ocfs2_flock is in here because all stacks handle UNIX file locks,
2139 * so we still want it in the case of no stack support for
2140 * plocks. Internally, it will do the right thing when asked to ignore
2141 * the cluster.
2142 */
2143const struct file_operations ocfs2_fops_no_plocks = {
2144 .llseek = generic_file_llseek,
2145 .read = do_sync_read,
2146 .write = do_sync_write,
2147 .mmap = ocfs2_mmap,
2148 .fsync = ocfs2_sync_file,
2149 .release = ocfs2_file_release,
2150 .open = ocfs2_file_open,
2151 .aio_read = ocfs2_file_aio_read,
2152 .aio_write = ocfs2_file_aio_write,
2153 .unlocked_ioctl = ocfs2_ioctl,
2154#ifdef CONFIG_COMPAT
2155 .compat_ioctl = ocfs2_compat_ioctl,
2156#endif
2157 .flock = ocfs2_flock,
2158 .splice_read = ocfs2_file_splice_read,
2159 .splice_write = ocfs2_file_splice_write,
2160};
2161
2162const struct file_operations ocfs2_dops_no_plocks = {
2163 .llseek = generic_file_llseek,
2164 .read = generic_read_dir,
2165 .readdir = ocfs2_readdir,
2166 .fsync = ocfs2_sync_file,
2167 .release = ocfs2_dir_release,
2168 .open = ocfs2_dir_open,
2169 .unlocked_ioctl = ocfs2_ioctl,
2170#ifdef CONFIG_COMPAT
2171 .compat_ioctl = ocfs2_compat_ioctl,
2172#endif
2270 .flock = ocfs2_flock, 2173 .flock = ocfs2_flock,
2271}; 2174};
diff --git a/fs/ocfs2/file.h b/fs/ocfs2/file.h
index 1e27b4d017ea..e92382cbca5f 100644
--- a/fs/ocfs2/file.h
+++ b/fs/ocfs2/file.h
@@ -28,9 +28,12 @@
28 28
29extern const struct file_operations ocfs2_fops; 29extern const struct file_operations ocfs2_fops;
30extern const struct file_operations ocfs2_dops; 30extern const struct file_operations ocfs2_dops;
31extern const struct file_operations ocfs2_fops_no_plocks;
32extern const struct file_operations ocfs2_dops_no_plocks;
31extern const struct inode_operations ocfs2_file_iops; 33extern const struct inode_operations ocfs2_file_iops;
32extern const struct inode_operations ocfs2_special_file_iops; 34extern const struct inode_operations ocfs2_special_file_iops;
33struct ocfs2_alloc_context; 35struct ocfs2_alloc_context;
36enum ocfs2_alloc_restarted;
34 37
35struct ocfs2_file_private { 38struct ocfs2_file_private {
36 struct file *fp_file; 39 struct file *fp_file;
@@ -38,27 +41,18 @@ struct ocfs2_file_private {
38 struct ocfs2_lock_res fp_flock; 41 struct ocfs2_lock_res fp_flock;
39}; 42};
40 43
41enum ocfs2_alloc_restarted { 44int ocfs2_add_inode_data(struct ocfs2_super *osb,
42 RESTART_NONE = 0, 45 struct inode *inode,
43 RESTART_TRANS, 46 u32 *logical_offset,
44 RESTART_META 47 u32 clusters_to_add,
45}; 48 int mark_unwritten,
46int ocfs2_do_extend_allocation(struct ocfs2_super *osb, 49 struct buffer_head *fe_bh,
47 struct inode *inode, 50 handle_t *handle,
48 u32 *logical_offset, 51 struct ocfs2_alloc_context *data_ac,
49 u32 clusters_to_add, 52 struct ocfs2_alloc_context *meta_ac,
50 int mark_unwritten, 53 enum ocfs2_alloc_restarted *reason_ret);
51 struct buffer_head *fe_bh,
52 handle_t *handle,
53 struct ocfs2_alloc_context *data_ac,
54 struct ocfs2_alloc_context *meta_ac,
55 enum ocfs2_alloc_restarted *reason_ret);
56int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, 54int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size,
57 u64 zero_to); 55 u64 zero_to);
58int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
59 u32 clusters_to_add, u32 extents_to_split,
60 struct ocfs2_alloc_context **data_ac,
61 struct ocfs2_alloc_context **meta_ac);
62int ocfs2_setattr(struct dentry *dentry, struct iattr *attr); 56int ocfs2_setattr(struct dentry *dentry, struct iattr *attr);
63int ocfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, 57int ocfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
64 struct kstat *stat); 58 struct kstat *stat);
diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c
index 7e9e4c79aec7..4903688f72a9 100644
--- a/fs/ocfs2/inode.c
+++ b/fs/ocfs2/inode.c
@@ -49,6 +49,7 @@
49#include "symlink.h" 49#include "symlink.h"
50#include "sysfile.h" 50#include "sysfile.h"
51#include "uptodate.h" 51#include "uptodate.h"
52#include "xattr.h"
52 53
53#include "buffer_head_io.h" 54#include "buffer_head_io.h"
54 55
@@ -219,6 +220,7 @@ int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
219 struct super_block *sb; 220 struct super_block *sb;
220 struct ocfs2_super *osb; 221 struct ocfs2_super *osb;
221 int status = -EINVAL; 222 int status = -EINVAL;
223 int use_plocks = 1;
222 224
223 mlog_entry("(0x%p, size:%llu)\n", inode, 225 mlog_entry("(0x%p, size:%llu)\n", inode,
224 (unsigned long long)le64_to_cpu(fe->i_size)); 226 (unsigned long long)le64_to_cpu(fe->i_size));
@@ -226,6 +228,10 @@ int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
226 sb = inode->i_sb; 228 sb = inode->i_sb;
227 osb = OCFS2_SB(sb); 229 osb = OCFS2_SB(sb);
228 230
231 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
232 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
233 use_plocks = 0;
234
229 /* this means that read_inode cannot create a superblock inode 235 /* this means that read_inode cannot create a superblock inode
230 * today. change if needed. */ 236 * today. change if needed. */
231 if (!OCFS2_IS_VALID_DINODE(fe) || 237 if (!OCFS2_IS_VALID_DINODE(fe) ||
@@ -295,13 +301,19 @@ int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
295 301
296 switch (inode->i_mode & S_IFMT) { 302 switch (inode->i_mode & S_IFMT) {
297 case S_IFREG: 303 case S_IFREG:
298 inode->i_fop = &ocfs2_fops; 304 if (use_plocks)
305 inode->i_fop = &ocfs2_fops;
306 else
307 inode->i_fop = &ocfs2_fops_no_plocks;
299 inode->i_op = &ocfs2_file_iops; 308 inode->i_op = &ocfs2_file_iops;
300 i_size_write(inode, le64_to_cpu(fe->i_size)); 309 i_size_write(inode, le64_to_cpu(fe->i_size));
301 break; 310 break;
302 case S_IFDIR: 311 case S_IFDIR:
303 inode->i_op = &ocfs2_dir_iops; 312 inode->i_op = &ocfs2_dir_iops;
304 inode->i_fop = &ocfs2_dops; 313 if (use_plocks)
314 inode->i_fop = &ocfs2_dops;
315 else
316 inode->i_fop = &ocfs2_dops_no_plocks;
305 i_size_write(inode, le64_to_cpu(fe->i_size)); 317 i_size_write(inode, le64_to_cpu(fe->i_size));
306 break; 318 break;
307 case S_IFLNK: 319 case S_IFLNK:
@@ -448,8 +460,11 @@ static int ocfs2_read_locked_inode(struct inode *inode,
448 } 460 }
449 } 461 }
450 462
451 status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0, 463 if (can_lock)
452 can_lock ? inode : NULL); 464 status = ocfs2_read_blocks(inode, args->fi_blkno, 1, &bh,
465 OCFS2_BH_IGNORE_CACHE);
466 else
467 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
453 if (status < 0) { 468 if (status < 0) {
454 mlog_errno(status); 469 mlog_errno(status);
455 goto bail; 470 goto bail;
@@ -522,6 +537,9 @@ static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
522 * data and fast symlinks. 537 * data and fast symlinks.
523 */ 538 */
524 if (fe->i_clusters) { 539 if (fe->i_clusters) {
540 if (ocfs2_should_order_data(inode))
541 ocfs2_begin_ordered_truncate(inode, 0);
542
525 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 543 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
526 if (IS_ERR(handle)) { 544 if (IS_ERR(handle)) {
527 status = PTR_ERR(handle); 545 status = PTR_ERR(handle);
@@ -730,6 +748,13 @@ static int ocfs2_wipe_inode(struct inode *inode,
730 goto bail_unlock_dir; 748 goto bail_unlock_dir;
731 } 749 }
732 750
751 /*Free extended attribute resources associated with this inode.*/
752 status = ocfs2_xattr_remove(inode, di_bh);
753 if (status < 0) {
754 mlog_errno(status);
755 goto bail_unlock_dir;
756 }
757
733 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, 758 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
734 orphan_dir_bh); 759 orphan_dir_bh);
735 if (status < 0) 760 if (status < 0)
@@ -1081,6 +1106,8 @@ void ocfs2_clear_inode(struct inode *inode)
1081 oi->ip_last_trans = 0; 1106 oi->ip_last_trans = 0;
1082 oi->ip_dir_start_lookup = 0; 1107 oi->ip_dir_start_lookup = 0;
1083 oi->ip_blkno = 0ULL; 1108 oi->ip_blkno = 0ULL;
1109 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1110 &oi->ip_jinode);
1084 1111
1085bail: 1112bail:
1086 mlog_exit_void(); 1113 mlog_exit_void();
@@ -1107,58 +1134,6 @@ void ocfs2_drop_inode(struct inode *inode)
1107} 1134}
1108 1135
1109/* 1136/*
1110 * TODO: this should probably be merged into ocfs2_get_block
1111 *
1112 * However, you now need to pay attention to the cont_prepare_write()
1113 * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
1114 * expects never to extend).
1115 */
1116struct buffer_head *ocfs2_bread(struct inode *inode,
1117 int block, int *err, int reada)
1118{
1119 struct buffer_head *bh = NULL;
1120 int tmperr;
1121 u64 p_blkno;
1122 int readflags = OCFS2_BH_CACHED;
1123
1124 if (reada)
1125 readflags |= OCFS2_BH_READAHEAD;
1126
1127 if (((u64)block << inode->i_sb->s_blocksize_bits) >=
1128 i_size_read(inode)) {
1129 BUG_ON(!reada);
1130 return NULL;
1131 }
1132
1133 down_read(&OCFS2_I(inode)->ip_alloc_sem);
1134 tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
1135 NULL);
1136 up_read(&OCFS2_I(inode)->ip_alloc_sem);
1137 if (tmperr < 0) {
1138 mlog_errno(tmperr);
1139 goto fail;
1140 }
1141
1142 tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
1143 readflags, inode);
1144 if (tmperr < 0)
1145 goto fail;
1146
1147 tmperr = 0;
1148
1149 *err = 0;
1150 return bh;
1151
1152fail:
1153 if (bh) {
1154 brelse(bh);
1155 bh = NULL;
1156 }
1157 *err = -EIO;
1158 return NULL;
1159}
1160
1161/*
1162 * This is called from our getattr. 1137 * This is called from our getattr.
1163 */ 1138 */
1164int ocfs2_inode_revalidate(struct dentry *dentry) 1139int ocfs2_inode_revalidate(struct dentry *dentry)
diff --git a/fs/ocfs2/inode.h b/fs/ocfs2/inode.h
index 390a85596aa0..2f37af9bcc4a 100644
--- a/fs/ocfs2/inode.h
+++ b/fs/ocfs2/inode.h
@@ -40,6 +40,9 @@ struct ocfs2_inode_info
40 /* protects allocation changes on this inode. */ 40 /* protects allocation changes on this inode. */
41 struct rw_semaphore ip_alloc_sem; 41 struct rw_semaphore ip_alloc_sem;
42 42
43 /* protects extended attribute changes on this inode */
44 struct rw_semaphore ip_xattr_sem;
45
43 /* These fields are protected by ip_lock */ 46 /* These fields are protected by ip_lock */
44 spinlock_t ip_lock; 47 spinlock_t ip_lock;
45 u32 ip_open_count; 48 u32 ip_open_count;
@@ -68,6 +71,7 @@ struct ocfs2_inode_info
68 struct ocfs2_extent_map ip_extent_map; 71 struct ocfs2_extent_map ip_extent_map;
69 72
70 struct inode vfs_inode; 73 struct inode vfs_inode;
74 struct jbd2_inode ip_jinode;
71}; 75};
72 76
73/* 77/*
@@ -113,8 +117,6 @@ extern struct kmem_cache *ocfs2_inode_cache;
113 117
114extern const struct address_space_operations ocfs2_aops; 118extern const struct address_space_operations ocfs2_aops;
115 119
116struct buffer_head *ocfs2_bread(struct inode *inode, int block,
117 int *err, int reada);
118void ocfs2_clear_inode(struct inode *inode); 120void ocfs2_clear_inode(struct inode *inode);
119void ocfs2_delete_inode(struct inode *inode); 121void ocfs2_delete_inode(struct inode *inode);
120void ocfs2_drop_inode(struct inode *inode); 122void ocfs2_drop_inode(struct inode *inode);
diff --git a/fs/ocfs2/ioctl.c b/fs/ocfs2/ioctl.c
index 7b142f0ce995..9fcd36dcc9a0 100644
--- a/fs/ocfs2/ioctl.c
+++ b/fs/ocfs2/ioctl.c
@@ -102,8 +102,7 @@ bail_unlock:
102bail: 102bail:
103 mutex_unlock(&inode->i_mutex); 103 mutex_unlock(&inode->i_mutex);
104 104
105 if (bh) 105 brelse(bh);
106 brelse(bh);
107 106
108 mlog_exit(status); 107 mlog_exit(status);
109 return status; 108 return status;
diff --git a/fs/ocfs2/journal.c b/fs/ocfs2/journal.c
index c47bc2a809c2..81e40677eecb 100644
--- a/fs/ocfs2/journal.c
+++ b/fs/ocfs2/journal.c
@@ -215,9 +215,9 @@ static int ocfs2_commit_cache(struct ocfs2_super *osb)
215 goto finally; 215 goto finally;
216 } 216 }
217 217
218 journal_lock_updates(journal->j_journal); 218 jbd2_journal_lock_updates(journal->j_journal);
219 status = journal_flush(journal->j_journal); 219 status = jbd2_journal_flush(journal->j_journal);
220 journal_unlock_updates(journal->j_journal); 220 jbd2_journal_unlock_updates(journal->j_journal);
221 if (status < 0) { 221 if (status < 0) {
222 up_write(&journal->j_trans_barrier); 222 up_write(&journal->j_trans_barrier);
223 mlog_errno(status); 223 mlog_errno(status);
@@ -264,7 +264,7 @@ handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
264 264
265 down_read(&osb->journal->j_trans_barrier); 265 down_read(&osb->journal->j_trans_barrier);
266 266
267 handle = journal_start(journal, max_buffs); 267 handle = jbd2_journal_start(journal, max_buffs);
268 if (IS_ERR(handle)) { 268 if (IS_ERR(handle)) {
269 up_read(&osb->journal->j_trans_barrier); 269 up_read(&osb->journal->j_trans_barrier);
270 270
@@ -290,7 +290,7 @@ int ocfs2_commit_trans(struct ocfs2_super *osb,
290 290
291 BUG_ON(!handle); 291 BUG_ON(!handle);
292 292
293 ret = journal_stop(handle); 293 ret = jbd2_journal_stop(handle);
294 if (ret < 0) 294 if (ret < 0)
295 mlog_errno(ret); 295 mlog_errno(ret);
296 296
@@ -304,7 +304,7 @@ int ocfs2_commit_trans(struct ocfs2_super *osb,
304 * transaction. extend_trans will either extend the current handle by 304 * transaction. extend_trans will either extend the current handle by
305 * nblocks, or commit it and start a new one with nblocks credits. 305 * nblocks, or commit it and start a new one with nblocks credits.
306 * 306 *
307 * This might call journal_restart() which will commit dirty buffers 307 * This might call jbd2_journal_restart() which will commit dirty buffers
308 * and then restart the transaction. Before calling 308 * and then restart the transaction. Before calling
309 * ocfs2_extend_trans(), any changed blocks should have been 309 * ocfs2_extend_trans(), any changed blocks should have been
310 * dirtied. After calling it, all blocks which need to be changed must 310 * dirtied. After calling it, all blocks which need to be changed must
@@ -332,7 +332,7 @@ int ocfs2_extend_trans(handle_t *handle, int nblocks)
332#ifdef CONFIG_OCFS2_DEBUG_FS 332#ifdef CONFIG_OCFS2_DEBUG_FS
333 status = 1; 333 status = 1;
334#else 334#else
335 status = journal_extend(handle, nblocks); 335 status = jbd2_journal_extend(handle, nblocks);
336 if (status < 0) { 336 if (status < 0) {
337 mlog_errno(status); 337 mlog_errno(status);
338 goto bail; 338 goto bail;
@@ -340,8 +340,10 @@ int ocfs2_extend_trans(handle_t *handle, int nblocks)
340#endif 340#endif
341 341
342 if (status > 0) { 342 if (status > 0) {
343 mlog(0, "journal_extend failed, trying journal_restart\n"); 343 mlog(0,
344 status = journal_restart(handle, nblocks); 344 "jbd2_journal_extend failed, trying "
345 "jbd2_journal_restart\n");
346 status = jbd2_journal_restart(handle, nblocks);
345 if (status < 0) { 347 if (status < 0) {
346 mlog_errno(status); 348 mlog_errno(status);
347 goto bail; 349 goto bail;
@@ -393,11 +395,11 @@ int ocfs2_journal_access(handle_t *handle,
393 switch (type) { 395 switch (type) {
394 case OCFS2_JOURNAL_ACCESS_CREATE: 396 case OCFS2_JOURNAL_ACCESS_CREATE:
395 case OCFS2_JOURNAL_ACCESS_WRITE: 397 case OCFS2_JOURNAL_ACCESS_WRITE:
396 status = journal_get_write_access(handle, bh); 398 status = jbd2_journal_get_write_access(handle, bh);
397 break; 399 break;
398 400
399 case OCFS2_JOURNAL_ACCESS_UNDO: 401 case OCFS2_JOURNAL_ACCESS_UNDO:
400 status = journal_get_undo_access(handle, bh); 402 status = jbd2_journal_get_undo_access(handle, bh);
401 break; 403 break;
402 404
403 default: 405 default:
@@ -422,7 +424,7 @@ int ocfs2_journal_dirty(handle_t *handle,
422 mlog_entry("(bh->b_blocknr=%llu)\n", 424 mlog_entry("(bh->b_blocknr=%llu)\n",
423 (unsigned long long)bh->b_blocknr); 425 (unsigned long long)bh->b_blocknr);
424 426
425 status = journal_dirty_metadata(handle, bh); 427 status = jbd2_journal_dirty_metadata(handle, bh);
426 if (status < 0) 428 if (status < 0)
427 mlog(ML_ERROR, "Could not dirty metadata buffer. " 429 mlog(ML_ERROR, "Could not dirty metadata buffer. "
428 "(bh->b_blocknr=%llu)\n", 430 "(bh->b_blocknr=%llu)\n",
@@ -432,6 +434,7 @@ int ocfs2_journal_dirty(handle_t *handle,
432 return status; 434 return status;
433} 435}
434 436
437#ifdef CONFIG_OCFS2_COMPAT_JBD
435int ocfs2_journal_dirty_data(handle_t *handle, 438int ocfs2_journal_dirty_data(handle_t *handle,
436 struct buffer_head *bh) 439 struct buffer_head *bh)
437{ 440{
@@ -443,8 +446,9 @@ int ocfs2_journal_dirty_data(handle_t *handle,
443 446
444 return err; 447 return err;
445} 448}
449#endif
446 450
447#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD_DEFAULT_MAX_COMMIT_AGE) 451#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
448 452
449void ocfs2_set_journal_params(struct ocfs2_super *osb) 453void ocfs2_set_journal_params(struct ocfs2_super *osb)
450{ 454{
@@ -457,9 +461,9 @@ void ocfs2_set_journal_params(struct ocfs2_super *osb)
457 spin_lock(&journal->j_state_lock); 461 spin_lock(&journal->j_state_lock);
458 journal->j_commit_interval = commit_interval; 462 journal->j_commit_interval = commit_interval;
459 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER) 463 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
460 journal->j_flags |= JFS_BARRIER; 464 journal->j_flags |= JBD2_BARRIER;
461 else 465 else
462 journal->j_flags &= ~JFS_BARRIER; 466 journal->j_flags &= ~JBD2_BARRIER;
463 spin_unlock(&journal->j_state_lock); 467 spin_unlock(&journal->j_state_lock);
464} 468}
465 469
@@ -524,14 +528,14 @@ int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
524 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters); 528 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
525 529
526 /* call the kernels journal init function now */ 530 /* call the kernels journal init function now */
527 j_journal = journal_init_inode(inode); 531 j_journal = jbd2_journal_init_inode(inode);
528 if (j_journal == NULL) { 532 if (j_journal == NULL) {
529 mlog(ML_ERROR, "Linux journal layer error\n"); 533 mlog(ML_ERROR, "Linux journal layer error\n");
530 status = -EINVAL; 534 status = -EINVAL;
531 goto done; 535 goto done;
532 } 536 }
533 537
534 mlog(0, "Returned from journal_init_inode\n"); 538 mlog(0, "Returned from jbd2_journal_init_inode\n");
535 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen); 539 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
536 540
537 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) & 541 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
@@ -550,8 +554,7 @@ done:
550 if (status < 0) { 554 if (status < 0) {
551 if (inode_lock) 555 if (inode_lock)
552 ocfs2_inode_unlock(inode, 1); 556 ocfs2_inode_unlock(inode, 1);
553 if (bh != NULL) 557 brelse(bh);
554 brelse(bh);
555 if (inode) { 558 if (inode) {
556 OCFS2_I(inode)->ip_open_count--; 559 OCFS2_I(inode)->ip_open_count--;
557 iput(inode); 560 iput(inode);
@@ -639,7 +642,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
639 if (journal->j_state != OCFS2_JOURNAL_LOADED) 642 if (journal->j_state != OCFS2_JOURNAL_LOADED)
640 goto done; 643 goto done;
641 644
642 /* need to inc inode use count as journal_destroy will iput. */ 645 /* need to inc inode use count - jbd2_journal_destroy will iput. */
643 if (!igrab(inode)) 646 if (!igrab(inode))
644 BUG(); 647 BUG();
645 648
@@ -668,9 +671,9 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
668 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0); 671 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
669 672
670 if (ocfs2_mount_local(osb)) { 673 if (ocfs2_mount_local(osb)) {
671 journal_lock_updates(journal->j_journal); 674 jbd2_journal_lock_updates(journal->j_journal);
672 status = journal_flush(journal->j_journal); 675 status = jbd2_journal_flush(journal->j_journal);
673 journal_unlock_updates(journal->j_journal); 676 jbd2_journal_unlock_updates(journal->j_journal);
674 if (status < 0) 677 if (status < 0)
675 mlog_errno(status); 678 mlog_errno(status);
676 } 679 }
@@ -686,7 +689,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
686 } 689 }
687 690
688 /* Shutdown the kernel journal system */ 691 /* Shutdown the kernel journal system */
689 journal_destroy(journal->j_journal); 692 jbd2_journal_destroy(journal->j_journal);
690 693
691 OCFS2_I(inode)->ip_open_count--; 694 OCFS2_I(inode)->ip_open_count--;
692 695
@@ -711,15 +714,15 @@ static void ocfs2_clear_journal_error(struct super_block *sb,
711{ 714{
712 int olderr; 715 int olderr;
713 716
714 olderr = journal_errno(journal); 717 olderr = jbd2_journal_errno(journal);
715 if (olderr) { 718 if (olderr) {
716 mlog(ML_ERROR, "File system error %d recorded in " 719 mlog(ML_ERROR, "File system error %d recorded in "
717 "journal %u.\n", olderr, slot); 720 "journal %u.\n", olderr, slot);
718 mlog(ML_ERROR, "File system on device %s needs checking.\n", 721 mlog(ML_ERROR, "File system on device %s needs checking.\n",
719 sb->s_id); 722 sb->s_id);
720 723
721 journal_ack_err(journal); 724 jbd2_journal_ack_err(journal);
722 journal_clear_err(journal); 725 jbd2_journal_clear_err(journal);
723 } 726 }
724} 727}
725 728
@@ -734,7 +737,7 @@ int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
734 737
735 osb = journal->j_osb; 738 osb = journal->j_osb;
736 739
737 status = journal_load(journal->j_journal); 740 status = jbd2_journal_load(journal->j_journal);
738 if (status < 0) { 741 if (status < 0) {
739 mlog(ML_ERROR, "Failed to load journal!\n"); 742 mlog(ML_ERROR, "Failed to load journal!\n");
740 goto done; 743 goto done;
@@ -778,7 +781,7 @@ int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
778 781
779 BUG_ON(!journal); 782 BUG_ON(!journal);
780 783
781 status = journal_wipe(journal->j_journal, full); 784 status = jbd2_journal_wipe(journal->j_journal, full);
782 if (status < 0) { 785 if (status < 0) {
783 mlog_errno(status); 786 mlog_errno(status);
784 goto bail; 787 goto bail;
@@ -847,9 +850,8 @@ static int ocfs2_force_read_journal(struct inode *inode)
847 850
848 /* We are reading journal data which should not 851 /* We are reading journal data which should not
849 * be put in the uptodate cache */ 852 * be put in the uptodate cache */
850 status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb), 853 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
851 p_blkno, p_blocks, bhs, 0, 854 p_blkno, p_blocks, bhs);
852 NULL);
853 if (status < 0) { 855 if (status < 0) {
854 mlog_errno(status); 856 mlog_errno(status);
855 goto bail; 857 goto bail;
@@ -865,8 +867,7 @@ static int ocfs2_force_read_journal(struct inode *inode)
865 867
866bail: 868bail:
867 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++) 869 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
868 if (bhs[i]) 870 brelse(bhs[i]);
869 brelse(bhs[i]);
870 mlog_exit(status); 871 mlog_exit(status);
871 return status; 872 return status;
872} 873}
@@ -1133,7 +1134,8 @@ static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1133 } 1134 }
1134 SET_INODE_JOURNAL(inode); 1135 SET_INODE_JOURNAL(inode);
1135 1136
1136 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, bh, 0, inode); 1137 status = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, bh,
1138 OCFS2_BH_IGNORE_CACHE);
1137 if (status < 0) { 1139 if (status < 0) {
1138 mlog_errno(status); 1140 mlog_errno(status);
1139 goto bail; 1141 goto bail;
@@ -1229,19 +1231,19 @@ static int ocfs2_replay_journal(struct ocfs2_super *osb,
1229 } 1231 }
1230 1232
1231 mlog(0, "calling journal_init_inode\n"); 1233 mlog(0, "calling journal_init_inode\n");
1232 journal = journal_init_inode(inode); 1234 journal = jbd2_journal_init_inode(inode);
1233 if (journal == NULL) { 1235 if (journal == NULL) {
1234 mlog(ML_ERROR, "Linux journal layer error\n"); 1236 mlog(ML_ERROR, "Linux journal layer error\n");
1235 status = -EIO; 1237 status = -EIO;
1236 goto done; 1238 goto done;
1237 } 1239 }
1238 1240
1239 status = journal_load(journal); 1241 status = jbd2_journal_load(journal);
1240 if (status < 0) { 1242 if (status < 0) {
1241 mlog_errno(status); 1243 mlog_errno(status);
1242 if (!igrab(inode)) 1244 if (!igrab(inode))
1243 BUG(); 1245 BUG();
1244 journal_destroy(journal); 1246 jbd2_journal_destroy(journal);
1245 goto done; 1247 goto done;
1246 } 1248 }
1247 1249
@@ -1249,9 +1251,9 @@ static int ocfs2_replay_journal(struct ocfs2_super *osb,
1249 1251
1250 /* wipe the journal */ 1252 /* wipe the journal */
1251 mlog(0, "flushing the journal.\n"); 1253 mlog(0, "flushing the journal.\n");
1252 journal_lock_updates(journal); 1254 jbd2_journal_lock_updates(journal);
1253 status = journal_flush(journal); 1255 status = jbd2_journal_flush(journal);
1254 journal_unlock_updates(journal); 1256 jbd2_journal_unlock_updates(journal);
1255 if (status < 0) 1257 if (status < 0)
1256 mlog_errno(status); 1258 mlog_errno(status);
1257 1259
@@ -1272,7 +1274,7 @@ static int ocfs2_replay_journal(struct ocfs2_super *osb,
1272 if (!igrab(inode)) 1274 if (!igrab(inode))
1273 BUG(); 1275 BUG();
1274 1276
1275 journal_destroy(journal); 1277 jbd2_journal_destroy(journal);
1276 1278
1277done: 1279done:
1278 /* drop the lock on this nodes journal */ 1280 /* drop the lock on this nodes journal */
@@ -1282,8 +1284,7 @@ done:
1282 if (inode) 1284 if (inode)
1283 iput(inode); 1285 iput(inode);
1284 1286
1285 if (bh) 1287 brelse(bh);
1286 brelse(bh);
1287 1288
1288 mlog_exit(status); 1289 mlog_exit(status);
1289 return status; 1290 return status;
diff --git a/fs/ocfs2/journal.h b/fs/ocfs2/journal.h
index 2178ebffa05f..d4d14e9a3cea 100644
--- a/fs/ocfs2/journal.h
+++ b/fs/ocfs2/journal.h
@@ -27,7 +27,12 @@
27#define OCFS2_JOURNAL_H 27#define OCFS2_JOURNAL_H
28 28
29#include <linux/fs.h> 29#include <linux/fs.h>
30#include <linux/jbd.h> 30#ifndef CONFIG_OCFS2_COMPAT_JBD
31# include <linux/jbd2.h>
32#else
33# include <linux/jbd.h>
34# include "ocfs2_jbd_compat.h"
35#endif
31 36
32enum ocfs2_journal_state { 37enum ocfs2_journal_state {
33 OCFS2_JOURNAL_FREE = 0, 38 OCFS2_JOURNAL_FREE = 0,
@@ -215,8 +220,8 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
215 * buffer. Will have to call ocfs2_journal_dirty once 220 * buffer. Will have to call ocfs2_journal_dirty once
216 * we've actually dirtied it. Type is one of . or . 221 * we've actually dirtied it. Type is one of . or .
217 * ocfs2_journal_dirty - Mark a journalled buffer as having dirty data. 222 * ocfs2_journal_dirty - Mark a journalled buffer as having dirty data.
218 * ocfs2_journal_dirty_data - Indicate that a data buffer should go out before 223 * ocfs2_jbd2_file_inode - Mark an inode so that its data goes out before
219 * the current handle commits. 224 * the current handle commits.
220 */ 225 */
221 226
222/* You must always start_trans with a number of buffs > 0, but it's 227/* You must always start_trans with a number of buffs > 0, but it's
@@ -268,8 +273,10 @@ int ocfs2_journal_access(handle_t *handle,
268 */ 273 */
269int ocfs2_journal_dirty(handle_t *handle, 274int ocfs2_journal_dirty(handle_t *handle,
270 struct buffer_head *bh); 275 struct buffer_head *bh);
276#ifdef CONFIG_OCFS2_COMPAT_JBD
271int ocfs2_journal_dirty_data(handle_t *handle, 277int ocfs2_journal_dirty_data(handle_t *handle,
272 struct buffer_head *bh); 278 struct buffer_head *bh);
279#endif
273 280
274/* 281/*
275 * Credit Macros: 282 * Credit Macros:
@@ -283,6 +290,9 @@ int ocfs2_journal_dirty_data(handle_t *handle,
283/* simple file updates like chmod, etc. */ 290/* simple file updates like chmod, etc. */
284#define OCFS2_INODE_UPDATE_CREDITS 1 291#define OCFS2_INODE_UPDATE_CREDITS 1
285 292
293/* extended attribute block update */
294#define OCFS2_XATTR_BLOCK_UPDATE_CREDITS 1
295
286/* group extend. inode update and last group update. */ 296/* group extend. inode update and last group update. */
287#define OCFS2_GROUP_EXTEND_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1) 297#define OCFS2_GROUP_EXTEND_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
288 298
@@ -340,11 +350,23 @@ int ocfs2_journal_dirty_data(handle_t *handle,
340#define OCFS2_RENAME_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 3 \ 350#define OCFS2_RENAME_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 3 \
341 + OCFS2_UNLINK_CREDITS) 351 + OCFS2_UNLINK_CREDITS)
342 352
353/* global bitmap dinode, group desc., relinked group,
354 * suballocator dinode, group desc., relinked group,
355 * dinode, xattr block */
356#define OCFS2_XATTR_BLOCK_CREATE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + \
357 + OCFS2_INODE_UPDATE_CREDITS \
358 + OCFS2_XATTR_BLOCK_UPDATE_CREDITS)
359
360/*
361 * Please note that the caller must make sure that root_el is the root
362 * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
363 * the result may be wrong.
364 */
343static inline int ocfs2_calc_extend_credits(struct super_block *sb, 365static inline int ocfs2_calc_extend_credits(struct super_block *sb,
344 struct ocfs2_dinode *fe, 366 struct ocfs2_extent_list *root_el,
345 u32 bits_wanted) 367 u32 bits_wanted)
346{ 368{
347 int bitmap_blocks, sysfile_bitmap_blocks, dinode_blocks; 369 int bitmap_blocks, sysfile_bitmap_blocks, extent_blocks;
348 370
349 /* bitmap dinode, group desc. + relinked group. */ 371 /* bitmap dinode, group desc. + relinked group. */
350 bitmap_blocks = OCFS2_SUBALLOC_ALLOC; 372 bitmap_blocks = OCFS2_SUBALLOC_ALLOC;
@@ -355,16 +377,16 @@ static inline int ocfs2_calc_extend_credits(struct super_block *sb,
355 * however many metadata chunks needed * a remaining suballoc 377 * however many metadata chunks needed * a remaining suballoc
356 * alloc. */ 378 * alloc. */
357 sysfile_bitmap_blocks = 1 + 379 sysfile_bitmap_blocks = 1 +
358 (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(fe); 380 (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(root_el);
359 381
360 /* this does not include *new* metadata blocks, which are 382 /* this does not include *new* metadata blocks, which are
361 * accounted for in sysfile_bitmap_blocks. fe + 383 * accounted for in sysfile_bitmap_blocks. root_el +
362 * prev. last_eb_blk + blocks along edge of tree. 384 * prev. last_eb_blk + blocks along edge of tree.
363 * calc_symlink_credits passes because we just need 1 385 * calc_symlink_credits passes because we just need 1
364 * credit for the dinode there. */ 386 * credit for the dinode there. */
365 dinode_blocks = 1 + 1 + le16_to_cpu(fe->id2.i_list.l_tree_depth); 387 extent_blocks = 1 + 1 + le16_to_cpu(root_el->l_tree_depth);
366 388
367 return bitmap_blocks + sysfile_bitmap_blocks + dinode_blocks; 389 return bitmap_blocks + sysfile_bitmap_blocks + extent_blocks;
368} 390}
369 391
370static inline int ocfs2_calc_symlink_credits(struct super_block *sb) 392static inline int ocfs2_calc_symlink_credits(struct super_block *sb)
@@ -415,4 +437,16 @@ static inline int ocfs2_calc_tree_trunc_credits(struct super_block *sb,
415 return credits; 437 return credits;
416} 438}
417 439
440static inline int ocfs2_jbd2_file_inode(handle_t *handle, struct inode *inode)
441{
442 return jbd2_journal_file_inode(handle, &OCFS2_I(inode)->ip_jinode);
443}
444
445static inline int ocfs2_begin_ordered_truncate(struct inode *inode,
446 loff_t new_size)
447{
448 return jbd2_journal_begin_ordered_truncate(&OCFS2_I(inode)->ip_jinode,
449 new_size);
450}
451
418#endif /* OCFS2_JOURNAL_H */ 452#endif /* OCFS2_JOURNAL_H */
diff --git a/fs/ocfs2/localalloc.c b/fs/ocfs2/localalloc.c
index 28e492e4ec88..687b28713c32 100644
--- a/fs/ocfs2/localalloc.c
+++ b/fs/ocfs2/localalloc.c
@@ -28,6 +28,7 @@
28#include <linux/slab.h> 28#include <linux/slab.h>
29#include <linux/highmem.h> 29#include <linux/highmem.h>
30#include <linux/bitops.h> 30#include <linux/bitops.h>
31#include <linux/debugfs.h>
31 32
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC 33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h> 34#include <cluster/masklog.h>
@@ -47,8 +48,6 @@
47 48
48#define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab)) 49#define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
49 50
50static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
51
52static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc); 51static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
53 52
54static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, 53static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
@@ -75,24 +74,129 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
75static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, 74static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76 struct inode *local_alloc_inode); 75 struct inode *local_alloc_inode);
77 76
78static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb) 77#ifdef CONFIG_OCFS2_FS_STATS
78
79static int ocfs2_la_debug_open(struct inode *inode, struct file *file)
80{
81 file->private_data = inode->i_private;
82 return 0;
83}
84
85#define LA_DEBUG_BUF_SZ PAGE_CACHE_SIZE
86#define LA_DEBUG_VER 1
87static ssize_t ocfs2_la_debug_read(struct file *file, char __user *userbuf,
88 size_t count, loff_t *ppos)
89{
90 static DEFINE_MUTEX(la_debug_mutex);
91 struct ocfs2_super *osb = file->private_data;
92 int written, ret;
93 char *buf = osb->local_alloc_debug_buf;
94
95 mutex_lock(&la_debug_mutex);
96 memset(buf, 0, LA_DEBUG_BUF_SZ);
97
98 written = snprintf(buf, LA_DEBUG_BUF_SZ,
99 "0x%x\t0x%llx\t%u\t%u\t0x%x\n",
100 LA_DEBUG_VER,
101 (unsigned long long)osb->la_last_gd,
102 osb->local_alloc_default_bits,
103 osb->local_alloc_bits, osb->local_alloc_state);
104
105 ret = simple_read_from_buffer(userbuf, count, ppos, buf, written);
106
107 mutex_unlock(&la_debug_mutex);
108 return ret;
109}
110
111static const struct file_operations ocfs2_la_debug_fops = {
112 .open = ocfs2_la_debug_open,
113 .read = ocfs2_la_debug_read,
114};
115
116static void ocfs2_init_la_debug(struct ocfs2_super *osb)
117{
118 osb->local_alloc_debug_buf = kmalloc(LA_DEBUG_BUF_SZ, GFP_NOFS);
119 if (!osb->local_alloc_debug_buf)
120 return;
121
122 osb->local_alloc_debug = debugfs_create_file("local_alloc_stats",
123 S_IFREG|S_IRUSR,
124 osb->osb_debug_root,
125 osb,
126 &ocfs2_la_debug_fops);
127 if (!osb->local_alloc_debug) {
128 kfree(osb->local_alloc_debug_buf);
129 osb->local_alloc_debug_buf = NULL;
130 }
131}
132
133static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb)
134{
135 if (osb->local_alloc_debug)
136 debugfs_remove(osb->local_alloc_debug);
137
138 if (osb->local_alloc_debug_buf)
139 kfree(osb->local_alloc_debug_buf);
140
141 osb->local_alloc_debug_buf = NULL;
142 osb->local_alloc_debug = NULL;
143}
144#else /* CONFIG_OCFS2_FS_STATS */
145static void ocfs2_init_la_debug(struct ocfs2_super *osb)
146{
147 return;
148}
149static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb)
150{
151 return;
152}
153#endif
154
155static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
79{ 156{
80 BUG_ON(osb->s_clustersize_bits > 20); 157 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
158 osb->local_alloc_state == OCFS2_LA_ENABLED);
159}
81 160
82 /* Size local alloc windows by the megabyte */ 161void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
83 return osb->local_alloc_size << (20 - osb->s_clustersize_bits); 162 unsigned int num_clusters)
163{
164 spin_lock(&osb->osb_lock);
165 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
166 osb->local_alloc_state == OCFS2_LA_THROTTLED)
167 if (num_clusters >= osb->local_alloc_default_bits) {
168 cancel_delayed_work(&osb->la_enable_wq);
169 osb->local_alloc_state = OCFS2_LA_ENABLED;
170 }
171 spin_unlock(&osb->osb_lock);
172}
173
174void ocfs2_la_enable_worker(struct work_struct *work)
175{
176 struct ocfs2_super *osb =
177 container_of(work, struct ocfs2_super,
178 la_enable_wq.work);
179 spin_lock(&osb->osb_lock);
180 osb->local_alloc_state = OCFS2_LA_ENABLED;
181 spin_unlock(&osb->osb_lock);
84} 182}
85 183
86/* 184/*
87 * Tell us whether a given allocation should use the local alloc 185 * Tell us whether a given allocation should use the local alloc
88 * file. Otherwise, it has to go to the main bitmap. 186 * file. Otherwise, it has to go to the main bitmap.
187 *
188 * This function does semi-dirty reads of local alloc size and state!
189 * This is ok however, as the values are re-checked once under mutex.
89 */ 190 */
90int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) 191int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
91{ 192{
92 int la_bits = ocfs2_local_alloc_window_bits(osb);
93 int ret = 0; 193 int ret = 0;
194 int la_bits;
195
196 spin_lock(&osb->osb_lock);
197 la_bits = osb->local_alloc_bits;
94 198
95 if (osb->local_alloc_state != OCFS2_LA_ENABLED) 199 if (!ocfs2_la_state_enabled(osb))
96 goto bail; 200 goto bail;
97 201
98 /* la_bits should be at least twice the size (in clusters) of 202 /* la_bits should be at least twice the size (in clusters) of
@@ -106,6 +210,7 @@ int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
106bail: 210bail:
107 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", 211 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
108 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); 212 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
213 spin_unlock(&osb->osb_lock);
109 return ret; 214 return ret;
110} 215}
111 216
@@ -120,14 +225,18 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
120 225
121 mlog_entry_void(); 226 mlog_entry_void();
122 227
123 if (osb->local_alloc_size == 0) 228 ocfs2_init_la_debug(osb);
229
230 if (osb->local_alloc_bits == 0)
124 goto bail; 231 goto bail;
125 232
126 if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) { 233 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
127 mlog(ML_NOTICE, "Requested local alloc window %d is larger " 234 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
128 "than max possible %u. Using defaults.\n", 235 "than max possible %u. Using defaults.\n",
129 ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1)); 236 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
130 osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE; 237 osb->local_alloc_bits =
238 ocfs2_megabytes_to_clusters(osb->sb,
239 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
131 } 240 }
132 241
133 /* read the alloc off disk */ 242 /* read the alloc off disk */
@@ -139,8 +248,8 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
139 goto bail; 248 goto bail;
140 } 249 }
141 250
142 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, 251 status = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1,
143 &alloc_bh, 0, inode); 252 &alloc_bh, OCFS2_BH_IGNORE_CACHE);
144 if (status < 0) { 253 if (status < 0) {
145 mlog_errno(status); 254 mlog_errno(status);
146 goto bail; 255 goto bail;
@@ -185,13 +294,14 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
185 294
186bail: 295bail:
187 if (status < 0) 296 if (status < 0)
188 if (alloc_bh) 297 brelse(alloc_bh);
189 brelse(alloc_bh);
190 if (inode) 298 if (inode)
191 iput(inode); 299 iput(inode);
192 300
193 mlog(0, "Local alloc window bits = %d\n", 301 if (status < 0)
194 ocfs2_local_alloc_window_bits(osb)); 302 ocfs2_shutdown_la_debug(osb);
303
304 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
195 305
196 mlog_exit(status); 306 mlog_exit(status);
197 return status; 307 return status;
@@ -217,6 +327,11 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
217 327
218 mlog_entry_void(); 328 mlog_entry_void();
219 329
330 cancel_delayed_work(&osb->la_enable_wq);
331 flush_workqueue(ocfs2_wq);
332
333 ocfs2_shutdown_la_debug(osb);
334
220 if (osb->local_alloc_state == OCFS2_LA_UNUSED) 335 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
221 goto out; 336 goto out;
222 337
@@ -295,8 +410,7 @@ out_commit:
295 ocfs2_commit_trans(osb, handle); 410 ocfs2_commit_trans(osb, handle);
296 411
297out_unlock: 412out_unlock:
298 if (main_bm_bh) 413 brelse(main_bm_bh);
299 brelse(main_bm_bh);
300 414
301 ocfs2_inode_unlock(main_bm_inode, 1); 415 ocfs2_inode_unlock(main_bm_inode, 1);
302 416
@@ -345,8 +459,8 @@ int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
345 459
346 mutex_lock(&inode->i_mutex); 460 mutex_lock(&inode->i_mutex);
347 461
348 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, 462 status = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1,
349 &alloc_bh, 0, inode); 463 &alloc_bh, OCFS2_BH_IGNORE_CACHE);
350 if (status < 0) { 464 if (status < 0) {
351 mlog_errno(status); 465 mlog_errno(status);
352 goto bail; 466 goto bail;
@@ -372,8 +486,7 @@ bail:
372 *alloc_copy = NULL; 486 *alloc_copy = NULL;
373 } 487 }
374 488
375 if (alloc_bh) 489 brelse(alloc_bh);
376 brelse(alloc_bh);
377 490
378 if (inode) { 491 if (inode) {
379 mutex_unlock(&inode->i_mutex); 492 mutex_unlock(&inode->i_mutex);
@@ -441,8 +554,7 @@ out_unlock:
441out_mutex: 554out_mutex:
442 mutex_unlock(&main_bm_inode->i_mutex); 555 mutex_unlock(&main_bm_inode->i_mutex);
443 556
444 if (main_bm_bh) 557 brelse(main_bm_bh);
445 brelse(main_bm_bh);
446 558
447 iput(main_bm_inode); 559 iput(main_bm_inode);
448 560
@@ -453,8 +565,48 @@ out:
453 return status; 565 return status;
454} 566}
455 567
568/* Check to see if the local alloc window is within ac->ac_max_block */
569static int ocfs2_local_alloc_in_range(struct inode *inode,
570 struct ocfs2_alloc_context *ac,
571 u32 bits_wanted)
572{
573 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
574 struct ocfs2_dinode *alloc;
575 struct ocfs2_local_alloc *la;
576 int start;
577 u64 block_off;
578
579 if (!ac->ac_max_block)
580 return 1;
581
582 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
583 la = OCFS2_LOCAL_ALLOC(alloc);
584
585 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
586 if (start == -1) {
587 mlog_errno(-ENOSPC);
588 return 0;
589 }
590
591 /*
592 * Converting (bm_off + start + bits_wanted) to blocks gives us
593 * the blkno just past our actual allocation. This is perfect
594 * to compare with ac_max_block.
595 */
596 block_off = ocfs2_clusters_to_blocks(inode->i_sb,
597 le32_to_cpu(la->la_bm_off) +
598 start + bits_wanted);
599 mlog(0, "Checking %llu against %llu\n",
600 (unsigned long long)block_off,
601 (unsigned long long)ac->ac_max_block);
602 if (block_off > ac->ac_max_block)
603 return 0;
604
605 return 1;
606}
607
456/* 608/*
457 * make sure we've got at least bitswanted contiguous bits in the 609 * make sure we've got at least bits_wanted contiguous bits in the
458 * local alloc. You lose them when you drop i_mutex. 610 * local alloc. You lose them when you drop i_mutex.
459 * 611 *
460 * We will add ourselves to the transaction passed in, but may start 612 * We will add ourselves to the transaction passed in, but may start
@@ -485,16 +637,18 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
485 637
486 mutex_lock(&local_alloc_inode->i_mutex); 638 mutex_lock(&local_alloc_inode->i_mutex);
487 639
488 if (osb->local_alloc_state != OCFS2_LA_ENABLED) { 640 /*
489 status = -ENOSPC; 641 * We must double check state and allocator bits because
490 goto bail; 642 * another process may have changed them while holding i_mutex.
491 } 643 */
492 644 spin_lock(&osb->osb_lock);
493 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) { 645 if (!ocfs2_la_state_enabled(osb) ||
494 mlog(0, "Asking for more than my max window size!\n"); 646 (bits_wanted > osb->local_alloc_bits)) {
647 spin_unlock(&osb->osb_lock);
495 status = -ENOSPC; 648 status = -ENOSPC;
496 goto bail; 649 goto bail;
497 } 650 }
651 spin_unlock(&osb->osb_lock);
498 652
499 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; 653 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
500 654
@@ -522,6 +676,36 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
522 mlog_errno(status); 676 mlog_errno(status);
523 goto bail; 677 goto bail;
524 } 678 }
679
680 /*
681 * Under certain conditions, the window slide code
682 * might have reduced the number of bits available or
683 * disabled the the local alloc entirely. Re-check
684 * here and return -ENOSPC if necessary.
685 */
686 status = -ENOSPC;
687 if (!ocfs2_la_state_enabled(osb))
688 goto bail;
689
690 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
691 le32_to_cpu(alloc->id1.bitmap1.i_used);
692 if (bits_wanted > free_bits)
693 goto bail;
694 }
695
696 if (ac->ac_max_block)
697 mlog(0, "Calling in_range for max block %llu\n",
698 (unsigned long long)ac->ac_max_block);
699
700 if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
701 bits_wanted)) {
702 /*
703 * The window is outside ac->ac_max_block.
704 * This errno tells the caller to keep localalloc enabled
705 * but to get the allocation from the main bitmap.
706 */
707 status = -EFBIG;
708 goto bail;
525 } 709 }
526 710
527 ac->ac_inode = local_alloc_inode; 711 ac->ac_inode = local_alloc_inode;
@@ -789,6 +973,85 @@ bail:
789 return status; 973 return status;
790} 974}
791 975
976enum ocfs2_la_event {
977 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
978 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
979 * enough bits theoretically
980 * free, but a contiguous
981 * allocation could not be
982 * found. */
983 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
984 * enough bits free to satisfy
985 * our request. */
986};
987#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
988/*
989 * Given an event, calculate the size of our next local alloc window.
990 *
991 * This should always be called under i_mutex of the local alloc inode
992 * so that local alloc disabling doesn't race with processes trying to
993 * use the allocator.
994 *
995 * Returns the state which the local alloc was left in. This value can
996 * be ignored by some paths.
997 */
998static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
999 enum ocfs2_la_event event)
1000{
1001 unsigned int bits;
1002 int state;
1003
1004 spin_lock(&osb->osb_lock);
1005 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
1006 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
1007 goto out_unlock;
1008 }
1009
1010 /*
1011 * ENOSPC and fragmentation are treated similarly for now.
1012 */
1013 if (event == OCFS2_LA_EVENT_ENOSPC ||
1014 event == OCFS2_LA_EVENT_FRAGMENTED) {
1015 /*
1016 * We ran out of contiguous space in the primary
1017 * bitmap. Drastically reduce the number of bits used
1018 * by local alloc until we have to disable it.
1019 */
1020 bits = osb->local_alloc_bits >> 1;
1021 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
1022 /*
1023 * By setting state to THROTTLED, we'll keep
1024 * the number of local alloc bits used down
1025 * until an event occurs which would give us
1026 * reason to assume the bitmap situation might
1027 * have changed.
1028 */
1029 osb->local_alloc_state = OCFS2_LA_THROTTLED;
1030 osb->local_alloc_bits = bits;
1031 } else {
1032 osb->local_alloc_state = OCFS2_LA_DISABLED;
1033 }
1034 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
1035 OCFS2_LA_ENABLE_INTERVAL);
1036 goto out_unlock;
1037 }
1038
1039 /*
1040 * Don't increase the size of the local alloc window until we
1041 * know we might be able to fulfill the request. Otherwise, we
1042 * risk bouncing around the global bitmap during periods of
1043 * low space.
1044 */
1045 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
1046 osb->local_alloc_bits = osb->local_alloc_default_bits;
1047
1048out_unlock:
1049 state = osb->local_alloc_state;
1050 spin_unlock(&osb->osb_lock);
1051
1052 return state;
1053}
1054
792static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, 1055static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
793 struct ocfs2_alloc_context **ac, 1056 struct ocfs2_alloc_context **ac,
794 struct inode **bitmap_inode, 1057 struct inode **bitmap_inode,
@@ -803,12 +1066,21 @@ static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
803 goto bail; 1066 goto bail;
804 } 1067 }
805 1068
806 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb); 1069retry_enospc:
1070 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
807 1071
808 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); 1072 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1073 if (status == -ENOSPC) {
1074 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1075 OCFS2_LA_DISABLED)
1076 goto bail;
1077
1078 ocfs2_free_ac_resource(*ac);
1079 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1080 goto retry_enospc;
1081 }
809 if (status < 0) { 1082 if (status < 0) {
810 if (status != -ENOSPC) 1083 mlog_errno(status);
811 mlog_errno(status);
812 goto bail; 1084 goto bail;
813 } 1085 }
814 1086
@@ -849,7 +1121,7 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
849 "one\n"); 1121 "one\n");
850 1122
851 mlog(0, "Allocating %u clusters for a new window.\n", 1123 mlog(0, "Allocating %u clusters for a new window.\n",
852 ocfs2_local_alloc_window_bits(osb)); 1124 osb->local_alloc_bits);
853 1125
854 /* Instruct the allocation code to try the most recently used 1126 /* Instruct the allocation code to try the most recently used
855 * cluster group. We'll re-record the group used this pass 1127 * cluster group. We'll re-record the group used this pass
@@ -859,9 +1131,36 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
859 /* we used the generic suballoc reserve function, but we set 1131 /* we used the generic suballoc reserve function, but we set
860 * everything up nicely, so there's no reason why we can't use 1132 * everything up nicely, so there's no reason why we can't use
861 * the more specific cluster api to claim bits. */ 1133 * the more specific cluster api to claim bits. */
862 status = ocfs2_claim_clusters(osb, handle, ac, 1134 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
863 ocfs2_local_alloc_window_bits(osb),
864 &cluster_off, &cluster_count); 1135 &cluster_off, &cluster_count);
1136 if (status == -ENOSPC) {
1137retry_enospc:
1138 /*
1139 * Note: We could also try syncing the journal here to
1140 * allow use of any free bits which the current
1141 * transaction can't give us access to. --Mark
1142 */
1143 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1144 OCFS2_LA_DISABLED)
1145 goto bail;
1146
1147 status = ocfs2_claim_clusters(osb, handle, ac,
1148 osb->local_alloc_bits,
1149 &cluster_off,
1150 &cluster_count);
1151 if (status == -ENOSPC)
1152 goto retry_enospc;
1153 /*
1154 * We only shrunk the *minimum* number of in our
1155 * request - it's entirely possible that the allocator
1156 * might give us more than we asked for.
1157 */
1158 if (status == 0) {
1159 spin_lock(&osb->osb_lock);
1160 osb->local_alloc_bits = cluster_count;
1161 spin_unlock(&osb->osb_lock);
1162 }
1163 }
865 if (status < 0) { 1164 if (status < 0) {
866 if (status != -ENOSPC) 1165 if (status != -ENOSPC)
867 mlog_errno(status); 1166 mlog_errno(status);
@@ -905,6 +1204,8 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
905 1204
906 mlog_entry_void(); 1205 mlog_entry_void();
907 1206
1207 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1208
908 /* This will lock the main bitmap for us. */ 1209 /* This will lock the main bitmap for us. */
909 status = ocfs2_local_alloc_reserve_for_window(osb, 1210 status = ocfs2_local_alloc_reserve_for_window(osb,
910 &ac, 1211 &ac,
@@ -976,8 +1277,7 @@ bail:
976 if (handle) 1277 if (handle)
977 ocfs2_commit_trans(osb, handle); 1278 ocfs2_commit_trans(osb, handle);
978 1279
979 if (main_bm_bh) 1280 brelse(main_bm_bh);
980 brelse(main_bm_bh);
981 1281
982 if (main_bm_inode) 1282 if (main_bm_inode)
983 iput(main_bm_inode); 1283 iput(main_bm_inode);
diff --git a/fs/ocfs2/localalloc.h b/fs/ocfs2/localalloc.h
index 3f76631e110c..ac5ea9f86653 100644
--- a/fs/ocfs2/localalloc.h
+++ b/fs/ocfs2/localalloc.h
@@ -52,4 +52,8 @@ int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
52 u32 *bit_off, 52 u32 *bit_off,
53 u32 *num_bits); 53 u32 *num_bits);
54 54
55void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
56 unsigned int num_clusters);
57void ocfs2_la_enable_worker(struct work_struct *work);
58
55#endif /* OCFS2_LOCALALLOC_H */ 59#endif /* OCFS2_LOCALALLOC_H */
diff --git a/fs/ocfs2/locks.c b/fs/ocfs2/locks.c
index 203f87143877..544ac6245175 100644
--- a/fs/ocfs2/locks.c
+++ b/fs/ocfs2/locks.c
@@ -24,6 +24,7 @@
24 */ 24 */
25 25
26#include <linux/fs.h> 26#include <linux/fs.h>
27#include <linux/fcntl.h>
27 28
28#define MLOG_MASK_PREFIX ML_INODE 29#define MLOG_MASK_PREFIX ML_INODE
29#include <cluster/masklog.h> 30#include <cluster/masklog.h>
@@ -32,6 +33,7 @@
32 33
33#include "dlmglue.h" 34#include "dlmglue.h"
34#include "file.h" 35#include "file.h"
36#include "inode.h"
35#include "locks.h" 37#include "locks.h"
36 38
37static int ocfs2_do_flock(struct file *file, struct inode *inode, 39static int ocfs2_do_flock(struct file *file, struct inode *inode,
@@ -123,3 +125,16 @@ int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
123 else 125 else
124 return ocfs2_do_flock(file, inode, cmd, fl); 126 return ocfs2_do_flock(file, inode, cmd, fl);
125} 127}
128
129int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl)
130{
131 struct inode *inode = file->f_mapping->host;
132 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
133
134 if (!(fl->fl_flags & FL_POSIX))
135 return -ENOLCK;
136 if (__mandatory_lock(inode))
137 return -ENOLCK;
138
139 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);
140}
diff --git a/fs/ocfs2/locks.h b/fs/ocfs2/locks.h
index 9743ef2324ec..496d488b271f 100644
--- a/fs/ocfs2/locks.h
+++ b/fs/ocfs2/locks.h
@@ -27,5 +27,6 @@
27#define OCFS2_LOCKS_H 27#define OCFS2_LOCKS_H
28 28
29int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl); 29int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl);
30int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl);
30 31
31#endif /* OCFS2_LOCKS_H */ 32#endif /* OCFS2_LOCKS_H */
diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c
index d5d808fe0140..485a6aa0ad39 100644
--- a/fs/ocfs2/namei.c
+++ b/fs/ocfs2/namei.c
@@ -60,6 +60,7 @@
60#include "symlink.h" 60#include "symlink.h"
61#include "sysfile.h" 61#include "sysfile.h"
62#include "uptodate.h" 62#include "uptodate.h"
63#include "xattr.h"
63 64
64#include "buffer_head_io.h" 65#include "buffer_head_io.h"
65 66
@@ -327,14 +328,9 @@ leave:
327 if (status == -ENOSPC) 328 if (status == -ENOSPC)
328 mlog(0, "Disk is full\n"); 329 mlog(0, "Disk is full\n");
329 330
330 if (new_fe_bh) 331 brelse(new_fe_bh);
331 brelse(new_fe_bh); 332 brelse(de_bh);
332 333 brelse(parent_fe_bh);
333 if (de_bh)
334 brelse(de_bh);
335
336 if (parent_fe_bh)
337 brelse(parent_fe_bh);
338 334
339 if ((status < 0) && inode) 335 if ((status < 0) && inode)
340 iput(inode); 336 iput(inode);
@@ -647,12 +643,9 @@ out_unlock_inode:
647out: 643out:
648 ocfs2_inode_unlock(dir, 1); 644 ocfs2_inode_unlock(dir, 1);
649 645
650 if (de_bh) 646 brelse(de_bh);
651 brelse(de_bh); 647 brelse(fe_bh);
652 if (fe_bh) 648 brelse(parent_fe_bh);
653 brelse(fe_bh);
654 if (parent_fe_bh)
655 brelse(parent_fe_bh);
656 649
657 mlog_exit(err); 650 mlog_exit(err);
658 651
@@ -851,17 +844,10 @@ leave:
851 iput(orphan_dir); 844 iput(orphan_dir);
852 } 845 }
853 846
854 if (fe_bh) 847 brelse(fe_bh);
855 brelse(fe_bh); 848 brelse(dirent_bh);
856 849 brelse(parent_node_bh);
857 if (dirent_bh) 850 brelse(orphan_entry_bh);
858 brelse(dirent_bh);
859
860 if (parent_node_bh)
861 brelse(parent_node_bh);
862
863 if (orphan_entry_bh)
864 brelse(orphan_entry_bh);
865 851
866 mlog_exit(status); 852 mlog_exit(status);
867 853
@@ -1372,24 +1358,15 @@ bail:
1372 1358
1373 if (new_inode) 1359 if (new_inode)
1374 iput(new_inode); 1360 iput(new_inode);
1375 if (newfe_bh) 1361 brelse(newfe_bh);
1376 brelse(newfe_bh); 1362 brelse(old_inode_bh);
1377 if (old_inode_bh) 1363 brelse(old_dir_bh);
1378 brelse(old_inode_bh); 1364 brelse(new_dir_bh);
1379 if (old_dir_bh) 1365 brelse(new_de_bh);
1380 brelse(old_dir_bh); 1366 brelse(old_de_bh);
1381 if (new_dir_bh) 1367 brelse(old_inode_de_bh);
1382 brelse(new_dir_bh); 1368 brelse(orphan_entry_bh);
1383 if (new_de_bh) 1369 brelse(insert_entry_bh);
1384 brelse(new_de_bh);
1385 if (old_de_bh)
1386 brelse(old_de_bh);
1387 if (old_inode_de_bh)
1388 brelse(old_inode_de_bh);
1389 if (orphan_entry_bh)
1390 brelse(orphan_entry_bh);
1391 if (insert_entry_bh)
1392 brelse(insert_entry_bh);
1393 1370
1394 mlog_exit(status); 1371 mlog_exit(status);
1395 1372
@@ -1492,8 +1469,7 @@ bail:
1492 1469
1493 if (bhs) { 1470 if (bhs) {
1494 for(i = 0; i < blocks; i++) 1471 for(i = 0; i < blocks; i++)
1495 if (bhs[i]) 1472 brelse(bhs[i]);
1496 brelse(bhs[i]);
1497 kfree(bhs); 1473 kfree(bhs);
1498 } 1474 }
1499 1475
@@ -1598,10 +1574,10 @@ static int ocfs2_symlink(struct inode *dir,
1598 u32 offset = 0; 1574 u32 offset = 0;
1599 1575
1600 inode->i_op = &ocfs2_symlink_inode_operations; 1576 inode->i_op = &ocfs2_symlink_inode_operations;
1601 status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0, 1577 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1602 new_fe_bh, 1578 new_fe_bh,
1603 handle, data_ac, NULL, 1579 handle, data_ac, NULL,
1604 NULL); 1580 NULL);
1605 if (status < 0) { 1581 if (status < 0) {
1606 if (status != -ENOSPC && status != -EINTR) { 1582 if (status != -ENOSPC && status != -EINTR) {
1607 mlog(ML_ERROR, 1583 mlog(ML_ERROR,
@@ -1659,12 +1635,9 @@ bail:
1659 1635
1660 ocfs2_inode_unlock(dir, 1); 1636 ocfs2_inode_unlock(dir, 1);
1661 1637
1662 if (new_fe_bh) 1638 brelse(new_fe_bh);
1663 brelse(new_fe_bh); 1639 brelse(parent_fe_bh);
1664 if (parent_fe_bh) 1640 brelse(de_bh);
1665 brelse(parent_fe_bh);
1666 if (de_bh)
1667 brelse(de_bh);
1668 if (inode_ac) 1641 if (inode_ac)
1669 ocfs2_free_alloc_context(inode_ac); 1642 ocfs2_free_alloc_context(inode_ac);
1670 if (data_ac) 1643 if (data_ac)
@@ -1759,8 +1732,7 @@ leave:
1759 iput(orphan_dir_inode); 1732 iput(orphan_dir_inode);
1760 } 1733 }
1761 1734
1762 if (orphan_dir_bh) 1735 brelse(orphan_dir_bh);
1763 brelse(orphan_dir_bh);
1764 1736
1765 mlog_exit(status); 1737 mlog_exit(status);
1766 return status; 1738 return status;
@@ -1780,10 +1752,9 @@ static int ocfs2_orphan_add(struct ocfs2_super *osb,
1780 1752
1781 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino); 1753 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1782 1754
1783 status = ocfs2_read_block(osb, 1755 status = ocfs2_read_block(orphan_dir_inode,
1784 OCFS2_I(orphan_dir_inode)->ip_blkno, 1756 OCFS2_I(orphan_dir_inode)->ip_blkno,
1785 &orphan_dir_bh, OCFS2_BH_CACHED, 1757 &orphan_dir_bh);
1786 orphan_dir_inode);
1787 if (status < 0) { 1758 if (status < 0) {
1788 mlog_errno(status); 1759 mlog_errno(status);
1789 goto leave; 1760 goto leave;
@@ -1829,8 +1800,7 @@ static int ocfs2_orphan_add(struct ocfs2_super *osb,
1829 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num); 1800 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
1830 1801
1831leave: 1802leave:
1832 if (orphan_dir_bh) 1803 brelse(orphan_dir_bh);
1833 brelse(orphan_dir_bh);
1834 1804
1835 mlog_exit(status); 1805 mlog_exit(status);
1836 return status; 1806 return status;
@@ -1898,8 +1868,7 @@ int ocfs2_orphan_del(struct ocfs2_super *osb,
1898 } 1868 }
1899 1869
1900leave: 1870leave:
1901 if (target_de_bh) 1871 brelse(target_de_bh);
1902 brelse(target_de_bh);
1903 1872
1904 mlog_exit(status); 1873 mlog_exit(status);
1905 return status; 1874 return status;
@@ -1918,4 +1887,8 @@ const struct inode_operations ocfs2_dir_iops = {
1918 .setattr = ocfs2_setattr, 1887 .setattr = ocfs2_setattr,
1919 .getattr = ocfs2_getattr, 1888 .getattr = ocfs2_getattr,
1920 .permission = ocfs2_permission, 1889 .permission = ocfs2_permission,
1890 .setxattr = generic_setxattr,
1891 .getxattr = generic_getxattr,
1892 .listxattr = ocfs2_listxattr,
1893 .removexattr = generic_removexattr,
1921}; 1894};
diff --git a/fs/ocfs2/ocfs2.h b/fs/ocfs2/ocfs2.h
index 7f625f2b1117..a21a465490c4 100644
--- a/fs/ocfs2/ocfs2.h
+++ b/fs/ocfs2/ocfs2.h
@@ -34,7 +34,12 @@
34#include <linux/workqueue.h> 34#include <linux/workqueue.h>
35#include <linux/kref.h> 35#include <linux/kref.h>
36#include <linux/mutex.h> 36#include <linux/mutex.h>
37#include <linux/jbd.h> 37#ifndef CONFIG_OCFS2_COMPAT_JBD
38# include <linux/jbd2.h>
39#else
40# include <linux/jbd.h>
41# include "ocfs2_jbd_compat.h"
42#endif
38 43
39/* For union ocfs2_dlm_lksb */ 44/* For union ocfs2_dlm_lksb */
40#include "stackglue.h" 45#include "stackglue.h"
@@ -171,9 +176,13 @@ struct ocfs2_alloc_stats
171 176
172enum ocfs2_local_alloc_state 177enum ocfs2_local_alloc_state
173{ 178{
174 OCFS2_LA_UNUSED = 0, 179 OCFS2_LA_UNUSED = 0, /* Local alloc will never be used for
175 OCFS2_LA_ENABLED, 180 * this mountpoint. */
176 OCFS2_LA_DISABLED 181 OCFS2_LA_ENABLED, /* Local alloc is in use. */
182 OCFS2_LA_THROTTLED, /* Local alloc is in use, but number
183 * of bits has been reduced. */
184 OCFS2_LA_DISABLED /* Local alloc has temporarily been
185 * disabled. */
177}; 186};
178 187
179enum ocfs2_mount_options 188enum ocfs2_mount_options
@@ -184,6 +193,8 @@ enum ocfs2_mount_options
184 OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */ 193 OCFS2_MOUNT_ERRORS_PANIC = 1 << 3, /* Panic on errors */
185 OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */ 194 OCFS2_MOUNT_DATA_WRITEBACK = 1 << 4, /* No data ordering */
186 OCFS2_MOUNT_LOCALFLOCKS = 1 << 5, /* No cluster aware user file locks */ 195 OCFS2_MOUNT_LOCALFLOCKS = 1 << 5, /* No cluster aware user file locks */
196 OCFS2_MOUNT_NOUSERXATTR = 1 << 6, /* No user xattr */
197 OCFS2_MOUNT_INODE64 = 1 << 7, /* Allow inode numbers > 2^32 */
187}; 198};
188 199
189#define OCFS2_OSB_SOFT_RO 0x0001 200#define OCFS2_OSB_SOFT_RO 0x0001
@@ -214,6 +225,7 @@ struct ocfs2_super
214 u32 bitmap_cpg; 225 u32 bitmap_cpg;
215 u8 *uuid; 226 u8 *uuid;
216 char *uuid_str; 227 char *uuid_str;
228 u32 uuid_hash;
217 u8 *vol_label; 229 u8 *vol_label;
218 u64 first_cluster_group_blkno; 230 u64 first_cluster_group_blkno;
219 u32 fs_generation; 231 u32 fs_generation;
@@ -241,6 +253,7 @@ struct ocfs2_super
241 int s_sectsize_bits; 253 int s_sectsize_bits;
242 int s_clustersize; 254 int s_clustersize;
243 int s_clustersize_bits; 255 int s_clustersize_bits;
256 unsigned int s_xattr_inline_size;
244 257
245 atomic_t vol_state; 258 atomic_t vol_state;
246 struct mutex recovery_lock; 259 struct mutex recovery_lock;
@@ -252,11 +265,27 @@ struct ocfs2_super
252 struct ocfs2_journal *journal; 265 struct ocfs2_journal *journal;
253 unsigned long osb_commit_interval; 266 unsigned long osb_commit_interval;
254 267
255 int local_alloc_size; 268 struct delayed_work la_enable_wq;
256 enum ocfs2_local_alloc_state local_alloc_state; 269
270 /*
271 * Must hold local alloc i_mutex and osb->osb_lock to change
272 * local_alloc_bits. Reads can be done under either lock.
273 */
274 unsigned int local_alloc_bits;
275 unsigned int local_alloc_default_bits;
276
277 enum ocfs2_local_alloc_state local_alloc_state; /* protected
278 * by osb_lock */
279
257 struct buffer_head *local_alloc_bh; 280 struct buffer_head *local_alloc_bh;
281
258 u64 la_last_gd; 282 u64 la_last_gd;
259 283
284#ifdef CONFIG_OCFS2_FS_STATS
285 struct dentry *local_alloc_debug;
286 char *local_alloc_debug_buf;
287#endif
288
260 /* Next two fields are for local node slot recovery during 289 /* Next two fields are for local node slot recovery during
261 * mount. */ 290 * mount. */
262 int dirty; 291 int dirty;
@@ -340,6 +369,13 @@ static inline int ocfs2_supports_inline_data(struct ocfs2_super *osb)
340 return 0; 369 return 0;
341} 370}
342 371
372static inline int ocfs2_supports_xattr(struct ocfs2_super *osb)
373{
374 if (osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR)
375 return 1;
376 return 0;
377}
378
343/* set / clear functions because cluster events can make these happen 379/* set / clear functions because cluster events can make these happen
344 * in parallel so we want the transitions to be atomic. this also 380 * in parallel so we want the transitions to be atomic. this also
345 * means that any future flags osb_flags must be protected by spinlock 381 * means that any future flags osb_flags must be protected by spinlock
@@ -554,6 +590,14 @@ static inline unsigned int ocfs2_pages_per_cluster(struct super_block *sb)
554 return pages_per_cluster; 590 return pages_per_cluster;
555} 591}
556 592
593static inline unsigned int ocfs2_megabytes_to_clusters(struct super_block *sb,
594 unsigned int megs)
595{
596 BUILD_BUG_ON(OCFS2_MAX_CLUSTERSIZE > 1048576);
597
598 return megs << (20 - OCFS2_SB(sb)->s_clustersize_bits);
599}
600
557static inline void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb) 601static inline void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
558{ 602{
559 spin_lock(&osb->osb_lock); 603 spin_lock(&osb->osb_lock);
diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h
index 4f619850ccf7..f24ce3d3f956 100644
--- a/fs/ocfs2/ocfs2_fs.h
+++ b/fs/ocfs2/ocfs2_fs.h
@@ -64,6 +64,7 @@
64#define OCFS2_INODE_SIGNATURE "INODE01" 64#define OCFS2_INODE_SIGNATURE "INODE01"
65#define OCFS2_EXTENT_BLOCK_SIGNATURE "EXBLK01" 65#define OCFS2_EXTENT_BLOCK_SIGNATURE "EXBLK01"
66#define OCFS2_GROUP_DESC_SIGNATURE "GROUP01" 66#define OCFS2_GROUP_DESC_SIGNATURE "GROUP01"
67#define OCFS2_XATTR_BLOCK_SIGNATURE "XATTR01"
67 68
68/* Compatibility flags */ 69/* Compatibility flags */
69#define OCFS2_HAS_COMPAT_FEATURE(sb,mask) \ 70#define OCFS2_HAS_COMPAT_FEATURE(sb,mask) \
@@ -90,7 +91,8 @@
90 | OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC \ 91 | OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC \
91 | OCFS2_FEATURE_INCOMPAT_INLINE_DATA \ 92 | OCFS2_FEATURE_INCOMPAT_INLINE_DATA \
92 | OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP \ 93 | OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP \
93 | OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK) 94 | OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK \
95 | OCFS2_FEATURE_INCOMPAT_XATTR)
94#define OCFS2_FEATURE_RO_COMPAT_SUPP OCFS2_FEATURE_RO_COMPAT_UNWRITTEN 96#define OCFS2_FEATURE_RO_COMPAT_SUPP OCFS2_FEATURE_RO_COMPAT_UNWRITTEN
95 97
96/* 98/*
@@ -127,10 +129,6 @@
127/* Support for data packed into inode blocks */ 129/* Support for data packed into inode blocks */
128#define OCFS2_FEATURE_INCOMPAT_INLINE_DATA 0x0040 130#define OCFS2_FEATURE_INCOMPAT_INLINE_DATA 0x0040
129 131
130/* Support for the extended slot map */
131#define OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP 0x100
132
133
134/* 132/*
135 * Support for alternate, userspace cluster stacks. If set, the superblock 133 * Support for alternate, userspace cluster stacks. If set, the superblock
136 * field s_cluster_info contains a tag for the alternate stack in use as 134 * field s_cluster_info contains a tag for the alternate stack in use as
@@ -142,6 +140,12 @@
142 */ 140 */
143#define OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK 0x0080 141#define OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK 0x0080
144 142
143/* Support for the extended slot map */
144#define OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP 0x100
145
146/* Support for extended attributes */
147#define OCFS2_FEATURE_INCOMPAT_XATTR 0x0200
148
145/* 149/*
146 * backup superblock flag is used to indicate that this volume 150 * backup superblock flag is used to indicate that this volume
147 * has backup superblocks. 151 * has backup superblocks.
@@ -299,6 +303,12 @@ struct ocfs2_new_group_input {
299 */ 303 */
300#define OCFS2_DEFAULT_LOCAL_ALLOC_SIZE 8 304#define OCFS2_DEFAULT_LOCAL_ALLOC_SIZE 8
301 305
306/*
307 * Inline extended attribute size (in bytes)
308 * The value chosen should be aligned to 16 byte boundaries.
309 */
310#define OCFS2_MIN_XATTR_INLINE_SIZE 256
311
302struct ocfs2_system_inode_info { 312struct ocfs2_system_inode_info {
303 char *si_name; 313 char *si_name;
304 int si_iflags; 314 int si_iflags;
@@ -563,7 +573,7 @@ struct ocfs2_super_block {
563/*40*/ __le16 s_max_slots; /* Max number of simultaneous mounts 573/*40*/ __le16 s_max_slots; /* Max number of simultaneous mounts
564 before tunefs required */ 574 before tunefs required */
565 __le16 s_tunefs_flag; 575 __le16 s_tunefs_flag;
566 __le32 s_reserved1; 576 __le32 s_uuid_hash; /* hash value of uuid */
567 __le64 s_first_cluster_group; /* Block offset of 1st cluster 577 __le64 s_first_cluster_group; /* Block offset of 1st cluster
568 * group header */ 578 * group header */
569/*50*/ __u8 s_label[OCFS2_MAX_VOL_LABEL_LEN]; /* Label for mounting, etc. */ 579/*50*/ __u8 s_label[OCFS2_MAX_VOL_LABEL_LEN]; /* Label for mounting, etc. */
@@ -571,7 +581,11 @@ struct ocfs2_super_block {
571/*A0*/ struct ocfs2_cluster_info s_cluster_info; /* Selected userspace 581/*A0*/ struct ocfs2_cluster_info s_cluster_info; /* Selected userspace
572 stack. Only valid 582 stack. Only valid
573 with INCOMPAT flag. */ 583 with INCOMPAT flag. */
574/*B8*/ __le64 s_reserved2[17]; /* Fill out superblock */ 584/*B8*/ __le16 s_xattr_inline_size; /* extended attribute inline size
585 for this fs*/
586 __le16 s_reserved0;
587 __le32 s_reserved1;
588/*C0*/ __le64 s_reserved2[16]; /* Fill out superblock */
575/*140*/ 589/*140*/
576 590
577 /* 591 /*
@@ -621,7 +635,8 @@ struct ocfs2_dinode {
621 belongs to */ 635 belongs to */
622 __le16 i_suballoc_bit; /* Bit offset in suballocator 636 __le16 i_suballoc_bit; /* Bit offset in suballocator
623 block group */ 637 block group */
624/*10*/ __le32 i_reserved0; 638/*10*/ __le16 i_reserved0;
639 __le16 i_xattr_inline_size;
625 __le32 i_clusters; /* Cluster count */ 640 __le32 i_clusters; /* Cluster count */
626 __le32 i_uid; /* Owner UID */ 641 __le32 i_uid; /* Owner UID */
627 __le32 i_gid; /* Owning GID */ 642 __le32 i_gid; /* Owning GID */
@@ -640,11 +655,12 @@ struct ocfs2_dinode {
640 __le32 i_atime_nsec; 655 __le32 i_atime_nsec;
641 __le32 i_ctime_nsec; 656 __le32 i_ctime_nsec;
642 __le32 i_mtime_nsec; 657 __le32 i_mtime_nsec;
643 __le32 i_attr; 658/*70*/ __le32 i_attr;
644 __le16 i_orphaned_slot; /* Only valid when OCFS2_ORPHANED_FL 659 __le16 i_orphaned_slot; /* Only valid when OCFS2_ORPHANED_FL
645 was set in i_flags */ 660 was set in i_flags */
646 __le16 i_dyn_features; 661 __le16 i_dyn_features;
647/*70*/ __le64 i_reserved2[8]; 662 __le64 i_xattr_loc;
663/*80*/ __le64 i_reserved2[7];
648/*B8*/ union { 664/*B8*/ union {
649 __le64 i_pad1; /* Generic way to refer to this 665 __le64 i_pad1; /* Generic way to refer to this
650 64bit union */ 666 64bit union */
@@ -715,6 +731,136 @@ struct ocfs2_group_desc
715/*40*/ __u8 bg_bitmap[0]; 731/*40*/ __u8 bg_bitmap[0];
716}; 732};
717 733
734/*
735 * On disk extended attribute structure for OCFS2.
736 */
737
738/*
739 * ocfs2_xattr_entry indicates one extend attribute.
740 *
741 * Note that it can be stored in inode, one block or one xattr bucket.
742 */
743struct ocfs2_xattr_entry {
744 __le32 xe_name_hash; /* hash value of xattr prefix+suffix. */
745 __le16 xe_name_offset; /* byte offset from the 1st etnry in the local
746 local xattr storage(inode, xattr block or
747 xattr bucket). */
748 __u8 xe_name_len; /* xattr name len, does't include prefix. */
749 __u8 xe_type; /* the low 7 bits indicates the name prefix's
750 * type and the highest 1 bits indicate whether
751 * the EA is stored in the local storage. */
752 __le64 xe_value_size; /* real xattr value length. */
753};
754
755/*
756 * On disk structure for xattr header.
757 *
758 * One ocfs2_xattr_header describes how many ocfs2_xattr_entry records in
759 * the local xattr storage.
760 */
761struct ocfs2_xattr_header {
762 __le16 xh_count; /* contains the count of how
763 many records are in the
764 local xattr storage. */
765 __le16 xh_free_start; /* current offset for storing
766 xattr. */
767 __le16 xh_name_value_len; /* total length of name/value
768 length in this bucket. */
769 __le16 xh_num_buckets; /* bucket nums in one extent
770 record, only valid in the
771 first bucket. */
772 __le64 xh_csum;
773 struct ocfs2_xattr_entry xh_entries[0]; /* xattr entry list. */
774};
775
776/*
777 * On disk structure for xattr value root.
778 *
779 * It is used when one extended attribute's size is larger, and we will save it
780 * in an outside cluster. It will stored in a b-tree like file content.
781 */
782struct ocfs2_xattr_value_root {
783/*00*/ __le32 xr_clusters; /* clusters covered by xattr value. */
784 __le32 xr_reserved0;
785 __le64 xr_last_eb_blk; /* Pointer to last extent block */
786/*10*/ struct ocfs2_extent_list xr_list; /* Extent record list */
787};
788
789/*
790 * On disk structure for xattr tree root.
791 *
792 * It is used when there are too many extended attributes for one file. These
793 * attributes will be organized and stored in an indexed-btree.
794 */
795struct ocfs2_xattr_tree_root {
796/*00*/ __le32 xt_clusters; /* clusters covered by xattr. */
797 __le32 xt_reserved0;
798 __le64 xt_last_eb_blk; /* Pointer to last extent block */
799/*10*/ struct ocfs2_extent_list xt_list; /* Extent record list */
800};
801
802#define OCFS2_XATTR_INDEXED 0x1
803#define OCFS2_HASH_SHIFT 5
804#define OCFS2_XATTR_ROUND 3
805#define OCFS2_XATTR_SIZE(size) (((size) + OCFS2_XATTR_ROUND) & \
806 ~(OCFS2_XATTR_ROUND))
807
808#define OCFS2_XATTR_BUCKET_SIZE 4096
809#define OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET (OCFS2_XATTR_BUCKET_SIZE \
810 / OCFS2_MIN_BLOCKSIZE)
811
812/*
813 * On disk structure for xattr block.
814 */
815struct ocfs2_xattr_block {
816/*00*/ __u8 xb_signature[8]; /* Signature for verification */
817 __le16 xb_suballoc_slot; /* Slot suballocator this
818 block belongs to. */
819 __le16 xb_suballoc_bit; /* Bit offset in suballocator
820 block group */
821 __le32 xb_fs_generation; /* Must match super block */
822/*10*/ __le64 xb_blkno; /* Offset on disk, in blocks */
823 __le64 xb_csum;
824/*20*/ __le16 xb_flags; /* Indicates whether this block contains
825 real xattr or a xattr tree. */
826 __le16 xb_reserved0;
827 __le32 xb_reserved1;
828 __le64 xb_reserved2;
829/*30*/ union {
830 struct ocfs2_xattr_header xb_header; /* xattr header if this
831 block contains xattr */
832 struct ocfs2_xattr_tree_root xb_root;/* xattr tree root if this
833 block cotains xattr
834 tree. */
835 } xb_attrs;
836};
837
838#define OCFS2_XATTR_ENTRY_LOCAL 0x80
839#define OCFS2_XATTR_TYPE_MASK 0x7F
840static inline void ocfs2_xattr_set_local(struct ocfs2_xattr_entry *xe,
841 int local)
842{
843 if (local)
844 xe->xe_type |= OCFS2_XATTR_ENTRY_LOCAL;
845 else
846 xe->xe_type &= ~OCFS2_XATTR_ENTRY_LOCAL;
847}
848
849static inline int ocfs2_xattr_is_local(struct ocfs2_xattr_entry *xe)
850{
851 return xe->xe_type & OCFS2_XATTR_ENTRY_LOCAL;
852}
853
854static inline void ocfs2_xattr_set_type(struct ocfs2_xattr_entry *xe, int type)
855{
856 xe->xe_type |= type & OCFS2_XATTR_TYPE_MASK;
857}
858
859static inline int ocfs2_xattr_get_type(struct ocfs2_xattr_entry *xe)
860{
861 return xe->xe_type & OCFS2_XATTR_TYPE_MASK;
862}
863
718#ifdef __KERNEL__ 864#ifdef __KERNEL__
719static inline int ocfs2_fast_symlink_chars(struct super_block *sb) 865static inline int ocfs2_fast_symlink_chars(struct super_block *sb)
720{ 866{
@@ -728,6 +874,20 @@ static inline int ocfs2_max_inline_data(struct super_block *sb)
728 offsetof(struct ocfs2_dinode, id2.i_data.id_data); 874 offsetof(struct ocfs2_dinode, id2.i_data.id_data);
729} 875}
730 876
877static inline int ocfs2_max_inline_data_with_xattr(struct super_block *sb,
878 struct ocfs2_dinode *di)
879{
880 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
881
882 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
883 return sb->s_blocksize -
884 offsetof(struct ocfs2_dinode, id2.i_data.id_data) -
885 xattrsize;
886 else
887 return sb->s_blocksize -
888 offsetof(struct ocfs2_dinode, id2.i_data.id_data);
889}
890
731static inline int ocfs2_extent_recs_per_inode(struct super_block *sb) 891static inline int ocfs2_extent_recs_per_inode(struct super_block *sb)
732{ 892{
733 int size; 893 int size;
@@ -738,6 +898,24 @@ static inline int ocfs2_extent_recs_per_inode(struct super_block *sb)
738 return size / sizeof(struct ocfs2_extent_rec); 898 return size / sizeof(struct ocfs2_extent_rec);
739} 899}
740 900
901static inline int ocfs2_extent_recs_per_inode_with_xattr(
902 struct super_block *sb,
903 struct ocfs2_dinode *di)
904{
905 int size;
906 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
907
908 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
909 size = sb->s_blocksize -
910 offsetof(struct ocfs2_dinode, id2.i_list.l_recs) -
911 xattrsize;
912 else
913 size = sb->s_blocksize -
914 offsetof(struct ocfs2_dinode, id2.i_list.l_recs);
915
916 return size / sizeof(struct ocfs2_extent_rec);
917}
918
741static inline int ocfs2_chain_recs_per_inode(struct super_block *sb) 919static inline int ocfs2_chain_recs_per_inode(struct super_block *sb)
742{ 920{
743 int size; 921 int size;
@@ -801,6 +979,17 @@ static inline u64 ocfs2_backup_super_blkno(struct super_block *sb, int index)
801 return 0; 979 return 0;
802 980
803} 981}
982
983static inline u16 ocfs2_xattr_recs_per_xb(struct super_block *sb)
984{
985 int size;
986
987 size = sb->s_blocksize -
988 offsetof(struct ocfs2_xattr_block,
989 xb_attrs.xb_root.xt_list.l_recs);
990
991 return size / sizeof(struct ocfs2_extent_rec);
992}
804#else 993#else
805static inline int ocfs2_fast_symlink_chars(int blocksize) 994static inline int ocfs2_fast_symlink_chars(int blocksize)
806{ 995{
@@ -884,6 +1073,17 @@ static inline uint64_t ocfs2_backup_super_blkno(int blocksize, int index)
884 1073
885 return 0; 1074 return 0;
886} 1075}
1076
1077static inline int ocfs2_xattr_recs_per_xb(int blocksize)
1078{
1079 int size;
1080
1081 size = blocksize -
1082 offsetof(struct ocfs2_xattr_block,
1083 xb_attrs.xb_root.xt_list.l_recs);
1084
1085 return size / sizeof(struct ocfs2_extent_rec);
1086}
887#endif /* __KERNEL__ */ 1087#endif /* __KERNEL__ */
888 1088
889 1089
diff --git a/fs/ocfs2/ocfs2_jbd_compat.h b/fs/ocfs2/ocfs2_jbd_compat.h
new file mode 100644
index 000000000000..b91c78f8f558
--- /dev/null
+++ b/fs/ocfs2/ocfs2_jbd_compat.h
@@ -0,0 +1,82 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * ocfs2_jbd_compat.h
5 *
6 * Compatibility defines for JBD.
7 *
8 * Copyright (C) 2008 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 */
19
20#ifndef OCFS2_JBD_COMPAT_H
21#define OCFS2_JBD_COMPAT_H
22
23#ifndef CONFIG_OCFS2_COMPAT_JBD
24# error Should not have been included
25#endif
26
27struct jbd2_inode {
28 unsigned int dummy;
29};
30
31#define JBD2_BARRIER JFS_BARRIER
32#define JBD2_DEFAULT_MAX_COMMIT_AGE JBD_DEFAULT_MAX_COMMIT_AGE
33
34#define jbd2_journal_ack_err journal_ack_err
35#define jbd2_journal_clear_err journal_clear_err
36#define jbd2_journal_destroy journal_destroy
37#define jbd2_journal_dirty_metadata journal_dirty_metadata
38#define jbd2_journal_errno journal_errno
39#define jbd2_journal_extend journal_extend
40#define jbd2_journal_flush journal_flush
41#define jbd2_journal_force_commit journal_force_commit
42#define jbd2_journal_get_write_access journal_get_write_access
43#define jbd2_journal_get_undo_access journal_get_undo_access
44#define jbd2_journal_init_inode journal_init_inode
45#define jbd2_journal_invalidatepage journal_invalidatepage
46#define jbd2_journal_load journal_load
47#define jbd2_journal_lock_updates journal_lock_updates
48#define jbd2_journal_restart journal_restart
49#define jbd2_journal_start journal_start
50#define jbd2_journal_start_commit journal_start_commit
51#define jbd2_journal_stop journal_stop
52#define jbd2_journal_try_to_free_buffers journal_try_to_free_buffers
53#define jbd2_journal_unlock_updates journal_unlock_updates
54#define jbd2_journal_wipe journal_wipe
55#define jbd2_log_wait_commit log_wait_commit
56
57static inline int jbd2_journal_file_inode(handle_t *handle,
58 struct jbd2_inode *inode)
59{
60 return 0;
61}
62
63static inline int jbd2_journal_begin_ordered_truncate(struct jbd2_inode *inode,
64 loff_t new_size)
65{
66 return 0;
67}
68
69static inline void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode,
70 struct inode *inode)
71{
72 return;
73}
74
75static inline void jbd2_journal_release_jbd_inode(journal_t *journal,
76 struct jbd2_inode *jinode)
77{
78 return;
79}
80
81
82#endif /* OCFS2_JBD_COMPAT_H */
diff --git a/fs/ocfs2/resize.c b/fs/ocfs2/resize.c
index 8166968e9015..ffd48db229a7 100644
--- a/fs/ocfs2/resize.c
+++ b/fs/ocfs2/resize.c
@@ -200,7 +200,7 @@ static int update_backups(struct inode * inode, u32 clusters, char *data)
200 if (cluster > clusters) 200 if (cluster > clusters)
201 break; 201 break;
202 202
203 ret = ocfs2_read_block(osb, blkno, &backup, 0, NULL); 203 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
204 if (ret < 0) { 204 if (ret < 0) {
205 mlog_errno(ret); 205 mlog_errno(ret);
206 break; 206 break;
@@ -236,8 +236,8 @@ static void ocfs2_update_super_and_backups(struct inode *inode,
236 * update the superblock last. 236 * update the superblock last.
237 * It doesn't matter if the write failed. 237 * It doesn't matter if the write failed.
238 */ 238 */
239 ret = ocfs2_read_block(osb, OCFS2_SUPER_BLOCK_BLKNO, 239 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
240 &super_bh, 0, NULL); 240 &super_bh);
241 if (ret < 0) { 241 if (ret < 0) {
242 mlog_errno(ret); 242 mlog_errno(ret);
243 goto out; 243 goto out;
@@ -332,8 +332,7 @@ int ocfs2_group_extend(struct inode * inode, int new_clusters)
332 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode, 332 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
333 first_new_cluster - 1); 333 first_new_cluster - 1);
334 334
335 ret = ocfs2_read_block(osb, lgd_blkno, &group_bh, OCFS2_BH_CACHED, 335 ret = ocfs2_read_block(main_bm_inode, lgd_blkno, &group_bh);
336 main_bm_inode);
337 if (ret < 0) { 336 if (ret < 0) {
338 mlog_errno(ret); 337 mlog_errno(ret);
339 goto out_unlock; 338 goto out_unlock;
@@ -540,7 +539,7 @@ int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
540 goto out_unlock; 539 goto out_unlock;
541 } 540 }
542 541
543 ret = ocfs2_read_block(osb, input->group, &group_bh, 0, NULL); 542 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
544 if (ret < 0) { 543 if (ret < 0) {
545 mlog(ML_ERROR, "Can't read the group descriptor # %llu " 544 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
546 "from the device.", (unsigned long long)input->group); 545 "from the device.", (unsigned long long)input->group);
diff --git a/fs/ocfs2/slot_map.c b/fs/ocfs2/slot_map.c
index bb5ff8939bf1..bdda2d8f8508 100644
--- a/fs/ocfs2/slot_map.c
+++ b/fs/ocfs2/slot_map.c
@@ -150,8 +150,8 @@ int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
150 * be !NULL. Thus, ocfs2_read_blocks() will ignore blocknr. If 150 * be !NULL. Thus, ocfs2_read_blocks() will ignore blocknr. If
151 * this is not true, the read of -1 (UINT64_MAX) will fail. 151 * this is not true, the read of -1 (UINT64_MAX) will fail.
152 */ 152 */
153 ret = ocfs2_read_blocks(osb, -1, si->si_blocks, si->si_bh, 0, 153 ret = ocfs2_read_blocks(si->si_inode, -1, si->si_blocks, si->si_bh,
154 si->si_inode); 154 OCFS2_BH_IGNORE_CACHE);
155 if (ret == 0) { 155 if (ret == 0) {
156 spin_lock(&osb->osb_lock); 156 spin_lock(&osb->osb_lock);
157 ocfs2_update_slot_info(si); 157 ocfs2_update_slot_info(si);
@@ -404,7 +404,8 @@ static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
404 (unsigned long long)blkno); 404 (unsigned long long)blkno);
405 405
406 bh = NULL; /* Acquire a fresh bh */ 406 bh = NULL; /* Acquire a fresh bh */
407 status = ocfs2_read_block(osb, blkno, &bh, 0, si->si_inode); 407 status = ocfs2_read_blocks(si->si_inode, blkno, 1, &bh,
408 OCFS2_BH_IGNORE_CACHE);
408 if (status < 0) { 409 if (status < 0) {
409 mlog_errno(status); 410 mlog_errno(status);
410 goto bail; 411 goto bail;
diff --git a/fs/ocfs2/stack_user.c b/fs/ocfs2/stack_user.c
index 353fc35c6748..faec2d879357 100644
--- a/fs/ocfs2/stack_user.c
+++ b/fs/ocfs2/stack_user.c
@@ -28,6 +28,7 @@
28#include "ocfs2.h" /* For struct ocfs2_lock_res */ 28#include "ocfs2.h" /* For struct ocfs2_lock_res */
29#include "stackglue.h" 29#include "stackglue.h"
30 30
31#include <linux/dlm_plock.h>
31 32
32/* 33/*
33 * The control protocol starts with a handshake. Until the handshake 34 * The control protocol starts with a handshake. Until the handshake
@@ -746,6 +747,37 @@ static void user_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
746{ 747{
747} 748}
748 749
750static int user_plock(struct ocfs2_cluster_connection *conn,
751 u64 ino,
752 struct file *file,
753 int cmd,
754 struct file_lock *fl)
755{
756 /*
757 * This more or less just demuxes the plock request into any
758 * one of three dlm calls.
759 *
760 * Internally, fs/dlm will pass these to a misc device, which
761 * a userspace daemon will read and write to.
762 *
763 * For now, cancel requests (which happen internally only),
764 * are turned into unlocks. Most of this function taken from
765 * gfs2_lock.
766 */
767
768 if (cmd == F_CANCELLK) {
769 cmd = F_SETLK;
770 fl->fl_type = F_UNLCK;
771 }
772
773 if (IS_GETLK(cmd))
774 return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
775 else if (fl->fl_type == F_UNLCK)
776 return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
777 else
778 return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
779}
780
749/* 781/*
750 * Compare a requested locking protocol version against the current one. 782 * Compare a requested locking protocol version against the current one.
751 * 783 *
@@ -839,6 +871,7 @@ static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
839 .dlm_unlock = user_dlm_unlock, 871 .dlm_unlock = user_dlm_unlock,
840 .lock_status = user_dlm_lock_status, 872 .lock_status = user_dlm_lock_status,
841 .lock_lvb = user_dlm_lvb, 873 .lock_lvb = user_dlm_lvb,
874 .plock = user_plock,
842 .dump_lksb = user_dlm_dump_lksb, 875 .dump_lksb = user_dlm_dump_lksb,
843}; 876};
844 877
diff --git a/fs/ocfs2/stackglue.c b/fs/ocfs2/stackglue.c
index 07f348b8d721..68b668b0e60a 100644
--- a/fs/ocfs2/stackglue.c
+++ b/fs/ocfs2/stackglue.c
@@ -288,6 +288,26 @@ void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
288} 288}
289EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb); 289EXPORT_SYMBOL_GPL(ocfs2_dlm_dump_lksb);
290 290
291int ocfs2_stack_supports_plocks(void)
292{
293 return active_stack && active_stack->sp_ops->plock;
294}
295EXPORT_SYMBOL_GPL(ocfs2_stack_supports_plocks);
296
297/*
298 * ocfs2_plock() can only be safely called if
299 * ocfs2_stack_supports_plocks() returned true
300 */
301int ocfs2_plock(struct ocfs2_cluster_connection *conn, u64 ino,
302 struct file *file, int cmd, struct file_lock *fl)
303{
304 WARN_ON_ONCE(active_stack->sp_ops->plock == NULL);
305 if (active_stack->sp_ops->plock)
306 return active_stack->sp_ops->plock(conn, ino, file, cmd, fl);
307 return -EOPNOTSUPP;
308}
309EXPORT_SYMBOL_GPL(ocfs2_plock);
310
291int ocfs2_cluster_connect(const char *stack_name, 311int ocfs2_cluster_connect(const char *stack_name,
292 const char *group, 312 const char *group,
293 int grouplen, 313 int grouplen,
diff --git a/fs/ocfs2/stackglue.h b/fs/ocfs2/stackglue.h
index db56281dd1be..c571af375ef8 100644
--- a/fs/ocfs2/stackglue.h
+++ b/fs/ocfs2/stackglue.h
@@ -28,6 +28,10 @@
28#include "dlm/dlmapi.h" 28#include "dlm/dlmapi.h"
29#include <linux/dlm.h> 29#include <linux/dlm.h>
30 30
31/* Needed for plock-related prototypes */
32struct file;
33struct file_lock;
34
31/* 35/*
32 * dlmconstants.h does not have a LOCAL flag. We hope to remove it 36 * dlmconstants.h does not have a LOCAL flag. We hope to remove it
33 * some day, but right now we need it. Let's fake it. This value is larger 37 * some day, but right now we need it. Let's fake it. This value is larger
@@ -187,6 +191,17 @@ struct ocfs2_stack_operations {
187 void *(*lock_lvb)(union ocfs2_dlm_lksb *lksb); 191 void *(*lock_lvb)(union ocfs2_dlm_lksb *lksb);
188 192
189 /* 193 /*
194 * Cluster-aware posix locks
195 *
196 * This is NULL for stacks which do not support posix locks.
197 */
198 int (*plock)(struct ocfs2_cluster_connection *conn,
199 u64 ino,
200 struct file *file,
201 int cmd,
202 struct file_lock *fl);
203
204 /*
190 * This is an optoinal debugging hook. If provided, the 205 * This is an optoinal debugging hook. If provided, the
191 * stack can dump debugging information about this lock. 206 * stack can dump debugging information about this lock.
192 */ 207 */
@@ -240,6 +255,10 @@ int ocfs2_dlm_lock_status(union ocfs2_dlm_lksb *lksb);
240void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb); 255void *ocfs2_dlm_lvb(union ocfs2_dlm_lksb *lksb);
241void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb); 256void ocfs2_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb);
242 257
258int ocfs2_stack_supports_plocks(void);
259int ocfs2_plock(struct ocfs2_cluster_connection *conn, u64 ino,
260 struct file *file, int cmd, struct file_lock *fl);
261
243void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto); 262void ocfs2_stack_glue_set_locking_protocol(struct ocfs2_locking_protocol *proto);
244 263
245 264
diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c
index d2d278fb9819..c5ff18b46b57 100644
--- a/fs/ocfs2/suballoc.c
+++ b/fs/ocfs2/suballoc.c
@@ -62,15 +62,18 @@ static int ocfs2_block_group_fill(handle_t *handle,
62 struct ocfs2_chain_list *cl); 62 struct ocfs2_chain_list *cl);
63static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 63static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode, 64 struct inode *alloc_inode,
65 struct buffer_head *bh); 65 struct buffer_head *bh,
66 u64 max_block);
66 67
67static int ocfs2_cluster_group_search(struct inode *inode, 68static int ocfs2_cluster_group_search(struct inode *inode,
68 struct buffer_head *group_bh, 69 struct buffer_head *group_bh,
69 u32 bits_wanted, u32 min_bits, 70 u32 bits_wanted, u32 min_bits,
71 u64 max_block,
70 u16 *bit_off, u16 *bits_found); 72 u16 *bit_off, u16 *bits_found);
71static int ocfs2_block_group_search(struct inode *inode, 73static int ocfs2_block_group_search(struct inode *inode,
72 struct buffer_head *group_bh, 74 struct buffer_head *group_bh,
73 u32 bits_wanted, u32 min_bits, 75 u32 bits_wanted, u32 min_bits,
76 u64 max_block,
74 u16 *bit_off, u16 *bits_found); 77 u16 *bit_off, u16 *bits_found);
75static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb, 78static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
76 struct ocfs2_alloc_context *ac, 79 struct ocfs2_alloc_context *ac,
@@ -110,8 +113,11 @@ static inline void ocfs2_block_to_cluster_group(struct inode *inode,
110 u64 data_blkno, 113 u64 data_blkno,
111 u64 *bg_blkno, 114 u64 *bg_blkno,
112 u16 *bg_bit_off); 115 u16 *bg_bit_off);
116static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
117 u32 bits_wanted, u64 max_block,
118 struct ocfs2_alloc_context **ac);
113 119
114static void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac) 120void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
115{ 121{
116 struct inode *inode = ac->ac_inode; 122 struct inode *inode = ac->ac_inode;
117 123
@@ -124,10 +130,8 @@ static void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
124 iput(inode); 130 iput(inode);
125 ac->ac_inode = NULL; 131 ac->ac_inode = NULL;
126 } 132 }
127 if (ac->ac_bh) { 133 brelse(ac->ac_bh);
128 brelse(ac->ac_bh); 134 ac->ac_bh = NULL;
129 ac->ac_bh = NULL;
130 }
131} 135}
132 136
133void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac) 137void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
@@ -276,7 +280,8 @@ static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
276 */ 280 */
277static int ocfs2_block_group_alloc(struct ocfs2_super *osb, 281static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
278 struct inode *alloc_inode, 282 struct inode *alloc_inode,
279 struct buffer_head *bh) 283 struct buffer_head *bh,
284 u64 max_block)
280{ 285{
281 int status, credits; 286 int status, credits;
282 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 287 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
@@ -294,9 +299,9 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
294 mlog_entry_void(); 299 mlog_entry_void();
295 300
296 cl = &fe->id2.i_chain; 301 cl = &fe->id2.i_chain;
297 status = ocfs2_reserve_clusters(osb, 302 status = ocfs2_reserve_clusters_with_limit(osb,
298 le16_to_cpu(cl->cl_cpg), 303 le16_to_cpu(cl->cl_cpg),
299 &ac); 304 max_block, &ac);
300 if (status < 0) { 305 if (status < 0) {
301 if (status != -ENOSPC) 306 if (status != -ENOSPC)
302 mlog_errno(status); 307 mlog_errno(status);
@@ -394,8 +399,7 @@ bail:
394 if (ac) 399 if (ac)
395 ocfs2_free_alloc_context(ac); 400 ocfs2_free_alloc_context(ac);
396 401
397 if (bg_bh) 402 brelse(bg_bh);
398 brelse(bg_bh);
399 403
400 mlog_exit(status); 404 mlog_exit(status);
401 return status; 405 return status;
@@ -469,7 +473,8 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
469 goto bail; 473 goto bail;
470 } 474 }
471 475
472 status = ocfs2_block_group_alloc(osb, alloc_inode, bh); 476 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
477 ac->ac_max_block);
473 if (status < 0) { 478 if (status < 0) {
474 if (status != -ENOSPC) 479 if (status != -ENOSPC)
475 mlog_errno(status); 480 mlog_errno(status);
@@ -486,16 +491,15 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
486 get_bh(bh); 491 get_bh(bh);
487 ac->ac_bh = bh; 492 ac->ac_bh = bh;
488bail: 493bail:
489 if (bh) 494 brelse(bh);
490 brelse(bh);
491 495
492 mlog_exit(status); 496 mlog_exit(status);
493 return status; 497 return status;
494} 498}
495 499
496int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 500int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
497 struct ocfs2_dinode *fe, 501 int blocks,
498 struct ocfs2_alloc_context **ac) 502 struct ocfs2_alloc_context **ac)
499{ 503{
500 int status; 504 int status;
501 u32 slot; 505 u32 slot;
@@ -507,7 +511,7 @@ int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
507 goto bail; 511 goto bail;
508 } 512 }
509 513
510 (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe); 514 (*ac)->ac_bits_wanted = blocks;
511 (*ac)->ac_which = OCFS2_AC_USE_META; 515 (*ac)->ac_which = OCFS2_AC_USE_META;
512 slot = osb->slot_num; 516 slot = osb->slot_num;
513 (*ac)->ac_group_search = ocfs2_block_group_search; 517 (*ac)->ac_group_search = ocfs2_block_group_search;
@@ -532,6 +536,15 @@ bail:
532 return status; 536 return status;
533} 537}
534 538
539int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
540 struct ocfs2_extent_list *root_el,
541 struct ocfs2_alloc_context **ac)
542{
543 return ocfs2_reserve_new_metadata_blocks(osb,
544 ocfs2_extend_meta_needed(root_el),
545 ac);
546}
547
535static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb, 548static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
536 struct ocfs2_alloc_context *ac) 549 struct ocfs2_alloc_context *ac)
537{ 550{
@@ -582,6 +595,14 @@ int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
582 (*ac)->ac_group_search = ocfs2_block_group_search; 595 (*ac)->ac_group_search = ocfs2_block_group_search;
583 596
584 /* 597 /*
598 * stat(2) can't handle i_ino > 32bits, so we tell the
599 * lower levels not to allocate us a block group past that
600 * limit. The 'inode64' mount option avoids this behavior.
601 */
602 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
603 (*ac)->ac_max_block = (u32)~0U;
604
605 /*
585 * slot is set when we successfully steal inode from other nodes. 606 * slot is set when we successfully steal inode from other nodes.
586 * It is reset in 3 places: 607 * It is reset in 3 places:
587 * 1. when we flush the truncate log 608 * 1. when we flush the truncate log
@@ -661,9 +682,9 @@ bail:
661/* Callers don't need to care which bitmap (local alloc or main) to 682/* Callers don't need to care which bitmap (local alloc or main) to
662 * use so we figure it out for them, but unfortunately this clutters 683 * use so we figure it out for them, but unfortunately this clutters
663 * things a bit. */ 684 * things a bit. */
664int ocfs2_reserve_clusters(struct ocfs2_super *osb, 685static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
665 u32 bits_wanted, 686 u32 bits_wanted, u64 max_block,
666 struct ocfs2_alloc_context **ac) 687 struct ocfs2_alloc_context **ac)
667{ 688{
668 int status; 689 int status;
669 690
@@ -677,24 +698,20 @@ int ocfs2_reserve_clusters(struct ocfs2_super *osb,
677 } 698 }
678 699
679 (*ac)->ac_bits_wanted = bits_wanted; 700 (*ac)->ac_bits_wanted = bits_wanted;
701 (*ac)->ac_max_block = max_block;
680 702
681 status = -ENOSPC; 703 status = -ENOSPC;
682 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) { 704 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
683 status = ocfs2_reserve_local_alloc_bits(osb, 705 status = ocfs2_reserve_local_alloc_bits(osb,
684 bits_wanted, 706 bits_wanted,
685 *ac); 707 *ac);
686 if ((status < 0) && (status != -ENOSPC)) { 708 if (status == -EFBIG) {
709 /* The local alloc window is outside ac_max_block.
710 * use the main bitmap. */
711 status = -ENOSPC;
712 } else if ((status < 0) && (status != -ENOSPC)) {
687 mlog_errno(status); 713 mlog_errno(status);
688 goto bail; 714 goto bail;
689 } else if (status == -ENOSPC) {
690 /* reserve_local_bits will return enospc with
691 * the local alloc inode still locked, so we
692 * can change this safely here. */
693 mlog(0, "Disabling local alloc\n");
694 /* We set to OCFS2_LA_DISABLED so that umount
695 * can clean up what's left of the local
696 * allocation */
697 osb->local_alloc_state = OCFS2_LA_DISABLED;
698 } 715 }
699 } 716 }
700 717
@@ -718,6 +735,13 @@ bail:
718 return status; 735 return status;
719} 736}
720 737
738int ocfs2_reserve_clusters(struct ocfs2_super *osb,
739 u32 bits_wanted,
740 struct ocfs2_alloc_context **ac)
741{
742 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
743}
744
721/* 745/*
722 * More or less lifted from ext3. I'll leave their description below: 746 * More or less lifted from ext3. I'll leave their description below:
723 * 747 *
@@ -1000,11 +1024,14 @@ static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg
1000static int ocfs2_cluster_group_search(struct inode *inode, 1024static int ocfs2_cluster_group_search(struct inode *inode,
1001 struct buffer_head *group_bh, 1025 struct buffer_head *group_bh,
1002 u32 bits_wanted, u32 min_bits, 1026 u32 bits_wanted, u32 min_bits,
1027 u64 max_block,
1003 u16 *bit_off, u16 *bits_found) 1028 u16 *bit_off, u16 *bits_found)
1004{ 1029{
1005 int search = -ENOSPC; 1030 int search = -ENOSPC;
1006 int ret; 1031 int ret;
1032 u64 blkoff;
1007 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data; 1033 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1034 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1008 u16 tmp_off, tmp_found; 1035 u16 tmp_off, tmp_found;
1009 unsigned int max_bits, gd_cluster_off; 1036 unsigned int max_bits, gd_cluster_off;
1010 1037
@@ -1037,6 +1064,17 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1037 if (ret) 1064 if (ret)
1038 return ret; 1065 return ret;
1039 1066
1067 if (max_block) {
1068 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1069 gd_cluster_off +
1070 tmp_off + tmp_found);
1071 mlog(0, "Checking %llu against %llu\n",
1072 (unsigned long long)blkoff,
1073 (unsigned long long)max_block);
1074 if (blkoff > max_block)
1075 return -ENOSPC;
1076 }
1077
1040 /* ocfs2_block_group_find_clear_bits() might 1078 /* ocfs2_block_group_find_clear_bits() might
1041 * return success, but we still want to return 1079 * return success, but we still want to return
1042 * -ENOSPC unless it found the minimum number 1080 * -ENOSPC unless it found the minimum number
@@ -1045,6 +1083,12 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1045 *bit_off = tmp_off; 1083 *bit_off = tmp_off;
1046 *bits_found = tmp_found; 1084 *bits_found = tmp_found;
1047 search = 0; /* success */ 1085 search = 0; /* success */
1086 } else if (tmp_found) {
1087 /*
1088 * Don't show bits which we'll be returning
1089 * for allocation to the local alloc bitmap.
1090 */
1091 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1048 } 1092 }
1049 } 1093 }
1050 1094
@@ -1054,19 +1098,31 @@ static int ocfs2_cluster_group_search(struct inode *inode,
1054static int ocfs2_block_group_search(struct inode *inode, 1098static int ocfs2_block_group_search(struct inode *inode,
1055 struct buffer_head *group_bh, 1099 struct buffer_head *group_bh,
1056 u32 bits_wanted, u32 min_bits, 1100 u32 bits_wanted, u32 min_bits,
1101 u64 max_block,
1057 u16 *bit_off, u16 *bits_found) 1102 u16 *bit_off, u16 *bits_found)
1058{ 1103{
1059 int ret = -ENOSPC; 1104 int ret = -ENOSPC;
1105 u64 blkoff;
1060 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data; 1106 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1061 1107
1062 BUG_ON(min_bits != 1); 1108 BUG_ON(min_bits != 1);
1063 BUG_ON(ocfs2_is_cluster_bitmap(inode)); 1109 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1064 1110
1065 if (bg->bg_free_bits_count) 1111 if (bg->bg_free_bits_count) {
1066 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb), 1112 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1067 group_bh, bits_wanted, 1113 group_bh, bits_wanted,
1068 le16_to_cpu(bg->bg_bits), 1114 le16_to_cpu(bg->bg_bits),
1069 bit_off, bits_found); 1115 bit_off, bits_found);
1116 if (!ret && max_block) {
1117 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1118 *bits_found;
1119 mlog(0, "Checking %llu against %llu\n",
1120 (unsigned long long)blkoff,
1121 (unsigned long long)max_block);
1122 if (blkoff > max_block)
1123 ret = -ENOSPC;
1124 }
1125 }
1070 1126
1071 return ret; 1127 return ret;
1072} 1128}
@@ -1116,8 +1172,7 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1116 struct ocfs2_group_desc *gd; 1172 struct ocfs2_group_desc *gd;
1117 struct inode *alloc_inode = ac->ac_inode; 1173 struct inode *alloc_inode = ac->ac_inode;
1118 1174
1119 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno, 1175 ret = ocfs2_read_block(alloc_inode, gd_blkno, &group_bh);
1120 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1121 if (ret < 0) { 1176 if (ret < 0) {
1122 mlog_errno(ret); 1177 mlog_errno(ret);
1123 return ret; 1178 return ret;
@@ -1131,7 +1186,7 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1131 } 1186 }
1132 1187
1133 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits, 1188 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1134 bit_off, &found); 1189 ac->ac_max_block, bit_off, &found);
1135 if (ret < 0) { 1190 if (ret < 0) {
1136 if (ret != -ENOSPC) 1191 if (ret != -ENOSPC)
1137 mlog_errno(ret); 1192 mlog_errno(ret);
@@ -1186,9 +1241,9 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1186 bits_wanted, chain, 1241 bits_wanted, chain,
1187 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno); 1242 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1188 1243
1189 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), 1244 status = ocfs2_read_block(alloc_inode,
1190 le64_to_cpu(cl->cl_recs[chain].c_blkno), 1245 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1191 &group_bh, OCFS2_BH_CACHED, alloc_inode); 1246 &group_bh);
1192 if (status < 0) { 1247 if (status < 0) {
1193 mlog_errno(status); 1248 mlog_errno(status);
1194 goto bail; 1249 goto bail;
@@ -1204,21 +1259,20 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1204 /* for now, the chain search is a bit simplistic. We just use 1259 /* for now, the chain search is a bit simplistic. We just use
1205 * the 1st group with any empty bits. */ 1260 * the 1st group with any empty bits. */
1206 while ((status = ac->ac_group_search(alloc_inode, group_bh, 1261 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1207 bits_wanted, min_bits, bit_off, 1262 bits_wanted, min_bits,
1263 ac->ac_max_block, bit_off,
1208 &tmp_bits)) == -ENOSPC) { 1264 &tmp_bits)) == -ENOSPC) {
1209 if (!bg->bg_next_group) 1265 if (!bg->bg_next_group)
1210 break; 1266 break;
1211 1267
1212 if (prev_group_bh) { 1268 brelse(prev_group_bh);
1213 brelse(prev_group_bh); 1269 prev_group_bh = NULL;
1214 prev_group_bh = NULL; 1270
1215 }
1216 next_group = le64_to_cpu(bg->bg_next_group); 1271 next_group = le64_to_cpu(bg->bg_next_group);
1217 prev_group_bh = group_bh; 1272 prev_group_bh = group_bh;
1218 group_bh = NULL; 1273 group_bh = NULL;
1219 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), 1274 status = ocfs2_read_block(alloc_inode,
1220 next_group, &group_bh, 1275 next_group, &group_bh);
1221 OCFS2_BH_CACHED, alloc_inode);
1222 if (status < 0) { 1276 if (status < 0) {
1223 mlog_errno(status); 1277 mlog_errno(status);
1224 goto bail; 1278 goto bail;
@@ -1307,10 +1361,8 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1307 *bg_blkno = le64_to_cpu(bg->bg_blkno); 1361 *bg_blkno = le64_to_cpu(bg->bg_blkno);
1308 *bits_left = le16_to_cpu(bg->bg_free_bits_count); 1362 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1309bail: 1363bail:
1310 if (group_bh) 1364 brelse(group_bh);
1311 brelse(group_bh); 1365 brelse(prev_group_bh);
1312 if (prev_group_bh)
1313 brelse(prev_group_bh);
1314 1366
1315 mlog_exit(status); 1367 mlog_exit(status);
1316 return status; 1368 return status;
@@ -1723,7 +1775,6 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
1723{ 1775{
1724 int status = 0; 1776 int status = 0;
1725 u32 tmp_used; 1777 u32 tmp_used;
1726 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1727 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data; 1778 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1728 struct ocfs2_chain_list *cl = &fe->id2.i_chain; 1779 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1729 struct buffer_head *group_bh = NULL; 1780 struct buffer_head *group_bh = NULL;
@@ -1742,8 +1793,7 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
1742 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count, 1793 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1743 (unsigned long long)bg_blkno, start_bit); 1794 (unsigned long long)bg_blkno, start_bit);
1744 1795
1745 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED, 1796 status = ocfs2_read_block(alloc_inode, bg_blkno, &group_bh);
1746 alloc_inode);
1747 if (status < 0) { 1797 if (status < 0) {
1748 mlog_errno(status); 1798 mlog_errno(status);
1749 goto bail; 1799 goto bail;
@@ -1784,8 +1834,7 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
1784 } 1834 }
1785 1835
1786bail: 1836bail:
1787 if (group_bh) 1837 brelse(group_bh);
1788 brelse(group_bh);
1789 1838
1790 mlog_exit(status); 1839 mlog_exit(status);
1791 return status; 1840 return status;
@@ -1838,9 +1887,15 @@ int ocfs2_free_clusters(handle_t *handle,
1838 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh, 1887 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1839 bg_start_bit, bg_blkno, 1888 bg_start_bit, bg_blkno,
1840 num_clusters); 1889 num_clusters);
1841 if (status < 0) 1890 if (status < 0) {
1842 mlog_errno(status); 1891 mlog_errno(status);
1892 goto out;
1893 }
1843 1894
1895 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1896 num_clusters);
1897
1898out:
1844 mlog_exit(status); 1899 mlog_exit(status);
1845 return status; 1900 return status;
1846} 1901}
@@ -1891,3 +1946,84 @@ static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1891 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); 1946 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1892 } 1947 }
1893} 1948}
1949
1950/*
1951 * For a given allocation, determine which allocators will need to be
1952 * accessed, and lock them, reserving the appropriate number of bits.
1953 *
1954 * Sparse file systems call this from ocfs2_write_begin_nolock()
1955 * and ocfs2_allocate_unwritten_extents().
1956 *
1957 * File systems which don't support holes call this from
1958 * ocfs2_extend_allocation().
1959 */
1960int ocfs2_lock_allocators(struct inode *inode,
1961 struct ocfs2_extent_tree *et,
1962 u32 clusters_to_add, u32 extents_to_split,
1963 struct ocfs2_alloc_context **data_ac,
1964 struct ocfs2_alloc_context **meta_ac)
1965{
1966 int ret = 0, num_free_extents;
1967 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1968 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1969
1970 *meta_ac = NULL;
1971 if (data_ac)
1972 *data_ac = NULL;
1973
1974 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1975
1976 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
1977 if (num_free_extents < 0) {
1978 ret = num_free_extents;
1979 mlog_errno(ret);
1980 goto out;
1981 }
1982
1983 /*
1984 * Sparse allocation file systems need to be more conservative
1985 * with reserving room for expansion - the actual allocation
1986 * happens while we've got a journal handle open so re-taking
1987 * a cluster lock (because we ran out of room for another
1988 * extent) will violate ordering rules.
1989 *
1990 * Most of the time we'll only be seeing this 1 cluster at a time
1991 * anyway.
1992 *
1993 * Always lock for any unwritten extents - we might want to
1994 * add blocks during a split.
1995 */
1996 if (!num_free_extents ||
1997 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
1998 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
1999 if (ret < 0) {
2000 if (ret != -ENOSPC)
2001 mlog_errno(ret);
2002 goto out;
2003 }
2004 }
2005
2006 if (clusters_to_add == 0)
2007 goto out;
2008
2009 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2010 if (ret < 0) {
2011 if (ret != -ENOSPC)
2012 mlog_errno(ret);
2013 goto out;
2014 }
2015
2016out:
2017 if (ret) {
2018 if (*meta_ac) {
2019 ocfs2_free_alloc_context(*meta_ac);
2020 *meta_ac = NULL;
2021 }
2022
2023 /*
2024 * We cannot have an error and a non null *data_ac.
2025 */
2026 }
2027
2028 return ret;
2029}
diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h
index 544c600662bd..4df159d8f450 100644
--- a/fs/ocfs2/suballoc.h
+++ b/fs/ocfs2/suballoc.h
@@ -28,10 +28,11 @@
28 28
29typedef int (group_search_t)(struct inode *, 29typedef int (group_search_t)(struct inode *,
30 struct buffer_head *, 30 struct buffer_head *,
31 u32, 31 u32, /* bits_wanted */
32 u32, 32 u32, /* min_bits */
33 u16 *, 33 u64, /* max_block */
34 u16 *); 34 u16 *, /* *bit_off */
35 u16 *); /* *bits_found */
35 36
36struct ocfs2_alloc_context { 37struct ocfs2_alloc_context {
37 struct inode *ac_inode; /* which bitmap are we allocating from? */ 38 struct inode *ac_inode; /* which bitmap are we allocating from? */
@@ -51,6 +52,8 @@ struct ocfs2_alloc_context {
51 group_search_t *ac_group_search; 52 group_search_t *ac_group_search;
52 53
53 u64 ac_last_group; 54 u64 ac_last_group;
55 u64 ac_max_block; /* Highest block number to allocate. 0 is
56 is the same as ~0 - unlimited */
54}; 57};
55 58
56void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac); 59void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac);
@@ -59,9 +62,17 @@ static inline int ocfs2_alloc_context_bits_left(struct ocfs2_alloc_context *ac)
59 return ac->ac_bits_wanted - ac->ac_bits_given; 62 return ac->ac_bits_wanted - ac->ac_bits_given;
60} 63}
61 64
65/*
66 * Please note that the caller must make sure that root_el is the root
67 * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
68 * the result may be wrong.
69 */
62int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, 70int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
63 struct ocfs2_dinode *fe, 71 struct ocfs2_extent_list *root_el,
64 struct ocfs2_alloc_context **ac); 72 struct ocfs2_alloc_context **ac);
73int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
74 int blocks,
75 struct ocfs2_alloc_context **ac);
65int ocfs2_reserve_new_inode(struct ocfs2_super *osb, 76int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
66 struct ocfs2_alloc_context **ac); 77 struct ocfs2_alloc_context **ac);
67int ocfs2_reserve_clusters(struct ocfs2_super *osb, 78int ocfs2_reserve_clusters(struct ocfs2_super *osb,
@@ -147,6 +158,7 @@ static inline int ocfs2_is_cluster_bitmap(struct inode *inode)
147 * apis above. */ 158 * apis above. */
148int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb, 159int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
149 struct ocfs2_alloc_context *ac); 160 struct ocfs2_alloc_context *ac);
161void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac);
150 162
151/* given a cluster offset, calculate which block group it belongs to 163/* given a cluster offset, calculate which block group it belongs to
152 * and return that block offset. */ 164 * and return that block offset. */
@@ -156,4 +168,8 @@ u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster);
156int ocfs2_check_group_descriptor(struct super_block *sb, 168int ocfs2_check_group_descriptor(struct super_block *sb,
157 struct ocfs2_dinode *di, 169 struct ocfs2_dinode *di,
158 struct ocfs2_group_desc *gd); 170 struct ocfs2_group_desc *gd);
171int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_extent_tree *et,
172 u32 clusters_to_add, u32 extents_to_split,
173 struct ocfs2_alloc_context **data_ac,
174 struct ocfs2_alloc_context **meta_ac);
159#endif /* _CHAINALLOC_H_ */ 175#endif /* _CHAINALLOC_H_ */
diff --git a/fs/ocfs2/super.c b/fs/ocfs2/super.c
index 70334d85aff1..304b63ac78cf 100644
--- a/fs/ocfs2/super.c
+++ b/fs/ocfs2/super.c
@@ -64,6 +64,7 @@
64#include "sysfile.h" 64#include "sysfile.h"
65#include "uptodate.h" 65#include "uptodate.h"
66#include "ver.h" 66#include "ver.h"
67#include "xattr.h"
67 68
68#include "buffer_head_io.h" 69#include "buffer_head_io.h"
69 70
@@ -154,6 +155,9 @@ enum {
154 Opt_localalloc, 155 Opt_localalloc,
155 Opt_localflocks, 156 Opt_localflocks,
156 Opt_stack, 157 Opt_stack,
158 Opt_user_xattr,
159 Opt_nouser_xattr,
160 Opt_inode64,
157 Opt_err, 161 Opt_err,
158}; 162};
159 163
@@ -173,6 +177,9 @@ static const match_table_t tokens = {
173 {Opt_localalloc, "localalloc=%d"}, 177 {Opt_localalloc, "localalloc=%d"},
174 {Opt_localflocks, "localflocks"}, 178 {Opt_localflocks, "localflocks"},
175 {Opt_stack, "cluster_stack=%s"}, 179 {Opt_stack, "cluster_stack=%s"},
180 {Opt_user_xattr, "user_xattr"},
181 {Opt_nouser_xattr, "nouser_xattr"},
182 {Opt_inode64, "inode64"},
176 {Opt_err, NULL} 183 {Opt_err, NULL}
177}; 184};
178 185
@@ -205,10 +212,11 @@ static int ocfs2_sync_fs(struct super_block *sb, int wait)
205 ocfs2_schedule_truncate_log_flush(osb, 0); 212 ocfs2_schedule_truncate_log_flush(osb, 0);
206 } 213 }
207 214
208 if (journal_start_commit(OCFS2_SB(sb)->journal->j_journal, &target)) { 215 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
216 &target)) {
209 if (wait) 217 if (wait)
210 log_wait_commit(OCFS2_SB(sb)->journal->j_journal, 218 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
211 target); 219 target);
212 } 220 }
213 return 0; 221 return 0;
214} 222}
@@ -325,6 +333,7 @@ static struct inode *ocfs2_alloc_inode(struct super_block *sb)
325 if (!oi) 333 if (!oi)
326 return NULL; 334 return NULL;
327 335
336 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
328 return &oi->vfs_inode; 337 return &oi->vfs_inode;
329} 338}
330 339
@@ -406,6 +415,15 @@ static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
406 goto out; 415 goto out;
407 } 416 }
408 417
418 /* Probably don't want this on remount; it might
419 * mess with other nodes */
420 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
421 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
422 ret = -EINVAL;
423 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
424 goto out;
425 }
426
409 /* We're going to/from readonly mode. */ 427 /* We're going to/from readonly mode. */
410 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) { 428 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
411 /* Lock here so the check of HARD_RO and the potential 429 /* Lock here so the check of HARD_RO and the potential
@@ -637,7 +655,8 @@ static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
637 osb->s_atime_quantum = parsed_options.atime_quantum; 655 osb->s_atime_quantum = parsed_options.atime_quantum;
638 osb->preferred_slot = parsed_options.slot; 656 osb->preferred_slot = parsed_options.slot;
639 osb->osb_commit_interval = parsed_options.commit_interval; 657 osb->osb_commit_interval = parsed_options.commit_interval;
640 osb->local_alloc_size = parsed_options.localalloc_opt; 658 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
659 osb->local_alloc_bits = osb->local_alloc_default_bits;
641 660
642 status = ocfs2_verify_userspace_stack(osb, &parsed_options); 661 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
643 if (status) 662 if (status)
@@ -743,8 +762,7 @@ static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
743 return status; 762 return status;
744 763
745read_super_error: 764read_super_error:
746 if (bh != NULL) 765 brelse(bh);
747 brelse(bh);
748 766
749 if (inode) 767 if (inode)
750 iput(inode); 768 iput(inode);
@@ -847,6 +865,12 @@ static int ocfs2_parse_options(struct super_block *sb,
847 case Opt_data_writeback: 865 case Opt_data_writeback:
848 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK; 866 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
849 break; 867 break;
868 case Opt_user_xattr:
869 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
870 break;
871 case Opt_nouser_xattr:
872 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
873 break;
850 case Opt_atime_quantum: 874 case Opt_atime_quantum:
851 if (match_int(&args[0], &option)) { 875 if (match_int(&args[0], &option)) {
852 status = 0; 876 status = 0;
@@ -873,7 +897,7 @@ static int ocfs2_parse_options(struct super_block *sb,
873 if (option < 0) 897 if (option < 0)
874 return 0; 898 return 0;
875 if (option == 0) 899 if (option == 0)
876 option = JBD_DEFAULT_MAX_COMMIT_AGE; 900 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
877 mopt->commit_interval = HZ * option; 901 mopt->commit_interval = HZ * option;
878 break; 902 break;
879 case Opt_localalloc: 903 case Opt_localalloc:
@@ -918,6 +942,9 @@ static int ocfs2_parse_options(struct super_block *sb,
918 OCFS2_STACK_LABEL_LEN); 942 OCFS2_STACK_LABEL_LEN);
919 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0'; 943 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
920 break; 944 break;
945 case Opt_inode64:
946 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
947 break;
921 default: 948 default:
922 mlog(ML_ERROR, 949 mlog(ML_ERROR,
923 "Unrecognized mount option \"%s\" " 950 "Unrecognized mount option \"%s\" "
@@ -938,6 +965,7 @@ static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
938{ 965{
939 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb); 966 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
940 unsigned long opts = osb->s_mount_opt; 967 unsigned long opts = osb->s_mount_opt;
968 unsigned int local_alloc_megs;
941 969
942 if (opts & OCFS2_MOUNT_HB_LOCAL) 970 if (opts & OCFS2_MOUNT_HB_LOCAL)
943 seq_printf(s, ",_netdev,heartbeat=local"); 971 seq_printf(s, ",_netdev,heartbeat=local");
@@ -970,8 +998,9 @@ static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
970 seq_printf(s, ",commit=%u", 998 seq_printf(s, ",commit=%u",
971 (unsigned) (osb->osb_commit_interval / HZ)); 999 (unsigned) (osb->osb_commit_interval / HZ));
972 1000
973 if (osb->local_alloc_size != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE) 1001 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
974 seq_printf(s, ",localalloc=%d", osb->local_alloc_size); 1002 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1003 seq_printf(s, ",localalloc=%d", local_alloc_megs);
975 1004
976 if (opts & OCFS2_MOUNT_LOCALFLOCKS) 1005 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
977 seq_printf(s, ",localflocks,"); 1006 seq_printf(s, ",localflocks,");
@@ -980,6 +1009,14 @@ static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
980 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN, 1009 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
981 osb->osb_cluster_stack); 1010 osb->osb_cluster_stack);
982 1011
1012 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1013 seq_printf(s, ",nouser_xattr");
1014 else
1015 seq_printf(s, ",user_xattr");
1016
1017 if (opts & OCFS2_MOUNT_INODE64)
1018 seq_printf(s, ",inode64");
1019
983 return 0; 1020 return 0;
984} 1021}
985 1022
@@ -1132,6 +1169,7 @@ static void ocfs2_inode_init_once(void *data)
1132 oi->ip_dir_start_lookup = 0; 1169 oi->ip_dir_start_lookup = 0;
1133 1170
1134 init_rwsem(&oi->ip_alloc_sem); 1171 init_rwsem(&oi->ip_alloc_sem);
1172 init_rwsem(&oi->ip_xattr_sem);
1135 mutex_init(&oi->ip_io_mutex); 1173 mutex_init(&oi->ip_io_mutex);
1136 1174
1137 oi->ip_blkno = 0ULL; 1175 oi->ip_blkno = 0ULL;
@@ -1375,6 +1413,7 @@ static int ocfs2_initialize_super(struct super_block *sb,
1375 sb->s_fs_info = osb; 1413 sb->s_fs_info = osb;
1376 sb->s_op = &ocfs2_sops; 1414 sb->s_op = &ocfs2_sops;
1377 sb->s_export_op = &ocfs2_export_ops; 1415 sb->s_export_op = &ocfs2_export_ops;
1416 sb->s_xattr = ocfs2_xattr_handlers;
1378 sb->s_time_gran = 1; 1417 sb->s_time_gran = 1;
1379 sb->s_flags |= MS_NOATIME; 1418 sb->s_flags |= MS_NOATIME;
1380 /* this is needed to support O_LARGEFILE */ 1419 /* this is needed to support O_LARGEFILE */
@@ -1421,8 +1460,12 @@ static int ocfs2_initialize_super(struct super_block *sb,
1421 1460
1422 osb->slot_num = OCFS2_INVALID_SLOT; 1461 osb->slot_num = OCFS2_INVALID_SLOT;
1423 1462
1463 osb->s_xattr_inline_size = le16_to_cpu(
1464 di->id2.i_super.s_xattr_inline_size);
1465
1424 osb->local_alloc_state = OCFS2_LA_UNUSED; 1466 osb->local_alloc_state = OCFS2_LA_UNUSED;
1425 osb->local_alloc_bh = NULL; 1467 osb->local_alloc_bh = NULL;
1468 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
1426 1469
1427 init_waitqueue_head(&osb->osb_mount_event); 1470 init_waitqueue_head(&osb->osb_mount_event);
1428 1471
@@ -1568,6 +1611,7 @@ static int ocfs2_initialize_super(struct super_block *sb,
1568 osb->first_cluster_group_blkno = 1611 osb->first_cluster_group_blkno =
1569 le64_to_cpu(di->id2.i_super.s_first_cluster_group); 1612 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
1570 osb->fs_generation = le32_to_cpu(di->i_fs_generation); 1613 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
1614 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1571 mlog(0, "vol_label: %s\n", osb->vol_label); 1615 mlog(0, "vol_label: %s\n", osb->vol_label);
1572 mlog(0, "uuid: %s\n", osb->uuid_str); 1616 mlog(0, "uuid: %s\n", osb->uuid_str);
1573 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n", 1617 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
diff --git a/fs/ocfs2/symlink.c b/fs/ocfs2/symlink.c
index ba9dbb51d25b..cbd03dfdc7b9 100644
--- a/fs/ocfs2/symlink.c
+++ b/fs/ocfs2/symlink.c
@@ -50,6 +50,7 @@
50#include "inode.h" 50#include "inode.h"
51#include "journal.h" 51#include "journal.h"
52#include "symlink.h" 52#include "symlink.h"
53#include "xattr.h"
53 54
54#include "buffer_head_io.h" 55#include "buffer_head_io.h"
55 56
@@ -83,11 +84,7 @@ static char *ocfs2_fast_symlink_getlink(struct inode *inode,
83 84
84 mlog_entry_void(); 85 mlog_entry_void();
85 86
86 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), 87 status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, bh);
87 OCFS2_I(inode)->ip_blkno,
88 bh,
89 OCFS2_BH_CACHED,
90 inode);
91 if (status < 0) { 88 if (status < 0) {
92 mlog_errno(status); 89 mlog_errno(status);
93 link = ERR_PTR(status); 90 link = ERR_PTR(status);
@@ -157,8 +154,7 @@ bail:
157 kunmap(page); 154 kunmap(page);
158 page_cache_release(page); 155 page_cache_release(page);
159 } 156 }
160 if (bh) 157 brelse(bh);
161 brelse(bh);
162 158
163 return ERR_PTR(status); 159 return ERR_PTR(status);
164} 160}
@@ -168,10 +164,18 @@ const struct inode_operations ocfs2_symlink_inode_operations = {
168 .follow_link = ocfs2_follow_link, 164 .follow_link = ocfs2_follow_link,
169 .getattr = ocfs2_getattr, 165 .getattr = ocfs2_getattr,
170 .setattr = ocfs2_setattr, 166 .setattr = ocfs2_setattr,
167 .setxattr = generic_setxattr,
168 .getxattr = generic_getxattr,
169 .listxattr = ocfs2_listxattr,
170 .removexattr = generic_removexattr,
171}; 171};
172const struct inode_operations ocfs2_fast_symlink_inode_operations = { 172const struct inode_operations ocfs2_fast_symlink_inode_operations = {
173 .readlink = ocfs2_readlink, 173 .readlink = ocfs2_readlink,
174 .follow_link = ocfs2_follow_link, 174 .follow_link = ocfs2_follow_link,
175 .getattr = ocfs2_getattr, 175 .getattr = ocfs2_getattr,
176 .setattr = ocfs2_setattr, 176 .setattr = ocfs2_setattr,
177 .setxattr = generic_setxattr,
178 .getxattr = generic_getxattr,
179 .listxattr = ocfs2_listxattr,
180 .removexattr = generic_removexattr,
177}; 181};
diff --git a/fs/ocfs2/uptodate.c b/fs/ocfs2/uptodate.c
index 4da8851f2b23..187b99ff0368 100644
--- a/fs/ocfs2/uptodate.c
+++ b/fs/ocfs2/uptodate.c
@@ -53,7 +53,11 @@
53#include <linux/highmem.h> 53#include <linux/highmem.h>
54#include <linux/buffer_head.h> 54#include <linux/buffer_head.h>
55#include <linux/rbtree.h> 55#include <linux/rbtree.h>
56#include <linux/jbd.h> 56#ifndef CONFIG_OCFS2_COMPAT_JBD
57# include <linux/jbd2.h>
58#else
59# include <linux/jbd.h>
60#endif
57 61
58#define MLOG_MASK_PREFIX ML_UPTODATE 62#define MLOG_MASK_PREFIX ML_UPTODATE
59 63
@@ -511,14 +515,10 @@ static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
511 ci->ci_num_cached--; 515 ci->ci_num_cached--;
512} 516}
513 517
514/* Called when we remove a chunk of metadata from an inode. We don't 518static void ocfs2_remove_block_from_cache(struct inode *inode,
515 * bother reverting things to an inlined array in the case of a remove 519 sector_t block)
516 * which moves us back under the limit. */
517void ocfs2_remove_from_cache(struct inode *inode,
518 struct buffer_head *bh)
519{ 520{
520 int index; 521 int index;
521 sector_t block = bh->b_blocknr;
522 struct ocfs2_meta_cache_item *item = NULL; 522 struct ocfs2_meta_cache_item *item = NULL;
523 struct ocfs2_inode_info *oi = OCFS2_I(inode); 523 struct ocfs2_inode_info *oi = OCFS2_I(inode);
524 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; 524 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
@@ -544,6 +544,30 @@ void ocfs2_remove_from_cache(struct inode *inode,
544 kmem_cache_free(ocfs2_uptodate_cachep, item); 544 kmem_cache_free(ocfs2_uptodate_cachep, item);
545} 545}
546 546
547/*
548 * Called when we remove a chunk of metadata from an inode. We don't
549 * bother reverting things to an inlined array in the case of a remove
550 * which moves us back under the limit.
551 */
552void ocfs2_remove_from_cache(struct inode *inode,
553 struct buffer_head *bh)
554{
555 sector_t block = bh->b_blocknr;
556
557 ocfs2_remove_block_from_cache(inode, block);
558}
559
560/* Called when we remove xattr clusters from an inode. */
561void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode,
562 sector_t block,
563 u32 c_len)
564{
565 unsigned int i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len;
566
567 for (i = 0; i < b_len; i++, block++)
568 ocfs2_remove_block_from_cache(inode, block);
569}
570
547int __init init_ocfs2_uptodate_cache(void) 571int __init init_ocfs2_uptodate_cache(void)
548{ 572{
549 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate", 573 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
diff --git a/fs/ocfs2/uptodate.h b/fs/ocfs2/uptodate.h
index 2e73206059a8..531b4b3a0c47 100644
--- a/fs/ocfs2/uptodate.h
+++ b/fs/ocfs2/uptodate.h
@@ -40,6 +40,9 @@ void ocfs2_set_new_buffer_uptodate(struct inode *inode,
40 struct buffer_head *bh); 40 struct buffer_head *bh);
41void ocfs2_remove_from_cache(struct inode *inode, 41void ocfs2_remove_from_cache(struct inode *inode,
42 struct buffer_head *bh); 42 struct buffer_head *bh);
43void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode,
44 sector_t block,
45 u32 c_len);
43int ocfs2_buffer_read_ahead(struct inode *inode, 46int ocfs2_buffer_read_ahead(struct inode *inode,
44 struct buffer_head *bh); 47 struct buffer_head *bh);
45 48
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
new file mode 100644
index 000000000000..c25780a70dfd
--- /dev/null
+++ b/fs/ocfs2/xattr.c
@@ -0,0 +1,4834 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
6 * Copyright (C) 2008 Oracle. All rights reserved.
7 *
8 * CREDITS:
9 * Lots of code in this file is taken from ext3.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/capability.h>
28#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/slab.h>
31#include <linux/highmem.h>
32#include <linux/pagemap.h>
33#include <linux/uio.h>
34#include <linux/sched.h>
35#include <linux/splice.h>
36#include <linux/mount.h>
37#include <linux/writeback.h>
38#include <linux/falloc.h>
39#include <linux/sort.h>
40#include <linux/init.h>
41#include <linux/module.h>
42#include <linux/string.h>
43
44#define MLOG_MASK_PREFIX ML_XATTR
45#include <cluster/masklog.h>
46
47#include "ocfs2.h"
48#include "alloc.h"
49#include "dlmglue.h"
50#include "file.h"
51#include "symlink.h"
52#include "sysfile.h"
53#include "inode.h"
54#include "journal.h"
55#include "ocfs2_fs.h"
56#include "suballoc.h"
57#include "uptodate.h"
58#include "buffer_head_io.h"
59#include "super.h"
60#include "xattr.h"
61
62
63struct ocfs2_xattr_def_value_root {
64 struct ocfs2_xattr_value_root xv;
65 struct ocfs2_extent_rec er;
66};
67
68struct ocfs2_xattr_bucket {
69 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 struct ocfs2_xattr_header *xh;
71};
72
73#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
74#define OCFS2_XATTR_INLINE_SIZE 80
75
76static struct ocfs2_xattr_def_value_root def_xv = {
77 .xv.xr_list.l_count = cpu_to_le16(1),
78};
79
80struct xattr_handler *ocfs2_xattr_handlers[] = {
81 &ocfs2_xattr_user_handler,
82 &ocfs2_xattr_trusted_handler,
83 NULL
84};
85
86static struct xattr_handler *ocfs2_xattr_handler_map[] = {
87 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
88 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
89};
90
91struct ocfs2_xattr_info {
92 int name_index;
93 const char *name;
94 const void *value;
95 size_t value_len;
96};
97
98struct ocfs2_xattr_search {
99 struct buffer_head *inode_bh;
100 /*
101 * xattr_bh point to the block buffer head which has extended attribute
102 * when extended attribute in inode, xattr_bh is equal to inode_bh.
103 */
104 struct buffer_head *xattr_bh;
105 struct ocfs2_xattr_header *header;
106 struct ocfs2_xattr_bucket bucket;
107 void *base;
108 void *end;
109 struct ocfs2_xattr_entry *here;
110 int not_found;
111};
112
113static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
114 struct ocfs2_xattr_header *xh,
115 int index,
116 int *block_off,
117 int *new_offset);
118
119static int ocfs2_xattr_index_block_find(struct inode *inode,
120 struct buffer_head *root_bh,
121 int name_index,
122 const char *name,
123 struct ocfs2_xattr_search *xs);
124
125static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
126 struct ocfs2_xattr_tree_root *xt,
127 char *buffer,
128 size_t buffer_size);
129
130static int ocfs2_xattr_create_index_block(struct inode *inode,
131 struct ocfs2_xattr_search *xs);
132
133static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
134 struct ocfs2_xattr_info *xi,
135 struct ocfs2_xattr_search *xs);
136
137static int ocfs2_delete_xattr_index_block(struct inode *inode,
138 struct buffer_head *xb_bh);
139
140static inline const char *ocfs2_xattr_prefix(int name_index)
141{
142 struct xattr_handler *handler = NULL;
143
144 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
145 handler = ocfs2_xattr_handler_map[name_index];
146
147 return handler ? handler->prefix : NULL;
148}
149
150static u32 ocfs2_xattr_name_hash(struct inode *inode,
151 const char *name,
152 int name_len)
153{
154 /* Get hash value of uuid from super block */
155 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
156 int i;
157
158 /* hash extended attribute name */
159 for (i = 0; i < name_len; i++) {
160 hash = (hash << OCFS2_HASH_SHIFT) ^
161 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
162 *name++;
163 }
164
165 return hash;
166}
167
168/*
169 * ocfs2_xattr_hash_entry()
170 *
171 * Compute the hash of an extended attribute.
172 */
173static void ocfs2_xattr_hash_entry(struct inode *inode,
174 struct ocfs2_xattr_header *header,
175 struct ocfs2_xattr_entry *entry)
176{
177 u32 hash = 0;
178 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
179
180 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
181 entry->xe_name_hash = cpu_to_le32(hash);
182
183 return;
184}
185
186static int ocfs2_xattr_extend_allocation(struct inode *inode,
187 u32 clusters_to_add,
188 struct buffer_head *xattr_bh,
189 struct ocfs2_xattr_value_root *xv)
190{
191 int status = 0;
192 int restart_func = 0;
193 int credits = 0;
194 handle_t *handle = NULL;
195 struct ocfs2_alloc_context *data_ac = NULL;
196 struct ocfs2_alloc_context *meta_ac = NULL;
197 enum ocfs2_alloc_restarted why;
198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
199 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
200 struct ocfs2_extent_tree et;
201
202 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
203
204 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
205
206restart_all:
207
208 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
209 &data_ac, &meta_ac);
210 if (status) {
211 mlog_errno(status);
212 goto leave;
213 }
214
215 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
216 clusters_to_add);
217 handle = ocfs2_start_trans(osb, credits);
218 if (IS_ERR(handle)) {
219 status = PTR_ERR(handle);
220 handle = NULL;
221 mlog_errno(status);
222 goto leave;
223 }
224
225restarted_transaction:
226 status = ocfs2_journal_access(handle, inode, xattr_bh,
227 OCFS2_JOURNAL_ACCESS_WRITE);
228 if (status < 0) {
229 mlog_errno(status);
230 goto leave;
231 }
232
233 prev_clusters = le32_to_cpu(xv->xr_clusters);
234 status = ocfs2_add_clusters_in_btree(osb,
235 inode,
236 &logical_start,
237 clusters_to_add,
238 0,
239 &et,
240 handle,
241 data_ac,
242 meta_ac,
243 &why);
244 if ((status < 0) && (status != -EAGAIN)) {
245 if (status != -ENOSPC)
246 mlog_errno(status);
247 goto leave;
248 }
249
250 status = ocfs2_journal_dirty(handle, xattr_bh);
251 if (status < 0) {
252 mlog_errno(status);
253 goto leave;
254 }
255
256 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
257
258 if (why != RESTART_NONE && clusters_to_add) {
259 if (why == RESTART_META) {
260 mlog(0, "restarting function.\n");
261 restart_func = 1;
262 } else {
263 BUG_ON(why != RESTART_TRANS);
264
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits = ocfs2_calc_extend_credits(osb->sb,
268 et.et_root_el,
269 clusters_to_add);
270 status = ocfs2_extend_trans(handle, credits);
271 if (status < 0) {
272 /* handle still has to be committed at
273 * this point. */
274 status = -ENOMEM;
275 mlog_errno(status);
276 goto leave;
277 }
278 goto restarted_transaction;
279 }
280 }
281
282leave:
283 if (handle) {
284 ocfs2_commit_trans(osb, handle);
285 handle = NULL;
286 }
287 if (data_ac) {
288 ocfs2_free_alloc_context(data_ac);
289 data_ac = NULL;
290 }
291 if (meta_ac) {
292 ocfs2_free_alloc_context(meta_ac);
293 meta_ac = NULL;
294 }
295 if ((!status) && restart_func) {
296 restart_func = 0;
297 goto restart_all;
298 }
299
300 return status;
301}
302
303static int __ocfs2_remove_xattr_range(struct inode *inode,
304 struct buffer_head *root_bh,
305 struct ocfs2_xattr_value_root *xv,
306 u32 cpos, u32 phys_cpos, u32 len,
307 struct ocfs2_cached_dealloc_ctxt *dealloc)
308{
309 int ret;
310 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
312 struct inode *tl_inode = osb->osb_tl_inode;
313 handle_t *handle;
314 struct ocfs2_alloc_context *meta_ac = NULL;
315 struct ocfs2_extent_tree et;
316
317 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
318
319 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
320 if (ret) {
321 mlog_errno(ret);
322 return ret;
323 }
324
325 mutex_lock(&tl_inode->i_mutex);
326
327 if (ocfs2_truncate_log_needs_flush(osb)) {
328 ret = __ocfs2_flush_truncate_log(osb);
329 if (ret < 0) {
330 mlog_errno(ret);
331 goto out;
332 }
333 }
334
335 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
336 if (IS_ERR(handle)) {
337 ret = PTR_ERR(handle);
338 mlog_errno(ret);
339 goto out;
340 }
341
342 ret = ocfs2_journal_access(handle, inode, root_bh,
343 OCFS2_JOURNAL_ACCESS_WRITE);
344 if (ret) {
345 mlog_errno(ret);
346 goto out_commit;
347 }
348
349 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
350 dealloc);
351 if (ret) {
352 mlog_errno(ret);
353 goto out_commit;
354 }
355
356 le32_add_cpu(&xv->xr_clusters, -len);
357
358 ret = ocfs2_journal_dirty(handle, root_bh);
359 if (ret) {
360 mlog_errno(ret);
361 goto out_commit;
362 }
363
364 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
365 if (ret)
366 mlog_errno(ret);
367
368out_commit:
369 ocfs2_commit_trans(osb, handle);
370out:
371 mutex_unlock(&tl_inode->i_mutex);
372
373 if (meta_ac)
374 ocfs2_free_alloc_context(meta_ac);
375
376 return ret;
377}
378
379static int ocfs2_xattr_shrink_size(struct inode *inode,
380 u32 old_clusters,
381 u32 new_clusters,
382 struct buffer_head *root_bh,
383 struct ocfs2_xattr_value_root *xv)
384{
385 int ret = 0;
386 u32 trunc_len, cpos, phys_cpos, alloc_size;
387 u64 block;
388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
389 struct ocfs2_cached_dealloc_ctxt dealloc;
390
391 ocfs2_init_dealloc_ctxt(&dealloc);
392
393 if (old_clusters <= new_clusters)
394 return 0;
395
396 cpos = new_clusters;
397 trunc_len = old_clusters - new_clusters;
398 while (trunc_len) {
399 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
400 &alloc_size, &xv->xr_list);
401 if (ret) {
402 mlog_errno(ret);
403 goto out;
404 }
405
406 if (alloc_size > trunc_len)
407 alloc_size = trunc_len;
408
409 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
410 phys_cpos, alloc_size,
411 &dealloc);
412 if (ret) {
413 mlog_errno(ret);
414 goto out;
415 }
416
417 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
418 ocfs2_remove_xattr_clusters_from_cache(inode, block,
419 alloc_size);
420 cpos += alloc_size;
421 trunc_len -= alloc_size;
422 }
423
424out:
425 ocfs2_schedule_truncate_log_flush(osb, 1);
426 ocfs2_run_deallocs(osb, &dealloc);
427
428 return ret;
429}
430
431static int ocfs2_xattr_value_truncate(struct inode *inode,
432 struct buffer_head *root_bh,
433 struct ocfs2_xattr_value_root *xv,
434 int len)
435{
436 int ret;
437 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
438 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
439
440 if (new_clusters == old_clusters)
441 return 0;
442
443 if (new_clusters > old_clusters)
444 ret = ocfs2_xattr_extend_allocation(inode,
445 new_clusters - old_clusters,
446 root_bh, xv);
447 else
448 ret = ocfs2_xattr_shrink_size(inode,
449 old_clusters, new_clusters,
450 root_bh, xv);
451
452 return ret;
453}
454
455static int ocfs2_xattr_list_entry(char *buffer, size_t size,
456 size_t *result, const char *prefix,
457 const char *name, int name_len)
458{
459 char *p = buffer + *result;
460 int prefix_len = strlen(prefix);
461 int total_len = prefix_len + name_len + 1;
462
463 *result += total_len;
464
465 /* we are just looking for how big our buffer needs to be */
466 if (!size)
467 return 0;
468
469 if (*result > size)
470 return -ERANGE;
471
472 memcpy(p, prefix, prefix_len);
473 memcpy(p + prefix_len, name, name_len);
474 p[prefix_len + name_len] = '\0';
475
476 return 0;
477}
478
479static int ocfs2_xattr_list_entries(struct inode *inode,
480 struct ocfs2_xattr_header *header,
481 char *buffer, size_t buffer_size)
482{
483 size_t result = 0;
484 int i, type, ret;
485 const char *prefix, *name;
486
487 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
488 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
489 type = ocfs2_xattr_get_type(entry);
490 prefix = ocfs2_xattr_prefix(type);
491
492 if (prefix) {
493 name = (const char *)header +
494 le16_to_cpu(entry->xe_name_offset);
495
496 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
497 &result, prefix, name,
498 entry->xe_name_len);
499 if (ret)
500 return ret;
501 }
502 }
503
504 return result;
505}
506
507static int ocfs2_xattr_ibody_list(struct inode *inode,
508 struct ocfs2_dinode *di,
509 char *buffer,
510 size_t buffer_size)
511{
512 struct ocfs2_xattr_header *header = NULL;
513 struct ocfs2_inode_info *oi = OCFS2_I(inode);
514 int ret = 0;
515
516 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
517 return ret;
518
519 header = (struct ocfs2_xattr_header *)
520 ((void *)di + inode->i_sb->s_blocksize -
521 le16_to_cpu(di->i_xattr_inline_size));
522
523 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
524
525 return ret;
526}
527
528static int ocfs2_xattr_block_list(struct inode *inode,
529 struct ocfs2_dinode *di,
530 char *buffer,
531 size_t buffer_size)
532{
533 struct buffer_head *blk_bh = NULL;
534 struct ocfs2_xattr_block *xb;
535 int ret = 0;
536
537 if (!di->i_xattr_loc)
538 return ret;
539
540 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
541 if (ret < 0) {
542 mlog_errno(ret);
543 return ret;
544 }
545 /*Verify the signature of xattr block*/
546 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
547 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
548 ret = -EFAULT;
549 goto cleanup;
550 }
551
552 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
553
554 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
555 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
556 ret = ocfs2_xattr_list_entries(inode, header,
557 buffer, buffer_size);
558 } else {
559 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
560 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
561 buffer, buffer_size);
562 }
563cleanup:
564 brelse(blk_bh);
565
566 return ret;
567}
568
569ssize_t ocfs2_listxattr(struct dentry *dentry,
570 char *buffer,
571 size_t size)
572{
573 int ret = 0, i_ret = 0, b_ret = 0;
574 struct buffer_head *di_bh = NULL;
575 struct ocfs2_dinode *di = NULL;
576 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
577
578 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
579 return -EOPNOTSUPP;
580
581 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
582 return ret;
583
584 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
585 if (ret < 0) {
586 mlog_errno(ret);
587 return ret;
588 }
589
590 di = (struct ocfs2_dinode *)di_bh->b_data;
591
592 down_read(&oi->ip_xattr_sem);
593 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
594 if (i_ret < 0)
595 b_ret = 0;
596 else {
597 if (buffer) {
598 buffer += i_ret;
599 size -= i_ret;
600 }
601 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
602 buffer, size);
603 if (b_ret < 0)
604 i_ret = 0;
605 }
606 up_read(&oi->ip_xattr_sem);
607 ocfs2_inode_unlock(dentry->d_inode, 0);
608
609 brelse(di_bh);
610
611 return i_ret + b_ret;
612}
613
614static int ocfs2_xattr_find_entry(int name_index,
615 const char *name,
616 struct ocfs2_xattr_search *xs)
617{
618 struct ocfs2_xattr_entry *entry;
619 size_t name_len;
620 int i, cmp = 1;
621
622 if (name == NULL)
623 return -EINVAL;
624
625 name_len = strlen(name);
626 entry = xs->here;
627 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
628 cmp = name_index - ocfs2_xattr_get_type(entry);
629 if (!cmp)
630 cmp = name_len - entry->xe_name_len;
631 if (!cmp)
632 cmp = memcmp(name, (xs->base +
633 le16_to_cpu(entry->xe_name_offset)),
634 name_len);
635 if (cmp == 0)
636 break;
637 entry += 1;
638 }
639 xs->here = entry;
640
641 return cmp ? -ENODATA : 0;
642}
643
644static int ocfs2_xattr_get_value_outside(struct inode *inode,
645 struct ocfs2_xattr_value_root *xv,
646 void *buffer,
647 size_t len)
648{
649 u32 cpos, p_cluster, num_clusters, bpc, clusters;
650 u64 blkno;
651 int i, ret = 0;
652 size_t cplen, blocksize;
653 struct buffer_head *bh = NULL;
654 struct ocfs2_extent_list *el;
655
656 el = &xv->xr_list;
657 clusters = le32_to_cpu(xv->xr_clusters);
658 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
659 blocksize = inode->i_sb->s_blocksize;
660
661 cpos = 0;
662 while (cpos < clusters) {
663 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
664 &num_clusters, el);
665 if (ret) {
666 mlog_errno(ret);
667 goto out;
668 }
669
670 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
671 /* Copy ocfs2_xattr_value */
672 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
673 ret = ocfs2_read_block(inode, blkno, &bh);
674 if (ret) {
675 mlog_errno(ret);
676 goto out;
677 }
678
679 cplen = len >= blocksize ? blocksize : len;
680 memcpy(buffer, bh->b_data, cplen);
681 len -= cplen;
682 buffer += cplen;
683
684 brelse(bh);
685 bh = NULL;
686 if (len == 0)
687 break;
688 }
689 cpos += num_clusters;
690 }
691out:
692 return ret;
693}
694
695static int ocfs2_xattr_ibody_get(struct inode *inode,
696 int name_index,
697 const char *name,
698 void *buffer,
699 size_t buffer_size,
700 struct ocfs2_xattr_search *xs)
701{
702 struct ocfs2_inode_info *oi = OCFS2_I(inode);
703 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
704 struct ocfs2_xattr_value_root *xv;
705 size_t size;
706 int ret = 0;
707
708 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
709 return -ENODATA;
710
711 xs->end = (void *)di + inode->i_sb->s_blocksize;
712 xs->header = (struct ocfs2_xattr_header *)
713 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
714 xs->base = (void *)xs->header;
715 xs->here = xs->header->xh_entries;
716
717 ret = ocfs2_xattr_find_entry(name_index, name, xs);
718 if (ret)
719 return ret;
720 size = le64_to_cpu(xs->here->xe_value_size);
721 if (buffer) {
722 if (size > buffer_size)
723 return -ERANGE;
724 if (ocfs2_xattr_is_local(xs->here)) {
725 memcpy(buffer, (void *)xs->base +
726 le16_to_cpu(xs->here->xe_name_offset) +
727 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
728 } else {
729 xv = (struct ocfs2_xattr_value_root *)
730 (xs->base + le16_to_cpu(
731 xs->here->xe_name_offset) +
732 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
733 ret = ocfs2_xattr_get_value_outside(inode, xv,
734 buffer, size);
735 if (ret < 0) {
736 mlog_errno(ret);
737 return ret;
738 }
739 }
740 }
741
742 return size;
743}
744
745static int ocfs2_xattr_block_get(struct inode *inode,
746 int name_index,
747 const char *name,
748 void *buffer,
749 size_t buffer_size,
750 struct ocfs2_xattr_search *xs)
751{
752 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
753 struct buffer_head *blk_bh = NULL;
754 struct ocfs2_xattr_block *xb;
755 struct ocfs2_xattr_value_root *xv;
756 size_t size;
757 int ret = -ENODATA, name_offset, name_len, block_off, i;
758
759 if (!di->i_xattr_loc)
760 return ret;
761
762 memset(&xs->bucket, 0, sizeof(xs->bucket));
763
764 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
765 if (ret < 0) {
766 mlog_errno(ret);
767 return ret;
768 }
769 /*Verify the signature of xattr block*/
770 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
771 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
772 ret = -EFAULT;
773 goto cleanup;
774 }
775
776 xs->xattr_bh = blk_bh;
777 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
778
779 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
780 xs->header = &xb->xb_attrs.xb_header;
781 xs->base = (void *)xs->header;
782 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
783 xs->here = xs->header->xh_entries;
784
785 ret = ocfs2_xattr_find_entry(name_index, name, xs);
786 } else
787 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
788 name_index,
789 name, xs);
790
791 if (ret)
792 goto cleanup;
793 size = le64_to_cpu(xs->here->xe_value_size);
794 if (buffer) {
795 ret = -ERANGE;
796 if (size > buffer_size)
797 goto cleanup;
798
799 name_offset = le16_to_cpu(xs->here->xe_name_offset);
800 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
801 i = xs->here - xs->header->xh_entries;
802
803 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
804 ret = ocfs2_xattr_bucket_get_name_value(inode,
805 xs->bucket.xh,
806 i,
807 &block_off,
808 &name_offset);
809 xs->base = xs->bucket.bhs[block_off]->b_data;
810 }
811 if (ocfs2_xattr_is_local(xs->here)) {
812 memcpy(buffer, (void *)xs->base +
813 name_offset + name_len, size);
814 } else {
815 xv = (struct ocfs2_xattr_value_root *)
816 (xs->base + name_offset + name_len);
817 ret = ocfs2_xattr_get_value_outside(inode, xv,
818 buffer, size);
819 if (ret < 0) {
820 mlog_errno(ret);
821 goto cleanup;
822 }
823 }
824 }
825 ret = size;
826cleanup:
827 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
828 brelse(xs->bucket.bhs[i]);
829 memset(&xs->bucket, 0, sizeof(xs->bucket));
830
831 brelse(blk_bh);
832 return ret;
833}
834
835/* ocfs2_xattr_get()
836 *
837 * Copy an extended attribute into the buffer provided.
838 * Buffer is NULL to compute the size of buffer required.
839 */
840int ocfs2_xattr_get(struct inode *inode,
841 int name_index,
842 const char *name,
843 void *buffer,
844 size_t buffer_size)
845{
846 int ret;
847 struct ocfs2_dinode *di = NULL;
848 struct buffer_head *di_bh = NULL;
849 struct ocfs2_inode_info *oi = OCFS2_I(inode);
850 struct ocfs2_xattr_search xis = {
851 .not_found = -ENODATA,
852 };
853 struct ocfs2_xattr_search xbs = {
854 .not_found = -ENODATA,
855 };
856
857 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
858 return -EOPNOTSUPP;
859
860 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
861 ret = -ENODATA;
862
863 ret = ocfs2_inode_lock(inode, &di_bh, 0);
864 if (ret < 0) {
865 mlog_errno(ret);
866 return ret;
867 }
868 xis.inode_bh = xbs.inode_bh = di_bh;
869 di = (struct ocfs2_dinode *)di_bh->b_data;
870
871 down_read(&oi->ip_xattr_sem);
872 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
873 buffer_size, &xis);
874 if (ret == -ENODATA)
875 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
876 buffer_size, &xbs);
877 up_read(&oi->ip_xattr_sem);
878 ocfs2_inode_unlock(inode, 0);
879
880 brelse(di_bh);
881
882 return ret;
883}
884
885static int __ocfs2_xattr_set_value_outside(struct inode *inode,
886 struct ocfs2_xattr_value_root *xv,
887 const void *value,
888 int value_len)
889{
890 int ret = 0, i, cp_len, credits;
891 u16 blocksize = inode->i_sb->s_blocksize;
892 u32 p_cluster, num_clusters;
893 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
894 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
895 u64 blkno;
896 struct buffer_head *bh = NULL;
897 handle_t *handle;
898
899 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
900
901 credits = clusters * bpc;
902 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
903 if (IS_ERR(handle)) {
904 ret = PTR_ERR(handle);
905 mlog_errno(ret);
906 goto out;
907 }
908
909 while (cpos < clusters) {
910 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
911 &num_clusters, &xv->xr_list);
912 if (ret) {
913 mlog_errno(ret);
914 goto out_commit;
915 }
916
917 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
918
919 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
920 ret = ocfs2_read_block(inode, blkno, &bh);
921 if (ret) {
922 mlog_errno(ret);
923 goto out_commit;
924 }
925
926 ret = ocfs2_journal_access(handle,
927 inode,
928 bh,
929 OCFS2_JOURNAL_ACCESS_WRITE);
930 if (ret < 0) {
931 mlog_errno(ret);
932 goto out_commit;
933 }
934
935 cp_len = value_len > blocksize ? blocksize : value_len;
936 memcpy(bh->b_data, value, cp_len);
937 value_len -= cp_len;
938 value += cp_len;
939 if (cp_len < blocksize)
940 memset(bh->b_data + cp_len, 0,
941 blocksize - cp_len);
942
943 ret = ocfs2_journal_dirty(handle, bh);
944 if (ret < 0) {
945 mlog_errno(ret);
946 goto out_commit;
947 }
948 brelse(bh);
949 bh = NULL;
950
951 /*
952 * XXX: do we need to empty all the following
953 * blocks in this cluster?
954 */
955 if (!value_len)
956 break;
957 }
958 cpos += num_clusters;
959 }
960out_commit:
961 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
962out:
963 brelse(bh);
964
965 return ret;
966}
967
968static int ocfs2_xattr_cleanup(struct inode *inode,
969 struct ocfs2_xattr_info *xi,
970 struct ocfs2_xattr_search *xs,
971 size_t offs)
972{
973 handle_t *handle = NULL;
974 int ret = 0;
975 size_t name_len = strlen(xi->name);
976 void *val = xs->base + offs;
977 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
978
979 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
980 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
981 if (IS_ERR(handle)) {
982 ret = PTR_ERR(handle);
983 mlog_errno(ret);
984 goto out;
985 }
986 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
987 OCFS2_JOURNAL_ACCESS_WRITE);
988 if (ret) {
989 mlog_errno(ret);
990 goto out_commit;
991 }
992 /* Decrease xattr count */
993 le16_add_cpu(&xs->header->xh_count, -1);
994 /* Remove the xattr entry and tree root which has already be set*/
995 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
996 memset(val, 0, size);
997
998 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
999 if (ret < 0)
1000 mlog_errno(ret);
1001out_commit:
1002 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1003out:
1004 return ret;
1005}
1006
1007static int ocfs2_xattr_update_entry(struct inode *inode,
1008 struct ocfs2_xattr_info *xi,
1009 struct ocfs2_xattr_search *xs,
1010 size_t offs)
1011{
1012 handle_t *handle = NULL;
1013 int ret = 0;
1014
1015 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1016 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1017 if (IS_ERR(handle)) {
1018 ret = PTR_ERR(handle);
1019 mlog_errno(ret);
1020 goto out;
1021 }
1022 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1023 OCFS2_JOURNAL_ACCESS_WRITE);
1024 if (ret) {
1025 mlog_errno(ret);
1026 goto out_commit;
1027 }
1028
1029 xs->here->xe_name_offset = cpu_to_le16(offs);
1030 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1031 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1032 ocfs2_xattr_set_local(xs->here, 1);
1033 else
1034 ocfs2_xattr_set_local(xs->here, 0);
1035 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1036
1037 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1038 if (ret < 0)
1039 mlog_errno(ret);
1040out_commit:
1041 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1042out:
1043 return ret;
1044}
1045
1046/*
1047 * ocfs2_xattr_set_value_outside()
1048 *
1049 * Set large size value in B tree.
1050 */
1051static int ocfs2_xattr_set_value_outside(struct inode *inode,
1052 struct ocfs2_xattr_info *xi,
1053 struct ocfs2_xattr_search *xs,
1054 size_t offs)
1055{
1056 size_t name_len = strlen(xi->name);
1057 void *val = xs->base + offs;
1058 struct ocfs2_xattr_value_root *xv = NULL;
1059 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1060 int ret = 0;
1061
1062 memset(val, 0, size);
1063 memcpy(val, xi->name, name_len);
1064 xv = (struct ocfs2_xattr_value_root *)
1065 (val + OCFS2_XATTR_SIZE(name_len));
1066 xv->xr_clusters = 0;
1067 xv->xr_last_eb_blk = 0;
1068 xv->xr_list.l_tree_depth = 0;
1069 xv->xr_list.l_count = cpu_to_le16(1);
1070 xv->xr_list.l_next_free_rec = 0;
1071
1072 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1073 xi->value_len);
1074 if (ret < 0) {
1075 mlog_errno(ret);
1076 return ret;
1077 }
1078 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1079 xi->value_len);
1080 if (ret < 0) {
1081 mlog_errno(ret);
1082 return ret;
1083 }
1084 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1085 if (ret < 0)
1086 mlog_errno(ret);
1087
1088 return ret;
1089}
1090
1091/*
1092 * ocfs2_xattr_set_entry_local()
1093 *
1094 * Set, replace or remove extended attribute in local.
1095 */
1096static void ocfs2_xattr_set_entry_local(struct inode *inode,
1097 struct ocfs2_xattr_info *xi,
1098 struct ocfs2_xattr_search *xs,
1099 struct ocfs2_xattr_entry *last,
1100 size_t min_offs)
1101{
1102 size_t name_len = strlen(xi->name);
1103 int i;
1104
1105 if (xi->value && xs->not_found) {
1106 /* Insert the new xattr entry. */
1107 le16_add_cpu(&xs->header->xh_count, 1);
1108 ocfs2_xattr_set_type(last, xi->name_index);
1109 ocfs2_xattr_set_local(last, 1);
1110 last->xe_name_len = name_len;
1111 } else {
1112 void *first_val;
1113 void *val;
1114 size_t offs, size;
1115
1116 first_val = xs->base + min_offs;
1117 offs = le16_to_cpu(xs->here->xe_name_offset);
1118 val = xs->base + offs;
1119
1120 if (le64_to_cpu(xs->here->xe_value_size) >
1121 OCFS2_XATTR_INLINE_SIZE)
1122 size = OCFS2_XATTR_SIZE(name_len) +
1123 OCFS2_XATTR_ROOT_SIZE;
1124 else
1125 size = OCFS2_XATTR_SIZE(name_len) +
1126 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1127
1128 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1129 OCFS2_XATTR_SIZE(xi->value_len)) {
1130 /* The old and the new value have the
1131 same size. Just replace the value. */
1132 ocfs2_xattr_set_local(xs->here, 1);
1133 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1134 /* Clear value bytes. */
1135 memset(val + OCFS2_XATTR_SIZE(name_len),
1136 0,
1137 OCFS2_XATTR_SIZE(xi->value_len));
1138 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1139 xi->value,
1140 xi->value_len);
1141 return;
1142 }
1143 /* Remove the old name+value. */
1144 memmove(first_val + size, first_val, val - first_val);
1145 memset(first_val, 0, size);
1146 xs->here->xe_name_hash = 0;
1147 xs->here->xe_name_offset = 0;
1148 ocfs2_xattr_set_local(xs->here, 1);
1149 xs->here->xe_value_size = 0;
1150
1151 min_offs += size;
1152
1153 /* Adjust all value offsets. */
1154 last = xs->header->xh_entries;
1155 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1156 size_t o = le16_to_cpu(last->xe_name_offset);
1157
1158 if (o < offs)
1159 last->xe_name_offset = cpu_to_le16(o + size);
1160 last += 1;
1161 }
1162
1163 if (!xi->value) {
1164 /* Remove the old entry. */
1165 last -= 1;
1166 memmove(xs->here, xs->here + 1,
1167 (void *)last - (void *)xs->here);
1168 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1169 le16_add_cpu(&xs->header->xh_count, -1);
1170 }
1171 }
1172 if (xi->value) {
1173 /* Insert the new name+value. */
1174 size_t size = OCFS2_XATTR_SIZE(name_len) +
1175 OCFS2_XATTR_SIZE(xi->value_len);
1176 void *val = xs->base + min_offs - size;
1177
1178 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1179 memset(val, 0, size);
1180 memcpy(val, xi->name, name_len);
1181 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1182 xi->value,
1183 xi->value_len);
1184 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1185 ocfs2_xattr_set_local(xs->here, 1);
1186 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1187 }
1188
1189 return;
1190}
1191
1192/*
1193 * ocfs2_xattr_set_entry()
1194 *
1195 * Set extended attribute entry into inode or block.
1196 *
1197 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1198 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1199 * then set value in B tree with set_value_outside().
1200 */
1201static int ocfs2_xattr_set_entry(struct inode *inode,
1202 struct ocfs2_xattr_info *xi,
1203 struct ocfs2_xattr_search *xs,
1204 int flag)
1205{
1206 struct ocfs2_xattr_entry *last;
1207 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1208 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1209 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1210 size_t size_l = 0;
1211 handle_t *handle = NULL;
1212 int free, i, ret;
1213 struct ocfs2_xattr_info xi_l = {
1214 .name_index = xi->name_index,
1215 .name = xi->name,
1216 .value = xi->value,
1217 .value_len = xi->value_len,
1218 };
1219
1220 /* Compute min_offs, last and free space. */
1221 last = xs->header->xh_entries;
1222
1223 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1224 size_t offs = le16_to_cpu(last->xe_name_offset);
1225 if (offs < min_offs)
1226 min_offs = offs;
1227 last += 1;
1228 }
1229
1230 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1231 if (free < 0)
1232 return -EFAULT;
1233
1234 if (!xs->not_found) {
1235 size_t size = 0;
1236 if (ocfs2_xattr_is_local(xs->here))
1237 size = OCFS2_XATTR_SIZE(name_len) +
1238 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1239 else
1240 size = OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_ROOT_SIZE;
1242 free += (size + sizeof(struct ocfs2_xattr_entry));
1243 }
1244 /* Check free space in inode or block */
1245 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1246 if (free < sizeof(struct ocfs2_xattr_entry) +
1247 OCFS2_XATTR_SIZE(name_len) +
1248 OCFS2_XATTR_ROOT_SIZE) {
1249 ret = -ENOSPC;
1250 goto out;
1251 }
1252 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1253 xi_l.value = (void *)&def_xv;
1254 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1255 } else if (xi->value) {
1256 if (free < sizeof(struct ocfs2_xattr_entry) +
1257 OCFS2_XATTR_SIZE(name_len) +
1258 OCFS2_XATTR_SIZE(xi->value_len)) {
1259 ret = -ENOSPC;
1260 goto out;
1261 }
1262 }
1263
1264 if (!xs->not_found) {
1265 /* For existing extended attribute */
1266 size_t size = OCFS2_XATTR_SIZE(name_len) +
1267 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1268 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1269 void *val = xs->base + offs;
1270
1271 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1272 /* Replace existing local xattr with tree root */
1273 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1274 offs);
1275 if (ret < 0)
1276 mlog_errno(ret);
1277 goto out;
1278 } else if (!ocfs2_xattr_is_local(xs->here)) {
1279 /* For existing xattr which has value outside */
1280 struct ocfs2_xattr_value_root *xv = NULL;
1281 xv = (struct ocfs2_xattr_value_root *)(val +
1282 OCFS2_XATTR_SIZE(name_len));
1283
1284 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1285 /*
1286 * If new value need set outside also,
1287 * first truncate old value to new value,
1288 * then set new value with set_value_outside().
1289 */
1290 ret = ocfs2_xattr_value_truncate(inode,
1291 xs->xattr_bh,
1292 xv,
1293 xi->value_len);
1294 if (ret < 0) {
1295 mlog_errno(ret);
1296 goto out;
1297 }
1298
1299 ret = __ocfs2_xattr_set_value_outside(inode,
1300 xv,
1301 xi->value,
1302 xi->value_len);
1303 if (ret < 0) {
1304 mlog_errno(ret);
1305 goto out;
1306 }
1307
1308 ret = ocfs2_xattr_update_entry(inode,
1309 xi,
1310 xs,
1311 offs);
1312 if (ret < 0)
1313 mlog_errno(ret);
1314 goto out;
1315 } else {
1316 /*
1317 * If new value need set in local,
1318 * just trucate old value to zero.
1319 */
1320 ret = ocfs2_xattr_value_truncate(inode,
1321 xs->xattr_bh,
1322 xv,
1323 0);
1324 if (ret < 0)
1325 mlog_errno(ret);
1326 }
1327 }
1328 }
1329
1330 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1331 OCFS2_INODE_UPDATE_CREDITS);
1332 if (IS_ERR(handle)) {
1333 ret = PTR_ERR(handle);
1334 mlog_errno(ret);
1335 goto out;
1336 }
1337
1338 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1339 OCFS2_JOURNAL_ACCESS_WRITE);
1340 if (ret) {
1341 mlog_errno(ret);
1342 goto out_commit;
1343 }
1344
1345 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1346 /* set extended attribute in external block. */
1347 ret = ocfs2_extend_trans(handle,
1348 OCFS2_INODE_UPDATE_CREDITS +
1349 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1350 if (ret) {
1351 mlog_errno(ret);
1352 goto out_commit;
1353 }
1354 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1355 OCFS2_JOURNAL_ACCESS_WRITE);
1356 if (ret) {
1357 mlog_errno(ret);
1358 goto out_commit;
1359 }
1360 }
1361
1362 /*
1363 * Set value in local, include set tree root in local.
1364 * This is the first step for value size >INLINE_SIZE.
1365 */
1366 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1367
1368 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1369 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1370 if (ret < 0) {
1371 mlog_errno(ret);
1372 goto out_commit;
1373 }
1374 }
1375
1376 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1377 (flag & OCFS2_INLINE_XATTR_FL)) {
1378 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1379 unsigned int xattrsize = osb->s_xattr_inline_size;
1380
1381 /*
1382 * Adjust extent record count or inline data size
1383 * to reserve space for extended attribute.
1384 */
1385 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1386 struct ocfs2_inline_data *idata = &di->id2.i_data;
1387 le16_add_cpu(&idata->id_count, -xattrsize);
1388 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1389 struct ocfs2_extent_list *el = &di->id2.i_list;
1390 le16_add_cpu(&el->l_count, -(xattrsize /
1391 sizeof(struct ocfs2_extent_rec)));
1392 }
1393 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1394 }
1395 /* Update xattr flag */
1396 spin_lock(&oi->ip_lock);
1397 oi->ip_dyn_features |= flag;
1398 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1399 spin_unlock(&oi->ip_lock);
1400 /* Update inode ctime */
1401 inode->i_ctime = CURRENT_TIME;
1402 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1403 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1404
1405 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1406 if (ret < 0)
1407 mlog_errno(ret);
1408
1409out_commit:
1410 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1411
1412 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1413 /*
1414 * Set value outside in B tree.
1415 * This is the second step for value size > INLINE_SIZE.
1416 */
1417 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1418 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1419 if (ret < 0) {
1420 int ret2;
1421
1422 mlog_errno(ret);
1423 /*
1424 * If set value outside failed, we have to clean
1425 * the junk tree root we have already set in local.
1426 */
1427 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1428 if (ret2 < 0)
1429 mlog_errno(ret2);
1430 }
1431 }
1432out:
1433 return ret;
1434
1435}
1436
1437static int ocfs2_remove_value_outside(struct inode*inode,
1438 struct buffer_head *bh,
1439 struct ocfs2_xattr_header *header)
1440{
1441 int ret = 0, i;
1442
1443 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1444 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1445
1446 if (!ocfs2_xattr_is_local(entry)) {
1447 struct ocfs2_xattr_value_root *xv;
1448 void *val;
1449
1450 val = (void *)header +
1451 le16_to_cpu(entry->xe_name_offset);
1452 xv = (struct ocfs2_xattr_value_root *)
1453 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1454 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1455 if (ret < 0) {
1456 mlog_errno(ret);
1457 return ret;
1458 }
1459 }
1460 }
1461
1462 return ret;
1463}
1464
1465static int ocfs2_xattr_ibody_remove(struct inode *inode,
1466 struct buffer_head *di_bh)
1467{
1468
1469 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1470 struct ocfs2_xattr_header *header;
1471 int ret;
1472
1473 header = (struct ocfs2_xattr_header *)
1474 ((void *)di + inode->i_sb->s_blocksize -
1475 le16_to_cpu(di->i_xattr_inline_size));
1476
1477 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1478
1479 return ret;
1480}
1481
1482static int ocfs2_xattr_block_remove(struct inode *inode,
1483 struct buffer_head *blk_bh)
1484{
1485 struct ocfs2_xattr_block *xb;
1486 int ret = 0;
1487
1488 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1489 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1490 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1491 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1492 } else
1493 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1494
1495 return ret;
1496}
1497
1498static int ocfs2_xattr_free_block(struct inode *inode,
1499 u64 block)
1500{
1501 struct inode *xb_alloc_inode;
1502 struct buffer_head *xb_alloc_bh = NULL;
1503 struct buffer_head *blk_bh = NULL;
1504 struct ocfs2_xattr_block *xb;
1505 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1506 handle_t *handle;
1507 int ret = 0;
1508 u64 blk, bg_blkno;
1509 u16 bit;
1510
1511 ret = ocfs2_read_block(inode, block, &blk_bh);
1512 if (ret < 0) {
1513 mlog_errno(ret);
1514 goto out;
1515 }
1516
1517 /*Verify the signature of xattr block*/
1518 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1519 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1520 ret = -EFAULT;
1521 goto out;
1522 }
1523
1524 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1525 if (ret < 0) {
1526 mlog_errno(ret);
1527 goto out;
1528 }
1529
1530 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1531 blk = le64_to_cpu(xb->xb_blkno);
1532 bit = le16_to_cpu(xb->xb_suballoc_bit);
1533 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1534
1535 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1536 EXTENT_ALLOC_SYSTEM_INODE,
1537 le16_to_cpu(xb->xb_suballoc_slot));
1538 if (!xb_alloc_inode) {
1539 ret = -ENOMEM;
1540 mlog_errno(ret);
1541 goto out;
1542 }
1543 mutex_lock(&xb_alloc_inode->i_mutex);
1544
1545 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1546 if (ret < 0) {
1547 mlog_errno(ret);
1548 goto out_mutex;
1549 }
1550
1551 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1552 if (IS_ERR(handle)) {
1553 ret = PTR_ERR(handle);
1554 mlog_errno(ret);
1555 goto out_unlock;
1556 }
1557
1558 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1559 bit, bg_blkno, 1);
1560 if (ret < 0)
1561 mlog_errno(ret);
1562
1563 ocfs2_commit_trans(osb, handle);
1564out_unlock:
1565 ocfs2_inode_unlock(xb_alloc_inode, 1);
1566 brelse(xb_alloc_bh);
1567out_mutex:
1568 mutex_unlock(&xb_alloc_inode->i_mutex);
1569 iput(xb_alloc_inode);
1570out:
1571 brelse(blk_bh);
1572 return ret;
1573}
1574
1575/*
1576 * ocfs2_xattr_remove()
1577 *
1578 * Free extended attribute resources associated with this inode.
1579 */
1580int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1581{
1582 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1583 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1584 handle_t *handle;
1585 int ret;
1586
1587 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1588 return 0;
1589
1590 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1591 return 0;
1592
1593 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1594 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1595 if (ret < 0) {
1596 mlog_errno(ret);
1597 goto out;
1598 }
1599 }
1600
1601 if (di->i_xattr_loc) {
1602 ret = ocfs2_xattr_free_block(inode,
1603 le64_to_cpu(di->i_xattr_loc));
1604 if (ret < 0) {
1605 mlog_errno(ret);
1606 goto out;
1607 }
1608 }
1609
1610 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1611 OCFS2_INODE_UPDATE_CREDITS);
1612 if (IS_ERR(handle)) {
1613 ret = PTR_ERR(handle);
1614 mlog_errno(ret);
1615 goto out;
1616 }
1617 ret = ocfs2_journal_access(handle, inode, di_bh,
1618 OCFS2_JOURNAL_ACCESS_WRITE);
1619 if (ret) {
1620 mlog_errno(ret);
1621 goto out_commit;
1622 }
1623
1624 di->i_xattr_loc = 0;
1625
1626 spin_lock(&oi->ip_lock);
1627 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1628 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1629 spin_unlock(&oi->ip_lock);
1630
1631 ret = ocfs2_journal_dirty(handle, di_bh);
1632 if (ret < 0)
1633 mlog_errno(ret);
1634out_commit:
1635 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1636out:
1637 return ret;
1638}
1639
1640static int ocfs2_xattr_has_space_inline(struct inode *inode,
1641 struct ocfs2_dinode *di)
1642{
1643 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1644 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1645 int free;
1646
1647 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1648 return 0;
1649
1650 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1651 struct ocfs2_inline_data *idata = &di->id2.i_data;
1652 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1653 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1654 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1655 le64_to_cpu(di->i_size);
1656 } else {
1657 struct ocfs2_extent_list *el = &di->id2.i_list;
1658 free = (le16_to_cpu(el->l_count) -
1659 le16_to_cpu(el->l_next_free_rec)) *
1660 sizeof(struct ocfs2_extent_rec);
1661 }
1662 if (free >= xattrsize)
1663 return 1;
1664
1665 return 0;
1666}
1667
1668/*
1669 * ocfs2_xattr_ibody_find()
1670 *
1671 * Find extended attribute in inode block and
1672 * fill search info into struct ocfs2_xattr_search.
1673 */
1674static int ocfs2_xattr_ibody_find(struct inode *inode,
1675 int name_index,
1676 const char *name,
1677 struct ocfs2_xattr_search *xs)
1678{
1679 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1680 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1681 int ret;
1682 int has_space = 0;
1683
1684 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1685 return 0;
1686
1687 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1688 down_read(&oi->ip_alloc_sem);
1689 has_space = ocfs2_xattr_has_space_inline(inode, di);
1690 up_read(&oi->ip_alloc_sem);
1691 if (!has_space)
1692 return 0;
1693 }
1694
1695 xs->xattr_bh = xs->inode_bh;
1696 xs->end = (void *)di + inode->i_sb->s_blocksize;
1697 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1698 xs->header = (struct ocfs2_xattr_header *)
1699 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1700 else
1701 xs->header = (struct ocfs2_xattr_header *)
1702 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1703 xs->base = (void *)xs->header;
1704 xs->here = xs->header->xh_entries;
1705
1706 /* Find the named attribute. */
1707 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1708 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1709 if (ret && ret != -ENODATA)
1710 return ret;
1711 xs->not_found = ret;
1712 }
1713
1714 return 0;
1715}
1716
1717/*
1718 * ocfs2_xattr_ibody_set()
1719 *
1720 * Set, replace or remove an extended attribute into inode block.
1721 *
1722 */
1723static int ocfs2_xattr_ibody_set(struct inode *inode,
1724 struct ocfs2_xattr_info *xi,
1725 struct ocfs2_xattr_search *xs)
1726{
1727 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1728 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1729 int ret;
1730
1731 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1732 return -ENOSPC;
1733
1734 down_write(&oi->ip_alloc_sem);
1735 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1736 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1737 ret = -ENOSPC;
1738 goto out;
1739 }
1740 }
1741
1742 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1743 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1744out:
1745 up_write(&oi->ip_alloc_sem);
1746
1747 return ret;
1748}
1749
1750/*
1751 * ocfs2_xattr_block_find()
1752 *
1753 * Find extended attribute in external block and
1754 * fill search info into struct ocfs2_xattr_search.
1755 */
1756static int ocfs2_xattr_block_find(struct inode *inode,
1757 int name_index,
1758 const char *name,
1759 struct ocfs2_xattr_search *xs)
1760{
1761 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1762 struct buffer_head *blk_bh = NULL;
1763 struct ocfs2_xattr_block *xb;
1764 int ret = 0;
1765
1766 if (!di->i_xattr_loc)
1767 return ret;
1768
1769 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1770 if (ret < 0) {
1771 mlog_errno(ret);
1772 return ret;
1773 }
1774 /*Verify the signature of xattr block*/
1775 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1776 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1777 ret = -EFAULT;
1778 goto cleanup;
1779 }
1780
1781 xs->xattr_bh = blk_bh;
1782 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1783
1784 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1785 xs->header = &xb->xb_attrs.xb_header;
1786 xs->base = (void *)xs->header;
1787 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1788 xs->here = xs->header->xh_entries;
1789
1790 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1791 } else
1792 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1793 name_index,
1794 name, xs);
1795
1796 if (ret && ret != -ENODATA) {
1797 xs->xattr_bh = NULL;
1798 goto cleanup;
1799 }
1800 xs->not_found = ret;
1801 return 0;
1802cleanup:
1803 brelse(blk_bh);
1804
1805 return ret;
1806}
1807
1808/*
1809 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1810 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1811 * re-initialized.
1812 */
1813static int ocfs2_restore_xattr_block(struct inode *inode,
1814 struct ocfs2_xattr_search *xs)
1815{
1816 int ret;
1817 handle_t *handle;
1818 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1819 struct ocfs2_xattr_block *xb =
1820 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1821 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1822 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1823
1824 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1825 le16_to_cpu(el->l_next_free_rec) != 0);
1826
1827 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1828 if (IS_ERR(handle)) {
1829 ret = PTR_ERR(handle);
1830 handle = NULL;
1831 goto out;
1832 }
1833
1834 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1835 OCFS2_JOURNAL_ACCESS_WRITE);
1836 if (ret < 0) {
1837 mlog_errno(ret);
1838 goto out_commit;
1839 }
1840
1841 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1842 offsetof(struct ocfs2_xattr_block, xb_attrs));
1843
1844 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1845
1846 ocfs2_journal_dirty(handle, xs->xattr_bh);
1847
1848out_commit:
1849 ocfs2_commit_trans(osb, handle);
1850out:
1851 return ret;
1852}
1853
1854/*
1855 * ocfs2_xattr_block_set()
1856 *
1857 * Set, replace or remove an extended attribute into external block.
1858 *
1859 */
1860static int ocfs2_xattr_block_set(struct inode *inode,
1861 struct ocfs2_xattr_info *xi,
1862 struct ocfs2_xattr_search *xs)
1863{
1864 struct buffer_head *new_bh = NULL;
1865 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1867 struct ocfs2_alloc_context *meta_ac = NULL;
1868 handle_t *handle = NULL;
1869 struct ocfs2_xattr_block *xblk = NULL;
1870 u16 suballoc_bit_start;
1871 u32 num_got;
1872 u64 first_blkno;
1873 int ret;
1874
1875 if (!xs->xattr_bh) {
1876 /*
1877 * Alloc one external block for extended attribute
1878 * outside of inode.
1879 */
1880 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1881 if (ret < 0) {
1882 mlog_errno(ret);
1883 goto out;
1884 }
1885 handle = ocfs2_start_trans(osb,
1886 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1887 if (IS_ERR(handle)) {
1888 ret = PTR_ERR(handle);
1889 mlog_errno(ret);
1890 goto out;
1891 }
1892 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1893 OCFS2_JOURNAL_ACCESS_CREATE);
1894 if (ret < 0) {
1895 mlog_errno(ret);
1896 goto out_commit;
1897 }
1898
1899 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1900 &suballoc_bit_start, &num_got,
1901 &first_blkno);
1902 if (ret < 0) {
1903 mlog_errno(ret);
1904 goto out_commit;
1905 }
1906
1907 new_bh = sb_getblk(inode->i_sb, first_blkno);
1908 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1909
1910 ret = ocfs2_journal_access(handle, inode, new_bh,
1911 OCFS2_JOURNAL_ACCESS_CREATE);
1912 if (ret < 0) {
1913 mlog_errno(ret);
1914 goto out_commit;
1915 }
1916
1917 /* Initialize ocfs2_xattr_block */
1918 xs->xattr_bh = new_bh;
1919 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1920 memset(xblk, 0, inode->i_sb->s_blocksize);
1921 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1922 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1923 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1924 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1925 xblk->xb_blkno = cpu_to_le64(first_blkno);
1926
1927 xs->header = &xblk->xb_attrs.xb_header;
1928 xs->base = (void *)xs->header;
1929 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1930 xs->here = xs->header->xh_entries;
1931
1932
1933 ret = ocfs2_journal_dirty(handle, new_bh);
1934 if (ret < 0) {
1935 mlog_errno(ret);
1936 goto out_commit;
1937 }
1938 di->i_xattr_loc = cpu_to_le64(first_blkno);
1939 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1940 if (ret < 0)
1941 mlog_errno(ret);
1942out_commit:
1943 ocfs2_commit_trans(osb, handle);
1944out:
1945 if (meta_ac)
1946 ocfs2_free_alloc_context(meta_ac);
1947 if (ret < 0)
1948 return ret;
1949 } else
1950 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1951
1952 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1953 /* Set extended attribute into external block */
1954 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1955 if (!ret || ret != -ENOSPC)
1956 goto end;
1957
1958 ret = ocfs2_xattr_create_index_block(inode, xs);
1959 if (ret)
1960 goto end;
1961 }
1962
1963 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1964 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1965 ret = ocfs2_restore_xattr_block(inode, xs);
1966
1967end:
1968
1969 return ret;
1970}
1971
1972/*
1973 * ocfs2_xattr_set()
1974 *
1975 * Set, replace or remove an extended attribute for this inode.
1976 * value is NULL to remove an existing extended attribute, else either
1977 * create or replace an extended attribute.
1978 */
1979int ocfs2_xattr_set(struct inode *inode,
1980 int name_index,
1981 const char *name,
1982 const void *value,
1983 size_t value_len,
1984 int flags)
1985{
1986 struct buffer_head *di_bh = NULL;
1987 struct ocfs2_dinode *di;
1988 int ret;
1989 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1990
1991 struct ocfs2_xattr_info xi = {
1992 .name_index = name_index,
1993 .name = name,
1994 .value = value,
1995 .value_len = value_len,
1996 };
1997
1998 struct ocfs2_xattr_search xis = {
1999 .not_found = -ENODATA,
2000 };
2001
2002 struct ocfs2_xattr_search xbs = {
2003 .not_found = -ENODATA,
2004 };
2005
2006 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2007 return -EOPNOTSUPP;
2008
2009 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2010 if (ret < 0) {
2011 mlog_errno(ret);
2012 return ret;
2013 }
2014 xis.inode_bh = xbs.inode_bh = di_bh;
2015 di = (struct ocfs2_dinode *)di_bh->b_data;
2016
2017 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2018 /*
2019 * Scan inode and external block to find the same name
2020 * extended attribute and collect search infomation.
2021 */
2022 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2023 if (ret)
2024 goto cleanup;
2025 if (xis.not_found) {
2026 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2027 if (ret)
2028 goto cleanup;
2029 }
2030
2031 if (xis.not_found && xbs.not_found) {
2032 ret = -ENODATA;
2033 if (flags & XATTR_REPLACE)
2034 goto cleanup;
2035 ret = 0;
2036 if (!value)
2037 goto cleanup;
2038 } else {
2039 ret = -EEXIST;
2040 if (flags & XATTR_CREATE)
2041 goto cleanup;
2042 }
2043
2044 if (!value) {
2045 /* Remove existing extended attribute */
2046 if (!xis.not_found)
2047 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2048 else if (!xbs.not_found)
2049 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2050 } else {
2051 /* We always try to set extended attribute into inode first*/
2052 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2053 if (!ret && !xbs.not_found) {
2054 /*
2055 * If succeed and that extended attribute existing in
2056 * external block, then we will remove it.
2057 */
2058 xi.value = NULL;
2059 xi.value_len = 0;
2060 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2061 } else if (ret == -ENOSPC) {
2062 if (di->i_xattr_loc && !xbs.xattr_bh) {
2063 ret = ocfs2_xattr_block_find(inode, name_index,
2064 name, &xbs);
2065 if (ret)
2066 goto cleanup;
2067 }
2068 /*
2069 * If no space in inode, we will set extended attribute
2070 * into external block.
2071 */
2072 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2073 if (ret)
2074 goto cleanup;
2075 if (!xis.not_found) {
2076 /*
2077 * If succeed and that extended attribute
2078 * existing in inode, we will remove it.
2079 */
2080 xi.value = NULL;
2081 xi.value_len = 0;
2082 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2083 }
2084 }
2085 }
2086cleanup:
2087 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2088 ocfs2_inode_unlock(inode, 1);
2089 brelse(di_bh);
2090 brelse(xbs.xattr_bh);
2091 for (i = 0; i < blk_per_bucket; i++)
2092 brelse(xbs.bucket.bhs[i]);
2093
2094 return ret;
2095}
2096
2097/*
2098 * Find the xattr extent rec which may contains name_hash.
2099 * e_cpos will be the first name hash of the xattr rec.
2100 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2101 */
2102static int ocfs2_xattr_get_rec(struct inode *inode,
2103 u32 name_hash,
2104 u64 *p_blkno,
2105 u32 *e_cpos,
2106 u32 *num_clusters,
2107 struct ocfs2_extent_list *el)
2108{
2109 int ret = 0, i;
2110 struct buffer_head *eb_bh = NULL;
2111 struct ocfs2_extent_block *eb;
2112 struct ocfs2_extent_rec *rec = NULL;
2113 u64 e_blkno = 0;
2114
2115 if (el->l_tree_depth) {
2116 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2117 if (ret) {
2118 mlog_errno(ret);
2119 goto out;
2120 }
2121
2122 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2123 el = &eb->h_list;
2124
2125 if (el->l_tree_depth) {
2126 ocfs2_error(inode->i_sb,
2127 "Inode %lu has non zero tree depth in "
2128 "xattr tree block %llu\n", inode->i_ino,
2129 (unsigned long long)eb_bh->b_blocknr);
2130 ret = -EROFS;
2131 goto out;
2132 }
2133 }
2134
2135 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2136 rec = &el->l_recs[i];
2137
2138 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2139 e_blkno = le64_to_cpu(rec->e_blkno);
2140 break;
2141 }
2142 }
2143
2144 if (!e_blkno) {
2145 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2146 "record (%u, %u, 0) in xattr", inode->i_ino,
2147 le32_to_cpu(rec->e_cpos),
2148 ocfs2_rec_clusters(el, rec));
2149 ret = -EROFS;
2150 goto out;
2151 }
2152
2153 *p_blkno = le64_to_cpu(rec->e_blkno);
2154 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2155 if (e_cpos)
2156 *e_cpos = le32_to_cpu(rec->e_cpos);
2157out:
2158 brelse(eb_bh);
2159 return ret;
2160}
2161
2162typedef int (xattr_bucket_func)(struct inode *inode,
2163 struct ocfs2_xattr_bucket *bucket,
2164 void *para);
2165
2166static int ocfs2_find_xe_in_bucket(struct inode *inode,
2167 struct buffer_head *header_bh,
2168 int name_index,
2169 const char *name,
2170 u32 name_hash,
2171 u16 *xe_index,
2172 int *found)
2173{
2174 int i, ret = 0, cmp = 1, block_off, new_offset;
2175 struct ocfs2_xattr_header *xh =
2176 (struct ocfs2_xattr_header *)header_bh->b_data;
2177 size_t name_len = strlen(name);
2178 struct ocfs2_xattr_entry *xe = NULL;
2179 struct buffer_head *name_bh = NULL;
2180 char *xe_name;
2181
2182 /*
2183 * We don't use binary search in the bucket because there
2184 * may be multiple entries with the same name hash.
2185 */
2186 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2187 xe = &xh->xh_entries[i];
2188
2189 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2190 continue;
2191 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2192 break;
2193
2194 cmp = name_index - ocfs2_xattr_get_type(xe);
2195 if (!cmp)
2196 cmp = name_len - xe->xe_name_len;
2197 if (cmp)
2198 continue;
2199
2200 ret = ocfs2_xattr_bucket_get_name_value(inode,
2201 xh,
2202 i,
2203 &block_off,
2204 &new_offset);
2205 if (ret) {
2206 mlog_errno(ret);
2207 break;
2208 }
2209
2210 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2211 &name_bh);
2212 if (ret) {
2213 mlog_errno(ret);
2214 break;
2215 }
2216 xe_name = name_bh->b_data + new_offset;
2217
2218 cmp = memcmp(name, xe_name, name_len);
2219 brelse(name_bh);
2220 name_bh = NULL;
2221
2222 if (cmp == 0) {
2223 *xe_index = i;
2224 *found = 1;
2225 ret = 0;
2226 break;
2227 }
2228 }
2229
2230 return ret;
2231}
2232
2233/*
2234 * Find the specified xattr entry in a series of buckets.
2235 * This series start from p_blkno and last for num_clusters.
2236 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2237 * the num of the valid buckets.
2238 *
2239 * Return the buffer_head this xattr should reside in. And if the xattr's
2240 * hash is in the gap of 2 buckets, return the lower bucket.
2241 */
2242static int ocfs2_xattr_bucket_find(struct inode *inode,
2243 int name_index,
2244 const char *name,
2245 u32 name_hash,
2246 u64 p_blkno,
2247 u32 first_hash,
2248 u32 num_clusters,
2249 struct ocfs2_xattr_search *xs)
2250{
2251 int ret, found = 0;
2252 struct buffer_head *bh = NULL;
2253 struct buffer_head *lower_bh = NULL;
2254 struct ocfs2_xattr_header *xh = NULL;
2255 struct ocfs2_xattr_entry *xe = NULL;
2256 u16 index = 0;
2257 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2258 int low_bucket = 0, bucket, high_bucket;
2259 u32 last_hash;
2260 u64 blkno;
2261
2262 ret = ocfs2_read_block(inode, p_blkno, &bh);
2263 if (ret) {
2264 mlog_errno(ret);
2265 goto out;
2266 }
2267
2268 xh = (struct ocfs2_xattr_header *)bh->b_data;
2269 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2270
2271 while (low_bucket <= high_bucket) {
2272 brelse(bh);
2273 bh = NULL;
2274 bucket = (low_bucket + high_bucket) / 2;
2275
2276 blkno = p_blkno + bucket * blk_per_bucket;
2277
2278 ret = ocfs2_read_block(inode, blkno, &bh);
2279 if (ret) {
2280 mlog_errno(ret);
2281 goto out;
2282 }
2283
2284 xh = (struct ocfs2_xattr_header *)bh->b_data;
2285 xe = &xh->xh_entries[0];
2286 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2287 high_bucket = bucket - 1;
2288 continue;
2289 }
2290
2291 /*
2292 * Check whether the hash of the last entry in our
2293 * bucket is larger than the search one. for an empty
2294 * bucket, the last one is also the first one.
2295 */
2296 if (xh->xh_count)
2297 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2298
2299 last_hash = le32_to_cpu(xe->xe_name_hash);
2300
2301 /* record lower_bh which may be the insert place. */
2302 brelse(lower_bh);
2303 lower_bh = bh;
2304 bh = NULL;
2305
2306 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2307 low_bucket = bucket + 1;
2308 continue;
2309 }
2310
2311 /* the searched xattr should reside in this bucket if exists. */
2312 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2313 name_index, name, name_hash,
2314 &index, &found);
2315 if (ret) {
2316 mlog_errno(ret);
2317 goto out;
2318 }
2319 break;
2320 }
2321
2322 /*
2323 * Record the bucket we have found.
2324 * When the xattr's hash value is in the gap of 2 buckets, we will
2325 * always set it to the previous bucket.
2326 */
2327 if (!lower_bh) {
2328 /*
2329 * We can't find any bucket whose first name_hash is less
2330 * than the find name_hash.
2331 */
2332 BUG_ON(bh->b_blocknr != p_blkno);
2333 lower_bh = bh;
2334 bh = NULL;
2335 }
2336 xs->bucket.bhs[0] = lower_bh;
2337 xs->bucket.xh = (struct ocfs2_xattr_header *)
2338 xs->bucket.bhs[0]->b_data;
2339 lower_bh = NULL;
2340
2341 xs->header = xs->bucket.xh;
2342 xs->base = xs->bucket.bhs[0]->b_data;
2343 xs->end = xs->base + inode->i_sb->s_blocksize;
2344
2345 if (found) {
2346 /*
2347 * If we have found the xattr enty, read all the blocks in
2348 * this bucket.
2349 */
2350 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2351 blk_per_bucket - 1, &xs->bucket.bhs[1],
2352 OCFS2_BH_CACHED);
2353 if (ret) {
2354 mlog_errno(ret);
2355 goto out;
2356 }
2357
2358 xs->here = &xs->header->xh_entries[index];
2359 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2360 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2361 } else
2362 ret = -ENODATA;
2363
2364out:
2365 brelse(bh);
2366 brelse(lower_bh);
2367 return ret;
2368}
2369
2370static int ocfs2_xattr_index_block_find(struct inode *inode,
2371 struct buffer_head *root_bh,
2372 int name_index,
2373 const char *name,
2374 struct ocfs2_xattr_search *xs)
2375{
2376 int ret;
2377 struct ocfs2_xattr_block *xb =
2378 (struct ocfs2_xattr_block *)root_bh->b_data;
2379 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2380 struct ocfs2_extent_list *el = &xb_root->xt_list;
2381 u64 p_blkno = 0;
2382 u32 first_hash, num_clusters = 0;
2383 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2384
2385 if (le16_to_cpu(el->l_next_free_rec) == 0)
2386 return -ENODATA;
2387
2388 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2389 name, name_hash, name_index);
2390
2391 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2392 &num_clusters, el);
2393 if (ret) {
2394 mlog_errno(ret);
2395 goto out;
2396 }
2397
2398 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2399
2400 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2401 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2402
2403 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2404 p_blkno, first_hash, num_clusters, xs);
2405
2406out:
2407 return ret;
2408}
2409
2410static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2411 u64 blkno,
2412 u32 clusters,
2413 xattr_bucket_func *func,
2414 void *para)
2415{
2416 int i, j, ret = 0;
2417 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2418 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2419 u32 num_buckets = clusters * bpc;
2420 struct ocfs2_xattr_bucket bucket;
2421
2422 memset(&bucket, 0, sizeof(bucket));
2423
2424 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2425 clusters, blkno);
2426
2427 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2428 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2429 bucket.bhs, OCFS2_BH_CACHED);
2430 if (ret) {
2431 mlog_errno(ret);
2432 goto out;
2433 }
2434
2435 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2436 /*
2437 * The real bucket num in this series of blocks is stored
2438 * in the 1st bucket.
2439 */
2440 if (i == 0)
2441 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2442
2443 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2444 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2445 if (func) {
2446 ret = func(inode, &bucket, para);
2447 if (ret) {
2448 mlog_errno(ret);
2449 break;
2450 }
2451 }
2452
2453 for (j = 0; j < blk_per_bucket; j++)
2454 brelse(bucket.bhs[j]);
2455 memset(&bucket, 0, sizeof(bucket));
2456 }
2457
2458out:
2459 for (j = 0; j < blk_per_bucket; j++)
2460 brelse(bucket.bhs[j]);
2461
2462 return ret;
2463}
2464
2465struct ocfs2_xattr_tree_list {
2466 char *buffer;
2467 size_t buffer_size;
2468 size_t result;
2469};
2470
2471static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2472 struct ocfs2_xattr_header *xh,
2473 int index,
2474 int *block_off,
2475 int *new_offset)
2476{
2477 u16 name_offset;
2478
2479 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2480 return -EINVAL;
2481
2482 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2483
2484 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2485 *new_offset = name_offset % inode->i_sb->s_blocksize;
2486
2487 return 0;
2488}
2489
2490static int ocfs2_list_xattr_bucket(struct inode *inode,
2491 struct ocfs2_xattr_bucket *bucket,
2492 void *para)
2493{
2494 int ret = 0, type;
2495 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2496 int i, block_off, new_offset;
2497 const char *prefix, *name;
2498
2499 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2500 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2501 type = ocfs2_xattr_get_type(entry);
2502 prefix = ocfs2_xattr_prefix(type);
2503
2504 if (prefix) {
2505 ret = ocfs2_xattr_bucket_get_name_value(inode,
2506 bucket->xh,
2507 i,
2508 &block_off,
2509 &new_offset);
2510 if (ret)
2511 break;
2512
2513 name = (const char *)bucket->bhs[block_off]->b_data +
2514 new_offset;
2515 ret = ocfs2_xattr_list_entry(xl->buffer,
2516 xl->buffer_size,
2517 &xl->result,
2518 prefix, name,
2519 entry->xe_name_len);
2520 if (ret)
2521 break;
2522 }
2523 }
2524
2525 return ret;
2526}
2527
2528static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2529 struct ocfs2_xattr_tree_root *xt,
2530 char *buffer,
2531 size_t buffer_size)
2532{
2533 struct ocfs2_extent_list *el = &xt->xt_list;
2534 int ret = 0;
2535 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2536 u64 p_blkno = 0;
2537 struct ocfs2_xattr_tree_list xl = {
2538 .buffer = buffer,
2539 .buffer_size = buffer_size,
2540 .result = 0,
2541 };
2542
2543 if (le16_to_cpu(el->l_next_free_rec) == 0)
2544 return 0;
2545
2546 while (name_hash > 0) {
2547 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2548 &e_cpos, &num_clusters, el);
2549 if (ret) {
2550 mlog_errno(ret);
2551 goto out;
2552 }
2553
2554 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2555 ocfs2_list_xattr_bucket,
2556 &xl);
2557 if (ret) {
2558 mlog_errno(ret);
2559 goto out;
2560 }
2561
2562 if (e_cpos == 0)
2563 break;
2564
2565 name_hash = e_cpos - 1;
2566 }
2567
2568 ret = xl.result;
2569out:
2570 return ret;
2571}
2572
2573static int cmp_xe(const void *a, const void *b)
2574{
2575 const struct ocfs2_xattr_entry *l = a, *r = b;
2576 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2577 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2578
2579 if (l_hash > r_hash)
2580 return 1;
2581 if (l_hash < r_hash)
2582 return -1;
2583 return 0;
2584}
2585
2586static void swap_xe(void *a, void *b, int size)
2587{
2588 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2589
2590 tmp = *l;
2591 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2592 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2593}
2594
2595/*
2596 * When the ocfs2_xattr_block is filled up, new bucket will be created
2597 * and all the xattr entries will be moved to the new bucket.
2598 * Note: we need to sort the entries since they are not saved in order
2599 * in the ocfs2_xattr_block.
2600 */
2601static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2602 struct buffer_head *xb_bh,
2603 struct buffer_head *xh_bh,
2604 struct buffer_head *data_bh)
2605{
2606 int i, blocksize = inode->i_sb->s_blocksize;
2607 u16 offset, size, off_change;
2608 struct ocfs2_xattr_entry *xe;
2609 struct ocfs2_xattr_block *xb =
2610 (struct ocfs2_xattr_block *)xb_bh->b_data;
2611 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2612 struct ocfs2_xattr_header *xh =
2613 (struct ocfs2_xattr_header *)xh_bh->b_data;
2614 u16 count = le16_to_cpu(xb_xh->xh_count);
2615 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2616
2617 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2618 (unsigned long long)xb_bh->b_blocknr,
2619 (unsigned long long)xh_bh->b_blocknr);
2620
2621 memset(xh_bh->b_data, 0, blocksize);
2622 if (data_bh)
2623 memset(data_bh->b_data, 0, blocksize);
2624 /*
2625 * Since the xe_name_offset is based on ocfs2_xattr_header,
2626 * there is a offset change corresponding to the change of
2627 * ocfs2_xattr_header's position.
2628 */
2629 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2630 xe = &xb_xh->xh_entries[count - 1];
2631 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2632 size = blocksize - offset;
2633
2634 /* copy all the names and values. */
2635 if (data_bh)
2636 target = data_bh->b_data;
2637 memcpy(target + offset, src + offset, size);
2638
2639 /* Init new header now. */
2640 xh->xh_count = xb_xh->xh_count;
2641 xh->xh_num_buckets = cpu_to_le16(1);
2642 xh->xh_name_value_len = cpu_to_le16(size);
2643 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2644
2645 /* copy all the entries. */
2646 target = xh_bh->b_data;
2647 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2648 size = count * sizeof(struct ocfs2_xattr_entry);
2649 memcpy(target + offset, (char *)xb_xh + offset, size);
2650
2651 /* Change the xe offset for all the xe because of the move. */
2652 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2653 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2654 for (i = 0; i < count; i++)
2655 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2656
2657 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2658 offset, size, off_change);
2659
2660 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2661 cmp_xe, swap_xe);
2662}
2663
2664/*
2665 * After we move xattr from block to index btree, we have to
2666 * update ocfs2_xattr_search to the new xe and base.
2667 *
2668 * When the entry is in xattr block, xattr_bh indicates the storage place.
2669 * While if the entry is in index b-tree, "bucket" indicates the
2670 * real place of the xattr.
2671 */
2672static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2673 struct ocfs2_xattr_search *xs,
2674 struct buffer_head *old_bh,
2675 struct buffer_head *new_bh)
2676{
2677 int ret = 0;
2678 char *buf = old_bh->b_data;
2679 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2680 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2681 int i, blocksize = inode->i_sb->s_blocksize;
2682 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2683
2684 xs->bucket.bhs[0] = new_bh;
2685 get_bh(new_bh);
2686 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2687 xs->header = xs->bucket.xh;
2688
2689 xs->base = new_bh->b_data;
2690 xs->end = xs->base + inode->i_sb->s_blocksize;
2691
2692 if (!xs->not_found) {
2693 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2694 ret = ocfs2_read_blocks(inode,
2695 xs->bucket.bhs[0]->b_blocknr + 1,
2696 blk_per_bucket - 1, &xs->bucket.bhs[1],
2697 OCFS2_BH_CACHED);
2698 if (ret) {
2699 mlog_errno(ret);
2700 return ret;
2701 }
2702
2703 i = xs->here - old_xh->xh_entries;
2704 xs->here = &xs->header->xh_entries[i];
2705 }
2706 }
2707
2708 return ret;
2709}
2710
2711static int ocfs2_xattr_create_index_block(struct inode *inode,
2712 struct ocfs2_xattr_search *xs)
2713{
2714 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2715 u32 bit_off, len;
2716 u64 blkno;
2717 handle_t *handle;
2718 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2719 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2720 struct ocfs2_alloc_context *data_ac;
2721 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2722 struct buffer_head *xb_bh = xs->xattr_bh;
2723 struct ocfs2_xattr_block *xb =
2724 (struct ocfs2_xattr_block *)xb_bh->b_data;
2725 struct ocfs2_xattr_tree_root *xr;
2726 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2727 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2728
2729 mlog(0, "create xattr index block for %llu\n",
2730 (unsigned long long)xb_bh->b_blocknr);
2731
2732 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2733
2734 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2735 if (ret) {
2736 mlog_errno(ret);
2737 goto out;
2738 }
2739
2740 /*
2741 * XXX:
2742 * We can use this lock for now, and maybe move to a dedicated mutex
2743 * if performance becomes a problem later.
2744 */
2745 down_write(&oi->ip_alloc_sem);
2746
2747 /*
2748 * 3 more credits, one for xattr block update, one for the 1st block
2749 * of the new xattr bucket and one for the value/data.
2750 */
2751 credits += 3;
2752 handle = ocfs2_start_trans(osb, credits);
2753 if (IS_ERR(handle)) {
2754 ret = PTR_ERR(handle);
2755 mlog_errno(ret);
2756 goto out_sem;
2757 }
2758
2759 ret = ocfs2_journal_access(handle, inode, xb_bh,
2760 OCFS2_JOURNAL_ACCESS_WRITE);
2761 if (ret) {
2762 mlog_errno(ret);
2763 goto out_commit;
2764 }
2765
2766 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2767 if (ret) {
2768 mlog_errno(ret);
2769 goto out_commit;
2770 }
2771
2772 /*
2773 * The bucket may spread in many blocks, and
2774 * we will only touch the 1st block and the last block
2775 * in the whole bucket(one for entry and one for data).
2776 */
2777 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2778
2779 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2780
2781 xh_bh = sb_getblk(inode->i_sb, blkno);
2782 if (!xh_bh) {
2783 ret = -EIO;
2784 mlog_errno(ret);
2785 goto out_commit;
2786 }
2787
2788 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2789
2790 ret = ocfs2_journal_access(handle, inode, xh_bh,
2791 OCFS2_JOURNAL_ACCESS_CREATE);
2792 if (ret) {
2793 mlog_errno(ret);
2794 goto out_commit;
2795 }
2796
2797 if (bpb > 1) {
2798 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2799 if (!data_bh) {
2800 ret = -EIO;
2801 mlog_errno(ret);
2802 goto out_commit;
2803 }
2804
2805 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2806
2807 ret = ocfs2_journal_access(handle, inode, data_bh,
2808 OCFS2_JOURNAL_ACCESS_CREATE);
2809 if (ret) {
2810 mlog_errno(ret);
2811 goto out_commit;
2812 }
2813 }
2814
2815 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2816
2817 ocfs2_journal_dirty(handle, xh_bh);
2818 if (data_bh)
2819 ocfs2_journal_dirty(handle, data_bh);
2820
2821 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2822
2823 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2824 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2825 offsetof(struct ocfs2_xattr_block, xb_attrs));
2826
2827 xr = &xb->xb_attrs.xb_root;
2828 xr->xt_clusters = cpu_to_le32(1);
2829 xr->xt_last_eb_blk = 0;
2830 xr->xt_list.l_tree_depth = 0;
2831 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2832 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2833
2834 xr->xt_list.l_recs[0].e_cpos = 0;
2835 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2836 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2837
2838 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2839
2840 ret = ocfs2_journal_dirty(handle, xb_bh);
2841 if (ret) {
2842 mlog_errno(ret);
2843 goto out_commit;
2844 }
2845
2846out_commit:
2847 ocfs2_commit_trans(osb, handle);
2848
2849out_sem:
2850 up_write(&oi->ip_alloc_sem);
2851
2852out:
2853 if (data_ac)
2854 ocfs2_free_alloc_context(data_ac);
2855
2856 brelse(xh_bh);
2857 brelse(data_bh);
2858
2859 return ret;
2860}
2861
2862static int cmp_xe_offset(const void *a, const void *b)
2863{
2864 const struct ocfs2_xattr_entry *l = a, *r = b;
2865 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2866 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2867
2868 if (l_name_offset < r_name_offset)
2869 return 1;
2870 if (l_name_offset > r_name_offset)
2871 return -1;
2872 return 0;
2873}
2874
2875/*
2876 * defrag a xattr bucket if we find that the bucket has some
2877 * holes beteen name/value pairs.
2878 * We will move all the name/value pairs to the end of the bucket
2879 * so that we can spare some space for insertion.
2880 */
2881static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2882 struct ocfs2_xattr_bucket *bucket)
2883{
2884 int ret, i;
2885 size_t end, offset, len, value_len;
2886 struct ocfs2_xattr_header *xh;
2887 char *entries, *buf, *bucket_buf = NULL;
2888 u64 blkno = bucket->bhs[0]->b_blocknr;
2889 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2890 u16 xh_free_start;
2891 size_t blocksize = inode->i_sb->s_blocksize;
2892 handle_t *handle;
2893 struct buffer_head **bhs;
2894 struct ocfs2_xattr_entry *xe;
2895
2896 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2897 GFP_NOFS);
2898 if (!bhs)
2899 return -ENOMEM;
2900
2901 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs,
2902 OCFS2_BH_CACHED);
2903 if (ret)
2904 goto out;
2905
2906 /*
2907 * In order to make the operation more efficient and generic,
2908 * we copy all the blocks into a contiguous memory and do the
2909 * defragment there, so if anything is error, we will not touch
2910 * the real block.
2911 */
2912 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2913 if (!bucket_buf) {
2914 ret = -EIO;
2915 goto out;
2916 }
2917
2918 buf = bucket_buf;
2919 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2920 memcpy(buf, bhs[i]->b_data, blocksize);
2921
2922 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2923 if (IS_ERR(handle)) {
2924 ret = PTR_ERR(handle);
2925 handle = NULL;
2926 mlog_errno(ret);
2927 goto out;
2928 }
2929
2930 for (i = 0; i < blk_per_bucket; i++) {
2931 ret = ocfs2_journal_access(handle, inode, bhs[i],
2932 OCFS2_JOURNAL_ACCESS_WRITE);
2933 if (ret < 0) {
2934 mlog_errno(ret);
2935 goto commit;
2936 }
2937 }
2938
2939 xh = (struct ocfs2_xattr_header *)bucket_buf;
2940 entries = (char *)xh->xh_entries;
2941 xh_free_start = le16_to_cpu(xh->xh_free_start);
2942
2943 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2944 "xh_free_start = %u, xh_name_value_len = %u.\n",
2945 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2946 le16_to_cpu(xh->xh_name_value_len));
2947
2948 /*
2949 * sort all the entries by their offset.
2950 * the largest will be the first, so that we can
2951 * move them to the end one by one.
2952 */
2953 sort(entries, le16_to_cpu(xh->xh_count),
2954 sizeof(struct ocfs2_xattr_entry),
2955 cmp_xe_offset, swap_xe);
2956
2957 /* Move all name/values to the end of the bucket. */
2958 xe = xh->xh_entries;
2959 end = OCFS2_XATTR_BUCKET_SIZE;
2960 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2961 offset = le16_to_cpu(xe->xe_name_offset);
2962 if (ocfs2_xattr_is_local(xe))
2963 value_len = OCFS2_XATTR_SIZE(
2964 le64_to_cpu(xe->xe_value_size));
2965 else
2966 value_len = OCFS2_XATTR_ROOT_SIZE;
2967 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2968
2969 /*
2970 * We must make sure that the name/value pair
2971 * exist in the same block. So adjust end to
2972 * the previous block end if needed.
2973 */
2974 if (((end - len) / blocksize !=
2975 (end - 1) / blocksize))
2976 end = end - end % blocksize;
2977
2978 if (end > offset + len) {
2979 memmove(bucket_buf + end - len,
2980 bucket_buf + offset, len);
2981 xe->xe_name_offset = cpu_to_le16(end - len);
2982 }
2983
2984 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2985 "bucket %llu\n", (unsigned long long)blkno);
2986
2987 end -= len;
2988 }
2989
2990 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2991 "bucket %llu\n", (unsigned long long)blkno);
2992
2993 if (xh_free_start == end)
2994 goto commit;
2995
2996 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2997 xh->xh_free_start = cpu_to_le16(end);
2998
2999 /* sort the entries by their name_hash. */
3000 sort(entries, le16_to_cpu(xh->xh_count),
3001 sizeof(struct ocfs2_xattr_entry),
3002 cmp_xe, swap_xe);
3003
3004 buf = bucket_buf;
3005 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3006 memcpy(bhs[i]->b_data, buf, blocksize);
3007 ocfs2_journal_dirty(handle, bhs[i]);
3008 }
3009
3010commit:
3011 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3012out:
3013
3014 if (bhs) {
3015 for (i = 0; i < blk_per_bucket; i++)
3016 brelse(bhs[i]);
3017 }
3018 kfree(bhs);
3019
3020 kfree(bucket_buf);
3021 return ret;
3022}
3023
3024/*
3025 * Move half nums of the xattr bucket in the previous cluster to this new
3026 * cluster. We only touch the last cluster of the previous extend record.
3027 *
3028 * first_bh is the first buffer_head of a series of bucket in the same
3029 * extent rec and header_bh is the header of one bucket in this cluster.
3030 * They will be updated if we move the data header_bh contains to the new
3031 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3032 */
3033static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3034 handle_t *handle,
3035 struct buffer_head **first_bh,
3036 struct buffer_head **header_bh,
3037 u64 new_blkno,
3038 u64 prev_blkno,
3039 u32 num_clusters,
3040 u32 *first_hash)
3041{
3042 int i, ret, credits;
3043 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3044 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3045 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3046 int blocksize = inode->i_sb->s_blocksize;
3047 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3048 struct ocfs2_xattr_header *new_xh;
3049 struct ocfs2_xattr_header *xh =
3050 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3051
3052 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3053 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3054
3055 prev_bh = *first_bh;
3056 get_bh(prev_bh);
3057 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3058
3059 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3060
3061 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3062 prev_blkno, new_blkno);
3063
3064 /*
3065 * We need to update the 1st half of the new cluster and
3066 * 1 more for the update of the 1st bucket of the previous
3067 * extent record.
3068 */
3069 credits = bpc / 2 + 1;
3070 ret = ocfs2_extend_trans(handle, credits);
3071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3074 }
3075
3076 ret = ocfs2_journal_access(handle, inode, prev_bh,
3077 OCFS2_JOURNAL_ACCESS_WRITE);
3078 if (ret) {
3079 mlog_errno(ret);
3080 goto out;
3081 }
3082
3083 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3084 old_bh = new_bh = NULL;
3085 new_bh = sb_getblk(inode->i_sb, new_blkno);
3086 if (!new_bh) {
3087 ret = -EIO;
3088 mlog_errno(ret);
3089 goto out;
3090 }
3091
3092 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3093
3094 ret = ocfs2_journal_access(handle, inode, new_bh,
3095 OCFS2_JOURNAL_ACCESS_CREATE);
3096 if (ret < 0) {
3097 mlog_errno(ret);
3098 brelse(new_bh);
3099 goto out;
3100 }
3101
3102 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3103 if (ret < 0) {
3104 mlog_errno(ret);
3105 brelse(new_bh);
3106 goto out;
3107 }
3108
3109 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3110
3111 if (i == 0) {
3112 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3113 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3114
3115 if (first_hash)
3116 *first_hash = le32_to_cpu(
3117 new_xh->xh_entries[0].xe_name_hash);
3118 new_first_bh = new_bh;
3119 get_bh(new_first_bh);
3120 }
3121
3122 ocfs2_journal_dirty(handle, new_bh);
3123
3124 if (*header_bh == old_bh) {
3125 brelse(*header_bh);
3126 *header_bh = new_bh;
3127 get_bh(*header_bh);
3128
3129 brelse(*first_bh);
3130 *first_bh = new_first_bh;
3131 get_bh(*first_bh);
3132 }
3133 brelse(new_bh);
3134 brelse(old_bh);
3135 }
3136
3137 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3138
3139 ocfs2_journal_dirty(handle, prev_bh);
3140out:
3141 brelse(prev_bh);
3142 brelse(new_first_bh);
3143 return ret;
3144}
3145
3146static int ocfs2_read_xattr_bucket(struct inode *inode,
3147 u64 blkno,
3148 struct buffer_head **bhs,
3149 int new)
3150{
3151 int ret = 0;
3152 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3153
3154 if (!new)
3155 return ocfs2_read_blocks(inode, blkno,
3156 blk_per_bucket, bhs,
3157 OCFS2_BH_CACHED);
3158
3159 for (i = 0; i < blk_per_bucket; i++) {
3160 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3161 if (bhs[i] == NULL) {
3162 ret = -EIO;
3163 mlog_errno(ret);
3164 break;
3165 }
3166 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3167 }
3168
3169 return ret;
3170}
3171
3172/*
3173 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3174 * first_hash will record the 1st hash of the new bucket.
3175 */
3176static int ocfs2_half_xattr_bucket(struct inode *inode,
3177 handle_t *handle,
3178 u64 blk,
3179 u64 new_blk,
3180 u32 *first_hash,
3181 int new_bucket_head)
3182{
3183 int ret, i;
3184 u16 count, start, len, name_value_len, xe_len, name_offset;
3185 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3186 struct buffer_head **s_bhs, **t_bhs = NULL;
3187 struct ocfs2_xattr_header *xh;
3188 struct ocfs2_xattr_entry *xe;
3189 int blocksize = inode->i_sb->s_blocksize;
3190
3191 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3192 blk, new_blk);
3193
3194 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3195 if (!s_bhs)
3196 return -ENOMEM;
3197
3198 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3199 if (ret) {
3200 mlog_errno(ret);
3201 goto out;
3202 }
3203
3204 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3205 OCFS2_JOURNAL_ACCESS_WRITE);
3206 if (ret) {
3207 mlog_errno(ret);
3208 goto out;
3209 }
3210
3211 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3212 if (!t_bhs) {
3213 ret = -ENOMEM;
3214 goto out;
3215 }
3216
3217 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3218 if (ret) {
3219 mlog_errno(ret);
3220 goto out;
3221 }
3222
3223 for (i = 0; i < blk_per_bucket; i++) {
3224 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3225 OCFS2_JOURNAL_ACCESS_CREATE);
3226 if (ret) {
3227 mlog_errno(ret);
3228 goto out;
3229 }
3230 }
3231
3232 /* copy the whole bucket to the new first. */
3233 for (i = 0; i < blk_per_bucket; i++)
3234 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3235
3236 /* update the new bucket. */
3237 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3238 count = le16_to_cpu(xh->xh_count);
3239 start = count / 2;
3240
3241 /*
3242 * Calculate the total name/value len and xh_free_start for
3243 * the old bucket first.
3244 */
3245 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3246 name_value_len = 0;
3247 for (i = 0; i < start; i++) {
3248 xe = &xh->xh_entries[i];
3249 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3250 if (ocfs2_xattr_is_local(xe))
3251 xe_len +=
3252 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3253 else
3254 xe_len += OCFS2_XATTR_ROOT_SIZE;
3255 name_value_len += xe_len;
3256 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3257 name_offset = le16_to_cpu(xe->xe_name_offset);
3258 }
3259
3260 /*
3261 * Now begin the modification to the new bucket.
3262 *
3263 * In the new bucket, We just move the xattr entry to the beginning
3264 * and don't touch the name/value. So there will be some holes in the
3265 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3266 * called.
3267 */
3268 xe = &xh->xh_entries[start];
3269 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3270 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3271 (int)((char *)xe - (char *)xh),
3272 (int)((char *)xh->xh_entries - (char *)xh));
3273 memmove((char *)xh->xh_entries, (char *)xe, len);
3274 xe = &xh->xh_entries[count - start];
3275 len = sizeof(struct ocfs2_xattr_entry) * start;
3276 memset((char *)xe, 0, len);
3277
3278 le16_add_cpu(&xh->xh_count, -start);
3279 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3280
3281 /* Calculate xh_free_start for the new bucket. */
3282 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3283 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3284 xe = &xh->xh_entries[i];
3285 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3286 if (ocfs2_xattr_is_local(xe))
3287 xe_len +=
3288 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3289 else
3290 xe_len += OCFS2_XATTR_ROOT_SIZE;
3291 if (le16_to_cpu(xe->xe_name_offset) <
3292 le16_to_cpu(xh->xh_free_start))
3293 xh->xh_free_start = xe->xe_name_offset;
3294 }
3295
3296 /* set xh->xh_num_buckets for the new xh. */
3297 if (new_bucket_head)
3298 xh->xh_num_buckets = cpu_to_le16(1);
3299 else
3300 xh->xh_num_buckets = 0;
3301
3302 for (i = 0; i < blk_per_bucket; i++) {
3303 ocfs2_journal_dirty(handle, t_bhs[i]);
3304 if (ret)
3305 mlog_errno(ret);
3306 }
3307
3308 /* store the first_hash of the new bucket. */
3309 if (first_hash)
3310 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3311
3312 /*
3313 * Now only update the 1st block of the old bucket.
3314 * Please note that the entry has been sorted already above.
3315 */
3316 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3317 memset(&xh->xh_entries[start], 0,
3318 sizeof(struct ocfs2_xattr_entry) * (count - start));
3319 xh->xh_count = cpu_to_le16(start);
3320 xh->xh_free_start = cpu_to_le16(name_offset);
3321 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3322
3323 ocfs2_journal_dirty(handle, s_bhs[0]);
3324 if (ret)
3325 mlog_errno(ret);
3326
3327out:
3328 if (s_bhs) {
3329 for (i = 0; i < blk_per_bucket; i++)
3330 brelse(s_bhs[i]);
3331 }
3332 kfree(s_bhs);
3333
3334 if (t_bhs) {
3335 for (i = 0; i < blk_per_bucket; i++)
3336 brelse(t_bhs[i]);
3337 }
3338 kfree(t_bhs);
3339
3340 return ret;
3341}
3342
3343/*
3344 * Copy xattr from one bucket to another bucket.
3345 *
3346 * The caller must make sure that the journal transaction
3347 * has enough space for journaling.
3348 */
3349static int ocfs2_cp_xattr_bucket(struct inode *inode,
3350 handle_t *handle,
3351 u64 s_blkno,
3352 u64 t_blkno,
3353 int t_is_new)
3354{
3355 int ret, i;
3356 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3357 int blocksize = inode->i_sb->s_blocksize;
3358 struct buffer_head **s_bhs, **t_bhs = NULL;
3359
3360 BUG_ON(s_blkno == t_blkno);
3361
3362 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3363 s_blkno, t_blkno, t_is_new);
3364
3365 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3366 GFP_NOFS);
3367 if (!s_bhs)
3368 return -ENOMEM;
3369
3370 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3371 if (ret)
3372 goto out;
3373
3374 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3375 GFP_NOFS);
3376 if (!t_bhs) {
3377 ret = -ENOMEM;
3378 goto out;
3379 }
3380
3381 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3382 if (ret)
3383 goto out;
3384
3385 for (i = 0; i < blk_per_bucket; i++) {
3386 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3387 OCFS2_JOURNAL_ACCESS_WRITE);
3388 if (ret)
3389 goto out;
3390 }
3391
3392 for (i = 0; i < blk_per_bucket; i++) {
3393 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3394 ocfs2_journal_dirty(handle, t_bhs[i]);
3395 }
3396
3397out:
3398 if (s_bhs) {
3399 for (i = 0; i < blk_per_bucket; i++)
3400 brelse(s_bhs[i]);
3401 }
3402 kfree(s_bhs);
3403
3404 if (t_bhs) {
3405 for (i = 0; i < blk_per_bucket; i++)
3406 brelse(t_bhs[i]);
3407 }
3408 kfree(t_bhs);
3409
3410 return ret;
3411}
3412
3413/*
3414 * Copy one xattr cluster from src_blk to to_blk.
3415 * The to_blk will become the first bucket header of the cluster, so its
3416 * xh_num_buckets will be initialized as the bucket num in the cluster.
3417 */
3418static int ocfs2_cp_xattr_cluster(struct inode *inode,
3419 handle_t *handle,
3420 struct buffer_head *first_bh,
3421 u64 src_blk,
3422 u64 to_blk,
3423 u32 *first_hash)
3424{
3425 int i, ret, credits;
3426 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3427 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3428 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3429 struct buffer_head *bh = NULL;
3430 struct ocfs2_xattr_header *xh;
3431 u64 to_blk_start = to_blk;
3432
3433 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3434
3435 /*
3436 * We need to update the new cluster and 1 more for the update of
3437 * the 1st bucket of the previous extent rec.
3438 */
3439 credits = bpc + 1;
3440 ret = ocfs2_extend_trans(handle, credits);
3441 if (ret) {
3442 mlog_errno(ret);
3443 goto out;
3444 }
3445
3446 ret = ocfs2_journal_access(handle, inode, first_bh,
3447 OCFS2_JOURNAL_ACCESS_WRITE);
3448 if (ret) {
3449 mlog_errno(ret);
3450 goto out;
3451 }
3452
3453 for (i = 0; i < num_buckets; i++) {
3454 ret = ocfs2_cp_xattr_bucket(inode, handle,
3455 src_blk, to_blk, 1);
3456 if (ret) {
3457 mlog_errno(ret);
3458 goto out;
3459 }
3460
3461 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3462 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3463 }
3464
3465 /* update the old bucket header. */
3466 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3467 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3468
3469 ocfs2_journal_dirty(handle, first_bh);
3470
3471 /* update the new bucket header. */
3472 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3473 if (ret < 0) {
3474 mlog_errno(ret);
3475 goto out;
3476 }
3477
3478 ret = ocfs2_journal_access(handle, inode, bh,
3479 OCFS2_JOURNAL_ACCESS_WRITE);
3480 if (ret) {
3481 mlog_errno(ret);
3482 goto out;
3483 }
3484
3485 xh = (struct ocfs2_xattr_header *)bh->b_data;
3486 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3487
3488 ocfs2_journal_dirty(handle, bh);
3489
3490 if (first_hash)
3491 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3492out:
3493 brelse(bh);
3494 return ret;
3495}
3496
3497/*
3498 * Move half of the xattrs in this cluster to the new cluster.
3499 * This function should only be called when bucket size == cluster size.
3500 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3501 */
3502static int ocfs2_half_xattr_cluster(struct inode *inode,
3503 handle_t *handle,
3504 u64 prev_blk,
3505 u64 new_blk,
3506 u32 *first_hash)
3507{
3508 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3509 int ret, credits = 2 * blk_per_bucket;
3510
3511 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3512
3513 ret = ocfs2_extend_trans(handle, credits);
3514 if (ret) {
3515 mlog_errno(ret);
3516 return ret;
3517 }
3518
3519 /* Move half of the xattr in start_blk to the next bucket. */
3520 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3521 new_blk, first_hash, 1);
3522}
3523
3524/*
3525 * Move some xattrs from the old cluster to the new one since they are not
3526 * contiguous in ocfs2 xattr tree.
3527 *
3528 * new_blk starts a new separate cluster, and we will move some xattrs from
3529 * prev_blk to it. v_start will be set as the first name hash value in this
3530 * new cluster so that it can be used as e_cpos during tree insertion and
3531 * don't collide with our original b-tree operations. first_bh and header_bh
3532 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3533 * to extend the insert bucket.
3534 *
3535 * The problem is how much xattr should we move to the new one and when should
3536 * we update first_bh and header_bh?
3537 * 1. If cluster size > bucket size, that means the previous cluster has more
3538 * than 1 bucket, so just move half nums of bucket into the new cluster and
3539 * update the first_bh and header_bh if the insert bucket has been moved
3540 * to the new cluster.
3541 * 2. If cluster_size == bucket_size:
3542 * a) If the previous extent rec has more than one cluster and the insert
3543 * place isn't in the last cluster, copy the entire last cluster to the
3544 * new one. This time, we don't need to upate the first_bh and header_bh
3545 * since they will not be moved into the new cluster.
3546 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3547 * the new one. And we set the extend flag to zero if the insert place is
3548 * moved into the new allocated cluster since no extend is needed.
3549 */
3550static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3551 handle_t *handle,
3552 struct buffer_head **first_bh,
3553 struct buffer_head **header_bh,
3554 u64 new_blk,
3555 u64 prev_blk,
3556 u32 prev_clusters,
3557 u32 *v_start,
3558 int *extend)
3559{
3560 int ret = 0;
3561 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3562
3563 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3564 prev_blk, prev_clusters, new_blk);
3565
3566 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3567 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3568 handle,
3569 first_bh,
3570 header_bh,
3571 new_blk,
3572 prev_blk,
3573 prev_clusters,
3574 v_start);
3575 else {
3576 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3577
3578 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3579 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3580 last_blk, new_blk,
3581 v_start);
3582 else {
3583 ret = ocfs2_half_xattr_cluster(inode, handle,
3584 last_blk, new_blk,
3585 v_start);
3586
3587 if ((*header_bh)->b_blocknr == last_blk && extend)
3588 *extend = 0;
3589 }
3590 }
3591
3592 return ret;
3593}
3594
3595/*
3596 * Add a new cluster for xattr storage.
3597 *
3598 * If the new cluster is contiguous with the previous one, it will be
3599 * appended to the same extent record, and num_clusters will be updated.
3600 * If not, we will insert a new extent for it and move some xattrs in
3601 * the last cluster into the new allocated one.
3602 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3603 * lose the benefits of hashing because we'll have to search large leaves.
3604 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3605 * if it's bigger).
3606 *
3607 * first_bh is the first block of the previous extent rec and header_bh
3608 * indicates the bucket we will insert the new xattrs. They will be updated
3609 * when the header_bh is moved into the new cluster.
3610 */
3611static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3612 struct buffer_head *root_bh,
3613 struct buffer_head **first_bh,
3614 struct buffer_head **header_bh,
3615 u32 *num_clusters,
3616 u32 prev_cpos,
3617 u64 prev_blkno,
3618 int *extend)
3619{
3620 int ret, credits;
3621 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3622 u32 prev_clusters = *num_clusters;
3623 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3624 u64 block;
3625 handle_t *handle = NULL;
3626 struct ocfs2_alloc_context *data_ac = NULL;
3627 struct ocfs2_alloc_context *meta_ac = NULL;
3628 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3629 struct ocfs2_extent_tree et;
3630
3631 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3632 "previous xattr blkno = %llu\n",
3633 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3634 prev_cpos, prev_blkno);
3635
3636 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3637
3638 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3639 &data_ac, &meta_ac);
3640 if (ret) {
3641 mlog_errno(ret);
3642 goto leave;
3643 }
3644
3645 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3646 clusters_to_add);
3647 handle = ocfs2_start_trans(osb, credits);
3648 if (IS_ERR(handle)) {
3649 ret = PTR_ERR(handle);
3650 handle = NULL;
3651 mlog_errno(ret);
3652 goto leave;
3653 }
3654
3655 ret = ocfs2_journal_access(handle, inode, root_bh,
3656 OCFS2_JOURNAL_ACCESS_WRITE);
3657 if (ret < 0) {
3658 mlog_errno(ret);
3659 goto leave;
3660 }
3661
3662 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3663 clusters_to_add, &bit_off, &num_bits);
3664 if (ret < 0) {
3665 if (ret != -ENOSPC)
3666 mlog_errno(ret);
3667 goto leave;
3668 }
3669
3670 BUG_ON(num_bits > clusters_to_add);
3671
3672 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3673 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3674 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3675
3676 if (prev_blkno + prev_clusters * bpc == block &&
3677 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3678 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3679 /*
3680 * If this cluster is contiguous with the old one and
3681 * adding this new cluster, we don't surpass the limit of
3682 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3683 * initialized and used like other buckets in the previous
3684 * cluster.
3685 * So add it as a contiguous one. The caller will handle
3686 * its init process.
3687 */
3688 v_start = prev_cpos + prev_clusters;
3689 *num_clusters = prev_clusters + num_bits;
3690 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3691 num_bits);
3692 } else {
3693 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3694 handle,
3695 first_bh,
3696 header_bh,
3697 block,
3698 prev_blkno,
3699 prev_clusters,
3700 &v_start,
3701 extend);
3702 if (ret) {
3703 mlog_errno(ret);
3704 goto leave;
3705 }
3706 }
3707
3708 if (handle->h_buffer_credits < credits) {
3709 /*
3710 * The journal has been restarted before, and don't
3711 * have enough space for the insertion, so extend it
3712 * here.
3713 */
3714 ret = ocfs2_extend_trans(handle, credits);
3715 if (ret) {
3716 mlog_errno(ret);
3717 goto leave;
3718 }
3719 }
3720 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3721 num_bits, block, v_start);
3722 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3723 num_bits, 0, meta_ac);
3724 if (ret < 0) {
3725 mlog_errno(ret);
3726 goto leave;
3727 }
3728
3729 ret = ocfs2_journal_dirty(handle, root_bh);
3730 if (ret < 0) {
3731 mlog_errno(ret);
3732 goto leave;
3733 }
3734
3735leave:
3736 if (handle)
3737 ocfs2_commit_trans(osb, handle);
3738 if (data_ac)
3739 ocfs2_free_alloc_context(data_ac);
3740 if (meta_ac)
3741 ocfs2_free_alloc_context(meta_ac);
3742
3743 return ret;
3744}
3745
3746/*
3747 * Extend a new xattr bucket and move xattrs to the end one by one until
3748 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3749 */
3750static int ocfs2_extend_xattr_bucket(struct inode *inode,
3751 struct buffer_head *first_bh,
3752 struct buffer_head *start_bh,
3753 u32 num_clusters)
3754{
3755 int ret, credits;
3756 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3757 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3758 u64 start_blk = start_bh->b_blocknr, end_blk;
3759 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3760 handle_t *handle;
3761 struct ocfs2_xattr_header *first_xh =
3762 (struct ocfs2_xattr_header *)first_bh->b_data;
3763 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3764
3765 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3766 "from %llu, len = %u\n", start_blk,
3767 (unsigned long long)first_bh->b_blocknr, num_clusters);
3768
3769 BUG_ON(bucket >= num_buckets);
3770
3771 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3772
3773 /*
3774 * We will touch all the buckets after the start_bh(include it).
3775 * Add one more bucket and modify the first_bh.
3776 */
3777 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3778 handle = ocfs2_start_trans(osb, credits);
3779 if (IS_ERR(handle)) {
3780 ret = PTR_ERR(handle);
3781 handle = NULL;
3782 mlog_errno(ret);
3783 goto out;
3784 }
3785
3786 ret = ocfs2_journal_access(handle, inode, first_bh,
3787 OCFS2_JOURNAL_ACCESS_WRITE);
3788 if (ret) {
3789 mlog_errno(ret);
3790 goto commit;
3791 }
3792
3793 while (end_blk != start_blk) {
3794 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3795 end_blk + blk_per_bucket, 0);
3796 if (ret)
3797 goto commit;
3798 end_blk -= blk_per_bucket;
3799 }
3800
3801 /* Move half of the xattr in start_blk to the next bucket. */
3802 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3803 start_blk + blk_per_bucket, NULL, 0);
3804
3805 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3806 ocfs2_journal_dirty(handle, first_bh);
3807
3808commit:
3809 ocfs2_commit_trans(osb, handle);
3810out:
3811 return ret;
3812}
3813
3814/*
3815 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3816 * xb_bh is the ocfs2_xattr_block.
3817 * We will move all the buckets starting from header_bh to the next place. As
3818 * for this one, half num of its xattrs will be moved to the next one.
3819 *
3820 * We will allocate a new cluster if current cluster is full and adjust
3821 * header_bh and first_bh if the insert place is moved to the new cluster.
3822 */
3823static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3824 struct buffer_head *xb_bh,
3825 struct buffer_head *header_bh)
3826{
3827 struct ocfs2_xattr_header *first_xh = NULL;
3828 struct buffer_head *first_bh = NULL;
3829 struct ocfs2_xattr_block *xb =
3830 (struct ocfs2_xattr_block *)xb_bh->b_data;
3831 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3832 struct ocfs2_extent_list *el = &xb_root->xt_list;
3833 struct ocfs2_xattr_header *xh =
3834 (struct ocfs2_xattr_header *)header_bh->b_data;
3835 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3836 struct super_block *sb = inode->i_sb;
3837 struct ocfs2_super *osb = OCFS2_SB(sb);
3838 int ret, num_buckets, extend = 1;
3839 u64 p_blkno;
3840 u32 e_cpos, num_clusters;
3841
3842 mlog(0, "Add new xattr bucket starting form %llu\n",
3843 (unsigned long long)header_bh->b_blocknr);
3844
3845 /*
3846 * Add refrence for header_bh here because it may be
3847 * changed in ocfs2_add_new_xattr_cluster and we need
3848 * to free it in the end.
3849 */
3850 get_bh(header_bh);
3851
3852 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3853 &num_clusters, el);
3854 if (ret) {
3855 mlog_errno(ret);
3856 goto out;
3857 }
3858
3859 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3860 if (ret) {
3861 mlog_errno(ret);
3862 goto out;
3863 }
3864
3865 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3866 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3867
3868 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3869 ret = ocfs2_add_new_xattr_cluster(inode,
3870 xb_bh,
3871 &first_bh,
3872 &header_bh,
3873 &num_clusters,
3874 e_cpos,
3875 p_blkno,
3876 &extend);
3877 if (ret) {
3878 mlog_errno(ret);
3879 goto out;
3880 }
3881 }
3882
3883 if (extend)
3884 ret = ocfs2_extend_xattr_bucket(inode,
3885 first_bh,
3886 header_bh,
3887 num_clusters);
3888 if (ret)
3889 mlog_errno(ret);
3890out:
3891 brelse(first_bh);
3892 brelse(header_bh);
3893 return ret;
3894}
3895
3896static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3897 struct ocfs2_xattr_bucket *bucket,
3898 int offs)
3899{
3900 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3901
3902 offs = offs % inode->i_sb->s_blocksize;
3903 return bucket->bhs[block_off]->b_data + offs;
3904}
3905
3906/*
3907 * Handle the normal xattr set, including replace, delete and new.
3908 *
3909 * Note: "local" indicates the real data's locality. So we can't
3910 * just its bucket locality by its length.
3911 */
3912static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3913 struct ocfs2_xattr_info *xi,
3914 struct ocfs2_xattr_search *xs,
3915 u32 name_hash,
3916 int local)
3917{
3918 struct ocfs2_xattr_entry *last, *xe;
3919 int name_len = strlen(xi->name);
3920 struct ocfs2_xattr_header *xh = xs->header;
3921 u16 count = le16_to_cpu(xh->xh_count), start;
3922 size_t blocksize = inode->i_sb->s_blocksize;
3923 char *val;
3924 size_t offs, size, new_size;
3925
3926 last = &xh->xh_entries[count];
3927 if (!xs->not_found) {
3928 xe = xs->here;
3929 offs = le16_to_cpu(xe->xe_name_offset);
3930 if (ocfs2_xattr_is_local(xe))
3931 size = OCFS2_XATTR_SIZE(name_len) +
3932 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3933 else
3934 size = OCFS2_XATTR_SIZE(name_len) +
3935 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3936
3937 /*
3938 * If the new value will be stored outside, xi->value has been
3939 * initalized as an empty ocfs2_xattr_value_root, and the same
3940 * goes with xi->value_len, so we can set new_size safely here.
3941 * See ocfs2_xattr_set_in_bucket.
3942 */
3943 new_size = OCFS2_XATTR_SIZE(name_len) +
3944 OCFS2_XATTR_SIZE(xi->value_len);
3945
3946 le16_add_cpu(&xh->xh_name_value_len, -size);
3947 if (xi->value) {
3948 if (new_size > size)
3949 goto set_new_name_value;
3950
3951 /* Now replace the old value with new one. */
3952 if (local)
3953 xe->xe_value_size = cpu_to_le64(xi->value_len);
3954 else
3955 xe->xe_value_size = 0;
3956
3957 val = ocfs2_xattr_bucket_get_val(inode,
3958 &xs->bucket, offs);
3959 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3960 size - OCFS2_XATTR_SIZE(name_len));
3961 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3962 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3963 xi->value, xi->value_len);
3964
3965 le16_add_cpu(&xh->xh_name_value_len, new_size);
3966 ocfs2_xattr_set_local(xe, local);
3967 return;
3968 } else {
3969 /*
3970 * Remove the old entry if there is more than one.
3971 * We don't remove the last entry so that we can
3972 * use it to indicate the hash value of the empty
3973 * bucket.
3974 */
3975 last -= 1;
3976 le16_add_cpu(&xh->xh_count, -1);
3977 if (xh->xh_count) {
3978 memmove(xe, xe + 1,
3979 (void *)last - (void *)xe);
3980 memset(last, 0,
3981 sizeof(struct ocfs2_xattr_entry));
3982 } else
3983 xh->xh_free_start =
3984 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3985
3986 return;
3987 }
3988 } else {
3989 /* find a new entry for insert. */
3990 int low = 0, high = count - 1, tmp;
3991 struct ocfs2_xattr_entry *tmp_xe;
3992
3993 while (low <= high && count) {
3994 tmp = (low + high) / 2;
3995 tmp_xe = &xh->xh_entries[tmp];
3996
3997 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3998 low = tmp + 1;
3999 else if (name_hash <
4000 le32_to_cpu(tmp_xe->xe_name_hash))
4001 high = tmp - 1;
4002 else {
4003 low = tmp;
4004 break;
4005 }
4006 }
4007
4008 xe = &xh->xh_entries[low];
4009 if (low != count)
4010 memmove(xe + 1, xe, (void *)last - (void *)xe);
4011
4012 le16_add_cpu(&xh->xh_count, 1);
4013 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4014 xe->xe_name_hash = cpu_to_le32(name_hash);
4015 xe->xe_name_len = name_len;
4016 ocfs2_xattr_set_type(xe, xi->name_index);
4017 }
4018
4019set_new_name_value:
4020 /* Insert the new name+value. */
4021 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4022
4023 /*
4024 * We must make sure that the name/value pair
4025 * exists in the same block.
4026 */
4027 offs = le16_to_cpu(xh->xh_free_start);
4028 start = offs - size;
4029
4030 if (start >> inode->i_sb->s_blocksize_bits !=
4031 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4032 offs = offs - offs % blocksize;
4033 xh->xh_free_start = cpu_to_le16(offs);
4034 }
4035
4036 val = ocfs2_xattr_bucket_get_val(inode,
4037 &xs->bucket, offs - size);
4038 xe->xe_name_offset = cpu_to_le16(offs - size);
4039
4040 memset(val, 0, size);
4041 memcpy(val, xi->name, name_len);
4042 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4043
4044 xe->xe_value_size = cpu_to_le64(xi->value_len);
4045 ocfs2_xattr_set_local(xe, local);
4046 xs->here = xe;
4047 le16_add_cpu(&xh->xh_free_start, -size);
4048 le16_add_cpu(&xh->xh_name_value_len, size);
4049
4050 return;
4051}
4052
4053static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4054 handle_t *handle,
4055 struct ocfs2_xattr_search *xs,
4056 struct buffer_head **bhs,
4057 u16 bh_num)
4058{
4059 int ret = 0, off, block_off;
4060 struct ocfs2_xattr_entry *xe = xs->here;
4061
4062 /*
4063 * First calculate all the blocks we should journal_access
4064 * and journal_dirty. The first block should always be touched.
4065 */
4066 ret = ocfs2_journal_dirty(handle, bhs[0]);
4067 if (ret)
4068 mlog_errno(ret);
4069
4070 /* calc the data. */
4071 off = le16_to_cpu(xe->xe_name_offset);
4072 block_off = off >> inode->i_sb->s_blocksize_bits;
4073 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4074 if (ret)
4075 mlog_errno(ret);
4076
4077 return ret;
4078}
4079
4080/*
4081 * Set the xattr entry in the specified bucket.
4082 * The bucket is indicated by xs->bucket and it should have the enough
4083 * space for the xattr insertion.
4084 */
4085static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4086 struct ocfs2_xattr_info *xi,
4087 struct ocfs2_xattr_search *xs,
4088 u32 name_hash,
4089 int local)
4090{
4091 int i, ret;
4092 handle_t *handle = NULL;
4093 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4094 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4095
4096 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4097 (unsigned long)xi->value_len, xi->name_index,
4098 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4099
4100 if (!xs->bucket.bhs[1]) {
4101 ret = ocfs2_read_blocks(inode,
4102 xs->bucket.bhs[0]->b_blocknr + 1,
4103 blk_per_bucket - 1, &xs->bucket.bhs[1],
4104 OCFS2_BH_CACHED);
4105 if (ret) {
4106 mlog_errno(ret);
4107 goto out;
4108 }
4109 }
4110
4111 handle = ocfs2_start_trans(osb, blk_per_bucket);
4112 if (IS_ERR(handle)) {
4113 ret = PTR_ERR(handle);
4114 handle = NULL;
4115 mlog_errno(ret);
4116 goto out;
4117 }
4118
4119 for (i = 0; i < blk_per_bucket; i++) {
4120 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4121 OCFS2_JOURNAL_ACCESS_WRITE);
4122 if (ret < 0) {
4123 mlog_errno(ret);
4124 goto out;
4125 }
4126 }
4127
4128 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4129
4130 /*Only dirty the blocks we have touched in set xattr. */
4131 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4132 xs->bucket.bhs, blk_per_bucket);
4133 if (ret)
4134 mlog_errno(ret);
4135out:
4136 ocfs2_commit_trans(osb, handle);
4137
4138 return ret;
4139}
4140
4141static int ocfs2_xattr_value_update_size(struct inode *inode,
4142 struct buffer_head *xe_bh,
4143 struct ocfs2_xattr_entry *xe,
4144 u64 new_size)
4145{
4146 int ret;
4147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4148 handle_t *handle = NULL;
4149
4150 handle = ocfs2_start_trans(osb, 1);
4151 if (handle == NULL) {
4152 ret = -ENOMEM;
4153 mlog_errno(ret);
4154 goto out;
4155 }
4156
4157 ret = ocfs2_journal_access(handle, inode, xe_bh,
4158 OCFS2_JOURNAL_ACCESS_WRITE);
4159 if (ret < 0) {
4160 mlog_errno(ret);
4161 goto out_commit;
4162 }
4163
4164 xe->xe_value_size = cpu_to_le64(new_size);
4165
4166 ret = ocfs2_journal_dirty(handle, xe_bh);
4167 if (ret < 0)
4168 mlog_errno(ret);
4169
4170out_commit:
4171 ocfs2_commit_trans(osb, handle);
4172out:
4173 return ret;
4174}
4175
4176/*
4177 * Truncate the specified xe_off entry in xattr bucket.
4178 * bucket is indicated by header_bh and len is the new length.
4179 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4180 *
4181 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4182 */
4183static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4184 struct buffer_head *header_bh,
4185 int xe_off,
4186 int len)
4187{
4188 int ret, offset;
4189 u64 value_blk;
4190 struct buffer_head *value_bh = NULL;
4191 struct ocfs2_xattr_value_root *xv;
4192 struct ocfs2_xattr_entry *xe;
4193 struct ocfs2_xattr_header *xh =
4194 (struct ocfs2_xattr_header *)header_bh->b_data;
4195 size_t blocksize = inode->i_sb->s_blocksize;
4196
4197 xe = &xh->xh_entries[xe_off];
4198
4199 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4200
4201 offset = le16_to_cpu(xe->xe_name_offset) +
4202 OCFS2_XATTR_SIZE(xe->xe_name_len);
4203
4204 value_blk = offset / blocksize;
4205
4206 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4207 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4208 value_blk += header_bh->b_blocknr;
4209
4210 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4211 if (ret) {
4212 mlog_errno(ret);
4213 goto out;
4214 }
4215
4216 xv = (struct ocfs2_xattr_value_root *)
4217 (value_bh->b_data + offset % blocksize);
4218
4219 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4220 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4221 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4222 if (ret) {
4223 mlog_errno(ret);
4224 goto out;
4225 }
4226
4227 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4228 if (ret) {
4229 mlog_errno(ret);
4230 goto out;
4231 }
4232
4233out:
4234 brelse(value_bh);
4235 return ret;
4236}
4237
4238static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4239 struct ocfs2_xattr_search *xs,
4240 int len)
4241{
4242 int ret, offset;
4243 struct ocfs2_xattr_entry *xe = xs->here;
4244 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4245
4246 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4247
4248 offset = xe - xh->xh_entries;
4249 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4250 offset, len);
4251 if (ret)
4252 mlog_errno(ret);
4253
4254 return ret;
4255}
4256
4257static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4258 struct ocfs2_xattr_search *xs,
4259 char *val,
4260 int value_len)
4261{
4262 int offset;
4263 struct ocfs2_xattr_value_root *xv;
4264 struct ocfs2_xattr_entry *xe = xs->here;
4265
4266 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4267
4268 offset = le16_to_cpu(xe->xe_name_offset) +
4269 OCFS2_XATTR_SIZE(xe->xe_name_len);
4270
4271 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4272
4273 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4274}
4275
4276static int ocfs2_rm_xattr_cluster(struct inode *inode,
4277 struct buffer_head *root_bh,
4278 u64 blkno,
4279 u32 cpos,
4280 u32 len)
4281{
4282 int ret;
4283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4284 struct inode *tl_inode = osb->osb_tl_inode;
4285 handle_t *handle;
4286 struct ocfs2_xattr_block *xb =
4287 (struct ocfs2_xattr_block *)root_bh->b_data;
4288 struct ocfs2_alloc_context *meta_ac = NULL;
4289 struct ocfs2_cached_dealloc_ctxt dealloc;
4290 struct ocfs2_extent_tree et;
4291
4292 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4293
4294 ocfs2_init_dealloc_ctxt(&dealloc);
4295
4296 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4297 cpos, len, (unsigned long long)blkno);
4298
4299 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4300
4301 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4302 if (ret) {
4303 mlog_errno(ret);
4304 return ret;
4305 }
4306
4307 mutex_lock(&tl_inode->i_mutex);
4308
4309 if (ocfs2_truncate_log_needs_flush(osb)) {
4310 ret = __ocfs2_flush_truncate_log(osb);
4311 if (ret < 0) {
4312 mlog_errno(ret);
4313 goto out;
4314 }
4315 }
4316
4317 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4318 if (handle == NULL) {
4319 ret = -ENOMEM;
4320 mlog_errno(ret);
4321 goto out;
4322 }
4323
4324 ret = ocfs2_journal_access(handle, inode, root_bh,
4325 OCFS2_JOURNAL_ACCESS_WRITE);
4326 if (ret) {
4327 mlog_errno(ret);
4328 goto out_commit;
4329 }
4330
4331 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4332 &dealloc);
4333 if (ret) {
4334 mlog_errno(ret);
4335 goto out_commit;
4336 }
4337
4338 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4339
4340 ret = ocfs2_journal_dirty(handle, root_bh);
4341 if (ret) {
4342 mlog_errno(ret);
4343 goto out_commit;
4344 }
4345
4346 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4347 if (ret)
4348 mlog_errno(ret);
4349
4350out_commit:
4351 ocfs2_commit_trans(osb, handle);
4352out:
4353 ocfs2_schedule_truncate_log_flush(osb, 1);
4354
4355 mutex_unlock(&tl_inode->i_mutex);
4356
4357 if (meta_ac)
4358 ocfs2_free_alloc_context(meta_ac);
4359
4360 ocfs2_run_deallocs(osb, &dealloc);
4361
4362 return ret;
4363}
4364
4365static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4366 struct ocfs2_xattr_search *xs)
4367{
4368 handle_t *handle = NULL;
4369 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4370 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4371 le16_to_cpu(xh->xh_count) - 1];
4372 int ret = 0;
4373
4374 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4375 if (IS_ERR(handle)) {
4376 ret = PTR_ERR(handle);
4377 mlog_errno(ret);
4378 return;
4379 }
4380
4381 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4382 OCFS2_JOURNAL_ACCESS_WRITE);
4383 if (ret) {
4384 mlog_errno(ret);
4385 goto out_commit;
4386 }
4387
4388 /* Remove the old entry. */
4389 memmove(xs->here, xs->here + 1,
4390 (void *)last - (void *)xs->here);
4391 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4392 le16_add_cpu(&xh->xh_count, -1);
4393
4394 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4395 if (ret < 0)
4396 mlog_errno(ret);
4397out_commit:
4398 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4399}
4400
4401/*
4402 * Set the xattr name/value in the bucket specified in xs.
4403 *
4404 * As the new value in xi may be stored in the bucket or in an outside cluster,
4405 * we divide the whole process into 3 steps:
4406 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4407 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4408 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4409 * 4. If the clusters for the new outside value can't be allocated, we need
4410 * to free the xattr we allocated in set.
4411 */
4412static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4413 struct ocfs2_xattr_info *xi,
4414 struct ocfs2_xattr_search *xs)
4415{
4416 int ret, local = 1;
4417 size_t value_len;
4418 char *val = (char *)xi->value;
4419 struct ocfs2_xattr_entry *xe = xs->here;
4420 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4421 strlen(xi->name));
4422
4423 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4424 /*
4425 * We need to truncate the xattr storage first.
4426 *
4427 * If both the old and new value are stored to
4428 * outside block, we only need to truncate
4429 * the storage and then set the value outside.
4430 *
4431 * If the new value should be stored within block,
4432 * we should free all the outside block first and
4433 * the modification to the xattr block will be done
4434 * by following steps.
4435 */
4436 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4437 value_len = xi->value_len;
4438 else
4439 value_len = 0;
4440
4441 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4442 value_len);
4443 if (ret)
4444 goto out;
4445
4446 if (value_len)
4447 goto set_value_outside;
4448 }
4449
4450 value_len = xi->value_len;
4451 /* So we have to handle the inside block change now. */
4452 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4453 /*
4454 * If the new value will be stored outside of block,
4455 * initalize a new empty value root and insert it first.
4456 */
4457 local = 0;
4458 xi->value = &def_xv;
4459 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4460 }
4461
4462 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4463 if (ret) {
4464 mlog_errno(ret);
4465 goto out;
4466 }
4467
4468 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4469 goto out;
4470
4471 /* allocate the space now for the outside block storage. */
4472 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4473 value_len);
4474 if (ret) {
4475 mlog_errno(ret);
4476
4477 if (xs->not_found) {
4478 /*
4479 * We can't allocate enough clusters for outside
4480 * storage and we have allocated xattr already,
4481 * so need to remove it.
4482 */
4483 ocfs2_xattr_bucket_remove_xs(inode, xs);
4484 }
4485 goto out;
4486 }
4487
4488set_value_outside:
4489 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4490out:
4491 return ret;
4492}
4493
4494/* check whether the xattr bucket is filled up with the same hash value. */
4495static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4496 struct ocfs2_xattr_bucket *bucket)
4497{
4498 struct ocfs2_xattr_header *xh = bucket->xh;
4499
4500 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4501 xh->xh_entries[0].xe_name_hash) {
4502 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4503 "hash = %u\n",
4504 (unsigned long long)bucket->bhs[0]->b_blocknr,
4505 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4506 return -ENOSPC;
4507 }
4508
4509 return 0;
4510}
4511
4512static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4513 struct ocfs2_xattr_info *xi,
4514 struct ocfs2_xattr_search *xs)
4515{
4516 struct ocfs2_xattr_header *xh;
4517 struct ocfs2_xattr_entry *xe;
4518 u16 count, header_size, xh_free_start;
4519 int i, free, max_free, need, old;
4520 size_t value_size = 0, name_len = strlen(xi->name);
4521 size_t blocksize = inode->i_sb->s_blocksize;
4522 int ret, allocation = 0;
4523 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4524
4525 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4526
4527try_again:
4528 xh = xs->header;
4529 count = le16_to_cpu(xh->xh_count);
4530 xh_free_start = le16_to_cpu(xh->xh_free_start);
4531 header_size = sizeof(struct ocfs2_xattr_header) +
4532 count * sizeof(struct ocfs2_xattr_entry);
4533 max_free = OCFS2_XATTR_BUCKET_SIZE -
4534 le16_to_cpu(xh->xh_name_value_len) - header_size;
4535
4536 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4537 "of %u which exceed block size\n",
4538 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4539 header_size);
4540
4541 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4542 value_size = OCFS2_XATTR_ROOT_SIZE;
4543 else if (xi->value)
4544 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4545
4546 if (xs->not_found)
4547 need = sizeof(struct ocfs2_xattr_entry) +
4548 OCFS2_XATTR_SIZE(name_len) + value_size;
4549 else {
4550 need = value_size + OCFS2_XATTR_SIZE(name_len);
4551
4552 /*
4553 * We only replace the old value if the new length is smaller
4554 * than the old one. Otherwise we will allocate new space in the
4555 * bucket to store it.
4556 */
4557 xe = xs->here;
4558 if (ocfs2_xattr_is_local(xe))
4559 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4560 else
4561 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4562
4563 if (old >= value_size)
4564 need = 0;
4565 }
4566
4567 free = xh_free_start - header_size;
4568 /*
4569 * We need to make sure the new name/value pair
4570 * can exist in the same block.
4571 */
4572 if (xh_free_start % blocksize < need)
4573 free -= xh_free_start % blocksize;
4574
4575 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4576 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4577 " %u\n", xs->not_found,
4578 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4579 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4580 le16_to_cpu(xh->xh_name_value_len));
4581
4582 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4583 if (need <= max_free &&
4584 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4585 /*
4586 * We can create the space by defragment. Since only the
4587 * name/value will be moved, the xe shouldn't be changed
4588 * in xs.
4589 */
4590 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4591 if (ret) {
4592 mlog_errno(ret);
4593 goto out;
4594 }
4595
4596 xh_free_start = le16_to_cpu(xh->xh_free_start);
4597 free = xh_free_start - header_size;
4598 if (xh_free_start % blocksize < need)
4599 free -= xh_free_start % blocksize;
4600
4601 if (free >= need)
4602 goto xattr_set;
4603
4604 mlog(0, "Can't get enough space for xattr insert by "
4605 "defragment. Need %u bytes, but we have %d, so "
4606 "allocate new bucket for it.\n", need, free);
4607 }
4608
4609 /*
4610 * We have to add new buckets or clusters and one
4611 * allocation should leave us enough space for insert.
4612 */
4613 BUG_ON(allocation);
4614
4615 /*
4616 * We do not allow for overlapping ranges between buckets. And
4617 * the maximum number of collisions we will allow for then is
4618 * one bucket's worth, so check it here whether we need to
4619 * add a new bucket for the insert.
4620 */
4621 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4622 if (ret) {
4623 mlog_errno(ret);
4624 goto out;
4625 }
4626
4627 ret = ocfs2_add_new_xattr_bucket(inode,
4628 xs->xattr_bh,
4629 xs->bucket.bhs[0]);
4630 if (ret) {
4631 mlog_errno(ret);
4632 goto out;
4633 }
4634
4635 for (i = 0; i < blk_per_bucket; i++)
4636 brelse(xs->bucket.bhs[i]);
4637
4638 memset(&xs->bucket, 0, sizeof(xs->bucket));
4639
4640 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4641 xi->name_index,
4642 xi->name, xs);
4643 if (ret && ret != -ENODATA)
4644 goto out;
4645 xs->not_found = ret;
4646 allocation = 1;
4647 goto try_again;
4648 }
4649
4650xattr_set:
4651 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4652out:
4653 mlog_exit(ret);
4654 return ret;
4655}
4656
4657static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4658 struct ocfs2_xattr_bucket *bucket,
4659 void *para)
4660{
4661 int ret = 0;
4662 struct ocfs2_xattr_header *xh = bucket->xh;
4663 u16 i;
4664 struct ocfs2_xattr_entry *xe;
4665
4666 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4667 xe = &xh->xh_entries[i];
4668 if (ocfs2_xattr_is_local(xe))
4669 continue;
4670
4671 ret = ocfs2_xattr_bucket_value_truncate(inode,
4672 bucket->bhs[0],
4673 i, 0);
4674 if (ret) {
4675 mlog_errno(ret);
4676 break;
4677 }
4678 }
4679
4680 return ret;
4681}
4682
4683static int ocfs2_delete_xattr_index_block(struct inode *inode,
4684 struct buffer_head *xb_bh)
4685{
4686 struct ocfs2_xattr_block *xb =
4687 (struct ocfs2_xattr_block *)xb_bh->b_data;
4688 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4689 int ret = 0;
4690 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4691 u64 p_blkno;
4692
4693 if (le16_to_cpu(el->l_next_free_rec) == 0)
4694 return 0;
4695
4696 while (name_hash > 0) {
4697 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4698 &e_cpos, &num_clusters, el);
4699 if (ret) {
4700 mlog_errno(ret);
4701 goto out;
4702 }
4703
4704 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4705 ocfs2_delete_xattr_in_bucket,
4706 NULL);
4707 if (ret) {
4708 mlog_errno(ret);
4709 goto out;
4710 }
4711
4712 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4713 p_blkno, e_cpos, num_clusters);
4714 if (ret) {
4715 mlog_errno(ret);
4716 break;
4717 }
4718
4719 if (e_cpos == 0)
4720 break;
4721
4722 name_hash = e_cpos - 1;
4723 }
4724
4725out:
4726 return ret;
4727}
4728
4729/*
4730 * 'trusted' attributes support
4731 */
4732
4733#define XATTR_TRUSTED_PREFIX "trusted."
4734
4735static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4736 size_t list_size, const char *name,
4737 size_t name_len)
4738{
4739 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4740 const size_t total_len = prefix_len + name_len + 1;
4741
4742 if (list && total_len <= list_size) {
4743 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4744 memcpy(list + prefix_len, name, name_len);
4745 list[prefix_len + name_len] = '\0';
4746 }
4747 return total_len;
4748}
4749
4750static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4751 void *buffer, size_t size)
4752{
4753 if (strcmp(name, "") == 0)
4754 return -EINVAL;
4755 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4756 buffer, size);
4757}
4758
4759static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4760 const void *value, size_t size, int flags)
4761{
4762 if (strcmp(name, "") == 0)
4763 return -EINVAL;
4764
4765 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4766 size, flags);
4767}
4768
4769struct xattr_handler ocfs2_xattr_trusted_handler = {
4770 .prefix = XATTR_TRUSTED_PREFIX,
4771 .list = ocfs2_xattr_trusted_list,
4772 .get = ocfs2_xattr_trusted_get,
4773 .set = ocfs2_xattr_trusted_set,
4774};
4775
4776
4777/*
4778 * 'user' attributes support
4779 */
4780
4781#define XATTR_USER_PREFIX "user."
4782
4783static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4784 size_t list_size, const char *name,
4785 size_t name_len)
4786{
4787 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4788 const size_t total_len = prefix_len + name_len + 1;
4789 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4790
4791 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4792 return 0;
4793
4794 if (list && total_len <= list_size) {
4795 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4796 memcpy(list + prefix_len, name, name_len);
4797 list[prefix_len + name_len] = '\0';
4798 }
4799 return total_len;
4800}
4801
4802static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4803 void *buffer, size_t size)
4804{
4805 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4806
4807 if (strcmp(name, "") == 0)
4808 return -EINVAL;
4809 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4810 return -EOPNOTSUPP;
4811 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4812 buffer, size);
4813}
4814
4815static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4816 const void *value, size_t size, int flags)
4817{
4818 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4819
4820 if (strcmp(name, "") == 0)
4821 return -EINVAL;
4822 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4823 return -EOPNOTSUPP;
4824
4825 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4826 size, flags);
4827}
4828
4829struct xattr_handler ocfs2_xattr_user_handler = {
4830 .prefix = XATTR_USER_PREFIX,
4831 .list = ocfs2_xattr_user_list,
4832 .get = ocfs2_xattr_user_get,
4833 .set = ocfs2_xattr_user_set,
4834};
diff --git a/fs/ocfs2/xattr.h b/fs/ocfs2/xattr.h
new file mode 100644
index 000000000000..c25c7c62a059
--- /dev/null
+++ b/fs/ocfs2/xattr.h
@@ -0,0 +1,68 @@
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.h
5 *
6 * Function prototypes
7 *
8 * Copyright (C) 2008 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#ifndef OCFS2_XATTR_H
27#define OCFS2_XATTR_H
28
29#include <linux/init.h>
30#include <linux/xattr.h>
31
32enum ocfs2_xattr_type {
33 OCFS2_XATTR_INDEX_USER = 1,
34 OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS,
35 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
36 OCFS2_XATTR_INDEX_TRUSTED,
37 OCFS2_XATTR_INDEX_SECURITY,
38 OCFS2_XATTR_MAX
39};
40
41extern struct xattr_handler ocfs2_xattr_user_handler;
42extern struct xattr_handler ocfs2_xattr_trusted_handler;
43
44extern ssize_t ocfs2_listxattr(struct dentry *, char *, size_t);
45extern int ocfs2_xattr_get(struct inode *, int, const char *, void *, size_t);
46extern int ocfs2_xattr_set(struct inode *, int, const char *, const void *,
47 size_t, int);
48extern int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh);
49extern struct xattr_handler *ocfs2_xattr_handlers[];
50
51static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
52{
53 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
54}
55
56static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
57{
58 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
59}
60
61static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
62{
63 u16 len = sb->s_blocksize -
64 offsetof(struct ocfs2_xattr_header, xh_entries);
65
66 return len / sizeof(struct ocfs2_xattr_entry);
67}
68#endif /* OCFS2_XATTR_H */
diff --git a/include/linux/hid.h b/include/linux/hid.h
index ac4e678a04ed..f13bca2dd53b 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -67,6 +67,7 @@
67#include <linux/types.h> 67#include <linux/types.h>
68#include <linux/slab.h> 68#include <linux/slab.h>
69#include <linux/list.h> 69#include <linux/list.h>
70#include <linux/mod_devicetable.h> /* hid_device_id */
70#include <linux/timer.h> 71#include <linux/timer.h>
71#include <linux/workqueue.h> 72#include <linux/workqueue.h>
72#include <linux/input.h> 73#include <linux/input.h>
@@ -246,6 +247,19 @@ struct hid_item {
246#define HID_FEATURE_REPORT 2 247#define HID_FEATURE_REPORT 2
247 248
248/* 249/*
250 * HID connect requests
251 */
252
253#define HID_CONNECT_HIDINPUT 0x01
254#define HID_CONNECT_HIDINPUT_FORCE 0x02
255#define HID_CONNECT_HIDRAW 0x04
256#define HID_CONNECT_HIDDEV 0x08
257#define HID_CONNECT_HIDDEV_FORCE 0x10
258#define HID_CONNECT_FF 0x20
259#define HID_CONNECT_DEFAULT (HID_CONNECT_HIDINPUT|HID_CONNECT_HIDRAW| \
260 HID_CONNECT_HIDDEV|HID_CONNECT_FF)
261
262/*
249 * HID device quirks. 263 * HID device quirks.
250 */ 264 */
251 265
@@ -256,48 +270,11 @@ struct hid_item {
256 270
257#define HID_QUIRK_INVERT 0x00000001 271#define HID_QUIRK_INVERT 0x00000001
258#define HID_QUIRK_NOTOUCH 0x00000002 272#define HID_QUIRK_NOTOUCH 0x00000002
259#define HID_QUIRK_IGNORE 0x00000004
260#define HID_QUIRK_NOGET 0x00000008 273#define HID_QUIRK_NOGET 0x00000008
261#define HID_QUIRK_HIDDEV 0x00000010
262#define HID_QUIRK_BADPAD 0x00000020 274#define HID_QUIRK_BADPAD 0x00000020
263#define HID_QUIRK_MULTI_INPUT 0x00000040 275#define HID_QUIRK_MULTI_INPUT 0x00000040
264#define HID_QUIRK_2WHEEL_MOUSE_HACK_7 0x00000080
265#define HID_QUIRK_2WHEEL_MOUSE_HACK_5 0x00000100
266#define HID_QUIRK_2WHEEL_MOUSE_HACK_ON 0x00000200
267#define HID_QUIRK_MIGHTYMOUSE 0x00000400
268#define HID_QUIRK_APPLE_HAS_FN 0x00000800
269#define HID_QUIRK_APPLE_FN_ON 0x00001000
270#define HID_QUIRK_INVERT_HWHEEL 0x00002000
271#define HID_QUIRK_APPLE_ISO_KEYBOARD 0x00004000
272#define HID_QUIRK_BAD_RELATIVE_KEYS 0x00008000
273#define HID_QUIRK_SKIP_OUTPUT_REPORTS 0x00010000 276#define HID_QUIRK_SKIP_OUTPUT_REPORTS 0x00010000
274#define HID_QUIRK_IGNORE_MOUSE 0x00020000
275#define HID_QUIRK_SONY_PS3_CONTROLLER 0x00040000
276#define HID_QUIRK_DUPLICATE_USAGES 0x00080000
277#define HID_QUIRK_RESET_LEDS 0x00100000
278#define HID_QUIRK_HIDINPUT 0x00200000
279#define HID_QUIRK_LOGITECH_IGNORE_DOUBLED_WHEEL 0x00400000
280#define HID_QUIRK_LOGITECH_EXPANDED_KEYMAP 0x00800000
281#define HID_QUIRK_IGNORE_HIDINPUT 0x01000000
282#define HID_QUIRK_2WHEEL_MOUSE_HACK_B8 0x02000000
283#define HID_QUIRK_HWHEEL_WHEEL_INVERT 0x04000000
284#define HID_QUIRK_MICROSOFT_KEYS 0x08000000
285#define HID_QUIRK_FULLSPEED_INTERVAL 0x10000000 277#define HID_QUIRK_FULLSPEED_INTERVAL 0x10000000
286#define HID_QUIRK_APPLE_NUMLOCK_EMULATION 0x20000000
287
288/*
289 * Separate quirks for runtime report descriptor fixup
290 */
291
292#define HID_QUIRK_RDESC_CYMOTION 0x00000001
293#define HID_QUIRK_RDESC_LOGITECH 0x00000002
294#define HID_QUIRK_RDESC_SWAPPED_MIN_MAX 0x00000004
295#define HID_QUIRK_RDESC_PETALYNX 0x00000008
296#define HID_QUIRK_RDESC_MACBOOK_JIS 0x00000010
297#define HID_QUIRK_RDESC_BUTTON_CONSUMER 0x00000020
298#define HID_QUIRK_RDESC_SAMSUNG_REMOTE 0x00000040
299#define HID_QUIRK_RDESC_MICROSOFT_RECV_1028 0x00000080
300#define HID_QUIRK_RDESC_SUNPLUS_WDESKTOP 0x00000100
301 278
302/* 279/*
303 * This is the global environment of the parser. This information is 280 * This is the global environment of the parser. This information is
@@ -411,12 +388,21 @@ struct hid_report_enum {
411struct hid_control_fifo { 388struct hid_control_fifo {
412 unsigned char dir; 389 unsigned char dir;
413 struct hid_report *report; 390 struct hid_report *report;
391 char *raw_report;
392};
393
394struct hid_output_fifo {
395 struct hid_report *report;
396 char *raw_report;
414}; 397};
415 398
416#define HID_CLAIMED_INPUT 1 399#define HID_CLAIMED_INPUT 1
417#define HID_CLAIMED_HIDDEV 2 400#define HID_CLAIMED_HIDDEV 2
418#define HID_CLAIMED_HIDRAW 4 401#define HID_CLAIMED_HIDRAW 4
419 402
403#define HID_STAT_ADDED 1
404#define HID_STAT_PARSED 2
405
420#define HID_CTRL_RUNNING 1 406#define HID_CTRL_RUNNING 1
421#define HID_OUT_RUNNING 2 407#define HID_OUT_RUNNING 2
422#define HID_IN_RUNNING 3 408#define HID_IN_RUNNING 3
@@ -431,22 +417,28 @@ struct hid_input {
431 struct input_dev *input; 417 struct input_dev *input;
432}; 418};
433 419
420struct hid_driver;
421struct hid_ll_driver;
422
434struct hid_device { /* device report descriptor */ 423struct hid_device { /* device report descriptor */
435 __u8 *rdesc; 424 __u8 *rdesc;
436 unsigned rsize; 425 unsigned rsize;
437 struct hid_collection *collection; /* List of HID collections */ 426 struct hid_collection *collection; /* List of HID collections */
438 unsigned collection_size; /* Number of allocated hid_collections */ 427 unsigned collection_size; /* Number of allocated hid_collections */
439 unsigned maxcollection; /* Number of parsed collections */ 428 unsigned maxcollection; /* Number of parsed collections */
440 unsigned maxapplication; /* Number of applications */ 429 unsigned maxapplication; /* Number of applications */
441 unsigned short bus; /* BUS ID */ 430 __u16 bus; /* BUS ID */
442 unsigned short vendor; /* Vendor ID */ 431 __u32 vendor; /* Vendor ID */
443 unsigned short product; /* Product ID */ 432 __u32 product; /* Product ID */
444 unsigned version; /* HID version */ 433 __u32 version; /* HID version */
445 unsigned country; /* HID country */ 434 unsigned country; /* HID country */
446 struct hid_report_enum report_enum[HID_REPORT_TYPES]; 435 struct hid_report_enum report_enum[HID_REPORT_TYPES];
447 436
448 struct device *dev; /* device */ 437 struct device dev; /* device */
438 struct hid_driver *driver;
439 struct hid_ll_driver *ll_driver;
449 440
441 unsigned int status; /* see STAT flags above */
450 unsigned claimed; /* Claimed by hidinput, hiddev? */ 442 unsigned claimed; /* Claimed by hidinput, hiddev? */
451 unsigned quirks; /* Various quirks the device can pull on us */ 443 unsigned quirks; /* Various quirks the device can pull on us */
452 444
@@ -462,26 +454,29 @@ struct hid_device { /* device report descriptor */
462 454
463 void *driver_data; 455 void *driver_data;
464 456
465 __s32 delayed_value; /* For A4 Tech mice hwheel quirk */ 457 /* temporary hid_ff handling (until moved to the drivers) */
466 458 int (*ff_init)(struct hid_device *);
467 /* device-specific function pointers */
468 int (*hidinput_input_event) (struct input_dev *, unsigned int, unsigned int, int);
469 int (*hid_open) (struct hid_device *);
470 void (*hid_close) (struct hid_device *);
471 459
472 /* hiddev event handler */ 460 /* hiddev event handler */
461 int (*hiddev_connect)(struct hid_device *, unsigned int);
473 void (*hiddev_hid_event) (struct hid_device *, struct hid_field *field, 462 void (*hiddev_hid_event) (struct hid_device *, struct hid_field *field,
474 struct hid_usage *, __s32); 463 struct hid_usage *, __s32);
475 void (*hiddev_report_event) (struct hid_device *, struct hid_report *); 464 void (*hiddev_report_event) (struct hid_device *, struct hid_report *);
476 465
477 /* handler for raw output data, used by hidraw */ 466 /* handler for raw output data, used by hidraw */
478 int (*hid_output_raw_report) (struct hid_device *, __u8 *, size_t); 467 int (*hid_output_raw_report) (struct hid_device *, __u8 *, size_t);
479#ifdef CONFIG_USB_HIDINPUT_POWERBOOK
480 unsigned long apple_pressed_fn[BITS_TO_LONGS(KEY_CNT)];
481 unsigned long pb_pressed_numlock[BITS_TO_LONGS(KEY_CNT)];
482#endif
483}; 468};
484 469
470static inline void *hid_get_drvdata(struct hid_device *hdev)
471{
472 return dev_get_drvdata(&hdev->dev);
473}
474
475static inline void hid_set_drvdata(struct hid_device *hdev, void *data)
476{
477 dev_set_drvdata(&hdev->dev, data);
478}
479
485#define HID_GLOBAL_STACK_SIZE 4 480#define HID_GLOBAL_STACK_SIZE 4
486#define HID_COLLECTION_STACK_SIZE 4 481#define HID_COLLECTION_STACK_SIZE 4
487 482
@@ -510,6 +505,107 @@ struct hid_descriptor {
510 struct hid_class_descriptor desc[1]; 505 struct hid_class_descriptor desc[1];
511} __attribute__ ((packed)); 506} __attribute__ ((packed));
512 507
508#define HID_DEVICE(b, ven, prod) \
509 .bus = (b), \
510 .vendor = (ven), .product = (prod)
511
512#define HID_USB_DEVICE(ven, prod) HID_DEVICE(BUS_USB, ven, prod)
513#define HID_BLUETOOTH_DEVICE(ven, prod) HID_DEVICE(BUS_BLUETOOTH, ven, prod)
514
515#define HID_REPORT_ID(rep) \
516 .report_type = (rep)
517#define HID_USAGE_ID(uhid, utype, ucode) \
518 .usage_hid = (uhid), .usage_type = (utype), .usage_code = (ucode)
519/* we don't want to catch types and codes equal to 0 */
520#define HID_TERMINATOR (HID_ANY_ID - 1)
521
522struct hid_report_id {
523 __u32 report_type;
524};
525struct hid_usage_id {
526 __u32 usage_hid;
527 __u32 usage_type;
528 __u32 usage_code;
529};
530
531/**
532 * struct hid_driver
533 * @name: driver name (e.g. "Footech_bar-wheel")
534 * @id_table: which devices is this driver for (must be non-NULL for probe
535 * to be called)
536 * @probe: new device inserted
537 * @remove: device removed (NULL if not a hot-plug capable driver)
538 * @report_table: on which reports to call raw_event (NULL means all)
539 * @raw_event: if report in report_table, this hook is called (NULL means nop)
540 * @usage_table: on which events to call event (NULL means all)
541 * @event: if usage in usage_table, this hook is called (NULL means nop)
542 * @report_fixup: called before report descriptor parsing (NULL means nop)
543 * @input_mapping: invoked on input registering before mapping an usage
544 * @input_mapped: invoked on input registering after mapping an usage
545 *
546 * raw_event and event should return 0 on no action performed, 1 when no
547 * further processing should be done and negative on error
548 *
549 * input_mapping shall return a negative value to completely ignore this usage
550 * (e.g. doubled or invalid usage), zero to continue with parsing of this
551 * usage by generic code (no special handling needed) or positive to skip
552 * generic parsing (needed special handling which was done in the hook already)
553 * input_mapped shall return negative to inform the layer that this usage
554 * should not be considered for further processing or zero to notify that
555 * no processing was performed and should be done in a generic manner
556 * Both these functions may be NULL which means the same behavior as returning
557 * zero from them.
558 */
559struct hid_driver {
560 char *name;
561 const struct hid_device_id *id_table;
562
563 int (*probe)(struct hid_device *dev, const struct hid_device_id *id);
564 void (*remove)(struct hid_device *dev);
565
566 const struct hid_report_id *report_table;
567 int (*raw_event)(struct hid_device *hdev, struct hid_report *report,
568 u8 *data, int size);
569 const struct hid_usage_id *usage_table;
570 int (*event)(struct hid_device *hdev, struct hid_field *field,
571 struct hid_usage *usage, __s32 value);
572
573 void (*report_fixup)(struct hid_device *hdev, __u8 *buf,
574 unsigned int size);
575
576 int (*input_mapping)(struct hid_device *hdev,
577 struct hid_input *hidinput, struct hid_field *field,
578 struct hid_usage *usage, unsigned long **bit, int *max);
579 int (*input_mapped)(struct hid_device *hdev,
580 struct hid_input *hidinput, struct hid_field *field,
581 struct hid_usage *usage, unsigned long **bit, int *max);
582/* private: */
583 struct device_driver driver;
584};
585
586/**
587 * hid_ll_driver - low level driver callbacks
588 * @start: called on probe to start the device
589 * @stop: called on remove
590 * @open: called by input layer on open
591 * @close: called by input layer on close
592 * @hidinput_input_event: event input event (e.g. ff or leds)
593 * @parse: this method is called only once to parse the device data,
594 * shouldn't allocate anything to not leak memory
595 */
596struct hid_ll_driver {
597 int (*start)(struct hid_device *hdev);
598 void (*stop)(struct hid_device *hdev);
599
600 int (*open)(struct hid_device *hdev);
601 void (*close)(struct hid_device *hdev);
602
603 int (*hidinput_input_event) (struct input_dev *idev, unsigned int type,
604 unsigned int code, int value);
605
606 int (*parse)(struct hid_device *hdev);
607};
608
513/* Applications from HID Usage Tables 4/8/99 Version 1.1 */ 609/* Applications from HID Usage Tables 4/8/99 Version 1.1 */
514/* We ignore a few input applications that are not widely used */ 610/* We ignore a few input applications that are not widely used */
515#define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || (a == 0x000c0001) || (a == 0x000d0002)) 611#define IS_INPUT_APPLICATION(a) (((a >= 0x00010000) && (a <= 0x00010008)) || (a == 0x00010080) || (a == 0x000c0001) || (a == 0x000d0002))
@@ -520,43 +616,157 @@ struct hid_descriptor {
520extern int hid_debug; 616extern int hid_debug;
521#endif 617#endif
522 618
619extern int hid_add_device(struct hid_device *);
620extern void hid_destroy_device(struct hid_device *);
621
622extern int __must_check __hid_register_driver(struct hid_driver *,
623 struct module *, const char *mod_name);
624static inline int __must_check hid_register_driver(struct hid_driver *driver)
625{
626 return __hid_register_driver(driver, THIS_MODULE, KBUILD_MODNAME);
627}
628extern void hid_unregister_driver(struct hid_driver *);
629
523extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32); 630extern void hidinput_hid_event(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
524extern void hidinput_report_event(struct hid_device *hid, struct hid_report *report); 631extern void hidinput_report_event(struct hid_device *hid, struct hid_report *report);
525extern int hidinput_connect(struct hid_device *); 632extern int hidinput_connect(struct hid_device *hid, unsigned int force);
526extern void hidinput_disconnect(struct hid_device *); 633extern void hidinput_disconnect(struct hid_device *);
527 634
528int hid_set_field(struct hid_field *, unsigned, __s32); 635int hid_set_field(struct hid_field *, unsigned, __s32);
529int hid_input_report(struct hid_device *, int type, u8 *, int, int); 636int hid_input_report(struct hid_device *, int type, u8 *, int, int);
530int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field); 637int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field);
531int hidinput_mapping_quirks(struct hid_usage *, struct input_dev *, unsigned long **, int *);
532int hidinput_event_quirks(struct hid_device *, struct hid_field *, struct hid_usage *, __s32);
533int hidinput_apple_event(struct hid_device *, struct input_dev *, struct hid_usage *, __s32);
534void hid_output_report(struct hid_report *report, __u8 *data); 638void hid_output_report(struct hid_report *report, __u8 *data);
535void hid_free_device(struct hid_device *device); 639struct hid_device *hid_allocate_device(void);
536struct hid_device *hid_parse_report(__u8 *start, unsigned size); 640int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size);
641int hid_connect(struct hid_device *hid, unsigned int connect_mask);
642
643/**
644 * hid_map_usage - map usage input bits
645 *
646 * @hidinput: hidinput which we are interested in
647 * @usage: usage to fill in
648 * @bit: pointer to input->{}bit (out parameter)
649 * @max: maximal valid usage->code to consider later (out parameter)
650 * @type: input event type (EV_KEY, EV_REL, ...)
651 * @c: code which corresponds to this usage and type
652 */
653static inline void hid_map_usage(struct hid_input *hidinput,
654 struct hid_usage *usage, unsigned long **bit, int *max,
655 __u8 type, __u16 c)
656{
657 struct input_dev *input = hidinput->input;
658
659 usage->type = type;
660 usage->code = c;
661
662 switch (type) {
663 case EV_ABS:
664 *bit = input->absbit;
665 *max = ABS_MAX;
666 break;
667 case EV_REL:
668 *bit = input->relbit;
669 *max = REL_MAX;
670 break;
671 case EV_KEY:
672 *bit = input->keybit;
673 *max = KEY_MAX;
674 break;
675 case EV_LED:
676 *bit = input->ledbit;
677 *max = LED_MAX;
678 break;
679 }
680}
681
682/**
683 * hid_map_usage_clear - map usage input bits and clear the input bit
684 *
685 * The same as hid_map_usage, except the @c bit is also cleared in supported
686 * bits (@bit).
687 */
688static inline void hid_map_usage_clear(struct hid_input *hidinput,
689 struct hid_usage *usage, unsigned long **bit, int *max,
690 __u8 type, __u16 c)
691{
692 hid_map_usage(hidinput, usage, bit, max, type, c);
693 clear_bit(c, *bit);
694}
695
696/**
697 * hid_parse - parse HW reports
698 *
699 * @hdev: hid device
700 *
701 * Call this from probe after you set up the device (if needed). Your
702 * report_fixup will be called (if non-NULL) after reading raw report from
703 * device before passing it to hid layer for real parsing.
704 */
705static inline int __must_check hid_parse(struct hid_device *hdev)
706{
707 int ret;
708
709 if (hdev->status & HID_STAT_PARSED)
710 return 0;
711
712 ret = hdev->ll_driver->parse(hdev);
713 if (!ret)
714 hdev->status |= HID_STAT_PARSED;
715
716 return ret;
717}
718
719/**
720 * hid_hw_start - start underlaying HW
721 *
722 * @hdev: hid device
723 * @connect_mask: which outputs to connect, see HID_CONNECT_*
724 *
725 * Call this in probe function *after* hid_parse. This will setup HW buffers
726 * and start the device (if not deffered to device open). hid_hw_stop must be
727 * called if this was successfull.
728 */
729static inline int __must_check hid_hw_start(struct hid_device *hdev,
730 unsigned int connect_mask)
731{
732 int ret = hdev->ll_driver->start(hdev);
733 if (ret || !connect_mask)
734 return ret;
735 ret = hid_connect(hdev, connect_mask);
736 if (ret)
737 hdev->ll_driver->stop(hdev);
738 return ret;
739}
740
741/**
742 * hid_hw_stop - stop underlaying HW
743 *
744 * @hdev: hid device
745 *
746 * This is usually called from remove function or from probe when something
747 * failed and hid_hw_start was called already.
748 */
749static inline void hid_hw_stop(struct hid_device *hdev)
750{
751 hdev->ll_driver->stop(hdev);
752}
753
754void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
755 int interrupt);
756
757extern int hid_generic_init(void);
758extern void hid_generic_exit(void);
537 759
538/* HID quirks API */ 760/* HID quirks API */
539u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct); 761u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct);
540int usbhid_quirks_init(char **quirks_param); 762int usbhid_quirks_init(char **quirks_param);
541void usbhid_quirks_exit(void); 763void usbhid_quirks_exit(void);
542void usbhid_fixup_report_descriptor(const u16, const u16, char *, unsigned, char **); 764void usbhid_set_leds(struct hid_device *hid);
543
544#ifdef CONFIG_HID_FF
545int hid_ff_init(struct hid_device *hid);
546 765
547int hid_lgff_init(struct hid_device *hid);
548int hid_lg2ff_init(struct hid_device *hid);
549int hid_plff_init(struct hid_device *hid);
550int hid_tmff_init(struct hid_device *hid);
551int hid_zpff_init(struct hid_device *hid);
552#ifdef CONFIG_HID_PID 766#ifdef CONFIG_HID_PID
553int hid_pidff_init(struct hid_device *hid); 767int hid_pidff_init(struct hid_device *hid);
554#else 768#else
555static inline int hid_pidff_init(struct hid_device *hid) { return -ENODEV; } 769#define hid_pidff_init NULL
556#endif
557
558#else
559static inline int hid_ff_init(struct hid_device *hid) { return -1; }
560#endif 770#endif
561 771
562#ifdef CONFIG_HID_DEBUG 772#ifdef CONFIG_HID_DEBUG
@@ -572,10 +782,23 @@ dbg_hid(const char *fmt, ...)
572 return 0; 782 return 0;
573} 783}
574#define dbg_hid_line dbg_hid 784#define dbg_hid_line dbg_hid
575#endif 785#endif /* HID_DEBUG */
576 786
577#define err_hid(format, arg...) printk(KERN_ERR "%s: " format "\n" , \ 787#define err_hid(format, arg...) printk(KERN_ERR "%s: " format "\n" , \
578 __FILE__ , ## arg) 788 __FILE__ , ## arg)
579#endif 789#endif /* HID_FF */
790
791#ifdef CONFIG_HID_COMPAT
792#define HID_COMPAT_LOAD_DRIVER(name) \
793void hid_compat_##name(void) { } \
794EXPORT_SYMBOL(hid_compat_##name)
795#else
796#define HID_COMPAT_LOAD_DRIVER(name)
797#endif /* HID_COMPAT */
798#define HID_COMPAT_CALL_DRIVER(name) do { \
799 extern void hid_compat_##name(void); \
800 hid_compat_##name(); \
801} while (0)
802
580#endif 803#endif
581 804
diff --git a/include/linux/hiddev.h b/include/linux/hiddev.h
index a416b904ba90..c760ae0eb6a1 100644
--- a/include/linux/hiddev.h
+++ b/include/linux/hiddev.h
@@ -182,26 +182,28 @@ struct hiddev_usage_ref_multi {
182/* To traverse the input report descriptor info for a HID device, perform the 182/* To traverse the input report descriptor info for a HID device, perform the
183 * following: 183 * following:
184 * 184 *
185 * rinfo.report_type = HID_REPORT_TYPE_INPUT; 185 * rinfo.report_type = HID_REPORT_TYPE_INPUT;
186 * rinfo.report_id = HID_REPORT_ID_FIRST; 186 * rinfo.report_id = HID_REPORT_ID_FIRST;
187 * ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo); 187 * ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
188 * 188 *
189 * while (ret >= 0) { 189 * while (ret >= 0) {
190 * for (i = 0; i < rinfo.num_fields; i++) { 190 * for (i = 0; i < rinfo.num_fields; i++) {
191 * finfo.report_type = rinfo.report_type; 191 * finfo.report_type = rinfo.report_type;
192 * finfo.report_id = rinfo.report_id; 192 * finfo.report_id = rinfo.report_id;
193 * finfo.field_index = i; 193 * finfo.field_index = i;
194 * ioctl(fd, HIDIOCGFIELDINFO, &finfo); 194 * ioctl(fd, HIDIOCGFIELDINFO, &finfo);
195 * for (j = 0; j < finfo.maxusage; j++) { 195 * for (j = 0; j < finfo.maxusage; j++) {
196 * uref.field_index = i; 196 * uref.report_type = rinfo.report_type;
197 * uref.usage_index = j; 197 * uref.report_id = rinfo.report_id;
198 * ioctl(fd, HIDIOCGUCODE, &uref); 198 * uref.field_index = i;
199 * ioctl(fd, HIDIOCGUSAGE, &uref); 199 * uref.usage_index = j;
200 * } 200 * ioctl(fd, HIDIOCGUCODE, &uref);
201 * } 201 * ioctl(fd, HIDIOCGUSAGE, &uref);
202 * rinfo.report_id |= HID_REPORT_ID_NEXT; 202 * }
203 * ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo); 203 * }
204 * } 204 * rinfo.report_id |= HID_REPORT_ID_NEXT;
205 * ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
206 * }
205 */ 207 */
206 208
207 209
@@ -217,7 +219,7 @@ struct hid_field;
217struct hid_report; 219struct hid_report;
218 220
219#ifdef CONFIG_USB_HIDDEV 221#ifdef CONFIG_USB_HIDDEV
220int hiddev_connect(struct hid_device *); 222int hiddev_connect(struct hid_device *hid, unsigned int force);
221void hiddev_disconnect(struct hid_device *); 223void hiddev_disconnect(struct hid_device *);
222void hiddev_hid_event(struct hid_device *hid, struct hid_field *field, 224void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
223 struct hid_usage *usage, __s32 value); 225 struct hid_usage *usage, __s32 value);
@@ -225,7 +227,9 @@ void hiddev_report_event(struct hid_device *hid, struct hid_report *report);
225int __init hiddev_init(void); 227int __init hiddev_init(void);
226void hiddev_exit(void); 228void hiddev_exit(void);
227#else 229#else
228static inline int hiddev_connect(struct hid_device *hid) { return -1; } 230static inline int hiddev_connect(struct hid_device *hid,
231 unsigned int force)
232{ return -1; }
229static inline void hiddev_disconnect(struct hid_device *hid) { } 233static inline void hiddev_disconnect(struct hid_device *hid) { }
230static inline void hiddev_hid_event(struct hid_device *hid, struct hid_field *field, 234static inline void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
231 struct hid_usage *usage, __s32 value) { } 235 struct hid_usage *usage, __s32 value) { }
diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
index 3481a7d5bc0a..d6a3f47e95cb 100644
--- a/include/linux/mod_devicetable.h
+++ b/include/linux/mod_devicetable.h
@@ -131,6 +131,16 @@ struct usb_device_id {
131#define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100 131#define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
132#define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 132#define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
133 133
134#define HID_ANY_ID (~0)
135
136struct hid_device_id {
137 __u16 bus;
138 __u32 vendor;
139 __u32 product;
140 kernel_ulong_t driver_data
141 __attribute__((aligned(sizeof(kernel_ulong_t))));
142};
143
134/* s390 CCW devices */ 144/* s390 CCW devices */
135struct ccw_device_id { 145struct ccw_device_id {
136 __u16 match_flags; /* which fields to match against */ 146 __u16 match_flags; /* which fields to match against */
diff --git a/net/bluetooth/hidp/core.c b/net/bluetooth/hidp/core.c
index 96434d774c84..acdeab3d9807 100644
--- a/net/bluetooth/hidp/core.c
+++ b/net/bluetooth/hidp/core.c
@@ -578,7 +578,7 @@ static int hidp_session(void *arg)
578 if (session->hid) { 578 if (session->hid) {
579 if (session->hid->claimed & HID_CLAIMED_INPUT) 579 if (session->hid->claimed & HID_CLAIMED_INPUT)
580 hidinput_disconnect(session->hid); 580 hidinput_disconnect(session->hid);
581 hid_free_device(session->hid); 581 hid_destroy_device(session->hid);
582 } 582 }
583 583
584 /* Wakeup user-space polling for socket errors */ 584 /* Wakeup user-space polling for socket errors */
@@ -623,9 +623,15 @@ static struct device *hidp_get_device(struct hidp_session *session)
623static int hidp_setup_input(struct hidp_session *session, 623static int hidp_setup_input(struct hidp_session *session,
624 struct hidp_connadd_req *req) 624 struct hidp_connadd_req *req)
625{ 625{
626 struct input_dev *input = session->input; 626 struct input_dev *input;
627 int i; 627 int i;
628 628
629 input = input_allocate_device();
630 if (!input)
631 return -ENOMEM;
632
633 session->input = input;
634
629 input_set_drvdata(input, session); 635 input_set_drvdata(input, session);
630 636
631 input->name = "Bluetooth HID Boot Protocol Device"; 637 input->name = "Bluetooth HID Boot Protocol Device";
@@ -677,67 +683,114 @@ static void hidp_close(struct hid_device *hid)
677{ 683{
678} 684}
679 685
680static const struct { 686static int hidp_parse(struct hid_device *hid)
681 __u16 idVendor; 687{
682 __u16 idProduct; 688 struct hidp_session *session = hid->driver_data;
683 unsigned quirks; 689 struct hidp_connadd_req *req = session->req;
684} hidp_blacklist[] = { 690 unsigned char *buf;
685 /* Apple wireless Mighty Mouse */ 691 int ret;
686 { 0x05ac, 0x030c, HID_QUIRK_MIGHTYMOUSE | HID_QUIRK_INVERT_HWHEEL },
687 692
688 { } /* Terminating entry */ 693 buf = kmalloc(req->rd_size, GFP_KERNEL);
689}; 694 if (!buf)
695 return -ENOMEM;
696
697 if (copy_from_user(buf, req->rd_data, req->rd_size)) {
698 kfree(buf);
699 return -EFAULT;
700 }
701
702 ret = hid_parse_report(session->hid, buf, req->rd_size);
703
704 kfree(buf);
705
706 if (ret)
707 return ret;
708
709 session->req = NULL;
710
711 return 0;
712}
713
714static int hidp_start(struct hid_device *hid)
715{
716 struct hidp_session *session = hid->driver_data;
717 struct hid_report *report;
690 718
691static void hidp_setup_quirks(struct hid_device *hid) 719 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
720 report_list, list)
721 hidp_send_report(session, report);
722
723 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].
724 report_list, list)
725 hidp_send_report(session, report);
726
727 return 0;
728}
729
730static void hidp_stop(struct hid_device *hid)
692{ 731{
693 unsigned int n; 732 struct hidp_session *session = hid->driver_data;
733
734 skb_queue_purge(&session->ctrl_transmit);
735 skb_queue_purge(&session->intr_transmit);
694 736
695 for (n = 0; hidp_blacklist[n].idVendor; n++) 737 if (hid->claimed & HID_CLAIMED_INPUT)
696 if (hidp_blacklist[n].idVendor == le16_to_cpu(hid->vendor) && 738 hidinput_disconnect(hid);
697 hidp_blacklist[n].idProduct == le16_to_cpu(hid->product)) 739 hid->claimed = 0;
698 hid->quirks = hidp_blacklist[n].quirks;
699} 740}
700 741
701static void hidp_setup_hid(struct hidp_session *session, 742static struct hid_ll_driver hidp_hid_driver = {
743 .parse = hidp_parse,
744 .start = hidp_start,
745 .stop = hidp_stop,
746 .open = hidp_open,
747 .close = hidp_close,
748 .hidinput_input_event = hidp_hidinput_event,
749};
750
751static int hidp_setup_hid(struct hidp_session *session,
702 struct hidp_connadd_req *req) 752 struct hidp_connadd_req *req)
703{ 753{
704 struct hid_device *hid = session->hid; 754 struct hid_device *hid;
705 struct hid_report *report;
706 bdaddr_t src, dst; 755 bdaddr_t src, dst;
756 int ret;
707 757
708 baswap(&src, &bt_sk(session->ctrl_sock->sk)->src); 758 hid = hid_allocate_device();
709 baswap(&dst, &bt_sk(session->ctrl_sock->sk)->dst); 759 if (IS_ERR(hid)) {
760 ret = PTR_ERR(session->hid);
761 goto err;
762 }
710 763
764 session->hid = hid;
765 session->req = req;
711 hid->driver_data = session; 766 hid->driver_data = session;
712 767
713 hid->country = req->country; 768 baswap(&src, &bt_sk(session->ctrl_sock->sk)->src);
769 baswap(&dst, &bt_sk(session->ctrl_sock->sk)->dst);
714 770
715 hid->bus = BUS_BLUETOOTH; 771 hid->bus = BUS_BLUETOOTH;
716 hid->vendor = req->vendor; 772 hid->vendor = req->vendor;
717 hid->product = req->product; 773 hid->product = req->product;
718 hid->version = req->version; 774 hid->version = req->version;
775 hid->country = req->country;
719 776
720 strncpy(hid->name, req->name, 128); 777 strncpy(hid->name, req->name, 128);
721 strncpy(hid->phys, batostr(&src), 64); 778 strncpy(hid->phys, batostr(&src), 64);
722 strncpy(hid->uniq, batostr(&dst), 64); 779 strncpy(hid->uniq, batostr(&dst), 64);
723 780
724 hid->dev = hidp_get_device(session); 781 hid->dev.parent = hidp_get_device(session);
725 782 hid->ll_driver = &hidp_hid_driver;
726 hid->hid_open = hidp_open;
727 hid->hid_close = hidp_close;
728
729 hid->hidinput_input_event = hidp_hidinput_event;
730 783
731 hidp_setup_quirks(hid); 784 ret = hid_add_device(hid);
785 if (ret)
786 goto err_hid;
732 787
733 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) 788 return 0;
734 hidp_send_report(session, report); 789err_hid:
735 790 hid_destroy_device(hid);
736 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list) 791 session->hid = NULL;
737 hidp_send_report(session, report); 792err:
738 793 return ret;
739 if (hidinput_connect(hid) == 0)
740 hid->claimed |= HID_CLAIMED_INPUT;
741} 794}
742 795
743int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock) 796int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
@@ -757,38 +810,6 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
757 810
758 BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size); 811 BT_DBG("rd_data %p rd_size %d", req->rd_data, req->rd_size);
759 812
760 if (req->rd_size > 0) {
761 unsigned char *buf = kmalloc(req->rd_size, GFP_KERNEL);
762
763 if (!buf) {
764 kfree(session);
765 return -ENOMEM;
766 }
767
768 if (copy_from_user(buf, req->rd_data, req->rd_size)) {
769 kfree(buf);
770 kfree(session);
771 return -EFAULT;
772 }
773
774 session->hid = hid_parse_report(buf, req->rd_size);
775
776 kfree(buf);
777
778 if (!session->hid) {
779 kfree(session);
780 return -EINVAL;
781 }
782 }
783
784 if (!session->hid) {
785 session->input = input_allocate_device();
786 if (!session->input) {
787 kfree(session);
788 return -ENOMEM;
789 }
790 }
791
792 down_write(&hidp_session_sem); 813 down_write(&hidp_session_sem);
793 814
794 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst); 815 s = __hidp_get_session(&bt_sk(ctrl_sock->sk)->dst);
@@ -816,15 +837,18 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
816 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID); 837 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
817 session->idle_to = req->idle_to; 838 session->idle_to = req->idle_to;
818 839
819 if (session->input) { 840 if (req->rd_size > 0) {
841 err = hidp_setup_hid(session, req);
842 if (err && err != -ENODEV)
843 goto err_skb;
844 }
845
846 if (!session->hid) {
820 err = hidp_setup_input(session, req); 847 err = hidp_setup_input(session, req);
821 if (err < 0) 848 if (err < 0)
822 goto failed; 849 goto err_skb;
823 } 850 }
824 851
825 if (session->hid)
826 hidp_setup_hid(session, req);
827
828 __hidp_link_session(session); 852 __hidp_link_session(session);
829 853
830 hidp_set_timer(session); 854 hidp_set_timer(session);
@@ -850,17 +874,16 @@ unlink:
850 874
851 __hidp_unlink_session(session); 875 __hidp_unlink_session(session);
852 876
853 if (session->input) { 877 if (session->input)
854 input_unregister_device(session->input); 878 input_unregister_device(session->input);
855 session->input = NULL; /* don't try to free it here */ 879 if (session->hid)
856 } 880 hid_destroy_device(session->hid);
857 881err_skb:
882 skb_queue_purge(&session->ctrl_transmit);
883 skb_queue_purge(&session->intr_transmit);
858failed: 884failed:
859 up_write(&hidp_session_sem); 885 up_write(&hidp_session_sem);
860 886
861 if (session->hid)
862 hid_free_device(session->hid);
863
864 input_free_device(session->input); 887 input_free_device(session->input);
865 kfree(session); 888 kfree(session);
866 return err; 889 return err;
@@ -950,18 +973,43 @@ int hidp_get_conninfo(struct hidp_conninfo *ci)
950 return err; 973 return err;
951} 974}
952 975
976static const struct hid_device_id hidp_table[] = {
977 { HID_BLUETOOTH_DEVICE(HID_ANY_ID, HID_ANY_ID) },
978 { }
979};
980
981static struct hid_driver hidp_driver = {
982 .name = "generic-bluetooth",
983 .id_table = hidp_table,
984};
985
953static int __init hidp_init(void) 986static int __init hidp_init(void)
954{ 987{
988 int ret;
989
955 l2cap_load(); 990 l2cap_load();
956 991
957 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION); 992 BT_INFO("HIDP (Human Interface Emulation) ver %s", VERSION);
958 993
959 return hidp_init_sockets(); 994 ret = hid_register_driver(&hidp_driver);
995 if (ret)
996 goto err;
997
998 ret = hidp_init_sockets();
999 if (ret)
1000 goto err_drv;
1001
1002 return 0;
1003err_drv:
1004 hid_unregister_driver(&hidp_driver);
1005err:
1006 return ret;
960} 1007}
961 1008
962static void __exit hidp_exit(void) 1009static void __exit hidp_exit(void)
963{ 1010{
964 hidp_cleanup_sockets(); 1011 hidp_cleanup_sockets();
1012 hid_unregister_driver(&hidp_driver);
965} 1013}
966 1014
967module_init(hidp_init); 1015module_init(hidp_init);
diff --git a/net/bluetooth/hidp/hidp.h b/net/bluetooth/hidp/hidp.h
index 343fb0566b3e..e503c89057ad 100644
--- a/net/bluetooth/hidp/hidp.h
+++ b/net/bluetooth/hidp/hidp.h
@@ -151,6 +151,8 @@ struct hidp_session {
151 151
152 struct sk_buff_head ctrl_transmit; 152 struct sk_buff_head ctrl_transmit;
153 struct sk_buff_head intr_transmit; 153 struct sk_buff_head intr_transmit;
154
155 struct hidp_connadd_req *req;
154}; 156};
155 157
156static inline void hidp_schedule(struct hidp_session *session) 158static inline void hidp_schedule(struct hidp_session *session)
diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 473f94e56ead..d4dc222a74f3 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -206,6 +206,20 @@ static void do_usb_table(void *symval, unsigned long size,
206 do_usb_entry_multi(symval + i, mod); 206 do_usb_entry_multi(symval + i, mod);
207} 207}
208 208
209/* Looks like: hid:bNvNpN */
210static int do_hid_entry(const char *filename,
211 struct hid_device_id *id, char *alias)
212{
213 id->vendor = TO_NATIVE(id->vendor);
214 id->product = TO_NATIVE(id->product);
215
216 sprintf(alias, "hid:b%04X", id->bus);
217 ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
218 ADD(alias, "p", id->product != HID_ANY_ID, id->product);
219
220 return 1;
221}
222
209/* Looks like: ieee1394:venNmoNspNverN */ 223/* Looks like: ieee1394:venNmoNspNverN */
210static int do_ieee1394_entry(const char *filename, 224static int do_ieee1394_entry(const char *filename,
211 struct ieee1394_device_id *id, char *alias) 225 struct ieee1394_device_id *id, char *alias)
@@ -745,6 +759,10 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
745 else if (sym_is(symname, "__mod_usb_device_table")) 759 else if (sym_is(symname, "__mod_usb_device_table"))
746 /* special case to handle bcdDevice ranges */ 760 /* special case to handle bcdDevice ranges */
747 do_usb_table(symval, sym->st_size, mod); 761 do_usb_table(symval, sym->st_size, mod);
762 else if (sym_is(symname, "__mod_hid_device_table"))
763 do_table(symval, sym->st_size,
764 sizeof(struct hid_device_id), "hid",
765 do_hid_entry, mod);
748 else if (sym_is(symname, "__mod_ieee1394_device_table")) 766 else if (sym_is(symname, "__mod_ieee1394_device_table"))
749 do_table(symval, sym->st_size, 767 do_table(symval, sym->st_size,
750 sizeof(struct ieee1394_device_id), "ieee1394", 768 sizeof(struct ieee1394_device_id), "ieee1394",