aboutsummaryrefslogtreecommitdiffstats
path: root/common/recipes-applications/smbus-test/files/smbus-test.c
blob: 5178f79822f5d99db19162f09bd8aa4a0d85b3b4 (plain)
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
85
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
340
341
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*****************************************************************************
*
* Copyright (c) 2014, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Advanced Micro Devices, Inc. nor the names of
*       its contributors may be used to endorse or promote products derived
*       from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
***************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <readline/readline.h>

#include "i2c-dev.h"

#define SMBUS_APP_VERSION "0.1"

static int adapter_nr = -1;
static int slave_addr = 0xFF;
static char filename[20];
static int fd = -1;

char *show_prompt(void)
{
	return "$ ";
}

void sighandler(int sig)
{
	printf("\n%s", show_prompt());
}

void show_license(void)
{
	printf("/*****************************************************************************\n"
	       "*\n"
	       "* Copyright (c) 2014, Advanced Micro Devices, Inc.\n"
	       "* All rights reserved.\n"
	       "*\n"
	       "* Redistribution and use in source and binary forms, with or without\n"
	       "* modification, are permitted provided that the following conditions are met:\n"
	       "*     * Redistributions of source code must retain the above copyright\n"
	       "*       notice, this list of conditions and the following disclaimer.\n"
	       "*     * Redistributions in binary form must reproduce the above copyright\n"
	       "*       notice, this list of conditions and the following disclaimer in the\n"
	       "*       documentation and/or other materials provided with the distribution.\n"
	       "*     * Neither the name of Advanced Micro Devices, Inc. nor the names of\n"
	       "*       its contributors may be used to endorse or promote products derived\n"
	       "*       from this software without specific prior written permission.\n"
	       "*\n"
	       "* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n"
	       "* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
	       "* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n"
	       "* DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY\n"
	       "* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"
	       "* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
	       "* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n"
	       "* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
	       "* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n"
	       "* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
	       "*\n"
	       "*\n"
	       "***************************************************************************/\n");
}

void print_usage(void)
{
	printf("\nCommands Supported ->\n");
	printf(" enumerate				: List all adapters available\n");
	printf(" setadapternum <num>			: Set the adapter number\n");
	printf(" getadapternum				: Get the current adapter number\n");
	printf(" setslaveaddr <addr>			: Set the slave device address\n");
	printf(" getslaveaddr				: Get the current slave device address\n");
	printf(" getadapterfunc				: Displays the functionalities supported by the adapter\n");
	printf(" quicksend <bit>			: Sends a single bit (0 or 1) to the device\n");
	printf(" receivebyte				: Receive a single byte from the slave device\n");
	printf(" sendbyte <byte>			: Sends a single byte to the slave device\n");
	printf(" readbyte <register>			: Reads a byte at register of the slave device\n");
	printf(" writebyte <register> <byte>		: Writes a byte at register of the slave device\n");
	printf(" readword <register>			: Reads a word at register of the slave device\n");
	printf(" writeword <register> <word>		: Writes a word at register of the slave device\n");
	printf(" readblock <register>			: Reads a block of data from register of the slave device\n");
	printf(" writeblock <register> <filename>	: Writes a block of data read from filename to register\n"
	       "                                          of the slave device\n");
	printf(" license				: Displays the terms of LICENSE for this application\n");
	printf(" help					: Displays help text\n");
	printf(" exit					: Exits the application\n\n");
}

void parse_cmd(const char *cmdline)
{
	struct i2c_smbus_ioctl_data param;
	union i2c_smbus_data smbus_data;
	unsigned long funcs;

	if ((cmdline == NULL) || (strncmp(cmdline, "exit", 4) == 0)) {
		printf("\nExiting...\n");
		if((fd != -1) && (close(fd) < 0))
			printf("Error closing device\n\n");

		exit(EXIT_SUCCESS);
	} else if (strncmp(cmdline, "enumerate", 9) == 0) {
		DIR *dir;
		struct dirent *dir_entry;
		int adapter_found = 0;

		/* Get the directory handle */
		if ((dir = opendir("/dev")) == NULL) {
			printf("Failed to open directory /dev. Probably you "
			       "do not have right privilege!\n\n");
			exit(EXIT_FAILURE);
		}

		/* Iterate over all the directory entries */
		while ((dir_entry = readdir(dir)) != NULL) {
			/*
			 * If the file is a character device, and its signature
			 * matches i2c-x, then we print the corresponding file.
			 */
			if ((dir_entry->d_type == DT_CHR) &&
			    (strncmp(dir_entry->d_name, "i2c-", 4) == 0)) {
				printf("%s\n", dir_entry->d_name);
				adapter_found = 1;
			}
		}

		printf("\n");

		/*
		 * In case we did not find even a single adapter, we print a
		 * message and exit.
		 */
		if (!adapter_found) {
			printf("No adapters found, load i2c-dev kernel module and try again\n\n");
			exit(EXIT_FAILURE);
		}
	} else if (strncmp(cmdline, "setadapternum", 13) == 0) {
		int input;
		int file_desc;

		cmdline += 14;
		if (sscanf(cmdline, "%d", &input) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		snprintf(filename, 19, "/dev/i2c-%d", input);
		file_desc = open(filename, O_RDWR);
		if (file_desc < 0) {
			printf("Error opening file %s\n\n", filename);
			return;
		}

		/* Once we have validated inputs, we store them into the global
		 * variables used at other places in the program.
		 */
		fd = file_desc;
		adapter_nr = input;
		printf("Adapter Number set to %d\n\n", adapter_nr);
	} else if (strncmp(cmdline, "getadapternum", 13) == 0) {
		if (adapter_nr == -1)
			printf("Adapter Number not set\n\n");
		else
			printf("Adapter Number set to %d\n\n", adapter_nr);
	} else if (strncmp(cmdline, "setslaveaddr", 12) == 0) {
		int addr;

		cmdline += 13;
		if (sscanf(cmdline, "0x%x", &addr) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		/* Set the slave address */
		if (ioctl(fd, I2C_SLAVE, addr) < 0) {
			printf("Error setting slave address. Please make sure "
			       "to provide 7-bit slave address\n\n");
			return;
		}

		slave_addr = addr;
		printf("Slave Address set to 0x%x\n\n", slave_addr);
	} else if (strncmp(cmdline, "getslaveaddr", 12) == 0) {
		if (slave_addr == 0xFF)
			printf("Slave Address not set\n\n");
		else
			printf("Slave Address set to 0x%x\n\n", slave_addr);
	} else if (strncmp(cmdline, "getadapterfunc", 14) == 0) {
		if (adapter_nr == -1) {
			printf("Please set adapter first\n\n");
			return;
		}

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (funcs & I2C_FUNC_I2C)
			printf("I2C_FUNC_I2C\n");
		if (funcs & I2C_FUNC_10BIT_ADDR)
			printf("I2C_FUNC_10BIT_ADDR\n");
		if (funcs & I2C_FUNC_PROTOCOL_MANGLING)
			printf("I2C_FUNC_PROTOCOL_MANGLING\n");
		if (funcs & I2C_FUNC_SMBUS_PEC)
			printf("I2C_FUNC_SMBUS_PEC\n");
		if (funcs & I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
			printf("I2C_FUNC_SMBUS_BLOCK_PROC_CALL\n");
		if (funcs & I2C_FUNC_SMBUS_QUICK)
			printf("I2C_FUNC_SMBUS_QUICK\n");
		if (funcs & I2C_FUNC_SMBUS_READ_BYTE)
			printf("I2C_FUNC_SMBUS_READ_BYTE\n");
		if (funcs & I2C_FUNC_SMBUS_WRITE_BYTE)
			printf("I2C_FUNC_SMBUS_WRITE_BYTE\n");
		if (funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)
			printf("I2C_FUNC_SMBUS_READ_BYTE_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
			printf("I2C_FUNC_SMBUS_WRITE_BYTE_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)
			printf("I2C_FUNC_SMBUS_READ_WORD_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)
			printf("I2C_FUNC_SMBUS_WRITE_WORD_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_PROC_CALL)
			printf("I2C_FUNC_SMBUS_PROC_CALL\n");
		if (funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)
			printf("I2C_FUNC_SMBUS_READ_BLOCK_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
			printf("I2C_FUNC_SMBUS_WRITE_BLOCK_DATA\n");
		if (funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)
			printf("I2C_FUNC_SMBUS_READ_I2C_BLOCK\n");
		if (funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
			printf("I2C_FUNC_SMBUS_WRITE_I2C_BLOCK\n");

		printf("\n");
	} else if (strncmp(cmdline, "quicksend", 9) == 0) {
		int bit;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_QUICK)) {
			printf("SMBus Quick command not supported by the adapter\n\n");
			return;
		}

		cmdline += 10;
		if (sscanf(cmdline, "%d", &bit) < 1) {
			printf("Invalid input, please try again\n\n");
			return;
		}

		/* Sanity check */
		if ((bit != 0) && (bit != 1)) {
			printf("Only 0 or 1 is allowed\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		param.data = NULL;

		if (bit)
			param.read_write = I2C_SMBUS_READ;
		else
			param.read_write = I2C_SMBUS_WRITE;

		param.size = I2C_SMBUS_QUICK;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Quick command failed\n\n");
			return;
		}

		printf("Quick Send %d successfull\n\n", bit);
	} else if (strncmp(cmdline, "receivebyte", 11) == 0) {
		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BYTE)) {
			printf("SMBus receivebyte command not supported by adapter\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.read_write = I2C_SMBUS_READ;
		param.size = I2C_SMBUS_BYTE;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Receive Byte command failed\n\n");
			return;
		}

		printf("Byte Received 0x%x\n\n", param.data->byte);
	} else if (strncmp(cmdline, "sendbyte", 8) == 0) {
		int byte;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BYTE)) {
			printf("SMBus sendbyte command not supported by adapter\n");
			return;
		}

		cmdline += 9;
		if (sscanf(cmdline, "0x%x", &byte) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		param.data = NULL;

		param.read_write = I2C_SMBUS_WRITE;
		param.command = byte;
		param.size = I2C_SMBUS_BYTE;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Send Byte command failed\n\n");
			return;
		}

		printf("Sent 0x%x to slave\n\n", byte);
	} else if (strncmp(cmdline, "readbyte", 8) == 0) {
		int reg;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BYTE_DATA)) {
			printf("SMBus readbyte command not supported by adapter\n");
			return;
		}

		cmdline += 9;
		if (sscanf(cmdline, "0x%x", &reg) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_READ;
		param.size = I2C_SMBUS_BYTE_DATA;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Read Byte Data command failed\n\n");
			return;
		}

		printf("Read 0x%x at register 0x%x of the slave\n\n", param.data->byte, reg);
	} else if (strncmp(cmdline, "writebyte", 9) == 0) {
		int reg, byte;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BYTE_DATA)) {
			printf("SMBus writebyte command not supported by adapter\n");
			return;
		}

		cmdline += 10;
		if (sscanf(cmdline, "0x%x 0x%x", &reg, &byte) < 2) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_WRITE;
		param.size = I2C_SMBUS_BYTE_DATA;
		param.data->byte = byte;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Write Byte Data command failed\n\n");
			return;
		}

		printf("Written 0x%x to register 0x%x of the slave\n\n", byte, reg);
	} else if (strncmp(cmdline, "readword", 8) == 0) {
		int reg;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_WORD_DATA)) {
			printf("SMBus readword command not supported by adapter\n");
			return;
		}

		cmdline += 9;
		if (sscanf(cmdline, "0x%x", &reg) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_READ;
		param.size = I2C_SMBUS_WORD_DATA;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Read Word Data command failed\n\n");
			return;
		}

		printf("Read 0x%.4x at register 0x%x of the slave\n\n", param.data->word, reg);
	} else if (strncmp(cmdline, "writeword", 9) == 0) {
		int reg, word;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_WORD_DATA)) {
			printf("SMBus writeword command not supported by adapter\n");
			return;
		}

		cmdline += 10;
		if (sscanf(cmdline, "0x%x 0x%x", &reg, &word) < 2) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_WRITE;
		param.size = I2C_SMBUS_WORD_DATA;
		param.data->word = word;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Write Word Data command failed\n\n");
			return;
		}

		printf("Written 0x%x to register 0x%x of the slave\n\n", word, reg);
	} else if (strncmp(cmdline, "readblock", 9) == 0) {
		int reg;
		int i;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BLOCK_DATA)) {
			printf("SMBus readblock command not supported by adapter\n");
			return;
		}

		cmdline += 10;
		if (sscanf(cmdline, "0x%x", &reg) < 1) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_READ;
		param.size = I2C_SMBUS_BLOCK_DATA;
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Read Block Data command failed\n\n");
			return;
		}

		printf("Reading %d bytes from register 0x%x:\n", param.data->block[0], reg);
		for (i = 1; i <= param.data->block[0]; i++)
			printf("0x%.2x ", param.data->block[i]);
		printf("\n\n");
	} else if (strncmp(cmdline, "writeblock", 10) == 0) {
		int reg;
		char infile[256];
		FILE *file;
		int data;
		int num_bytes = 0;
		int i;

		/* Get the adapter functionality */
		if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
			printf("Error getting adapter functionalities\n\n");
			return;
		}

		if (!(funcs & I2C_FUNC_SMBUS_BLOCK_DATA)) {
			printf("SMBus writeblock command not supported by adapter\n");
			return;
		}

		cmdline += 11;
		if (sscanf(cmdline, "0x%x %s", &reg, infile) < 2) {
			printf("Invalid inputs, please try again\n\n");
			return;
		}

		/* open infile for reading */
		if ((file = fopen(infile, "r")) == NULL) {
			printf("Failed to open file %s\n\n", infile);
			return;
		}

		/* setup parameters to the IOCTL call */
		memset(&param, 0, sizeof(struct i2c_smbus_ioctl_data));
		memset(&smbus_data, 0, sizeof(union i2c_smbus_data));
		param.data = &smbus_data;

		param.command = reg;
		param.read_write = I2C_SMBUS_WRITE;
		param.size = I2C_SMBUS_BLOCK_DATA;

		/*
		 * populate block data to be sent to the device. SMBus sets a
		 * limit of 32 bytes to be sent or received in a block.
		 */
		for (i = 1; i <= 32; i++) {
			if (fscanf(file, "0x%x ", &data) == EOF)
				break;

			param.data->block[i] = data;
			num_bytes++;
		}

		/* The very first block data should be the number of bytes
		 * in the block.
		 */
		param.data->block[0] = num_bytes;

		printf("Writing %d bytes to register 0x%.2x of device\n\n",
		       param.data->block[0], reg);

		/* Execute block write command */
		if (ioctl(fd, I2C_SMBUS, &param) == -1) {
			printf("SMBus Write Block Data command failed\n\n");
			return;
		}

		fclose(file);
	} else if (strncmp(cmdline, "license", 7) == 0) {
		show_license();
	} else if (strncmp(cmdline, "help", 4) == 0) {
		print_usage();
	} else {
		printf("\nUnknown command\n");
		print_usage();
	}
}

int main(void)
{
	char *cmdline= NULL;

	printf("SMBus sample application version: %s\n", SMBUS_APP_VERSION);
	printf("Copyright (c) 2014, Advanced Micro Devices, Inc.\n"
	       "This sample application comes with ABSOLUTELY NO WARRANTY;\n"
	       "This is free software, and you are welcome to redistribute it\n"
	       "under certain conditions; type `license` for details.\n\n");

	/* Handler for Ctrl+C */
	signal(SIGINT, sighandler);

	while (1) {
		cmdline = readline(show_prompt());
		parse_cmd(cmdline);
		/* Free the memory malloc'ed by readline */
		free(cmdline);
	}

	/* Should never reach here */
	return 0;
}