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