1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/threads.h>
26#include <linux/sched.h>
27#include <linux/ioport.h>
28#include <linux/pci.h>
29#include <linux/delay.h>
30
31#include <media/lirc_dev.h>
32
33static int poll_main(void);
34static int atir_init_start(void);
35
36static void write_index(unsigned char index, unsigned int value);
37static unsigned int read_index(unsigned char index);
38
39static void do_i2c_start(void);
40static void do_i2c_stop(void);
41
42static void seems_wr_byte(unsigned char al);
43static unsigned char seems_rd_byte(void);
44
45static unsigned int read_index(unsigned char al);
46static void write_index(unsigned char ah, unsigned int edx);
47
48static void cycle_delay(int cycle);
49
50static void do_set_bits(unsigned char bl);
51static unsigned char do_get_bits(void);
52
53#define DATA_PCI_OFF 0x7FFC00
54#define WAIT_CYCLE 20
55
56#define DRIVER_NAME "lirc_bt829"
57
58static bool debug;
59
60static int atir_minor;
61static phys_addr_t pci_addr_phys;
62static unsigned char __iomem *pci_addr_lin;
63
64static struct lirc_driver atir_driver;
65
66static struct pci_dev *do_pci_probe(void)
67{
68 struct pci_dev *my_dev;
69
70 my_dev = pci_get_device(PCI_VENDOR_ID_ATI,
71 PCI_DEVICE_ID_ATI_264VT, NULL);
72 if (my_dev) {
73 pr_err("Using device: %s\n", pci_name(my_dev));
74 pci_addr_phys = 0;
75 if (my_dev->resource[0].flags & IORESOURCE_MEM) {
76 pci_addr_phys = my_dev->resource[0].start;
77 pr_info("memory at %pa\n", &pci_addr_phys);
78 }
79 if (pci_addr_phys == 0) {
80 pr_err("no memory resource ?\n");
81 pci_dev_put(my_dev);
82 return NULL;
83 }
84 } else {
85 pr_err("pci_probe failed\n");
86 return NULL;
87 }
88 return my_dev;
89}
90
91static int atir_add_to_buf(void *data, struct lirc_buffer *buf)
92{
93 unsigned char key;
94 int status;
95
96 status = poll_main();
97 key = (status >> 8) & 0xFF;
98 if (status & 0xFF) {
99 dev_dbg(atir_driver.dev, "reading key %02X\n", key);
100 lirc_buffer_write(buf, &key);
101 return 0;
102 }
103 return -ENODATA;
104}
105
106static int atir_set_use_inc(void *data)
107{
108 dev_dbg(atir_driver.dev, "driver is opened\n");
109 return 0;
110}
111
112static void atir_set_use_dec(void *data)
113{
114 dev_dbg(atir_driver.dev, "driver is closed\n");
115}
116
117int init_module(void)
118{
119 struct pci_dev *pdev;
120 int rc;
121
122 pdev = do_pci_probe();
123 if (pdev == NULL)
124 return -ENODEV;
125
126 rc = pci_enable_device(pdev);
127 if (rc)
128 goto err_put_dev;
129
130 if (!atir_init_start()) {
131 rc = -ENODEV;
132 goto err_disable;
133 }
134
135 strcpy(atir_driver.name, "ATIR");
136 atir_driver.minor = -1;
137 atir_driver.code_length = 8;
138 atir_driver.sample_rate = 10;
139 atir_driver.data = NULL;
140 atir_driver.add_to_buf = atir_add_to_buf;
141 atir_driver.set_use_inc = atir_set_use_inc;
142 atir_driver.set_use_dec = atir_set_use_dec;
143 atir_driver.dev = &pdev->dev;
144 atir_driver.owner = THIS_MODULE;
145
146 atir_minor = lirc_register_driver(&atir_driver);
147 if (atir_minor < 0) {
148 pr_err("failed to register driver!\n");
149 rc = atir_minor;
150 goto err_unmap;
151 }
152 dev_dbg(atir_driver.dev, "driver is registered on minor %d\n",
153 atir_minor);
154
155 return 0;
156
157err_unmap:
158 iounmap(pci_addr_lin);
159err_disable:
160 pci_disable_device(pdev);
161err_put_dev:
162 pci_dev_put(pdev);
163 return rc;
164}
165
166
167void cleanup_module(void)
168{
169 struct pci_dev *pdev = to_pci_dev(atir_driver.dev);
170
171 lirc_unregister_driver(atir_minor);
172 iounmap(pci_addr_lin);
173 pci_disable_device(pdev);
174 pci_dev_put(pdev);
175}
176
177
178static int atir_init_start(void)
179{
180 pci_addr_lin = ioremap(pci_addr_phys + DATA_PCI_OFF, 0x400);
181 if (!pci_addr_lin) {
182 pr_info("pci mem must be mapped\n");
183 return 0;
184 }
185 return 1;
186}
187
188static void cycle_delay(int cycle)
189{
190 udelay(WAIT_CYCLE*cycle);
191}
192
193
194static int poll_main(void)
195{
196 unsigned char status_high, status_low;
197
198 do_i2c_start();
199
200 seems_wr_byte(0xAA);
201 seems_wr_byte(0x01);
202
203 do_i2c_start();
204
205 seems_wr_byte(0xAB);
206
207 status_low = seems_rd_byte();
208 status_high = seems_rd_byte();
209
210 do_i2c_stop();
211
212 return (status_high << 8) | status_low;
213}
214
215static void do_i2c_start(void)
216{
217 do_set_bits(3);
218 cycle_delay(4);
219
220 do_set_bits(1);
221 cycle_delay(7);
222
223 do_set_bits(0);
224 cycle_delay(2);
225}
226
227static void do_i2c_stop(void)
228{
229 unsigned char bits;
230
231 bits = do_get_bits() & 0xFD;
232 do_set_bits(bits);
233 cycle_delay(1);
234
235 bits |= 1;
236 do_set_bits(bits);
237 cycle_delay(2);
238
239 bits |= 2;
240 do_set_bits(bits);
241 bits = 3;
242 do_set_bits(bits);
243 cycle_delay(2);
244}
245
246static void seems_wr_byte(unsigned char value)
247{
248 int i;
249 unsigned char reg;
250
251 reg = do_get_bits();
252 for (i = 0; i < 8; i++) {
253 if (value & 0x80)
254 reg |= 0x02;
255 else
256 reg &= 0xFD;
257
258 do_set_bits(reg);
259 cycle_delay(1);
260
261 reg |= 1;
262 do_set_bits(reg);
263 cycle_delay(1);
264
265 reg &= 0xFE;
266 do_set_bits(reg);
267 cycle_delay(1);
268 value <<= 1;
269 }
270 cycle_delay(2);
271
272 reg |= 2;
273 do_set_bits(reg);
274
275 reg |= 1;
276 do_set_bits(reg);
277
278 cycle_delay(1);
279 do_get_bits();
280
281 reg &= 0xFE;
282 do_set_bits(reg);
283 cycle_delay(3);
284}
285
286static unsigned char seems_rd_byte(void)
287{
288 int i;
289 int rd_byte;
290 unsigned char bits_2, bits_1;
291
292 bits_1 = do_get_bits() | 2;
293 do_set_bits(bits_1);
294
295 rd_byte = 0;
296 for (i = 0; i < 8; i++) {
297 bits_1 &= 0xFE;
298 do_set_bits(bits_1);
299 cycle_delay(2);
300
301 bits_1 |= 1;
302 do_set_bits(bits_1);
303 cycle_delay(1);
304
305 bits_2 = do_get_bits();
306 if (bits_2 & 2)
307 rd_byte |= 1;
308
309 rd_byte <<= 1;
310 }
311
312 bits_1 = 0;
313 if (bits_2 == 0)
314 bits_1 |= 2;
315
316 do_set_bits(bits_1);
317 cycle_delay(2);
318
319 bits_1 |= 1;
320 do_set_bits(bits_1);
321 cycle_delay(3);
322
323 bits_1 &= 0xFE;
324 do_set_bits(bits_1);
325 cycle_delay(2);
326
327 rd_byte >>= 1;
328 rd_byte &= 0xFF;
329 return rd_byte;
330}
331
332static void do_set_bits(unsigned char new_bits)
333{
334 int reg_val;
335
336 reg_val = read_index(0x34);
337 if (new_bits & 2) {
338 reg_val &= 0xFFFFFFDF;
339 reg_val |= 1;
340 } else {
341 reg_val &= 0xFFFFFFFE;
342 reg_val |= 0x20;
343 }
344 reg_val |= 0x10;
345 write_index(0x34, reg_val);
346
347 reg_val = read_index(0x31);
348 if (new_bits & 1)
349 reg_val |= 0x1000000;
350 else
351 reg_val &= 0xFEFFFFFF;
352
353 reg_val |= 0x8000000;
354 write_index(0x31, reg_val);
355}
356
357static unsigned char do_get_bits(void)
358{
359 unsigned char bits;
360 int reg_val;
361
362 reg_val = read_index(0x34);
363 reg_val |= 0x10;
364 reg_val &= 0xFFFFFFDF;
365 write_index(0x34, reg_val);
366
367 reg_val = read_index(0x34);
368 bits = 0;
369 if (reg_val & 8)
370 bits |= 2;
371 else
372 bits &= 0xFD;
373
374 reg_val = read_index(0x31);
375 if (reg_val & 0x1000000)
376 bits |= 1;
377 else
378 bits &= 0xFE;
379
380 return bits;
381}
382
383static unsigned int read_index(unsigned char index)
384{
385 unsigned char __iomem *addr;
386
387 addr = pci_addr_lin + ((index & 0xFF) << 2);
388 return readl(addr);
389}
390
391static void write_index(unsigned char index, unsigned int reg_val)
392{
393 unsigned char __iomem *addr;
394
395 addr = pci_addr_lin + ((index & 0xFF) << 2);
396 writel(reg_val, addr);
397}
398
399MODULE_AUTHOR("Froenchenko Leonid");
400MODULE_DESCRIPTION("IR remote driver for bt829 based TV cards");
401MODULE_LICENSE("GPL");
402
403module_param(debug, bool, S_IRUGO | S_IWUSR);
404MODULE_PARM_DESC(debug, "Debug enabled or not");
405