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)
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
166void cleanup_module(void)
167{
168 struct pci_dev *pdev = to_pci_dev(atir_driver.dev);
169
170 lirc_unregister_driver(atir_minor);
171 iounmap(pci_addr_lin);
172 pci_disable_device(pdev);
173 pci_dev_put(pdev);
174}
175
176static int atir_init_start(void)
177{
178 pci_addr_lin = ioremap(pci_addr_phys + DATA_PCI_OFF, 0x400);
179 if (!pci_addr_lin) {
180 pr_info("pci mem must be mapped\n");
181 return 0;
182 }
183 return 1;
184}
185
186static void cycle_delay(int cycle)
187{
188 udelay(WAIT_CYCLE * cycle);
189}
190
191static int poll_main(void)
192{
193 unsigned char status_high, status_low;
194
195 do_i2c_start();
196
197 seems_wr_byte(0xAA);
198 seems_wr_byte(0x01);
199
200 do_i2c_start();
201
202 seems_wr_byte(0xAB);
203
204 status_low = seems_rd_byte();
205 status_high = seems_rd_byte();
206
207 do_i2c_stop();
208
209 return (status_high << 8) | status_low;
210}
211
212static void do_i2c_start(void)
213{
214 do_set_bits(3);
215 cycle_delay(4);
216
217 do_set_bits(1);
218 cycle_delay(7);
219
220 do_set_bits(0);
221 cycle_delay(2);
222}
223
224static void do_i2c_stop(void)
225{
226 unsigned char bits;
227
228 bits = do_get_bits() & 0xFD;
229 do_set_bits(bits);
230 cycle_delay(1);
231
232 bits |= 1;
233 do_set_bits(bits);
234 cycle_delay(2);
235
236 bits |= 2;
237 do_set_bits(bits);
238 bits = 3;
239 do_set_bits(bits);
240 cycle_delay(2);
241}
242
243static void seems_wr_byte(unsigned char value)
244{
245 int i;
246 unsigned char reg;
247
248 reg = do_get_bits();
249 for (i = 0; i < 8; i++) {
250 if (value & 0x80)
251 reg |= 0x02;
252 else
253 reg &= 0xFD;
254
255 do_set_bits(reg);
256 cycle_delay(1);
257
258 reg |= 1;
259 do_set_bits(reg);
260 cycle_delay(1);
261
262 reg &= 0xFE;
263 do_set_bits(reg);
264 cycle_delay(1);
265 value <<= 1;
266 }
267 cycle_delay(2);
268
269 reg |= 2;
270 do_set_bits(reg);
271
272 reg |= 1;
273 do_set_bits(reg);
274
275 cycle_delay(1);
276 do_get_bits();
277
278 reg &= 0xFE;
279 do_set_bits(reg);
280 cycle_delay(3);
281}
282
283static unsigned char seems_rd_byte(void)
284{
285 int i;
286 int rd_byte;
287 unsigned char bits_2, bits_1;
288
289 bits_1 = do_get_bits() | 2;
290 do_set_bits(bits_1);
291
292 rd_byte = 0;
293 for (i = 0; i < 8; i++) {
294 bits_1 &= 0xFE;
295 do_set_bits(bits_1);
296 cycle_delay(2);
297
298 bits_1 |= 1;
299 do_set_bits(bits_1);
300 cycle_delay(1);
301
302 bits_2 = do_get_bits();
303 if (bits_2 & 2)
304 rd_byte |= 1;
305
306 rd_byte <<= 1;
307 }
308
309 bits_1 = 0;
310 if (bits_2 == 0)
311 bits_1 |= 2;
312
313 do_set_bits(bits_1);
314 cycle_delay(2);
315
316 bits_1 |= 1;
317 do_set_bits(bits_1);
318 cycle_delay(3);
319
320 bits_1 &= 0xFE;
321 do_set_bits(bits_1);
322 cycle_delay(2);
323
324 rd_byte >>= 1;
325 rd_byte &= 0xFF;
326 return rd_byte;
327}
328
329static void do_set_bits(unsigned char new_bits)
330{
331 int reg_val;
332
333 reg_val = read_index(0x34);
334 if (new_bits & 2) {
335 reg_val &= 0xFFFFFFDF;
336 reg_val |= 1;
337 } else {
338 reg_val &= 0xFFFFFFFE;
339 reg_val |= 0x20;
340 }
341 reg_val |= 0x10;
342 write_index(0x34, reg_val);
343
344 reg_val = read_index(0x31);
345 if (new_bits & 1)
346 reg_val |= 0x1000000;
347 else
348 reg_val &= 0xFEFFFFFF;
349
350 reg_val |= 0x8000000;
351 write_index(0x31, reg_val);
352}
353
354static unsigned char do_get_bits(void)
355{
356 unsigned char bits;
357 int reg_val;
358
359 reg_val = read_index(0x34);
360 reg_val |= 0x10;
361 reg_val &= 0xFFFFFFDF;
362 write_index(0x34, reg_val);
363
364 reg_val = read_index(0x34);
365 bits = 0;
366 if (reg_val & 8)
367 bits |= 2;
368 else
369 bits &= 0xFD;
370
371 reg_val = read_index(0x31);
372 if (reg_val & 0x1000000)
373 bits |= 1;
374 else
375 bits &= 0xFE;
376
377 return bits;
378}
379
380static unsigned int read_index(unsigned char index)
381{
382 unsigned char __iomem *addr;
383
384 addr = pci_addr_lin + ((index & 0xFF) << 2);
385 return readl(addr);
386}
387
388static void write_index(unsigned char index, unsigned int reg_val)
389{
390 unsigned char __iomem *addr;
391
392 addr = pci_addr_lin + ((index & 0xFF) << 2);
393 writel(reg_val, addr);
394}
395
396MODULE_AUTHOR("Froenchenko Leonid");
397MODULE_DESCRIPTION("IR remote driver for bt829 based TV cards");
398MODULE_LICENSE("GPL");
399
400module_param(debug, bool, S_IRUGO | S_IWUSR);
401MODULE_PARM_DESC(debug, "Debug enabled or not");
402