Пример #1
0
/* Load the firmware. Optionally it can be in ROM or newer versions
 * can be on disk, saving the expense of the ROM hardware. */
int saa7164_downloadfirmware(struct saa7164_dev *dev)
{
	/* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
	u32 tmp, filesize, version, err_flags, first_timeout, fwlength;
	u32 second_timeout, updatebootloader = 1, bootloadersize = 0;
	const struct firmware *fw = NULL;
	struct fw_header *hdr, *boothdr = NULL, *fwhdr;
	u32 bootloaderversion = 0, fwloadersize;
	u8 *bootloaderoffset = NULL, *fwloaderoffset;
	char *fwname;
	int ret;

	dprintk(DBGLVL_FW, "%s()\n", __func__);

	if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) {
		fwname = SAA7164_REV2_FIRMWARE;
		fwlength = SAA7164_REV2_FIRMWARE_SIZE;
	} else {
		fwname = SAA7164_REV3_FIRMWARE;
		fwlength = SAA7164_REV3_FIRMWARE_SIZE;
	}

	version = saa7164_getcurrentfirmwareversion(dev);

	if (version == 0x00) {

		second_timeout = 100;
		first_timeout = 100;
		err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
		dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
			__func__, err_flags);

		while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
			dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
				__func__, err_flags);
			msleep(10); /* Checkpatch throws a < 20ms warning */

			if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
				printk(KERN_ERR "%s() firmware corrupt\n",
					__func__);
				break;
			}
			if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
				printk(KERN_ERR "%s() device memory corrupt\n",
					__func__);
				break;
			}
			if (err_flags & SAA_DEVICE_NO_IMAGE) {
				printk(KERN_ERR "%s() no first image\n",
				__func__);
				break;
			}
			if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
				first_timeout -= 10;
				if (first_timeout == 0) {
					printk(KERN_ERR
						"%s() no first image\n",
						__func__);
					break;
				}
			} else if (err_flags & SAA_DEVICE_IMAGE_LOADING) {
				second_timeout -= 10;
				if (second_timeout == 0) {
					printk(KERN_ERR
					"%s() FW load time exceeded\n",
						__func__);
					break;
				}
			} else {
				second_timeout -= 10;
				if (second_timeout == 0) {
					printk(KERN_ERR
					"%s() Unknown bootloader flags 0x%x\n",
						__func__, err_flags);
					break;
				}
			}

			err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
		} /* While != Booting */

		if (err_flags == SAA_DEVICE_IMAGE_BOOTING) {
			dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n",
				__func__);
			first_timeout = SAA_DEVICE_TIMEOUT;
			second_timeout = 60 * SAA_DEVICE_TIMEOUT;
			second_timeout = 100;

			err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
			dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
				__func__, err_flags);
			while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
				dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
					__func__, err_flags);
				msleep(10); /* Checkpatch throws a < 20ms warning */

				if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
					printk(KERN_ERR
						"%s() firmware corrupt\n",
						__func__);
					break;
				}
				if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
					printk(KERN_ERR
						"%s() device memory corrupt\n",
						__func__);
					break;
				}
				if (err_flags & SAA_DEVICE_NO_IMAGE) {
					printk(KERN_ERR "%s() no first image\n",
						__func__);
					break;
				}
				if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
					first_timeout -= 10;
					if (first_timeout == 0) {
						printk(KERN_ERR
						"%s() no second image\n",
							__func__);
						break;
					}
				} else if (err_flags &
					SAA_DEVICE_IMAGE_LOADING) {
					second_timeout -= 10;
					if (second_timeout == 0) {
						printk(KERN_ERR
						"%s() FW load time exceeded\n",
							__func__);
						break;
					}
				} else {
					second_timeout -= 10;
					if (second_timeout == 0) {
						printk(KERN_ERR
					"%s() Unknown bootloader flags 0x%x\n",
							__func__, err_flags);
						break;
					}
				}

				err_flags =
				saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
			} /* err_flags != SAA_DEVICE_IMAGE_BOOTING */

			dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n",
				__func__,
				saa7164_readl(SAA_BOOTLOADERERROR_FLAGS),
				saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS));

		} /* err_flags == SAA_DEVICE_IMAGE_BOOTING */

		/* It's possible for both firmwares to have booted,
		 * but that doesn't mean they've finished booting yet.
		 */
		if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
			SAA_DEVICE_IMAGE_BOOTING) &&
			(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
			SAA_DEVICE_IMAGE_BOOTING)) {


			dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n",
				__func__);

			first_timeout = SAA_DEVICE_TIMEOUT;
			while (first_timeout) {
				msleep(10); /* Checkpatch throws a < 20ms warning */

				version =
					saa7164_getcurrentfirmwareversion(dev);
				if (version) {
					dprintk(DBGLVL_FW,
					"%s() All f/w loaded successfully\n",
						__func__);
					break;
				} else {
					first_timeout -= 10;
					if (first_timeout == 0) {
						printk(KERN_ERR
						"%s() FW did not boot\n",
							__func__);
						break;
					}
				}
			}
		}
		version = saa7164_getcurrentfirmwareversion(dev);
	} /* version == 0 */

	/* Has the firmware really booted? */
	if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
		SAA_DEVICE_IMAGE_BOOTING) &&
		(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
		SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) {

		printk(KERN_ERR
			"%s() The firmware hung, probably bad firmware\n",
			__func__);

		/* Tell the second stage loader we have a deadlock */
		saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET,
			SAA_DEVICE_DEADLOCK_DETECTED);

		saa7164_getfirmwarestatus(dev);

		return -ENOMEM;
	}

	dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n",
		(version & 0x0000fc00) >> 10,
		(version & 0x000003e0) >> 5,
		(version & 0x0000001f),
		(version & 0xffff0000) >> 16);

	/* Load the firmwware from the disk if required */
	if (version == 0) {

		printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n",
			__func__, fwname);

		ret = request_firmware(&fw, fwname, &dev->pci->dev);
		if (ret) {
			printk(KERN_ERR "%s() Upload failed. "
				"(file not found?)\n", __func__);
			return -ENOMEM;
		}

		printk(KERN_INFO "%s() firmware read %Zu bytes.\n",
			__func__, fw->size);

		if (fw->size != fwlength) {
			printk(KERN_ERR "xc5000: firmware incorrect size\n");
			ret = -ENOMEM;
			goto out;
		}

		printk(KERN_INFO "%s() firmware loaded.\n", __func__);

		hdr = (struct fw_header *)fw->data;
		printk(KERN_INFO "Firmware file header part 1:\n");
		printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize);
		printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize);
		printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved);
		printk(KERN_INFO " .Version = 0x%x\n", hdr->version);

		/* Retrieve bootloader if reqd */
		if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0))
			/* Second bootloader in the firmware file */
			filesize = hdr->reserved * 16;
		else
			filesize = (hdr->firmwaresize + hdr->bslsize) *
				16 + sizeof(struct fw_header);

		printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n",
			__func__, filesize);

		/* Get bootloader (if reqd) and firmware header */
		if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
			/* Second boot loader is required */

			/* Get the loader header */
			boothdr = (struct fw_header *)(fw->data +
				sizeof(struct fw_header));

			bootloaderversion =
				saa7164_readl(SAA_DEVICE_2ND_VERSION);
			dprintk(DBGLVL_FW, "Onboard BootLoader:\n");
			dprintk(DBGLVL_FW, "->Flag 0x%x\n",
				saa7164_readl(SAA_BOOTLOADERERROR_FLAGS));
			dprintk(DBGLVL_FW, "->Ack 0x%x\n",
				saa7164_readl(SAA_DATAREADY_FLAG_ACK));
			dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version);
			dprintk(DBGLVL_FW, "->Loader Version 0x%x\n",
				bootloaderversion);

			if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
				0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK)
				== 0x00) && (version == 0x00)) {

				dprintk(DBGLVL_FW, "BootLoader version in  "
					"rom %d.%d.%d.%d\n",
					(bootloaderversion & 0x0000fc00) >> 10,
					(bootloaderversion & 0x000003e0) >> 5,
					(bootloaderversion & 0x0000001f),
					(bootloaderversion & 0xffff0000) >> 16
					);
				dprintk(DBGLVL_FW, "BootLoader version "
					"in file %d.%d.%d.%d\n",
					(boothdr->version & 0x0000fc00) >> 10,
					(boothdr->version & 0x000003e0) >> 5,
					(boothdr->version & 0x0000001f),
					(boothdr->version & 0xffff0000) >> 16
					);

				if (bootloaderversion == boothdr->version)
					updatebootloader = 0;
			}
Пример #2
0
/* TODO: Excessive levels of debug */
int saa7164_downloadimage(struct saa7164_dev *dev, u8 *src, u32 srcsize,
	u32 dlflags, u8 *dst, u32 dstsize)
{
	u32 reg, timeout, offset;
	u8 *srcbuf = NULL;
	int ret;

	u32 dlflag = dlflags;
	u32 dlflag_ack = dlflag + 4;
	u32 drflag = dlflag_ack + 4;
	u32 drflag_ack = drflag + 4;
	u32 bleflag = drflag_ack + 4;

	dprintk(DBGLVL_FW,
		"%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
		__func__, src, srcsize, dlflags, dst, dstsize);

	if ((src == NULL) || (dst == NULL)) {
		ret = -EIO;
		goto out;
	}

	srcbuf = kzalloc(4 * 1048576, GFP_KERNEL);
	if (NULL == srcbuf) {
		ret = -ENOMEM;
		goto out;
	}

	if (srcsize > (4*1048576)) {
		ret = -ENOMEM;
		goto out;
	}

	memcpy(srcbuf, src, srcsize);

	dprintk(DBGLVL_FW, "%s() dlflag = 0x%x\n", __func__, dlflag);
	dprintk(DBGLVL_FW, "%s() dlflag_ack = 0x%x\n", __func__, dlflag_ack);
	dprintk(DBGLVL_FW, "%s() drflag = 0x%x\n", __func__, drflag);
	dprintk(DBGLVL_FW, "%s() drflag_ack = 0x%x\n", __func__, drflag_ack);
	dprintk(DBGLVL_FW, "%s() bleflag = 0x%x\n", __func__, bleflag);

	reg = saa7164_readl(dlflag);
	dprintk(DBGLVL_FW, "%s() dlflag (0x%x)= 0x%x\n", __func__, dlflag, reg);
	if (reg == 1)
		dprintk(DBGLVL_FW,
			"%s() Download flag already set, please reboot\n",
			__func__);

	/* Indicate download start */
	saa7164_writel(dlflag, 1);
	ret = saa7164_dl_wait_ack(dev, dlflag_ack);
	if (ret < 0)
		goto out;

	/* Ack download start, then wait for wait */
	saa7164_writel(dlflag, 0);
	ret = saa7164_dl_wait_clr(dev, dlflag_ack);
	if (ret < 0)
		goto out;

	/* Deal with the raw firmware, in the appropriate chunk size */
	for (offset = 0; srcsize > dstsize;
		srcsize -= dstsize, offset += dstsize) {

		dprintk(DBGLVL_FW, "%s() memcpy %d\n", __func__, dstsize);
		memcpy(dst, srcbuf + offset, dstsize);

		/* Flag the data as ready */
		saa7164_writel(drflag, 1);
		ret = saa7164_dl_wait_ack(dev, drflag_ack);
		if (ret < 0)
			goto out;

		/* Wait for indication data was received */
		saa7164_writel(drflag, 0);
		ret = saa7164_dl_wait_clr(dev, drflag_ack);
		if (ret < 0)
			goto out;

	}

	dprintk(DBGLVL_FW, "%s() memcpy(l) %d\n", __func__, dstsize);
	/* Write last block to the device */
	memcpy(dst, srcbuf+offset, srcsize);

	/* Flag the data as ready */
	saa7164_writel(drflag, 1);
	ret = saa7164_dl_wait_ack(dev, drflag_ack);
	if (ret < 0)
		goto out;

	saa7164_writel(drflag, 0);
	timeout = 0;
	while (saa7164_readl(bleflag) != SAA_DEVICE_IMAGE_BOOTING) {
		if (saa7164_readl(bleflag) & SAA_DEVICE_IMAGE_CORRUPT) {
			printk(KERN_ERR "%s() image corrupt\n", __func__);
			ret = -EBUSY;
			goto out;
		}

		if (saa7164_readl(bleflag) & SAA_DEVICE_MEMORY_CORRUPT) {
			printk(KERN_ERR "%s() device memory corrupt\n",
				__func__);
			ret = -EBUSY;
			goto out;
		}

		msleep(10); /* Checkpatch throws a < 20ms warning */
		if (timeout++ > 60)
			break;
	}

	printk(KERN_INFO "%s() Image downloaded, booting...\n", __func__);

	ret = saa7164_dl_wait_clr(dev, drflag_ack);
	if (ret < 0)
		goto out;

	printk(KERN_INFO "%s() Image booted successfully.\n", __func__);
	ret = 0;

out:
	kfree(srcbuf);
	return ret;
}
Пример #3
0
/*
 * Receive a command or a response from the bus. The implementation does not
 * know if it is a command or a response it simply dequeues the data,
 * depending on the bus information given in the struct tmComResBusInfo
 * structure.
 *
 * Return Value:
 *  0          The function executed successfully.
 *  < 0        One or more members are not initialized.
 */
int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
	void *buf, int peekonly)
{
	struct tmComResBusInfo *bus = &dev->bus;
	u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
		new_grp, buf_size, space_rem;
	struct tmComResInfo msg_tmp;
	int ret = SAA_ERR_BAD_PARAMETER;

	saa7164_bus_verify(dev);

	if (msg == NULL)
		return ret;

	if (msg->size > dev->bus.m_wMaxReqSize) {
		printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
			__func__);
		return ret;
	}

	if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
		printk(KERN_ERR
			"%s() Missing msg buf, size should be %d bytes\n",
			__func__, msg->size);
		return ret;
	}

	mutex_lock(&bus->lock);

	/* Peek the bus to see if a msg exists, if it's not what we're expecting
	 * then return cleanly else read the message from the bus.
	 */
	curr_gwp = le32_to_cpu(saa7164_readl(bus->m_dwGetWritePos));
	curr_grp = le32_to_cpu(saa7164_readl(bus->m_dwGetReadPos));

	if (curr_gwp == curr_grp) {
		ret = SAA_ERR_EMPTY;
		goto out;
	}

	bytes_to_read = sizeof(*msg);

	/* Calculate write distance to current read position */
	write_distance = 0;
	if (curr_gwp >= curr_grp)
		/* Write doesn't wrap around the ring */
		write_distance = curr_gwp - curr_grp;
	else
		/* Write wraps around the ring */
		write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;

	if (bytes_to_read > write_distance) {
		printk(KERN_ERR "%s() No message/response found\n", __func__);
		ret = SAA_ERR_INVALID_COMMAND;
		goto out;
	}

	/* Calculate the new read position */
	new_grp = curr_grp + bytes_to_read;
	if (new_grp > bus->m_dwSizeGetRing) {

		/* Ring wraps */
		new_grp -= bus->m_dwSizeGetRing;
		space_rem = bus->m_dwSizeGetRing - curr_grp;

		memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
		memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
			bytes_to_read - space_rem);

	} else {
		/* No wrapping */
		memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
	}

	/* No need to update the read positions, because this was a peek */
	/* If the caller specifically want to peek, return */
	if (peekonly) {
		memcpy(msg, &msg_tmp, sizeof(*msg));
		goto peekout;
	}

	/* Check if the command/response matches what is expected */
	if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
		(msg_tmp.controlselector != msg->controlselector) ||
		(msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {

		printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
		saa7164_bus_dumpmsg(dev, msg, buf);
		saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
		ret = SAA_ERR_INVALID_COMMAND;
		goto out;
	}

	/* Get the actual command and response from the bus */
	buf_size = msg->size;

	bytes_to_read = sizeof(*msg) + msg->size;
	/* Calculate write distance to current read position */
	write_distance = 0;
	if (curr_gwp >= curr_grp)
		/* Write doesn't wrap around the ring */
		write_distance = curr_gwp - curr_grp;
	else
		/* Write wraps around the ring */
		write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;

	if (bytes_to_read > write_distance) {
		printk(KERN_ERR "%s() Invalid bus state, missing msg "
			"or mangled ring, faulty H/W / bad code?\n", __func__);
		ret = SAA_ERR_INVALID_COMMAND;
		goto out;
	}

	/* Calculate the new read position */
	new_grp = curr_grp + bytes_to_read;
	if (new_grp > bus->m_dwSizeGetRing) {

		/* Ring wraps */
		new_grp -= bus->m_dwSizeGetRing;
		space_rem = bus->m_dwSizeGetRing - curr_grp;

		if (space_rem < sizeof(*msg)) {
			/* msg wraps around the ring */
			memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
			memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
				sizeof(*msg) - space_rem);
			if (buf)
				memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
					space_rem, buf_size);

		} else if (space_rem == sizeof(*msg)) {
			memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
			if (buf)
				memcpy(buf, bus->m_pdwGetRing, buf_size);
		} else {
			/* Additional data wraps around the ring */
			memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
			if (buf) {
				memcpy(buf, bus->m_pdwGetRing + curr_grp +
					sizeof(*msg), space_rem - sizeof(*msg));
				memcpy(buf + space_rem - sizeof(*msg),
					bus->m_pdwGetRing, bytes_to_read -
					space_rem);
			}

		}

	} else {
		/* No wrapping */
		memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
		if (buf)
			memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
				buf_size);
	}

	/* Update the read positions, adjusting the ring */
	saa7164_writel(bus->m_dwGetReadPos, cpu_to_le32(new_grp));

peekout:
	msg->size = le16_to_cpu(msg->size);
	msg->command = le16_to_cpu(msg->command);
	msg->controlselector = le16_to_cpu(msg->controlselector);
	ret = SAA_OK;
out:
	mutex_unlock(&bus->lock);
	saa7164_bus_verify(dev);
	return ret;
}
Пример #4
0
/*
 * Places a command or a response on the bus. The implementation does not
 * know if it is a command or a response it just places the data on the
 * bus depending on the bus information given in the struct tmComResBusInfo
 * structure. If the command or response does not fit into the bus ring
 * buffer it will be refused.
 *
 * Return Value:
 *  SAA_OK     The function executed successfully.
 *  < 0        One or more members are not initialized.
 */
int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
	void *buf)
{
	struct tmComResBusInfo *bus = &dev->bus;
	u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
	u32 new_swp, space_rem;
	int ret = SAA_ERR_BAD_PARAMETER;

	if (!msg) {
		printk(KERN_ERR "%s() !msg\n", __func__);
		return SAA_ERR_BAD_PARAMETER;
	}

	dprintk(DBGLVL_BUS, "%s()\n", __func__);

	saa7164_bus_verify(dev);

	msg->size = cpu_to_le16(msg->size);
	msg->command = cpu_to_le16(msg->command);
	msg->controlselector = cpu_to_le16(msg->controlselector);

	if (msg->size > dev->bus.m_wMaxReqSize) {
		printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
			__func__);
		return SAA_ERR_BAD_PARAMETER;
	}

	if ((msg->size > 0) && (buf == NULL)) {
		printk(KERN_ERR "%s() Missing message buffer\n", __func__);
		return SAA_ERR_BAD_PARAMETER;
	}

	/* Lock the bus from any other access */
	mutex_lock(&bus->lock);

	bytes_to_write = sizeof(*msg) + msg->size;
	free_write_space = 0;
	timeout = SAA_BUS_TIMEOUT;
	curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
	curr_swp = le32_to_cpu(saa7164_readl(bus->m_dwSetWritePos));

	/* Deal with ring wrapping issues */
	if (curr_srp > curr_swp)
		/* Deal with the wrapped ring */
		free_write_space = curr_srp - curr_swp;
	else
		/* The ring has not wrapped yet */
		free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;

	dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
		bytes_to_write);

	dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
		free_write_space);

	dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
	dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);

	/* Process the msg and write the content onto the bus */
	while (bytes_to_write >= free_write_space) {

		if (timeout-- == 0) {
			printk(KERN_ERR "%s() bus timeout\n", __func__);
			ret = SAA_ERR_NO_RESOURCES;
			goto out;
		}

		/* TODO: Review this delay, efficient? */
		/* Wait, allowing the hardware fetch time */
		mdelay(1);

		/* Check the space usage again */
		curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));

		/* Deal with ring wrapping issues */
		if (curr_srp > curr_swp)
			/* Deal with the wrapped ring */
			free_write_space = curr_srp - curr_swp;
		else
			/* Read didn't wrap around the buffer */
			free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
				curr_swp;

	}

	/* Calculate the new write position */
	new_swp = curr_swp + bytes_to_write;

	dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
	dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
		bus->m_dwSizeSetRing);

	/* Mental Note: line 462 tmmhComResBusPCIe.cpp */

	/* Check if we're going to wrap again */
	if (new_swp > bus->m_dwSizeSetRing) {

		/* Ring wraps */
		new_swp -= bus->m_dwSizeSetRing;

		space_rem = bus->m_dwSizeSetRing - curr_swp;

		dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
			space_rem);

		dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
			(u32)sizeof(*msg));

		if (space_rem < sizeof(*msg)) {
			dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);

			/* Split the msg into pieces as the ring wraps */
			memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
			memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
				sizeof(*msg) - space_rem);

			memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
				buf, msg->size);

		} else if (space_rem == sizeof(*msg)) {
			dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);

			/* Additional data at the beginning of the ring */
			memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
			memcpy(bus->m_pdwSetRing, buf, msg->size);

		} else {
			/* Additional data wraps around the ring */
			memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
			if (msg->size > 0) {
				memcpy(bus->m_pdwSetRing + curr_swp +
					sizeof(*msg), buf, space_rem -
					sizeof(*msg));
				memcpy(bus->m_pdwSetRing, (u8 *)buf +
					space_rem - sizeof(*msg),
					bytes_to_write - space_rem);
			}

		}

	} /* (new_swp > bus->m_dwSizeSetRing) */
	else {
		dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);

		/* The ring buffer doesn't wrap, two simple copies */
		memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
		memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
			msg->size);
	}

	dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);

	/* Update the bus write position */
	saa7164_writel(bus->m_dwSetWritePos, cpu_to_le32(new_swp));
	ret = SAA_OK;

out:
	saa7164_bus_dump(dev);
	mutex_unlock(&bus->lock);
	saa7164_bus_verify(dev);
	return ret;
}
Пример #5
0
/* Load the firmware. Optionally it can be in ROM or newer versions
 * can be on disk, saving the expense of the ROM hardware. */
int saa7164_downloadfirmware(struct saa7164_dev *dev)
{
	/* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
	u32 tmp, filesize, version, err_flags, first_timeout, fwlength;
	u32 second_timeout, updatebootloader = 1, bootloadersize = 0;
	const struct firmware *fw = NULL;
	struct fw_header *hdr, *boothdr = NULL, *fwhdr;
	u32 bootloaderversion = 0, fwloadersize;
	u8 *bootloaderoffset = NULL, *fwloaderoffset;
	char *fwname;
	int ret;

;

	if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) {
		fwname = SAA7164_REV2_FIRMWARE;
		fwlength = SAA7164_REV2_FIRMWARE_SIZE;
	} else {
		fwname = SAA7164_REV3_FIRMWARE;
		fwlength = SAA7164_REV3_FIRMWARE_SIZE;
	}

	version = saa7164_getcurrentfirmwareversion(dev);

	if (version == 0x00) {

		second_timeout = 100;
		first_timeout = 100;
		err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
//		dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
;

		while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
//			dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
;
			msleep(10); /* Checkpatch throws a < 20ms warning */

			if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
//				printk(KERN_ERR "%s() firmware corrupt\n",
;
				break;
			}
			if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
//				printk(KERN_ERR "%s() device memory corrupt\n",
;
				break;
			}
			if (err_flags & SAA_DEVICE_NO_IMAGE) {
//				printk(KERN_ERR "%s() no first image\n",
;
				break;
			}
			if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
				first_timeout -= 10;
				if (first_timeout == 0) {
//					printk(KERN_ERR
//						"%s() no first image\n",
;
					break;
				}
			} else if (err_flags & SAA_DEVICE_IMAGE_LOADING) {
				second_timeout -= 10;
				if (second_timeout == 0) {
//					printk(KERN_ERR
//					"%s() FW load time exceeded\n",
;
					break;
				}
			} else {
				second_timeout -= 10;
				if (second_timeout == 0) {
//					printk(KERN_ERR
//					"%s() Unknown bootloader flags 0x%x\n",
;
					break;
				}
			}

			err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
		} /* While != Booting */

		if (err_flags == SAA_DEVICE_IMAGE_BOOTING) {
//			dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n",
;
			first_timeout = SAA_DEVICE_TIMEOUT;
			second_timeout = 60 * SAA_DEVICE_TIMEOUT;
			second_timeout = 100;

			err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
//			dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
;
			while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
//				dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
;
				msleep(10); /* Checkpatch throws a < 20ms warning */

				if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
//					printk(KERN_ERR
//						"%s() firmware corrupt\n",
;
					break;
				}
				if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
//					printk(KERN_ERR
//						"%s() device memory corrupt\n",
;
					break;
				}
				if (err_flags & SAA_DEVICE_NO_IMAGE) {
//					printk(KERN_ERR "%s() no first image\n",
;
					break;
				}
				if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
					first_timeout -= 10;
					if (first_timeout == 0) {
//						printk(KERN_ERR
//						"%s() no second image\n",
;
						break;
					}
				} else if (err_flags &
					SAA_DEVICE_IMAGE_LOADING) {
					second_timeout -= 10;
					if (second_timeout == 0) {
//						printk(KERN_ERR
//						"%s() FW load time exceeded\n",
;
						break;
					}
				} else {
					second_timeout -= 10;
					if (second_timeout == 0) {
//						printk(KERN_ERR
//					"%s() Unknown bootloader flags 0x%x\n",
;
						break;
					}
				}

				err_flags =
				saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
			} /* err_flags != SAA_DEVICE_IMAGE_BOOTING */

//			dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n",
//				__func__,
//				saa7164_readl(SAA_BOOTLOADERERROR_FLAGS),
;

		} /* err_flags == SAA_DEVICE_IMAGE_BOOTING */

		/* It's possible for both firmwares to have booted,
		 * but that doesn't mean they've finished booting yet.
		 */
		if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
			SAA_DEVICE_IMAGE_BOOTING) &&
			(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
			SAA_DEVICE_IMAGE_BOOTING)) {


//			dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n",
;

			first_timeout = SAA_DEVICE_TIMEOUT;
			while (first_timeout) {
				msleep(10); /* Checkpatch throws a < 20ms warning */

				version =
					saa7164_getcurrentfirmwareversion(dev);
				if (version) {
//					dprintk(DBGLVL_FW,
//					"%s() All f/w loaded successfully\n",
;
					break;
				} else {
					first_timeout -= 10;
					if (first_timeout == 0) {
//						printk(KERN_ERR
//						"%s() FW did not boot\n",
;
						break;
					}
				}
			}
		}
		version = saa7164_getcurrentfirmwareversion(dev);
	} /* version == 0 */

	/* Has the firmware really booted? */
	if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
		SAA_DEVICE_IMAGE_BOOTING) &&
		(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
		SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) {

//		printk(KERN_ERR
//			"%s() The firmware hung, probably bad firmware\n",
;

		/* Tell the second stage loader we have a deadlock */
		saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET,
			SAA_DEVICE_DEADLOCK_DETECTED);

		saa7164_getfirmwarestatus(dev);

		return -ENOMEM;
	}

//	dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n",
//		(version & 0x0000fc00) >> 10,
//		(version & 0x000003e0) >> 5,
//		(version & 0x0000001f),
;

	/* Load the firmwware from the disk if required */
	if (version == 0) {

//		printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n",
;

		ret = request_firmware(&fw, fwname, &dev->pci->dev);
		if (ret) {
//			printk(KERN_ERR "%s() Upload failed. "
;
			return -ENOMEM;
		}

//		printk(KERN_INFO "%s() firmware read %Zu bytes.\n",
;

		if (fw->size != fwlength) {
;
			ret = -ENOMEM;
			goto out;
		}

;

		hdr = (struct fw_header *)fw->data;
;
;
;
;
;

		/* Retrieve bootloader if reqd */
		if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0))
			/* Second bootloader in the firmware file */
			filesize = hdr->reserved * 16;
		else
			filesize = (hdr->firmwaresize + hdr->bslsize) *
				16 + sizeof(struct fw_header);

//		printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n",
;

		/* Get bootloader (if reqd) and firmware header */
		if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
			/* Second boot loader is required */

			/* Get the loader header */
			boothdr = (struct fw_header *)(fw->data +
				sizeof(struct fw_header));

			bootloaderversion =
				saa7164_readl(SAA_DEVICE_2ND_VERSION);
;
//			dprintk(DBGLVL_FW, "->Flag 0x%x\n",
;
//			dprintk(DBGLVL_FW, "->Ack 0x%x\n",
;
;
//			dprintk(DBGLVL_FW, "->Loader Version 0x%x\n",
;

			if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
				0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK)
				== 0x00) && (version == 0x00)) {

//				dprintk(DBGLVL_FW, "BootLoader version in  "
//					"rom %d.%d.%d.%d\n",
//					(bootloaderversion & 0x0000fc00) >> 10,
//					(bootloaderversion & 0x000003e0) >> 5,
//					(bootloaderversion & 0x0000001f),
//					(bootloaderversion & 0xffff0000) >> 16
;
//				dprintk(DBGLVL_FW, "BootLoader version "
//					"in file %d.%d.%d.%d\n",
//					(boothdr->version & 0x0000fc00) >> 10,
//					(boothdr->version & 0x000003e0) >> 5,
//					(boothdr->version & 0x0000001f),
//					(boothdr->version & 0xffff0000) >> 16
;

				if (bootloaderversion == boothdr->version)
					updatebootloader = 0;
			}

			/* Calculate offset to firmware header */
			tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 +
				(sizeof(struct fw_header) +
				sizeof(struct fw_header));

			fwhdr = (struct fw_header *)(fw->data+tmp);
		} else {
			/* No second boot loader */
			fwhdr = hdr;
		}

//		dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n",
//			(fwhdr->version & 0x0000fc00) >> 10,
//			(fwhdr->version & 0x000003e0) >> 5,
//			(fwhdr->version & 0x0000001f),
//			(fwhdr->version & 0xffff0000) >> 16
;

		if (version == fwhdr->version) {
			/* No download, firmware already on board */
			ret = 0;
			goto out;
		}

		if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
			if (updatebootloader) {
				/* Get ready to upload the bootloader */
				bootloadersize = (boothdr->firmwaresize +
					boothdr->bslsize) * 16 +
					sizeof(struct fw_header);

				bootloaderoffset = (u8 *)(fw->data +
					sizeof(struct fw_header));

;
//				printk(KERN_INFO "%s() FirmwareSize = 0x%x\n",
;
//				printk(KERN_INFO "%s() BSLSize = 0x%x\n",
;
//				printk(KERN_INFO "%s() Reserved = 0x%x\n",
;
//				printk(KERN_INFO "%s() Version = 0x%x\n",
;
				ret = saa7164_downloadimage(
					dev,
					bootloaderoffset,
					bootloadersize,
					SAA_DOWNLOAD_FLAGS,
					dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
					SAA_DEVICE_BUFFERBLOCKSIZE);
				if (ret < 0) {
//					printk(KERN_ERR
;
					goto out;
				}
//				dprintk(DBGLVL_FW,
;

			}

;
			bootloadersize = (boothdr->firmwaresize +
				boothdr->bslsize) * 16 +
				sizeof(struct fw_header);

			bootloaderoffset =
				(u8 *)(fw->data + sizeof(struct fw_header));

			fwloaderoffset = bootloaderoffset + bootloadersize;

			/* TODO: fix this bounds overrun here with old f/ws */
			fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) *
				16 + sizeof(struct fw_header);

			ret = saa7164_downloadimage(
				dev,
				fwloaderoffset,
				fwloadersize,
				SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET,
				dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET,
				SAA_DEVICE_2ND_BUFFERBLOCKSIZE);
			if (ret < 0) {
;
				goto out;
			}
;

		} else {

			/* No bootloader update reqd, download firmware only */
;

			ret = saa7164_downloadimage(
				dev,
				(u8 *)fw->data,
				fw->size,
				SAA_DOWNLOAD_FLAGS,
				dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
				SAA_DEVICE_BUFFERBLOCKSIZE);
			if (ret < 0) {
;
				goto out;
			}
;
		}
	}

	dev->firmwareloaded = 1;
	ret = 0;

out:
	release_firmware(fw);
	return ret;
}
int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
                    void *buf, int peekonly)
{
    struct tmComResBusInfo *bus = &dev->bus;
    u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
        new_grp, buf_size, space_rem;
    struct tmComResInfo msg_tmp;
    int ret = SAA_ERR_BAD_PARAMETER;

    saa7164_bus_verify(dev);

    if (msg == NULL)
        return ret;

    if (msg->size > dev->bus.m_wMaxReqSize) {
        printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
               __func__);
        return ret;
    }

    if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
        printk(KERN_ERR
               "%s() Missing msg buf, size should be %d bytes\n",
               __func__, msg->size);
        return ret;
    }

    mutex_lock(&bus->lock);

    curr_gwp = le32_to_cpu(saa7164_readl(bus->m_dwGetWritePos));
    curr_grp = le32_to_cpu(saa7164_readl(bus->m_dwGetReadPos));

    if (curr_gwp == curr_grp) {
        ret = SAA_ERR_EMPTY;
        goto out;
    }

    bytes_to_read = sizeof(*msg);


    write_distance = 0;
    if (curr_gwp >= curr_grp)

        write_distance = curr_gwp - curr_grp;
    else

        write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;

    if (bytes_to_read > write_distance) {
        printk(KERN_ERR "%s() No message/response found\n", __func__);
        ret = SAA_ERR_INVALID_COMMAND;
        goto out;
    }


    new_grp = curr_grp + bytes_to_read;
    if (new_grp > bus->m_dwSizeGetRing) {


        new_grp -= bus->m_dwSizeGetRing;
        space_rem = bus->m_dwSizeGetRing - curr_grp;

        memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
        memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
               bytes_to_read - space_rem);

    } else {

        memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
    }



    if (peekonly) {
        memcpy(msg, &msg_tmp, sizeof(*msg));
        goto peekout;
    }


    if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
            (msg_tmp.controlselector != msg->controlselector) ||
            (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {

        printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
        saa7164_bus_dumpmsg(dev, msg, buf);
        saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
        ret = SAA_ERR_INVALID_COMMAND;
        goto out;
    }


    buf_size = msg->size;

    bytes_to_read = sizeof(*msg) + msg->size;

    write_distance = 0;
    if (curr_gwp >= curr_grp)

        write_distance = curr_gwp - curr_grp;
    else

        write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;

    if (bytes_to_read > write_distance) {
        printk(KERN_ERR "%s() Invalid bus state, missing msg "
               "or mangled ring, faulty H/W / bad code?\n", __func__);
        ret = SAA_ERR_INVALID_COMMAND;
        goto out;
    }


    new_grp = curr_grp + bytes_to_read;
    if (new_grp > bus->m_dwSizeGetRing) {


        new_grp -= bus->m_dwSizeGetRing;
        space_rem = bus->m_dwSizeGetRing - curr_grp;

        if (space_rem < sizeof(*msg)) {

            memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
            memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
                   sizeof(*msg) - space_rem);
            if (buf)
                memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
                       space_rem, buf_size);

        } else if (space_rem == sizeof(*msg)) {
            memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
            if (buf)
                memcpy(buf, bus->m_pdwGetRing, buf_size);
        } else {

            memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
            if (buf) {
                memcpy(buf, bus->m_pdwGetRing + curr_grp +
                       sizeof(*msg), space_rem - sizeof(*msg));
                memcpy(buf + space_rem - sizeof(*msg),
                       bus->m_pdwGetRing, bytes_to_read -
                       space_rem);
            }

        }

    } else {

        memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
        if (buf)
            memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
                   buf_size);
    }


    saa7164_writel(bus->m_dwGetReadPos, cpu_to_le32(new_grp));

peekout:
    msg->size = le16_to_cpu(msg->size);
    msg->command = le32_to_cpu(msg->command);
    msg->controlselector = le16_to_cpu(msg->controlselector);
    ret = SAA_OK;
out:
    mutex_unlock(&bus->lock);
    saa7164_bus_verify(dev);
    return ret;
}
int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
                    void *buf)
{
    struct tmComResBusInfo *bus = &dev->bus;
    u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
    u32 new_swp, space_rem;
    int ret = SAA_ERR_BAD_PARAMETER;

    if (!msg) {
        printk(KERN_ERR "%s() !msg\n", __func__);
        return SAA_ERR_BAD_PARAMETER;
    }

    dprintk(DBGLVL_BUS, "%s()\n", __func__);

    saa7164_bus_verify(dev);

    msg->size = cpu_to_le16(msg->size);
    msg->command = cpu_to_le32(msg->command);
    msg->controlselector = cpu_to_le16(msg->controlselector);

    if (msg->size > dev->bus.m_wMaxReqSize) {
        printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
               __func__);
        return SAA_ERR_BAD_PARAMETER;
    }

    if ((msg->size > 0) && (buf == NULL)) {
        printk(KERN_ERR "%s() Missing message buffer\n", __func__);
        return SAA_ERR_BAD_PARAMETER;
    }


    mutex_lock(&bus->lock);

    bytes_to_write = sizeof(*msg) + msg->size;
    free_write_space = 0;
    timeout = SAA_BUS_TIMEOUT;
    curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
    curr_swp = le32_to_cpu(saa7164_readl(bus->m_dwSetWritePos));


    if (curr_srp > curr_swp)

        free_write_space = curr_srp - curr_swp;
    else

        free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;

    dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
            bytes_to_write);

    dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
            free_write_space);

    dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
    dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);


    while (bytes_to_write >= free_write_space) {

        if (timeout-- == 0) {
            printk(KERN_ERR "%s() bus timeout\n", __func__);
            ret = SAA_ERR_NO_RESOURCES;
            goto out;
        }



        mdelay(1);


        curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));


        if (curr_srp > curr_swp)

            free_write_space = curr_srp - curr_swp;
        else

            free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
                               curr_swp;

    }


    new_swp = curr_swp + bytes_to_write;

    dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
    dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
            bus->m_dwSizeSetRing);




    if (new_swp > bus->m_dwSizeSetRing) {


        new_swp -= bus->m_dwSizeSetRing;

        space_rem = bus->m_dwSizeSetRing - curr_swp;

        dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
                space_rem);

        dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
                (u32)sizeof(*msg));

        if (space_rem < sizeof(*msg)) {
            dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);


            memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
            memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
                   sizeof(*msg) - space_rem);

            memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
                   buf, msg->size);

        } else if (space_rem == sizeof(*msg)) {
            dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);


            memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
            memcpy(bus->m_pdwSetRing, buf, msg->size);

        } else {

            memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
            if (msg->size > 0) {
                memcpy(bus->m_pdwSetRing + curr_swp +
                       sizeof(*msg), buf, space_rem -
                       sizeof(*msg));
                memcpy(bus->m_pdwSetRing, (u8 *)buf +
                       space_rem - sizeof(*msg),
                       bytes_to_write - space_rem);
            }

        }

    }
    else {
        dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);


        memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
        memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
               msg->size);
    }

    dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);


    saa7164_writel(bus->m_dwSetWritePos, cpu_to_le32(new_swp));
    ret = SAA_OK;

out:
    saa7164_bus_dump(dev);
    mutex_unlock(&bus->lock);
    saa7164_bus_verify(dev);
    return ret;
}