/* * util_write_all -- a wrapper for util_write * * writes exactly count bytes from buf to file referred to by fd * returns -1 on error, 0 otherwise */ int util_write_all(int fd, const char *buf, size_t count) { ssize_t n_wrote = 0; size_t total = 0; while (count > total) { n_wrote = util_write(fd, buf, count - total); if (n_wrote <= 0) return -1; buf += (size_t)n_wrote; total += (size_t)n_wrote; } return 0; }
/* * pool_btt_write -- (internal) perform write in BTT file mode */ static inline ssize_t pool_btt_write(struct pool_data *pool, const void *src, size_t count) { ssize_t nwrite = 0; size_t total = 0; while (count > total && (nwrite = util_write(pool->set_file->fd, src, count - total))) { if (nwrite == -1) { ERR("!write"); return total ? (ssize_t)total : -1; } src = (void *)((ssize_t)src + nwrite); total += (size_t)nwrite; } return (ssize_t)total; }
int isp_send( int sd, int type, char *buffer, int datalen, int to ) { UINT8 *ptr; int msglen; static char *fid = "isp_send"; if (to < 1) to = 10; ptr = (UINT8 *) buffer; ptr += utilPackINT16(ptr, (UINT16) datalen+2); ptr += utilPackINT16(ptr, (UINT16) type); msglen = datalen + 4; if (util_write(sd, buffer, msglen, to) != msglen) { util_log(1, "%s: util_write: %s", fid, syserrmsg(errno)); return ISP_ERROR; } return ISP_OK; }
/** @internal @This asks to open the given device. * * @param upipe description structure of the pipe * @param path relative or absolute path of the file * @return an error code */ static int upipe_dveo_asi_sink_open(struct upipe *upipe) { #define BYPASS_MODE 1 struct upipe_dveo_asi_sink *upipe_dveo_asi_sink = upipe_dveo_asi_sink_from_upipe(upipe); char path[20], sys[50], buf[20]; memset(buf, 0, sizeof(buf)); static const char dev_fmt[] = "/dev/asitx%u"; static const char sys_fmt[] = "/sys/class/asi/asitx%u/%s"; static const char dvbm_sys_fmt[] = "/sys/class/dvbm/%u/%s"; snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "timestamps"); snprintf(buf, sizeof(buf), "%u\n", 2); if (util_write(sys, buf, sizeof(buf)) < 0) { upipe_err_va(upipe, "Couldn't set timestamp mode (%m)"); return UBASE_ERR_EXTERNAL; } snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "bufsize"); snprintf(buf, sizeof(buf), "%u\n", 6*(188+8)); /* minimum is 1024 */ if (util_write(sys, buf, sizeof(buf)) < 0) { upipe_err_va(upipe, "Couldn't set buffer size (%m)"); return UBASE_ERR_EXTERNAL; } snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "buffers"); snprintf(buf, sizeof(buf), "%u\n", 500); if (util_write(sys, buf, sizeof(buf)) < 0) { upipe_err_va(upipe, "Couldn't set # of buffers (%m)"); return UBASE_ERR_EXTERNAL; } snprintf(path, sizeof(path), dev_fmt, upipe_dveo_asi_sink->card_idx); int fd = open(path, O_WRONLY | O_NONBLOCK | O_CLOEXEC); if (unlikely(fd < 0)) { upipe_err_va(upipe, "can't open file %s (%m)", path); return UBASE_ERR_EXTERNAL; } snprintf(sys, sizeof(sys), dvbm_sys_fmt, upipe_dveo_asi_sink->card_idx, "bypass_mode"); snprintf(buf, sizeof(buf), "%u", BYPASS_MODE); util_write(sys, buf, sizeof(buf)); /* Not all cards have this so don't fail */ unsigned int cap; if (ioctl(fd, ASI_IOC_TXGETCAP, &cap) < 0) { upipe_err_va(upipe, "can't get tx caps (%m)"); goto error; } unsigned long int bufsize; snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "bufsize"); if (util_strtoul(sys, &bufsize) < 0) { upipe_err(upipe, "Couldn't read buffer size"); goto error; } unsigned long int mode; snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "mode"); if (util_strtoul(sys, &mode) < 0) { upipe_err(upipe, "Couldn't read buffer size"); goto error; } switch (mode) { case ASI_CTL_TX_MODE_MAKE204: upipe_dbg(upipe, "Appending 0x00 bytes to make 204 bytes packets"); case ASI_CTL_TX_MODE_188: case ASI_CTL_TX_MODE_204: break; default: upipe_err_va(upipe, "Unknown TX mode %lu", mode); goto error; } unsigned long int clock_source = 0; if (cap & ASI_CAP_TX_SETCLKSRC) { snprintf(sys, sizeof(sys), sys_fmt, upipe_dveo_asi_sink->card_idx, "clock_source"); if (util_strtoul(sys, &clock_source) < 0) { upipe_err(upipe, "Couldn't read clock source"); goto error; } } switch (clock_source) { case ASI_CTL_TX_CLKSRC_ONBOARD: upipe_dbg(upipe, "Using onboard oscillator"); break; case ASI_CTL_TX_CLKSRC_EXT: upipe_dbg(upipe, "Using external reference clock"); break; case ASI_CTL_TX_CLKSRC_RX: upipe_dbg(upipe, "Using recovered receive clock"); break; default: upipe_dbg(upipe, "Unknown clock source"); break; } if (!(cap & ASI_CAP_TX_TIMESTAMPS)) { upipe_err(upipe, "Device does not support timestamps"); goto error; } upipe_dveo_asi_sink->fd = fd; upipe_notice_va(upipe, "opening file %s", path); return UBASE_ERR_NONE; error: close(fd); return UBASE_ERR_EXTERNAL; }