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