V4L/DVB (12821): tm6000: update USB request names and clean up i2c routine

Update the descriptions of the USB request types so that they match what
we now know they do.

Rework the i2c_xfer function so that it is more explicit what sort of I2C
transfers it is that the tm6000 can't perform.

Signed-off-by: Chris Pascoe <c.pascoe@itee.uq.edu.au>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/staging/tm6000/tm6000-i2c.c b/drivers/staging/tm6000/tm6000-i2c.c
index 8569cfc..12e2c98 100644
--- a/drivers/staging/tm6000/tm6000-i2c.c
+++ b/drivers/staging/tm6000/tm6000-i2c.c
@@ -73,7 +73,7 @@
 	/* This sends addr + 1 byte with 0 */
 	rc = tm6000_read_write_usb (dev,
 		USB_DIR_IN | USB_TYPE_VENDOR,
-		REQ_16_SET_GET_I2CSEQ,
+		REQ_16_SET_GET_I2C_WR1_RDN,
 		addr, 0,
 		buf, 0);
 	msleep(10);
@@ -94,82 +94,61 @@
 {
 	struct tm6000_core *dev = i2c_adap->algo_data;
 	int addr, rc, i, byte;
-	int prev_reg = -1;
 
 	if (num <= 0)
 		return 0;
 	for (i = 0; i < num; i++) {
-		addr = (msgs[i].addr << 1) &0xff;
+		addr = (msgs[i].addr << 1) & 0xff;
 		i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
 			 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
 			 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
 		if (!msgs[i].len) {
 			/* Do I2C scan */
-			rc=tm6000_i2c_scan(i2c_adap, addr);
+			rc = tm6000_i2c_scan(i2c_adap, addr);
 		} else if (msgs[i].flags & I2C_M_RD) {
-			/* Read bytes */
-	/* I2C is assumed to have always a subaddr at the first byte of the
-	   message bus. Also, the first i2c value of the answer is returned
-	   out of message data.
-	 */
-			/* SMBus Read Byte command */
-			if (prev_reg < 0)
-				printk("XXX read from unknown prev_reg\n");
-			rc = tm6000_read_write_usb (dev,
-				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-				REQ_16_SET_GET_I2CSEQ,
-				addr | (prev_reg << 8), 0,
-				msgs[i].buf, msgs[i].len);
-			if (prev_reg >= 0)
-				prev_reg += msgs[i].len;
-			if (i2c_debug>=2) {
-				for (byte = 0; byte < msgs[i].len; byte++) {
-					printk(" %02x", msgs[i].buf[byte]);
-				}
-			}
-		} else if (i+1 < num && msgs[i].len == 2 &&
-			   (msgs[i+1].flags & I2C_M_RD) &&
-			   msgs[i].addr == msgs[i+1].addr) {
-			i2c_dprintk(2, "msg %d: write 2, read %d", i,
-				    msgs[i+1].len);
-			/* Write 2 Read N command */
-			rc = tm6000_read_write_usb (dev,
-				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-				REQ_14_SET_GET_EEPROM_PAGE, /* XXX wrong name */
-				addr | msgs[i].buf[0] << 8, msgs[i].buf[1],
-				msgs[i+1].buf, msgs[i+1].len);
-			i++;
-			if (i2c_debug>=2) {
-				for (byte = 0; byte < msgs[i].len; byte++) {
-					printk(" %02x", msgs[i].buf[byte]);
-				}
-			}
-			prev_reg = -1;
-		} else {
-			/* write bytes */
-			if (i2c_debug>=2) {
+			/* read request without preceding register selection */
+			/*
+			 * The TM6000 only supports a read transaction
+			 * immediately after a 1 or 2 byte write to select
+			 * a register.  We cannot fulfil this request.
+			 */
+			i2c_dprintk(2, " read without preceding write not"
+				       " supported");
+			rc = -EOPNOTSUPP;
+			goto err;
+		} else if (i + 1 < num && msgs[i].len <= 2 &&
+			   (msgs[i + 1].flags & I2C_M_RD) &&
+			   msgs[i].addr == msgs[i + 1].addr) {
+			/* 1 or 2 byte write followed by a read */
+			if (i2c_debug >= 2)
 				for (byte = 0; byte < msgs[i].len; byte++)
 					printk(" %02x", msgs[i].buf[byte]);
-			}
-
-			/* SMBus Write Byte command followed by a read command */
-			if(msgs[i].len == 1 && i+1 < num && msgs[i+1].flags & I2C_M_RD
-					    && msgs[i+1].addr == msgs[i].addr) {
-				prev_reg = msgs[i].buf[0];
-				if (i2c_debug >= 2)
-					printk("\n");
-				continue;
-			}
-
+			i2c_dprintk(2, "; joined to read %s len=%d:",
+				    i == num - 2 ? "stop" : "nonstop",
+				    msgs[i + 1].len);
 			rc = tm6000_read_write_usb (dev,
+				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+				msgs[i].len == 1 ? REQ_16_SET_GET_I2C_WR1_RDN
+						 : REQ_14_SET_GET_I2C_WR2_RDN,
+				addr | msgs[i].buf[0] << 8,
+				msgs[i].len == 1 ? 0 : msgs[i].buf[1],
+				msgs[i + 1].buf, msgs[i + 1].len);
+			i++;
+			if (i2c_debug >= 2)
+				for (byte = 0; byte < msgs[i].len; byte++)
+					printk(" %02x", msgs[i].buf[byte]);
+		} else {
+			/* write bytes */
+			if (i2c_debug >= 2)
+				for (byte = 0; byte < msgs[i].len; byte++)
+					printk(" %02x", msgs[i].buf[byte]);
+			rc = tm6000_read_write_usb(dev,
 				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-				REQ_16_SET_GET_I2CSEQ,
-				addr|(*msgs[i].buf)<<8, 0,
-				msgs[i].buf+1, msgs[i].len-1);
-
-			prev_reg = -1;
+				REQ_16_SET_GET_I2C_WR1_RDN,
+				addr | msgs[i].buf[0] << 8, 0,
+				msgs[i].buf + 1, msgs[i].len - 1);
 		}
-		if (i2c_debug>=2)
+		if (i2c_debug >= 2)
 			printk("\n");
 		if (rc < 0)
 			goto err;
@@ -181,7 +160,6 @@
 	return rc;
 }
 
-
 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
 			     unsigned char *eedata, int len)
 {
@@ -196,7 +174,7 @@
 	*p = i;
 	rc = tm6000_read_write_usb (dev,
 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-		REQ_16_SET_GET_I2CSEQ, 0xa0 | i<<8, 0, p, 1);
+		REQ_16_SET_GET_I2C_WR1_RDN, 0xa0 | i<<8, 0, p, 1);
 		if (rc < 1) {
 			if (p == eedata)
 				goto noeeprom;
@@ -273,7 +251,7 @@
 #define mass_write(addr, reg, data...)					\
 	{ const static u8 _val[] = data;				\
 	rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR,	\
-	REQ_16_SET_GET_I2CSEQ,(reg<<8)+addr, 0x00, (u8 *) _val,		\
+	REQ_16_SET_GET_I2C_WR1_RDN,(reg<<8)+addr, 0x00, (u8 *) _val,	\
 	ARRAY_SIZE(_val));						\
 	if (rc<0) {							\
 		printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);	\
diff --git a/drivers/staging/tm6000/tm6000-regs.h b/drivers/staging/tm6000/tm6000-regs.h
index 414852e..9b6456e 100644
--- a/drivers/staging/tm6000/tm6000-regs.h
+++ b/drivers/staging/tm6000/tm6000-regs.h
@@ -38,11 +38,11 @@
 #define REQ_11_SET_EEPROM_ADDR		11
 #define REQ_12_SET_GET_EEPROMBYTE	12
 #define REQ_13_GET_EEPROM_SEQREAD	13
-#define REQ_14_SET_GET_EEPROM_PAGE	14
+#define REQ_14_SET_GET_I2C_WR2_RDN	14
 #define REQ_15_SET_GET_I2CBYTE		15
 	/* Write: Subaddr, Slave Addr, value, 0 */
 	/* Read : Subaddr, Slave Addr, value, 1 */
-#define REQ_16_SET_GET_I2CSEQ		16
+#define REQ_16_SET_GET_I2C_WR1_RDN	16
 	/* Subaddr, Slave Addr, 0, length */
 #define REQ_17_SET_GET_I2CFP		17
 	/* Write: Slave Addr, register, value */