pvrusb2-eeprom.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. */
  17. #include <linux/slab.h>
  18. #include "pvrusb2-eeprom.h"
  19. #include "pvrusb2-hdw-internal.h"
  20. #include "pvrusb2-debug.h"
  21. #define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
  22. /*
  23. Read and analyze data in the eeprom. Use tveeprom to figure out
  24. the packet structure, since this is another Hauppauge device and
  25. internally it has a family resemblance to ivtv-type devices
  26. */
  27. #include <media/tveeprom.h>
  28. /* We seem to only be interested in the last 128 bytes of the EEPROM */
  29. #define EEPROM_SIZE 128
  30. /* Grab EEPROM contents, needed for direct method. */
  31. static u8 *pvr2_eeprom_fetch(struct pvr2_hdw *hdw)
  32. {
  33. struct i2c_msg msg[2];
  34. u8 *eeprom;
  35. u8 iadd[2];
  36. u8 addr;
  37. u16 eepromSize;
  38. unsigned int offs;
  39. int ret;
  40. int mode16 = 0;
  41. unsigned pcnt,tcnt;
  42. eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
  43. if (!eeprom) {
  44. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  45. "Failed to allocate memory required to read eeprom");
  46. return NULL;
  47. }
  48. trace_eeprom("Value for eeprom addr from controller was 0x%x",
  49. hdw->eeprom_addr);
  50. addr = hdw->eeprom_addr;
  51. /* Seems that if the high bit is set, then the *real* eeprom
  52. address is shifted right now bit position (noticed this in
  53. newer PVR USB2 hardware) */
  54. if (addr & 0x80) addr >>= 1;
  55. /* FX2 documentation states that a 16bit-addressed eeprom is
  56. expected if the I2C address is an odd number (yeah, this is
  57. strange but it's what they do) */
  58. mode16 = (addr & 1);
  59. eepromSize = (mode16 ? 4096 : 256);
  60. trace_eeprom("Examining %d byte eeprom at location 0x%x using %d bit addressing",
  61. eepromSize, addr,
  62. mode16 ? 16 : 8);
  63. msg[0].addr = addr;
  64. msg[0].flags = 0;
  65. msg[0].len = mode16 ? 2 : 1;
  66. msg[0].buf = iadd;
  67. msg[1].addr = addr;
  68. msg[1].flags = I2C_M_RD;
  69. /* We have to do the actual eeprom data fetch ourselves, because
  70. (1) we're only fetching part of the eeprom, and (2) if we were
  71. getting the whole thing our I2C driver can't grab it in one
  72. pass - which is what tveeprom is otherwise going to attempt */
  73. memset(eeprom,0,EEPROM_SIZE);
  74. for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
  75. pcnt = 16;
  76. if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
  77. offs = tcnt + (eepromSize - EEPROM_SIZE);
  78. if (mode16) {
  79. iadd[0] = offs >> 8;
  80. iadd[1] = offs;
  81. } else {
  82. iadd[0] = offs;
  83. }
  84. msg[1].len = pcnt;
  85. msg[1].buf = eeprom+tcnt;
  86. if ((ret = i2c_transfer(&hdw->i2c_adap,
  87. msg,ARRAY_SIZE(msg))) != 2) {
  88. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  89. "eeprom fetch set offs err=%d",ret);
  90. kfree(eeprom);
  91. return NULL;
  92. }
  93. }
  94. return eeprom;
  95. }
  96. /* Directly call eeprom analysis function within tveeprom. */
  97. int pvr2_eeprom_analyze(struct pvr2_hdw *hdw)
  98. {
  99. u8 *eeprom;
  100. struct tveeprom tvdata;
  101. memset(&tvdata,0,sizeof(tvdata));
  102. eeprom = pvr2_eeprom_fetch(hdw);
  103. if (!eeprom)
  104. return -EINVAL;
  105. tveeprom_hauppauge_analog(&tvdata, eeprom);
  106. trace_eeprom("eeprom assumed v4l tveeprom module");
  107. trace_eeprom("eeprom direct call results:");
  108. trace_eeprom("has_radio=%d",tvdata.has_radio);
  109. trace_eeprom("tuner_type=%d",tvdata.tuner_type);
  110. trace_eeprom("tuner_formats=0x%x",tvdata.tuner_formats);
  111. trace_eeprom("audio_processor=%d",tvdata.audio_processor);
  112. trace_eeprom("model=%d",tvdata.model);
  113. trace_eeprom("revision=%d",tvdata.revision);
  114. trace_eeprom("serial_number=%d",tvdata.serial_number);
  115. trace_eeprom("rev_str=%s",tvdata.rev_str);
  116. hdw->tuner_type = tvdata.tuner_type;
  117. hdw->tuner_updated = !0;
  118. hdw->serial_number = tvdata.serial_number;
  119. hdw->std_mask_eeprom = tvdata.tuner_formats;
  120. kfree(eeprom);
  121. return 0;
  122. }