pvrusb2-eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/slab.h>
  22. #include "pvrusb2-eeprom.h"
  23. #include "pvrusb2-hdw-internal.h"
  24. #include "pvrusb2-debug.h"
  25. #define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
  26. /*
  27. Read and analyze data in the eeprom. Use tveeprom to figure out
  28. the packet structure, since this is another Hauppauge device and
  29. internally it has a family resemblance to ivtv-type devices
  30. */
  31. #include <media/tveeprom.h>
  32. /* We seem to only be interested in the last 128 bytes of the EEPROM */
  33. #define EEPROM_SIZE 128
  34. /* Grab EEPROM contents, needed for direct method. */
  35. static u8 *pvr2_eeprom_fetch(struct pvr2_hdw *hdw)
  36. {
  37. struct i2c_msg msg[2];
  38. u8 *eeprom;
  39. u8 iadd[2];
  40. u8 addr;
  41. u16 eepromSize;
  42. unsigned int offs;
  43. int ret;
  44. int mode16 = 0;
  45. unsigned pcnt,tcnt;
  46. eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
  47. if (!eeprom) {
  48. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  49. "Failed to allocate memory"
  50. " required to read eeprom");
  51. return NULL;
  52. }
  53. trace_eeprom("Value for eeprom addr from controller was 0x%x",
  54. hdw->eeprom_addr);
  55. addr = hdw->eeprom_addr;
  56. /* Seems that if the high bit is set, then the *real* eeprom
  57. address is shifted right now bit position (noticed this in
  58. newer PVR USB2 hardware) */
  59. if (addr & 0x80) addr >>= 1;
  60. /* FX2 documentation states that a 16bit-addressed eeprom is
  61. expected if the I2C address is an odd number (yeah, this is
  62. strange but it's what they do) */
  63. mode16 = (addr & 1);
  64. eepromSize = (mode16 ? 4096 : 256);
  65. trace_eeprom("Examining %d byte eeprom at location 0x%x"
  66. " using %d bit addressing",eepromSize,addr,
  67. mode16 ? 16 : 8);
  68. msg[0].addr = addr;
  69. msg[0].flags = 0;
  70. msg[0].len = mode16 ? 2 : 1;
  71. msg[0].buf = iadd;
  72. msg[1].addr = addr;
  73. msg[1].flags = I2C_M_RD;
  74. /* We have to do the actual eeprom data fetch ourselves, because
  75. (1) we're only fetching part of the eeprom, and (2) if we were
  76. getting the whole thing our I2C driver can't grab it in one
  77. pass - which is what tveeprom is otherwise going to attempt */
  78. memset(eeprom,0,EEPROM_SIZE);
  79. for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
  80. pcnt = 16;
  81. if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
  82. offs = tcnt + (eepromSize - EEPROM_SIZE);
  83. if (mode16) {
  84. iadd[0] = offs >> 8;
  85. iadd[1] = offs;
  86. } else {
  87. iadd[0] = offs;
  88. }
  89. msg[1].len = pcnt;
  90. msg[1].buf = eeprom+tcnt;
  91. if ((ret = i2c_transfer(&hdw->i2c_adap,
  92. msg,ARRAY_SIZE(msg))) != 2) {
  93. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  94. "eeprom fetch set offs err=%d",ret);
  95. kfree(eeprom);
  96. return NULL;
  97. }
  98. }
  99. return eeprom;
  100. }
  101. /* Directly call eeprom analysis function within tveeprom. */
  102. int pvr2_eeprom_analyze(struct pvr2_hdw *hdw)
  103. {
  104. u8 *eeprom;
  105. struct tveeprom tvdata;
  106. memset(&tvdata,0,sizeof(tvdata));
  107. eeprom = pvr2_eeprom_fetch(hdw);
  108. if (!eeprom)
  109. return -EINVAL;
  110. tveeprom_hauppauge_analog(NULL, &tvdata, eeprom);
  111. trace_eeprom("eeprom assumed v4l tveeprom module");
  112. trace_eeprom("eeprom direct call results:");
  113. trace_eeprom("has_radio=%d",tvdata.has_radio);
  114. trace_eeprom("tuner_type=%d",tvdata.tuner_type);
  115. trace_eeprom("tuner_formats=0x%x",tvdata.tuner_formats);
  116. trace_eeprom("audio_processor=%d",tvdata.audio_processor);
  117. trace_eeprom("model=%d",tvdata.model);
  118. trace_eeprom("revision=%d",tvdata.revision);
  119. trace_eeprom("serial_number=%d",tvdata.serial_number);
  120. trace_eeprom("rev_str=%s",tvdata.rev_str);
  121. hdw->tuner_type = tvdata.tuner_type;
  122. hdw->tuner_updated = !0;
  123. hdw->serial_number = tvdata.serial_number;
  124. hdw->std_mask_eeprom = tvdata.tuner_formats;
  125. kfree(eeprom);
  126. return 0;
  127. }