1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/usb.h>
28#include <linux/i2c.h>
29#include <linux/jiffies.h>
30
31#include "em28xx.h"
32#include "tuner-xc2028.h"
33#include <media/v4l2-common.h>
34#include <media/tuner.h>
35
36
37
38static unsigned int i2c_scan;
39module_param(i2c_scan, int, 0444);
40MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
41
42static unsigned int i2c_debug;
43module_param(i2c_debug, int, 0644);
44MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
45
46
47
48
49
50static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
51{
52 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
53 int ret;
54 u8 b2[6];
55
56 if (len < 1 || len > 4)
57 return -EOPNOTSUPP;
58
59 BUG_ON(len < 1 || len > 4);
60 b2[5] = 0x80 + len - 1;
61 b2[4] = addr;
62 b2[3] = buf[0];
63 if (len > 1)
64 b2[2] = buf[1];
65 if (len > 2)
66 b2[1] = buf[2];
67 if (len > 3)
68 b2[0] = buf[3];
69
70
71 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
72 if (ret != 2 + len) {
73 em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
74 addr, ret);
75 return (ret < 0) ? ret : -EIO;
76 }
77
78 while (time_is_after_jiffies(timeout)) {
79 ret = dev->em28xx_read_reg(dev, 0x05);
80 if (ret == 0x80 + len - 1)
81 return len;
82 if (ret == 0x94 + len - 1) {
83 if (i2c_debug == 1)
84 em28xx_warn("R05 returned 0x%02x: I2C ACK error\n",
85 ret);
86 return -ENXIO;
87 }
88 if (ret < 0) {
89 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
90 ret);
91 return ret;
92 }
93 msleep(5);
94 }
95 if (i2c_debug)
96 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
97 return -ETIMEDOUT;
98}
99
100
101
102
103
104static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
105{
106 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
107 u8 buf2[4];
108 int ret;
109 int i;
110
111 if (len < 1 || len > 4)
112 return -EOPNOTSUPP;
113
114
115 buf2[1] = 0x84 + len - 1;
116 buf2[0] = addr;
117 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
118 if (ret != 2) {
119 em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
120 addr, ret);
121 return (ret < 0) ? ret : -EIO;
122 }
123
124
125 while (time_is_after_jiffies(timeout)) {
126 ret = dev->em28xx_read_reg(dev, 0x05);
127 if (ret == 0x84 + len - 1)
128 break;
129 if (ret == 0x94 + len - 1) {
130 if (i2c_debug == 1)
131 em28xx_warn("R05 returned 0x%02x: I2C ACK error\n",
132 ret);
133 return -ENXIO;
134 }
135 if (ret < 0) {
136 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
137 ret);
138 return ret;
139 }
140 msleep(5);
141 }
142 if (ret != 0x84 + len - 1) {
143 if (i2c_debug)
144 em28xx_warn("read from i2c device at 0x%x timed out\n",
145 addr);
146 }
147
148
149 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
150 if (ret != len) {
151 em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
152 addr, ret);
153 return (ret < 0) ? ret : -EIO;
154 }
155 for (i = 0; i < len; i++)
156 buf[i] = buf2[len - 1 - i];
157
158 return ret;
159}
160
161
162
163
164
165static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
166{
167 u8 buf;
168 int ret;
169
170 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
171 if (ret == 1)
172 return 0;
173 return (ret < 0) ? ret : -EIO;
174}
175
176
177
178
179static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
180 u16 len, int stop)
181{
182 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
183 int ret;
184
185 if (len < 1 || len > 64)
186 return -EOPNOTSUPP;
187
188
189
190
191
192
193 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
194 if (ret != len) {
195 if (ret < 0) {
196 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
197 addr, ret);
198 return ret;
199 } else {
200 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
201 len, addr, ret);
202 return -EIO;
203 }
204 }
205
206
207 while (time_is_after_jiffies(timeout)) {
208 ret = dev->em28xx_read_reg(dev, 0x05);
209 if (ret == 0)
210 return len;
211 if (ret == 0x10) {
212 if (i2c_debug == 1)
213 em28xx_warn("I2C ACK error on writing to addr 0x%02x\n",
214 addr);
215 return -ENXIO;
216 }
217 if (ret < 0) {
218 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
219 ret);
220 return ret;
221 }
222 msleep(5);
223
224
225
226
227
228 }
229
230 if (ret == 0x02 || ret == 0x04) {
231
232 if (i2c_debug)
233 em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n",
234 addr, ret);
235 return -ETIMEDOUT;
236 }
237
238 em28xx_warn("write to i2c device at 0x%x failed with unknown error (status=%i)\n",
239 addr, ret);
240 return -EIO;
241}
242
243
244
245
246
247static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
248{
249 int ret;
250
251 if (len < 1 || len > 64)
252 return -EOPNOTSUPP;
253
254
255
256
257
258
259 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
260 if (ret < 0) {
261 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
262 addr, ret);
263 return ret;
264 }
265
266
267
268
269
270
271
272
273
274
275 ret = dev->em28xx_read_reg(dev, 0x05);
276 if (ret == 0)
277 return len;
278 if (ret < 0) {
279 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
280 ret);
281 return ret;
282 }
283 if (ret == 0x10) {
284 if (i2c_debug == 1)
285 em28xx_warn("I2C ACK error on writing to addr 0x%02x\n",
286 addr);
287 return -ENXIO;
288 }
289
290 if (ret == 0x02 || ret == 0x04) {
291
292 if (i2c_debug)
293 em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n",
294 addr, ret);
295 return -ETIMEDOUT;
296 }
297
298 em28xx_warn("write to i2c device at 0x%x failed with unknown error (status=%i)\n",
299 addr, ret);
300 return -EIO;
301}
302
303
304
305
306
307static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
308{
309 int ret;
310 u8 buf;
311
312 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
313 if (ret == 1)
314 return 0;
315 return (ret < 0) ? ret : -EIO;
316}
317
318
319
320
321
322static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
323 u16 len)
324{
325 int ret;
326
327 if (len < 1 || len > 64)
328 return -EOPNOTSUPP;
329
330
331
332
333
334
335 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
336 if (ret != len) {
337 if (ret < 0) {
338 em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
339 addr, ret);
340 return ret;
341 } else {
342 em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
343 len, addr, ret);
344 return -EIO;
345 }
346 }
347
348 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
349
350
351
352
353 if (!ret)
354 return len;
355 else if (ret > 0) {
356 if (i2c_debug == 1)
357 em28xx_warn("Bus B R08 returned 0x%02x: I2C ACK error\n",
358 ret);
359 return -ENXIO;
360 }
361
362 return ret;
363
364
365
366
367
368}
369
370
371
372
373
374static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
375 u16 len)
376{
377 int ret;
378
379 if (len < 1 || len > 64)
380 return -EOPNOTSUPP;
381
382
383
384
385
386
387 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
388 if (ret < 0) {
389 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
390 addr, ret);
391 return ret;
392 }
393
394
395
396
397
398
399
400
401
402
403 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
404
405
406
407
408 if (!ret)
409 return len;
410 else if (ret > 0) {
411 if (i2c_debug == 1)
412 em28xx_warn("Bus B R08 returned 0x%02x: I2C ACK error\n",
413 ret);
414 return -ENXIO;
415 }
416
417 return ret;
418
419
420
421
422
423}
424
425
426
427
428
429static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
430{
431 u8 buf;
432 int ret;
433
434 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
435 if (ret < 0)
436 return ret;
437
438 return 0;
439
440
441
442
443}
444
445static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
446{
447 struct em28xx *dev = i2c_bus->dev;
448 int rc = -EOPNOTSUPP;
449
450 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
451 rc = em28xx_i2c_check_for_device(dev, addr);
452 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
453 rc = em2800_i2c_check_for_device(dev, addr);
454 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
455 rc = em25xx_bus_B_check_for_device(dev, addr);
456 return rc;
457}
458
459static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
460 struct i2c_msg msg)
461{
462 struct em28xx *dev = i2c_bus->dev;
463 u16 addr = msg.addr << 1;
464 int rc = -EOPNOTSUPP;
465
466 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
467 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
468 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
469 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
470 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
471 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
472 return rc;
473}
474
475static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
476 struct i2c_msg msg, int stop)
477{
478 struct em28xx *dev = i2c_bus->dev;
479 u16 addr = msg.addr << 1;
480 int rc = -EOPNOTSUPP;
481
482 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
483 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
484 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
485 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
486 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
487 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
488 return rc;
489}
490
491
492
493
494
495static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
496 struct i2c_msg msgs[], int num)
497{
498 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
499 struct em28xx *dev = i2c_bus->dev;
500 unsigned bus = i2c_bus->bus;
501 int addr, rc, i;
502 u8 reg;
503
504 rc = rt_mutex_trylock(&dev->i2c_bus_lock);
505 if (rc < 0)
506 return rc;
507
508
509 if (bus != dev->cur_i2c_bus &&
510 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
511 if (bus == 1)
512 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
513 else
514 reg = 0;
515 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
516 EM2874_I2C_SECONDARY_BUS_SELECT);
517 dev->cur_i2c_bus = bus;
518 }
519
520 if (num <= 0) {
521 rt_mutex_unlock(&dev->i2c_bus_lock);
522 return 0;
523 }
524 for (i = 0; i < num; i++) {
525 addr = msgs[i].addr << 1;
526 if (i2c_debug > 1)
527 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
528 dev->name, __func__ ,
529 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
530 i == num - 1 ? "stop" : "nonstop",
531 addr, msgs[i].len);
532 if (!msgs[i].len) {
533
534
535
536
537 rc = i2c_check_for_device(i2c_bus, addr);
538 if (rc < 0) {
539 if (rc == -ENXIO) {
540 if (i2c_debug > 1)
541 printk(KERN_CONT " no device\n");
542 rc = -ENODEV;
543 } else {
544 if (i2c_debug > 1)
545 printk(KERN_CONT " ERROR: %i\n", rc);
546 }
547 rt_mutex_unlock(&dev->i2c_bus_lock);
548 return rc;
549 }
550 } else if (msgs[i].flags & I2C_M_RD) {
551
552 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
553
554 if (i2c_debug > 1 && rc >= 0)
555 printk(KERN_CONT " %*ph",
556 msgs[i].len, msgs[i].buf);
557 } else {
558 if (i2c_debug > 1)
559 printk(KERN_CONT " %*ph",
560 msgs[i].len, msgs[i].buf);
561
562
563 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
564 }
565 if (rc < 0) {
566 if (i2c_debug > 1)
567 printk(KERN_CONT " ERROR: %i\n", rc);
568 rt_mutex_unlock(&dev->i2c_bus_lock);
569 return rc;
570 }
571 if (i2c_debug > 1)
572 printk(KERN_CONT "\n");
573 }
574
575 rt_mutex_unlock(&dev->i2c_bus_lock);
576 return num;
577}
578
579
580
581
582
583
584static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
585{
586 unsigned long hash = 0;
587 unsigned long l = 0;
588 int len = 0;
589 unsigned char c;
590 do {
591 if (len == length) {
592 c = (char)len;
593 len = -1;
594 } else
595 c = *buf++;
596 l = (l << 8) | c;
597 len++;
598 if ((len & (32 / 8 - 1)) == 0)
599 hash = ((hash^l) * 0x9e370001UL);
600 } while (len);
601
602 return (hash >> (32 - bits)) & 0xffffffffUL;
603}
604
605
606
607
608
609static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
610 bool addr_w16, u16 len, u8 *data)
611{
612 int remain = len, rsize, rsize_max, ret;
613 u8 buf[2];
614
615
616 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
617 return -EINVAL;
618
619 buf[0] = addr >> 8;
620 buf[1] = addr & 0xff;
621 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
622 if (ret < 0)
623 return ret;
624
625 if (dev->board.is_em2800)
626 rsize_max = 4;
627 else
628 rsize_max = 64;
629 while (remain > 0) {
630 if (remain > rsize_max)
631 rsize = rsize_max;
632 else
633 rsize = remain;
634
635 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
636 if (ret < 0)
637 return ret;
638
639 remain -= rsize;
640 data += rsize;
641 }
642
643 return len;
644}
645
646static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
647 u8 **eedata, u16 *eedata_len)
648{
649 const u16 len = 256;
650
651
652
653
654
655 int err;
656 struct em28xx_eeprom *dev_config;
657 u8 buf, *data;
658
659 *eedata = NULL;
660 *eedata_len = 0;
661
662
663
664 dev->i2c_client[bus].addr = 0xa0 >> 1;
665
666
667 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
668 if (err < 0) {
669 em28xx_info("board has no eeprom\n");
670 return -ENODEV;
671 }
672
673 data = kzalloc(len, GFP_KERNEL);
674 if (data == NULL)
675 return -ENOMEM;
676
677
678 err = em28xx_i2c_read_block(dev, bus, 0x0000,
679 dev->eeprom_addrwidth_16bit,
680 len, data);
681 if (err != len) {
682 em28xx_errdev("failed to read eeprom (err=%d)\n", err);
683 goto error;
684 }
685
686 if (i2c_debug) {
687
688 print_hex_dump(KERN_INFO, "eeprom ", DUMP_PREFIX_OFFSET,
689 16, 1, data, len, true);
690
691 if (dev->eeprom_addrwidth_16bit)
692 em28xx_info("eeprom %06x: ... (skipped)\n", 256);
693 }
694
695 if (dev->eeprom_addrwidth_16bit &&
696 data[0] == 0x26 && data[3] == 0x00) {
697
698 u16 mc_start;
699 u16 hwconf_offset;
700
701 dev->hash = em28xx_hash_mem(data, len, 32);
702 mc_start = (data[1] << 8) + 4;
703
704 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
705 data[0], data[1], data[2], data[3], dev->hash);
706 em28xx_info("EEPROM info:\n");
707 em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
708 mc_start, data[2]);
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
724 data);
725 if (err != 2) {
726 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
727 err);
728 goto error;
729 }
730
731
732 hwconf_offset = mc_start + data[0] + (data[1] << 8);
733
734
735
736
737
738
739
740
741 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
742 data);
743 if (err != len) {
744 em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
745 err);
746 goto error;
747 }
748
749
750
751 if (data[0] != 0x1a || data[1] != 0xeb ||
752 data[2] != 0x67 || data[3] != 0x95) {
753 em28xx_info("\tno hardware configuration dataset found in eeprom\n");
754 kfree(data);
755 return 0;
756 }
757
758
759
760 } else if (!dev->eeprom_addrwidth_16bit &&
761 data[0] == 0x1a && data[1] == 0xeb &&
762 data[2] == 0x67 && data[3] == 0x95) {
763 dev->hash = em28xx_hash_mem(data, len, 32);
764 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
765 data[0], data[1], data[2], data[3], dev->hash);
766 em28xx_info("EEPROM info:\n");
767 } else {
768 em28xx_info("unknown eeprom format or eeprom corrupted !\n");
769 err = -ENODEV;
770 goto error;
771 }
772
773 *eedata = data;
774 *eedata_len = len;
775 dev_config = (void *)*eedata;
776
777 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
778 case 0:
779 em28xx_info("\tNo audio on board.\n");
780 break;
781 case 1:
782 em28xx_info("\tAC97 audio (5 sample rates)\n");
783 break;
784 case 2:
785 if (dev->chip_id < CHIP_ID_EM2860)
786 em28xx_info("\tI2S audio, sample rate=32k\n");
787 else
788 em28xx_info("\tI2S audio, 3 sample rates\n");
789 break;
790 case 3:
791 if (dev->chip_id < CHIP_ID_EM2860)
792 em28xx_info("\tI2S audio, 3 sample rates\n");
793 else
794 em28xx_info("\tI2S audio, 5 sample rates\n");
795 break;
796 }
797
798 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
799 em28xx_info("\tUSB Remote wakeup capable\n");
800
801 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
802 em28xx_info("\tUSB Self power capable\n");
803
804 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
805 case 0:
806 em28xx_info("\t500mA max power\n");
807 break;
808 case 1:
809 em28xx_info("\t400mA max power\n");
810 break;
811 case 2:
812 em28xx_info("\t300mA max power\n");
813 break;
814 case 3:
815 em28xx_info("\t200mA max power\n");
816 break;
817 }
818 em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
819 dev_config->string_idx_table,
820 le16_to_cpu(dev_config->string1),
821 le16_to_cpu(dev_config->string2),
822 le16_to_cpu(dev_config->string3));
823
824 return 0;
825
826error:
827 kfree(data);
828 return err;
829}
830
831
832
833
834
835
836static u32 functionality(struct i2c_adapter *i2c_adap)
837{
838 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
839
840 if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
841 (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
842 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
843 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
844 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
845 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
846 }
847
848 WARN(1, "Unknown i2c bus algorithm.\n");
849 return 0;
850}
851
852static struct i2c_algorithm em28xx_algo = {
853 .master_xfer = em28xx_i2c_xfer,
854 .functionality = functionality,
855};
856
857static struct i2c_adapter em28xx_adap_template = {
858 .owner = THIS_MODULE,
859 .name = "em28xx",
860 .algo = &em28xx_algo,
861};
862
863static struct i2c_client em28xx_client_template = {
864 .name = "em28xx internal",
865};
866
867
868
869
870
871
872
873static char *i2c_devs[128] = {
874 [0x3e >> 1] = "remote IR sensor",
875 [0x4a >> 1] = "saa7113h",
876 [0x52 >> 1] = "drxk",
877 [0x60 >> 1] = "remote IR sensor",
878 [0x8e >> 1] = "remote IR sensor",
879 [0x86 >> 1] = "tda9887",
880 [0x80 >> 1] = "msp34xx",
881 [0x88 >> 1] = "msp34xx",
882 [0xa0 >> 1] = "eeprom",
883 [0xb0 >> 1] = "tda9874",
884 [0xb8 >> 1] = "tvp5150a",
885 [0xba >> 1] = "webcam sensor or tvp5150a",
886 [0xc0 >> 1] = "tuner (analog)",
887 [0xc2 >> 1] = "tuner (analog)",
888 [0xc4 >> 1] = "tuner (analog)",
889 [0xc6 >> 1] = "tuner (analog)",
890};
891
892
893
894
895
896void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
897{
898 u8 i2c_devicelist[128];
899 unsigned char buf;
900 int i, rc;
901
902 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
903
904 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
905 dev->i2c_client[bus].addr = i;
906 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
907 if (rc < 0)
908 continue;
909 i2c_devicelist[i] = i;
910 em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
911 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
912 }
913
914 if (bus == dev->def_i2c_bus)
915 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
916 ARRAY_SIZE(i2c_devicelist), 32);
917}
918
919
920
921
922
923int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
924 enum em28xx_i2c_algo_type algo_type)
925{
926 int retval;
927
928 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
929 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
930
931 if (bus >= NUM_I2C_BUSES)
932 return -ENODEV;
933
934 dev->i2c_adap[bus] = em28xx_adap_template;
935 dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
936 strcpy(dev->i2c_adap[bus].name, dev->name);
937
938 dev->i2c_bus[bus].bus = bus;
939 dev->i2c_bus[bus].algo_type = algo_type;
940 dev->i2c_bus[bus].dev = dev;
941 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
942
943 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
944 if (retval < 0) {
945 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
946 __func__, retval);
947 return retval;
948 }
949
950 dev->i2c_client[bus] = em28xx_client_template;
951 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
952
953
954 if (!bus) {
955 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
956 if ((retval < 0) && (retval != -ENODEV)) {
957 em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
958 __func__, retval);
959
960 return retval;
961 }
962 }
963
964 if (i2c_scan)
965 em28xx_do_i2c_scan(dev, bus);
966
967 return 0;
968}
969
970
971
972
973
974int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
975{
976 if (bus >= NUM_I2C_BUSES)
977 return -ENODEV;
978
979 i2c_del_adapter(&dev->i2c_adap[bus]);
980 return 0;
981}
982