2
* Copyright 2012 Luis R. Rodriguez <mcgrof@frijolero.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation.
8
* Compatibility file for Linux wireless for kernels 3.4.
12
#include <linux/module.h>
13
#include <linux/wait.h>
15
/* __wake_up_common was declared as part of the wait.h until
16
* 2.6.31 in which they made it private to the scheduler. Prefix it with
17
* compat to avoid double declaration issues.
19
static void compat_wake_up_common(wait_queue_head_t *q, unsigned int mode,
20
int nr_exclusive, int wake_flags, void *key)
22
wait_queue_t *curr, *next;
24
list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
25
unsigned flags = curr->flags;
27
if (curr->func(curr, mode, wake_flags, key) &&
28
(flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
33
/* The last 'nr' parameter was added to the __wake_up_locked() function
34
* in 3.4 kernel. Define a new one prefixed with compat_ for the new API.
36
void compat_wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
38
compat_wake_up_common(q, mode, nr, 0, NULL);
40
EXPORT_SYMBOL_GPL(compat_wake_up_locked);
43
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34))
44
#include <linux/i2c.h>
45
#include <linux/i2c-algo-bit.h>
46
#include <linux/delay.h>
48
#define setsda(adap, val) adap->setsda(adap->data, val)
49
#define setscl(adap, val) adap->setscl(adap->data, val)
50
#define getsda(adap) adap->getsda(adap->data)
51
#define getscl(adap) adap->getscl(adap->data)
53
#define bit_dbg(level, dev, format, args...) \
56
static inline void sdalo(struct i2c_algo_bit_data *adap)
59
udelay((adap->udelay + 1) / 2);
62
static inline void sdahi(struct i2c_algo_bit_data *adap)
65
udelay((adap->udelay + 1) / 2);
68
static inline void scllo(struct i2c_algo_bit_data *adap)
71
udelay(adap->udelay / 2);
74
static int sclhi(struct i2c_algo_bit_data *adap)
80
/* Not all adapters have scl sense line... */
85
while (!getscl(adap)) {
86
/* This hw knows how to read the clock line, so we wait
87
* until it actually gets high. This is safer as some
88
* chips may hold it low ("clock stretching") while they
89
* are processing data internally.
91
if (time_after(jiffies, start + adap->timeout)) {
92
/* Test one last time, as we may have been preempted
93
* between last check and timeout test.
102
if (jiffies != start && i2c_debug >= 3)
103
pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "
104
"high\n", jiffies - start);
108
udelay(adap->udelay);
112
static void i2c_start(struct i2c_algo_bit_data *adap)
114
/* assert: scl, sda are high */
116
udelay(adap->udelay);
120
static void i2c_repstart(struct i2c_algo_bit_data *adap)
122
/* assert: scl is low */
126
udelay(adap->udelay);
131
static void i2c_stop(struct i2c_algo_bit_data *adap)
133
/* assert: scl is low */
137
udelay(adap->udelay);
140
static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
145
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
147
/* assert: scl is low */
148
for (i = 7; i >= 0; i--) {
151
udelay((adap->udelay + 1) / 2);
152
if (sclhi(adap) < 0) { /* timed out */
153
bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
154
"timeout at bit #%d\n", (int)c, i);
157
/* FIXME do arbitration here:
158
* if (sb && !getsda(adap)) -> ouch! Get out of here.
160
* Report a unique code, so higher level code can retry
161
* the whole (combined) message and *NOT* issue STOP.
166
if (sclhi(adap) < 0) { /* timeout */
167
bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
168
"timeout at ack\n", (int)c);
172
/* read ack: SDA should be pulled down by slave, or it may
173
* NAK (usually to report problems with the data we wrote).
175
ack = !getsda(adap); /* ack: sda is pulled low -> success */
176
bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
181
/* assert: scl is low (sda undef) */
184
static int i2c_inb(struct i2c_adapter *i2c_adap)
186
/* read byte via i2c port, without start/stop sequence */
187
/* acknowledge is sent in i2c_read. */
189
unsigned char indata = 0;
190
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
192
/* assert: scl is low */
194
for (i = 0; i < 8; i++) {
195
if (sclhi(adap) < 0) { /* timeout */
196
bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit "
204
udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
206
/* assert: scl is low */
210
static int try_address(struct i2c_adapter *i2c_adap,
211
unsigned char addr, int retries)
213
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
216
for (i = 0; i <= retries; i++) {
217
ret = i2c_outb(i2c_adap, addr);
218
if (ret == 1 || i == retries)
220
bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
222
udelay(adap->udelay);
224
bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
228
bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "
229
"0x%02x: %s\n", i + 1,
230
addr & 1 ? "read from" : "write to", addr >> 1,
231
ret == 1 ? "success" : "failed, timeout?");
235
static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
237
unsigned short flags = msg->flags;
238
unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
239
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
244
retries = nak_ok ? 0 : i2c_adap->retries;
246
if (flags & I2C_M_TEN) {
247
/* a ten bit address */
248
addr = 0xf0 | ((msg->addr >> 7) & 0x06);
249
bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
250
/* try extended address code...*/
251
ret = try_address(i2c_adap, addr, retries);
252
if ((ret != 1) && !nak_ok) {
253
dev_err(&i2c_adap->dev,
254
"died at extended address code\n");
257
/* the remaining 8 bit address */
258
ret = i2c_outb(i2c_adap, msg->addr & 0xff);
259
if ((ret != 1) && !nak_ok) {
260
/* the chip did not ack / xmission error occurred */
261
dev_err(&i2c_adap->dev, "died at 2nd address code\n");
264
if (flags & I2C_M_RD) {
265
bit_dbg(3, &i2c_adap->dev, "emitting repeated "
266
"start condition\n");
268
/* okay, now switch into reading mode */
270
ret = try_address(i2c_adap, addr, retries);
271
if ((ret != 1) && !nak_ok) {
272
dev_err(&i2c_adap->dev,
273
"died at repeated address code\n");
277
} else { /* normal 7bit address */
278
addr = msg->addr << 1;
279
if (flags & I2C_M_RD)
281
if (flags & I2C_M_REV_DIR_ADDR)
283
ret = try_address(i2c_adap, addr, retries);
284
if ((ret != 1) && !nak_ok)
291
static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
293
const unsigned char *temp = msg->buf;
294
int count = msg->len;
295
unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
300
retval = i2c_outb(i2c_adap, *temp);
302
/* OK/ACK; or ignored NAK */
303
if ((retval > 0) || (nak_ok && (retval == 0))) {
308
/* A slave NAKing the master means the slave didn't like
309
* something about the data it saw. For example, maybe
310
* the SMBus PEC was wrong.
312
} else if (retval == 0) {
313
dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
316
/* Timeout; or (someday) lost arbitration
318
* FIXME Lost ARB implies retrying the transaction from
319
* the first message, after the "winning" master issues
320
* its STOP. As a rule, upper layer code has no reason
321
* to know or care about this ... it is *NOT* an error.
324
dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
332
static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
334
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
336
/* assert: sda is high */
337
if (is_ack) /* send ack */
339
udelay((adap->udelay + 1) / 2);
340
if (sclhi(adap) < 0) { /* timeout */
341
dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
348
static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
351
int rdcount = 0; /* counts bytes read */
352
unsigned char *temp = msg->buf;
353
int count = msg->len;
354
const unsigned flags = msg->flags;
357
inval = i2c_inb(i2c_adap);
361
} else { /* read timed out */
368
/* Some SMBus transactions require that we receive the
369
transaction length as the first read byte. */
370
if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
371
if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
372
if (!(flags & I2C_M_NO_RD_ACK))
374
dev_err(&i2c_adap->dev, "readbytes: invalid "
375
"block length (%d)\n", inval);
378
/* The original count value accounts for the extra
379
bytes, that is, either 1 for a regular transaction,
380
or 2 for a PEC transaction. */
385
bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
387
(flags & I2C_M_NO_RD_ACK)
389
: (count ? "A" : "NA"));
391
if (!(flags & I2C_M_NO_RD_ACK)) {
392
inval = acknak(i2c_adap, count);
401
static u32 bit_func(struct i2c_adapter *adap)
403
return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL |
404
I2C_FUNC_SMBUS_READ_BLOCK_DATA |
405
I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
406
I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
409
static int bit_xfer(struct i2c_adapter *i2c_adap,
410
struct i2c_msg msgs[], int num)
412
struct i2c_msg *pmsg;
413
struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
415
unsigned short nak_ok;
417
if (adap->pre_xfer) {
418
ret = adap->pre_xfer(i2c_adap);
423
bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
425
for (i = 0; i < num; i++) {
427
nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
428
if (!(pmsg->flags & I2C_M_NOSTART)) {
430
bit_dbg(3, &i2c_adap->dev, "emitting "
431
"repeated start condition\n");
434
ret = bit_doAddress(i2c_adap, pmsg);
435
if ((ret != 0) && !nak_ok) {
436
bit_dbg(1, &i2c_adap->dev, "NAK from "
437
"device addr 0x%02x msg #%d\n",
442
if (pmsg->flags & I2C_M_RD) {
443
/* read bytes into buffer*/
444
ret = readbytes(i2c_adap, pmsg);
446
bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
447
ret, ret == 1 ? "" : "s");
448
if (ret < pmsg->len) {
454
/* write bytes from buffer */
455
ret = sendbytes(i2c_adap, pmsg);
457
bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
458
ret, ret == 1 ? "" : "s");
459
if (ret < pmsg->len) {
469
bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
473
adap->post_xfer(i2c_adap);
478
const struct i2c_algorithm i2c_bit_algo = {
479
.master_xfer = bit_xfer,
480
.functionality = bit_func,
482
EXPORT_SYMBOL(i2c_bit_algo);
485
int simple_open(struct inode *inode, struct file *file)
487
if (inode->i_private)
488
file->private_data = inode->i_private;
491
EXPORT_SYMBOL_GPL(simple_open);