pvrusb2-debugifc.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 *
 *  $Id$
 *
 *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/string.h>
#include <linux/slab.h>
#include "pvrusb2-debugifc.h"
#include "pvrusb2-hdw.h"
#include "pvrusb2-debug.h"
#include "pvrusb2-i2c-core.h"

struct debugifc_mask_item {
	const char *name;
	unsigned long msk;
};

static struct debugifc_mask_item mask_items[] = {
	{"ENC_FIRMWARE",(1<<PVR2_SUBSYS_B_ENC_FIRMWARE)},
	{"ENC_CFG",(1<<PVR2_SUBSYS_B_ENC_CFG)},
	{"DIG_RUN",(1<<PVR2_SUBSYS_B_DIGITIZER_RUN)},
	{"USB_RUN",(1<<PVR2_SUBSYS_B_USBSTREAM_RUN)},
	{"ENC_RUN",(1<<PVR2_SUBSYS_B_ENC_RUN)},
};


static unsigned int debugifc_count_whitespace(const char *buf,
					      unsigned int count)
{
	unsigned int scnt;
	char ch;

	for (scnt = 0; scnt < count; scnt++) {
		ch = buf[scnt];
		if (ch == ' ') continue;
		if (ch == '\t') continue;
		if (ch == '\n') continue;
		break;
	}
	return scnt;
}


static unsigned int debugifc_count_nonwhitespace(const char *buf,
						 unsigned int count)
{
	unsigned int scnt;
	char ch;

	for (scnt = 0; scnt < count; scnt++) {
		ch = buf[scnt];
		if (ch == ' ') break;
		if (ch == '\t') break;
		if (ch == '\n') break;
	}
	return scnt;
}


static unsigned int debugifc_isolate_word(const char *buf,unsigned int count,
					  const char **wstrPtr,
					  unsigned int *wlenPtr)
{
	const char *wptr;
	unsigned int consume_cnt = 0;
	unsigned int wlen;
	unsigned int scnt;

85
	wptr = NULL;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	wlen = 0;
	scnt = debugifc_count_whitespace(buf,count);
	consume_cnt += scnt; count -= scnt; buf += scnt;
	if (!count) goto done;

	scnt = debugifc_count_nonwhitespace(buf,count);
	if (!scnt) goto done;
	wptr = buf;
	wlen = scnt;
	consume_cnt += scnt; count -= scnt; buf += scnt;

 done:
	*wstrPtr = wptr;
	*wlenPtr = wlen;
	return consume_cnt;
}


static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
					  u32 *num_ptr)
{
	u32 result = 0;
	u32 val;
	int ch;
	int radix = 10;
	if ((count >= 2) && (buf[0] == '0') &&
	    ((buf[1] == 'x') || (buf[1] == 'X'))) {
		radix = 16;
		count -= 2;
		buf += 2;
	} else if ((count >= 1) && (buf[0] == '0')) {
		radix = 8;
	}

	while (count--) {
		ch = *buf++;
		if ((ch >= '0') && (ch <= '9')) {
			val = ch - '0';
		} else if ((ch >= 'a') && (ch <= 'f')) {
			val = ch - 'a' + 10;
		} else if ((ch >= 'A') && (ch <= 'F')) {
			val = ch - 'A' + 10;
		} else {
			return -EINVAL;
		}
		if (val >= radix) return -EINVAL;
		result *= radix;
		result += val;
	}
	*num_ptr = result;
	return 0;
}


static int debugifc_match_keyword(const char *buf,unsigned int count,
				  const char *keyword)
{
	unsigned int kl;
	if (!keyword) return 0;
	kl = strlen(keyword);
	if (kl != count) return 0;
	return !memcmp(buf,keyword,kl);
}


static unsigned long debugifc_find_mask(const char *buf,unsigned int count)
{
	struct debugifc_mask_item *mip;
	unsigned int idx;
155
	for (idx = 0; idx < ARRAY_SIZE(mask_items); idx++) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		mip = mask_items + idx;
		if (debugifc_match_keyword(buf,count,mip->name)) {
			return mip->msk;
		}
	}
	return 0;
}


static int debugifc_print_mask(char *buf,unsigned int sz,
			       unsigned long msk,unsigned long val)
{
	struct debugifc_mask_item *mip;
	unsigned int idx;
	int bcnt = 0;
	int ccnt;
172
	for (idx = 0; idx < ARRAY_SIZE(mask_items); idx++) {
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		mip = mask_items + idx;
		if (!(mip->msk & msk)) continue;
		ccnt = scnprintf(buf,sz,"%s%c%s",
				 (bcnt ? " " : ""),
				 ((mip->msk & val) ? '+' : '-'),
				 mip->name);
		sz -= ccnt;
		buf += ccnt;
		bcnt += ccnt;
	}
	return bcnt;
}

static unsigned int debugifc_parse_subsys_mask(const char *buf,
					       unsigned int count,
					       unsigned long *mskPtr,
					       unsigned long *valPtr)
{
	const char *wptr;
	unsigned int consume_cnt = 0;
	unsigned int scnt;
	unsigned int wlen;
	int mode;
	unsigned long m1,msk,val;

	msk = 0;
	val = 0;

	while (count) {
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (!scnt) break;
		consume_cnt += scnt; count -= scnt; buf += scnt;
		if (!wptr) break;

		mode = 0;
		if (wlen) switch (wptr[0]) {
		case '+':
			wptr++;
			wlen--;
			break;
		case '-':
			mode = 1;
			wptr++;
			wlen--;
			break;
		}
		if (!wlen) continue;
		m1 = debugifc_find_mask(wptr,wlen);
		if (!m1) break;
		msk |= m1;
		if (!mode) val |= m1;
	}
	*mskPtr = msk;
	*valPtr = val;
	return consume_cnt;
}


int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
{
	int bcnt = 0;
	int ccnt;
	struct pvr2_hdw_debug_info dbg;

	pvr2_hdw_get_debug_info(hdw,&dbg);

	ccnt = scnprintf(buf,acnt,"big lock %s; ctl lock %s",
			 (dbg.big_lock_held ? "held" : "free"),
			 (dbg.ctl_lock_held ? "held" : "free"));
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	if (dbg.ctl_lock_held) {
		ccnt = scnprintf(buf,acnt,"; cmd_state=%d cmd_code=%d"
				 " cmd_wlen=%d cmd_rlen=%d"
				 " wpend=%d rpend=%d tmout=%d rstatus=%d"
				 " wstatus=%d",
				 dbg.cmd_debug_state,dbg.cmd_code,
				 dbg.cmd_debug_write_len,
				 dbg.cmd_debug_read_len,
				 dbg.cmd_debug_write_pend,
				 dbg.cmd_debug_read_pend,
				 dbg.cmd_debug_timeout,
				 dbg.cmd_debug_rstatus,
				 dbg.cmd_debug_wstatus);
		bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	}
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(
		buf,acnt,"driver flags: %s %s %s\n",
		(dbg.flag_init_ok ? "initialized" : "uninitialized"),
		(dbg.flag_ok ? "ok" : "fail"),
		(dbg.flag_disconnected ? "disconnected" : "connected"));
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"Subsystems enabled / configured: ");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = debugifc_print_mask(buf,acnt,dbg.subsys_flags,dbg.subsys_flags);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"Subsystems disabled / unconfigured: ");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = debugifc_print_mask(buf,acnt,~dbg.subsys_flags,dbg.subsys_flags);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	ccnt = scnprintf(buf,acnt,"Attached I2C modules:\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = pvr2_i2c_report(hdw,buf,acnt);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	return bcnt;
}


int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
			       char *buf,unsigned int acnt)
{
	int bcnt = 0;
	int ccnt;
	unsigned long msk;
	int ret;
	u32 gpio_dir,gpio_in,gpio_out;

	ret = pvr2_hdw_is_hsm(hdw);
	ccnt = scnprintf(buf,acnt,"USB link speed: %s\n",
			 (ret < 0 ? "FAIL" : (ret ? "high" : "full")));
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	gpio_dir = 0; gpio_in = 0; gpio_out = 0;
	pvr2_hdw_gpio_get_dir(hdw,&gpio_dir);
	pvr2_hdw_gpio_get_out(hdw,&gpio_out);
	pvr2_hdw_gpio_get_in(hdw,&gpio_in);
	ccnt = scnprintf(buf,acnt,"GPIO state: dir=0x%x in=0x%x out=0x%x\n",
			 gpio_dir,gpio_in,gpio_out);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	ccnt = scnprintf(buf,acnt,"Streaming is %s\n",
			 pvr2_hdw_get_streaming(hdw) ? "on" : "off");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	msk = pvr2_hdw_subsys_get(hdw);
	ccnt = scnprintf(buf,acnt,"Subsystems enabled / configured: ");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = debugifc_print_mask(buf,acnt,msk,msk);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"Subsystems disabled / unconfigured: ");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = debugifc_print_mask(buf,acnt,~msk,msk);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	msk = pvr2_hdw_subsys_stream_get(hdw);
	ccnt = scnprintf(buf,acnt,"Subsystems stopped on stream shutdown: ");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = debugifc_print_mask(buf,acnt,msk,msk);
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
	ccnt = scnprintf(buf,acnt,"\n");
	bcnt += ccnt; acnt -= ccnt; buf += ccnt;

	return bcnt;
}


340 341
static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
				unsigned int count)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
	const char *wptr;
	unsigned int wlen;
	unsigned int scnt;

	scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
	if (!scnt) return 0;
	count -= scnt; buf += scnt;
	if (!wptr) return 0;

	pvr2_trace(PVR2_TRACE_DEBUGIFC,"debugifc cmd: \"%.*s\"",wlen,wptr);
	if (debugifc_match_keyword(wptr,wlen,"reset")) {
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (!scnt) return -EINVAL;
		count -= scnt; buf += scnt;
		if (!wptr) return -EINVAL;
		if (debugifc_match_keyword(wptr,wlen,"cpu")) {
			pvr2_hdw_cpureset_assert(hdw,!0);
			pvr2_hdw_cpureset_assert(hdw,0);
			return 0;
		} else if (debugifc_match_keyword(wptr,wlen,"bus")) {
			pvr2_hdw_device_reset(hdw);
		} else if (debugifc_match_keyword(wptr,wlen,"soft")) {
			return pvr2_hdw_cmd_powerup(hdw);
		} else if (debugifc_match_keyword(wptr,wlen,"deep")) {
			return pvr2_hdw_cmd_deep_reset(hdw);
		} else if (debugifc_match_keyword(wptr,wlen,"firmware")) {
			return pvr2_upload_firmware2(hdw);
		} else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
			return pvr2_hdw_cmd_decoder_reset(hdw);
		}
		return -EINVAL;
	} else if (debugifc_match_keyword(wptr,wlen,"subsys_flags")) {
		unsigned long msk = 0;
		unsigned long val = 0;
		if (debugifc_parse_subsys_mask(buf,count,&msk,&val) != count) {
			pvr2_trace(PVR2_TRACE_DEBUGIFC,
				   "debugifc parse error on subsys mask");
			return -EINVAL;
		}
		pvr2_hdw_subsys_bit_chg(hdw,msk,val);
		return 0;
	} else if (debugifc_match_keyword(wptr,wlen,"stream_flags")) {
		unsigned long msk = 0;
		unsigned long val = 0;
		if (debugifc_parse_subsys_mask(buf,count,&msk,&val) != count) {
			pvr2_trace(PVR2_TRACE_DEBUGIFC,
				   "debugifc parse error on stream mask");
			return -EINVAL;
		}
		pvr2_hdw_subsys_stream_bit_chg(hdw,msk,val);
		return 0;
	} else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (!scnt) return -EINVAL;
		count -= scnt; buf += scnt;
		if (!wptr) return -EINVAL;
		if (debugifc_match_keyword(wptr,wlen,"fetch")) {
			pvr2_hdw_cpufw_set_enabled(hdw,!0);
			return 0;
		} else if (debugifc_match_keyword(wptr,wlen,"done")) {
			pvr2_hdw_cpufw_set_enabled(hdw,0);
			return 0;
		} else {
			return -EINVAL;
		}
	} else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
		int dir_fl = 0;
		int ret;
		u32 msk,val;
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (!scnt) return -EINVAL;
		count -= scnt; buf += scnt;
		if (!wptr) return -EINVAL;
		if (debugifc_match_keyword(wptr,wlen,"dir")) {
			dir_fl = !0;
		} else if (!debugifc_match_keyword(wptr,wlen,"out")) {
			return -EINVAL;
		}
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (!scnt) return -EINVAL;
		count -= scnt; buf += scnt;
		if (!wptr) return -EINVAL;
		ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
		if (ret) return ret;
		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
		if (wptr) {
			ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
			if (ret) return ret;
		} else {
			val = msk;
			msk = 0xffffffff;
		}
		if (dir_fl) {
			ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
		} else {
			ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
		}
		return ret;
	}
	pvr2_trace(PVR2_TRACE_DEBUGIFC,
		   "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
	return -EINVAL;
}


int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
			unsigned int count)
{
	unsigned int bcnt = 0;
	int ret;

	while (count) {
		for (bcnt = 0; bcnt < count; bcnt++) {
			if (buf[bcnt] == '\n') break;
		}

		ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
		if (ret < 0) return ret;
		if (bcnt < count) bcnt++;
		buf += bcnt;
		count -= bcnt;
	}

	return 0;
}


/*
  Stuff for Emacs to see, in order to encourage consistent editing style:
  *** Local Variables: ***
  *** mode: c ***
  *** fill-column: 75 ***
  *** tab-width: 8 ***
  *** c-basic-offset: 8 ***
  *** End: ***
  */