Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
/* cyanblkdev_block.c - West Bridge Linux Block Driver source file
## ===========================
## Copyright (C) 2010  Cypress Semiconductor
##
## 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, or (at your option) any later version.
##
## 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., 51 Franklin Street, Fifth Floor
## Boston, MA  02110-1301, USA.
## ===========================
*/

/*
 * Linux block driver implementation for Cypress West Bridge.
 * Based on the mmc block driver implementation by Andrew Christian
 * for the linux 2.6.26 kernel.
 * mmc_block.c, 5/28/2002
 */

/*
 * Block driver for media (i.e., flash cards)
 *
 * Copyright 2002 Hewlett-Packard Company
 *
 * Use consistent with the GNU GPL is permitted,
 * provided that this copyright notice is
 * preserved in its entirety in all copies and derived works.
 *
 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
 * FITNESS FOR ANY PARTICULAR PURPOSE.
 *
 * Many thanks to Alessandro Rubini and Jonathan Corbet!
 *
 * Author:  Andrew Christian
 *		  28 May 2002
 */

#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>

#include <asm/system.h>
#include <linux/uaccess.h>

#include <linux/scatterlist.h>
#include <linux/time.h>
#include <linux/signal.h>
#include <linux/delay.h>

#include "cyasblkdev_queue.h"

#define CYASBLKDEV_SHIFT	0 /* Only a single partition. */
#define CYASBLKDEV_MAX_REQ_LEN	(256)
#define CYASBLKDEV_NUM_MINORS	(256 >> CYASBLKDEV_SHIFT)
#define CY_AS_TEST_NUM_BLOCKS   (64)
#define CYASBLKDEV_MINOR_0 1
#define CYASBLKDEV_MINOR_1 2
#define CYASBLKDEV_MINOR_2 3

static int major;
module_param(major, int, 0444);
MODULE_PARM_DESC(major,
	"specify the major device number for cyasblkdev block driver");

/* parameters passed from the user space */
static int vfat_search;
module_param(vfat_search, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(vfat_search,
	"dynamically find the location of the first sector");

static int private_partition_bus = -1;
module_param(private_partition_bus, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(private_partition_bus,
	"bus number for private partition");

static int private_partition_size = -1;
module_param(private_partition_size, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(private_partition_size,
	"size of the private partition");

/*
 * There is one cyasblkdev_blk_data per slot.
 */
struct cyasblkdev_blk_data {
	spinlock_t	  lock;
	int media_count[2];
	const struct block_device_operations *blkops;
	unsigned int	usage;
	unsigned int	suspended;

	/* handle to the west bridge device this handle, typdefed as *void  */
	cy_as_device_handle		dev_handle;

	/* our custom structure, in addition to request queue,
	 * adds lock & semaphore items*/
	struct cyasblkdev_queue queue;

	/* 16 entries is enough given max request size
	 * 16 * 4K (64 K per request)*/
	struct scatterlist	  sg[16];

	/* non-zero enables printk of executed reqests */
	unsigned int	dbgprn_flags;

	/*gen_disk for private, system disk */
	struct gendisk  *system_disk;
	cy_as_media_type   system_disk_type;
	cy_bool			 system_disk_read_only;
	cy_bool			 system_disk_bus_num;

	/* sector size for the medium */
	unsigned int	system_disk_blk_size;
	unsigned int	system_disk_first_sector;
	unsigned int	system_disk_unit_no;

	/*gen_disk for bus 0 */
	struct gendisk  *user_disk_0;
	cy_as_media_type   user_disk_0_type;
	cy_bool			 user_disk_0_read_only;
	cy_bool			 user_disk_0_bus_num;

	/* sector size for the medium */
	unsigned int	user_disk_0_blk_size;
	unsigned int	user_disk_0_first_sector;
	unsigned int	user_disk_0_unit_no;

	/*gen_disk for bus 1 */
	struct gendisk  *user_disk_1;
	cy_as_media_type   user_disk_1_type;
	cy_bool			 user_disk_1_read_only;
	cy_bool			 user_disk_1_bus_num;

	/* sector size for the medium */
	unsigned int	user_disk_1_blk_size;
	unsigned int	user_disk_1_first_sector;
	unsigned int	user_disk_1_unit_no;
};

/* pointer to west bridge block data device superstructure */
static struct cyasblkdev_blk_data *gl_bd;

static DEFINE_SEMAPHORE(open_lock);

/* local forwardd declarationss  */
static cy_as_device_handle *cyas_dev_handle;
static void cyasblkdev_blk_deinit(struct cyasblkdev_blk_data *bd);

/*change debug print options */
 #define DBGPRN_RD_RQ	   (1 < 0)
 #define DBGPRN_WR_RQ		(1 < 1)
 #define DBGPRN_RQ_END	  (1 < 2)

int blkdev_ctl_dbgprn(
						int prn_flags
						)
{
	int cur_options = gl_bd->dbgprn_flags;

	DBGPRN_FUNC_NAME;

	/* set new debug print options */
	gl_bd->dbgprn_flags = prn_flags;

	/* return previous */
	return cur_options;
}
EXPORT_SYMBOL(blkdev_ctl_dbgprn);

static struct cyasblkdev_blk_data *cyasblkdev_blk_get(
							struct gendisk *disk
							)
{
	struct cyasblkdev_blk_data *bd;

	DBGPRN_FUNC_NAME;

	down(&open_lock);

	bd = disk->private_data;

	if (bd && (bd->usage == 0))
		bd = NULL;

	if (bd) {
		bd->usage++;
		#ifndef NBDEBUG
		cy_as_hal_print_message(
			"cyasblkdev_blk_get: usage = %d\n", bd->usage);
		#endif
	}
	up(&open_lock);

	return bd;
}

static void cyasblkdev_blk_put(
			struct cyasblkdev_blk_data *bd
			)
{
	DBGPRN_FUNC_NAME;

	down(&open_lock);

	if (bd) {
		bd->usage--;
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			" cyasblkdev_blk_put , bd->usage= %d\n", bd->usage);
		#endif
	} else  {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"cyasblkdev: blk_put(bd) on bd = NULL!: usage = %d\n",
			bd->usage);
		#endif
		up(&open_lock);
		return;
	}

	if (bd->usage == 0) {
		put_disk(bd->user_disk_0);
		put_disk(bd->user_disk_1);
		put_disk(bd->system_disk);
		cyasblkdev_cleanup_queue(&bd->queue);

		if (CY_AS_ERROR_SUCCESS !=
			cy_as_storage_release(bd->dev_handle, 0, 0, 0, 0)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"cyasblkdev: cannot release bus 0\n");
			#endif
		}

		if (CY_AS_ERROR_SUCCESS !=
			cy_as_storage_release(bd->dev_handle, 1, 0, 0, 0)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"cyasblkdev: cannot release bus 1\n");
			#endif
		}

		if (CY_AS_ERROR_SUCCESS !=
			cy_as_storage_stop(bd->dev_handle, 0, 0)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"cyasblkdev: cannot stop storage stack\n");
			#endif
		}

	#ifdef __CY_ASTORIA_SCM_KERNEL_HAL__
		/* If the SCM Kernel HAL is being used, disable the use
		 * of scatter/gather lists at the end of block driver usage.
		 */
		cy_as_hal_disable_scatter_list(cyasdevice_gethaltag());
	#endif

		/*ptr to global struct cyasblkdev_blk_data */
		gl_bd = NULL;
		kfree(bd);
	}

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"cyasblkdev (blk_put): usage = %d\n",
		bd->usage);
	#endif
	up(&open_lock);
}

static int cyasblkdev_blk_open(
					struct block_device *bdev,
					fmode_t mode
					)
{
	struct cyasblkdev_blk_data *bd = cyasblkdev_blk_get(bdev->bd_disk);
	int ret = -ENXIO;

	DBGPRN_FUNC_NAME;

	if (bd) {
		if (bd->usage == 2)
			check_disk_change(bdev);

		ret = 0;

		if (bdev->bd_disk == bd->user_disk_0) {
			if ((mode & FMODE_WRITE) && bd->user_disk_0_read_only) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"device marked as readonly "
					"and write requested\n");
				#endif

				cyasblkdev_blk_put(bd);
				ret = -EROFS;
			}
		} else if (bdev->bd_disk == bd->user_disk_1) {
			if ((mode & FMODE_WRITE) && bd->user_disk_1_read_only) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"device marked as readonly "
					"and write requested\n");
				#endif

				cyasblkdev_blk_put(bd);
				ret = -EROFS;
			}
		} else if (bdev->bd_disk == bd->system_disk) {
			if ((mode & FMODE_WRITE) && bd->system_disk_read_only) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"device marked as readonly "
					"and write requested\n");
				#endif

				cyasblkdev_blk_put(bd);
				ret = -EROFS;
			}
		}
	}

	return ret;
}

static int cyasblkdev_blk_release(
					struct gendisk *disk,
					fmode_t mode
					)
{
	struct cyasblkdev_blk_data *bd = disk->private_data;

	DBGPRN_FUNC_NAME;

	cyasblkdev_blk_put(bd);
	return 0;
}

static int cyasblkdev_blk_ioctl(
			struct block_device *bdev,
			fmode_t mode,
			unsigned int cmd,
			unsigned long arg
			)
{
	DBGPRN_FUNC_NAME;

	if (cmd == HDIO_GETGEO) {
		/*for now  we only process geometry IOCTL*/
		struct hd_geometry geo;

		memset(&geo, 0, sizeof(struct hd_geometry));

		geo.cylinders	= get_capacity(bdev->bd_disk) / (4 * 16);
		geo.heads	= 4;
		geo.sectors	= 16;
		geo.start	= get_start_sect(bdev);

		/* copy to user space */
		return copy_to_user((void __user *)arg, &geo, sizeof(geo))
			? -EFAULT : 0;
	}

	return -ENOTTY;
}

/* Media_changed block_device opp
 * this one is called by kernel to confirm if the media really changed
 * as we indicated by issuing check_disk_change() call */
int cyasblkdev_media_changed(struct gendisk *gd)
{
	struct cyasblkdev_blk_data *bd;

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message("cyasblkdev_media_changed() is called\n");
	#endif

	if (gd)
		bd = gd->private_data;
	else {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"cyasblkdev_media_changed() is called, "
			"but gd is null\n");
		#endif
	}

	/* return media change state "1" yes, 0 no */
	return 0;
}

/*  this one called by kernel to give us a chence
 * to prep the new media before it starts to rescaning
 * of the newlly inserted SD media */
int cyasblkdev_revalidate_disk(struct gendisk *gd)
{
	/*int (*revalidate_disk) (struct gendisk *); */

	#ifndef WESTBRIDGE_NDEBUG
	if (gd)
		cy_as_hal_print_message(
			"cyasblkdev_revalidate_disk() is called, "
			"(gl_bd->usage:%d)\n", gl_bd->usage);
	#endif

	/* 0 means ok, kern can go ahead with partition rescan */
	return 0;
}


/*standard block device driver interface */
static struct block_device_operations cyasblkdev_bdops = {
	.open			= cyasblkdev_blk_open,
	.release		= cyasblkdev_blk_release,
	.ioctl			= cyasblkdev_blk_ioctl,
	/* .getgeo		= cyasblkdev_blk_getgeo, */
	/* added to support media removal( real and simulated) media */
	.media_changed  = cyasblkdev_media_changed,
	/* added to support media removal( real and simulated) media */
	.revalidate_disk = cyasblkdev_revalidate_disk,
	.owner			= THIS_MODULE,
};

/* west bridge block device prep request function */
static int cyasblkdev_blk_prep_rq(
					struct cyasblkdev_queue *bq,
					struct request *req
					)
{
	struct cyasblkdev_blk_data *bd = bq->data;
	int stat = BLKPREP_OK;

	DBGPRN_FUNC_NAME;

	/* If we have no device, we haven't finished initialising. */
	if (!bd || !bd->dev_handle) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(KERN_ERR
			"cyasblkdev %s: killing request - no device/host\n",
			req->rq_disk->disk_name);
		#endif
		stat = BLKPREP_KILL;
	}

	if (bd->suspended) {
		blk_plug_device(bd->queue.queue);
		stat = BLKPREP_DEFER;
	}

	/* Check for excessive requests.*/
	if (blk_rq_pos(req) + blk_rq_sectors(req) > get_capacity(req->rq_disk)) {
		cy_as_hal_print_message("cyasblkdev: bad request address\n");
		stat = BLKPREP_KILL;
	}

	return stat;
}

/*west bridge storage async api on_completed callback */
static void cyasblkdev_issuecallback(
	/* Handle to the device completing the storage operation */
	cy_as_device_handle handle,
	/* The media type completing the operation */
	cy_as_media_type type,
	/* The device completing the operation */
	uint32_t device,
	/* The unit completing the operation */
	uint32_t unit,
	/* The block number of the completed operation */
	uint32_t block_number,
	/* The type of operation */
	cy_as_oper_type op,
	/* The error status */
	cy_as_return_status_t status
	)
{
	int retry_cnt = 0;
	DBGPRN_FUNC_NAME;

	if (status != CY_AS_ERROR_SUCCESS) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
		  "%s: async r/w: op:%d failed with error %d at address %d\n",
			__func__, op, status, block_number);
		#endif
	}

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s calling blk_end_request from issue_callback "
		"req=0x%x, status=0x%x, nr_sectors=0x%x\n",
		__func__, (unsigned int) gl_bd->queue.req, status,
		(unsigned int) blk_rq_sectors(gl_bd->queue.req));
	#endif

	/* note: blk_end_request w/o __ prefix should
	 * not require spinlocks on the queue*/
	while (blk_end_request(gl_bd->queue.req,
	status, blk_rq_sectors(gl_bd->queue.req)*512)) {
		retry_cnt++;
	};

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s blkdev_callback: ended rq on %d sectors, "
		"with err:%d, n:%d times\n", __func__,
		(int)blk_rq_sectors(gl_bd->queue.req), status,
		retry_cnt
	);
	#endif

	spin_lock_irq(&gl_bd->lock);

	/*elevate next request, if there is one*/
	if (!blk_queue_plugged(gl_bd->queue.queue)) {
		/* queue is not plugged */
		gl_bd->queue.req = blk_fetch_request(gl_bd->queue.queue);
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s blkdev_callback: "
		"blk_fetch_request():%p\n",
			__func__, gl_bd->queue.req);
		#endif
	}

	if (gl_bd->queue.req) {
		spin_unlock_irq(&gl_bd->lock);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s blkdev_callback: about to "
		"call issue_fn:%p\n", __func__, gl_bd->queue.req);
		#endif

		gl_bd->queue.issue_fn(&gl_bd->queue, gl_bd->queue.req);
	} else {
		spin_unlock_irq(&gl_bd->lock);
	}
}

/* issue astoria blkdev request (issue_fn) */
static int cyasblkdev_blk_issue_rq(
					struct cyasblkdev_queue *bq,
					struct request *req
					)
{
	struct cyasblkdev_blk_data *bd = bq->data;
	int index = 0;
	int ret = CY_AS_ERROR_SUCCESS;
	uint32_t req_sector = 0;
	uint32_t req_nr_sectors = 0;
	int bus_num = 0;
	int lcl_unit_no = 0;

	DBGPRN_FUNC_NAME;

	/*
	 * will construct a scatterlist for the given request;
	 * the return value is the number of actually used
	 * entries in the resulting list. Then, this scatterlist
	 * can be used for the actual DMA prep operation.
	 */
	spin_lock_irq(&bd->lock);
	index = blk_rq_map_sg(bq->queue, req, bd->sg);

	if (req->rq_disk == bd->user_disk_0) {
		bus_num = bd->user_disk_0_bus_num;
		req_sector = blk_rq_pos(req) + gl_bd->user_disk_0_first_sector;
		req_nr_sectors = blk_rq_sectors(req);
		lcl_unit_no = gl_bd->user_disk_0_unit_no;

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: request made to disk 0 "
			"for sector=%d, num_sectors=%d, unit_no=%d\n",
			__func__, req_sector, (int) blk_rq_sectors(req),
			lcl_unit_no);
		#endif
	} else if (req->rq_disk == bd->user_disk_1) {
		bus_num = bd->user_disk_1_bus_num;
		req_sector = blk_rq_pos(req) + gl_bd->user_disk_1_first_sector;
		/*SECT_NUM_TRANSLATE(blk_rq_sectors(req));*/
		req_nr_sectors = blk_rq_sectors(req);
		lcl_unit_no = gl_bd->user_disk_1_unit_no;

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: request made to disk 1 for "
			"sector=%d, num_sectors=%d, unit_no=%d\n", __func__,
			req_sector, (int) blk_rq_sectors(req), lcl_unit_no);
		#endif
	} else if (req->rq_disk == bd->system_disk) {
		bus_num = bd->system_disk_bus_num;
		req_sector = blk_rq_pos(req) + gl_bd->system_disk_first_sector;
		req_nr_sectors = blk_rq_sectors(req);
		lcl_unit_no = gl_bd->system_disk_unit_no;

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: request made to system disk "
			"for sector=%d, num_sectors=%d, unit_no=%d\n", __func__,
			req_sector, (int) blk_rq_sectors(req), lcl_unit_no);
		#endif
	}
	#ifndef WESTBRIDGE_NDEBUG
	else {
		cy_as_hal_print_message(
			"%s: invalid disk used for request\n", __func__);
	}
	#endif

	spin_unlock_irq(&bd->lock);

	if (rq_data_dir(req) == READ) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: calling readasync() "
			"req_sector=0x%x, req_nr_sectors=0x%x, bd->sg:%x\n\n",
			__func__, req_sector, req_nr_sectors, (uint32_t)bd->sg);
		#endif

		ret = cy_as_storage_read_async(bd->dev_handle, bus_num, 0,
			lcl_unit_no, req_sector, bd->sg, req_nr_sectors,
			(cy_as_storage_callback)cyasblkdev_issuecallback);

		if (ret != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s:readasync() error %d at "
				"address %ld, unit no %d\n", __func__, ret,
				blk_rq_pos(req), lcl_unit_no);
			cy_as_hal_print_message("%s:ending i/o request "
				"on reg:%x\n", __func__, (uint32_t)req);
			#endif

			while (blk_end_request(req,
				(ret == CY_AS_ERROR_SUCCESS),
				req_nr_sectors*512))
				;

			bq->req = NULL;
		}
	} else {
		ret = cy_as_storage_write_async(bd->dev_handle, bus_num, 0,
			lcl_unit_no, req_sector, bd->sg, req_nr_sectors,
			(cy_as_storage_callback)cyasblkdev_issuecallback);

		if (ret != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: write failed with "
			"error %d at address %ld, unit no %d\n",
			__func__, ret, blk_rq_pos(req), lcl_unit_no);
			#endif

			/*end IO op on this request(does both
			 * end_that_request_... _first & _last) */
			while (blk_end_request(req,
				(ret == CY_AS_ERROR_SUCCESS),
				req_nr_sectors*512))
				;

			bq->req = NULL;
		}
	}

	return ret;
}

static unsigned long
dev_use[CYASBLKDEV_NUM_MINORS / (8 * sizeof(unsigned long))];


/* storage event callback (note: called in astoria isr context) */
static void cyasblkdev_storage_callback(
					cy_as_device_handle dev_h,
					cy_as_bus_number_t bus,
					uint32_t device,
					cy_as_storage_event evtype,
					void *evdata
					)
{
	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message("%s: bus:%d, device:%d, evtype:%d, "
	"evdata:%p\n ", __func__, bus, device, evtype, evdata);
	#endif

	switch (evtype) {
	case cy_as_storage_processor:
		break;

	case cy_as_storage_removed:
		break;

	case cy_as_storage_inserted:
		break;

	default:
		break;
	}
}

#define SECTORS_TO_SCAN 4096

uint32_t cyasblkdev_get_vfat_offset(int bus_num, int unit_no)
{
	/*
	 * for sd media, vfat partition boot record is not always
	 * located at sector it greatly depends on the system and
	 * software that was used to format the sd however, linux
	 * fs layer always expects it at sector 0, this function
	 * finds the offset and then uses it in all media r/w
	 * operations
	 */
	int sect_no, stat;
	uint8_t *sect_buf;
	bool br_found = false;

	DBGPRN_FUNC_NAME;

	sect_buf = kmalloc(1024, GFP_KERNEL);

	/* since HAL layer always uses sg lists instead of the
	 * buffer (for hw dmas) we need to initialize the sg list
	 * for local buffer*/
	sg_init_one(gl_bd->sg, sect_buf, 512);

	/*
	* Check MPR partition table 1st, then try to scan through
	* 1st 384 sectors until BR signature(intel JMP istruction
	* code and ,0x55AA) is found
	*/
	#ifndef WESTBRIDGE_NDEBUG
	  cy_as_hal_print_message(
		"%s scanning media for vfat partition...\n", __func__);
	#endif

	for (sect_no = 0; sect_no < SECTORS_TO_SCAN; sect_no++) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s before cyasstorageread "
			"gl_bd->sg addr=0x%x\n", __func__,
			(unsigned int) gl_bd->sg);
		#endif

		stat = cy_as_storage_read(
			/* Handle to the device of interest */
			gl_bd->dev_handle,
			/* The bus to access */
			bus_num,
			/* The device to access */
			0,
			/* The unit to access */
			unit_no,
			/* absolute sector number */
			sect_no,
			/* sg structure */
			gl_bd->sg,
			/* The number of blocks to be read */
			1
		);

		/* try only sectors with boot signature */
		if ((sect_buf[510] == 0x55) && (sect_buf[511] == 0xaa)) {
			/* vfat boot record may also be located at
			 * sector 0, check it first  */
			if (sect_buf[0] == 0xEB) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s vfat partition found "
					"at sector:%d\n",
					__func__, sect_no);
				#endif

				br_found = true;
				   break;
			}
		}

		if (stat != 0) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s sector scan error\n",
				__func__);
			#endif
			break;
		}
	}

	kfree(sect_buf);

	if (br_found) {
		return sect_no;
	} else {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s vfat partition is not found, using 0 offset\n",
			__func__);
		#endif
		return 0;
	}
}

cy_as_storage_query_device_data dev_data = {0};

static int cyasblkdev_add_disks(int bus_num,
	struct cyasblkdev_blk_data *bd,
	int total_media_count,
	int devidx)
{
	int ret = 0;
	uint64_t disk_cap;
	int lcl_unit_no;
	cy_as_storage_query_unit_data unit_data = {0};

	#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s:query device: "
		"type:%d, removable:%d, writable:%d, "
		"blksize %d, units:%d, locked:%d, "
		"erase_sz:%d\n",
		__func__,
		dev_data.desc_p.type,
		dev_data.desc_p.removable,
		dev_data.desc_p.writeable,
		dev_data.desc_p.block_size,
		dev_data.desc_p.number_units,
		dev_data.desc_p.locked,
		dev_data.desc_p.erase_unit_size
		);
	#endif

	/*  make sure that device is not locked  */
	if (dev_data.desc_p.locked) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: device is locked\n", __func__);
		#endif
		ret = cy_as_storage_release(
			bd->dev_handle, bus_num, 0, 0, 0);
		if (ret != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s cannot release"
				" storage\n", __func__);
			#endif
			goto out;
		}
		goto out;
	}

	unit_data.device = 0;
	unit_data.unit   = 0;
	unit_data.bus    = bus_num;
	ret = cy_as_storage_query_unit(bd->dev_handle,
		&unit_data, 0, 0);
	if (ret != CY_AS_ERROR_SUCCESS) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: cannot query "
			"%d device unit - reason code %d\n",
			__func__, bus_num, ret);
		#endif
		goto out;
	}

	if (private_partition_bus == bus_num) {
		if (private_partition_size > 0) {
			ret = cy_as_storage_create_p_partition(
				bd->dev_handle, bus_num, 0,
				private_partition_size, 0, 0);
			if ((ret != CY_AS_ERROR_SUCCESS) &&
			(ret != CY_AS_ERROR_ALREADY_PARTITIONED)) {
			#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message("%s: cy_as_storage_"
				"create_p_partition after size > 0 check "
				"failed with error code %d\n",
				__func__, ret);
			#endif

				disk_cap = (uint64_t)
					(unit_data.desc_p.unit_size);
				lcl_unit_no = 0;

			} else if (ret == CY_AS_ERROR_ALREADY_PARTITIONED) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
				"%s: cy_as_storage_create_p_partition "
				"indicates memory already partitioned\n",
				__func__);
				#endif

				/*check to see that partition
				 * matches size */
				if (unit_data.desc_p.unit_size !=
					private_partition_size) {
					ret = cy_as_storage_remove_p_partition(
						bd->dev_handle,
						bus_num, 0, 0, 0);
					if (ret == CY_AS_ERROR_SUCCESS) {
						ret = cy_as_storage_create_p_partition(
							bd->dev_handle, bus_num, 0,
							private_partition_size, 0, 0);
						if (ret == CY_AS_ERROR_SUCCESS) {
							unit_data.bus = bus_num;
							unit_data.device = 0;
							unit_data.unit = 1;
						} else {
							#ifndef WESTBRIDGE_NDEBUG
							cy_as_hal_print_message(
							"%s: cy_as_storage_create_p_partition "
							"after removal unexpectedly failed "
							"with error %d\n", __func__, ret);
							#endif

							/* need to requery bus
							 * seeing as delete
							 * successful and create
							 * failed we have changed
							 * the disk properties */
							unit_data.bus	= bus_num;
							unit_data.device = 0;
							unit_data.unit   = 0;
						}

						ret = cy_as_storage_query_unit(
						bd->dev_handle,
						&unit_data, 0, 0);
						if (ret != CY_AS_ERROR_SUCCESS) {
							#ifndef WESTBRIDGE_NDEBUG
							cy_as_hal_print_message(
							"%s: cannot query %d "
							"device unit - reason code %d\n",
							__func__, bus_num, ret);
							#endif
							goto out;
						} else {
							disk_cap = (uint64_t)
								(unit_data.desc_p.unit_size);
							lcl_unit_no =
								unit_data.unit;
						}
					} else {
					#ifndef WESTBRIDGE_NDEBUG
					cy_as_hal_print_message(
					"%s: cy_as_storage_remove_p_partition "
					"failed with error %d\n",
					__func__, ret);
					#endif

						unit_data.bus = bus_num;
						unit_data.device = 0;
						unit_data.unit = 1;

						ret = cy_as_storage_query_unit(
							bd->dev_handle, &unit_data, 0, 0);
						if (ret != CY_AS_ERROR_SUCCESS) {
						#ifndef WESTBRIDGE_NDEBUG
							cy_as_hal_print_message(
							"%s: cannot query %d "
							"device unit - reason "
							"code %d\n", __func__,
							bus_num, ret);
						#endif
							goto out;
						}

						disk_cap = (uint64_t)
							(unit_data.desc_p.unit_size);
						lcl_unit_no =
							unit_data.unit;
					}
				} else {
					#ifndef WESTBRIDGE_NDEBUG
					cy_as_hal_print_message("%s: partition "
						"exists and sizes equal\n",
						__func__);
					#endif

					/*partition already existed,
					 * need to query second unit*/
					unit_data.bus = bus_num;
					unit_data.device = 0;
					unit_data.unit = 1;

					ret = cy_as_storage_query_unit(
						bd->dev_handle, &unit_data, 0, 0);
					if (ret != CY_AS_ERROR_SUCCESS) {
					#ifndef WESTBRIDGE_NDEBUG
						cy_as_hal_print_message(
							"%s: cannot query %d "
							"device unit "
							"- reason code %d\n",
							__func__, bus_num, ret);
					#endif
						goto out;
					} else {
						disk_cap = (uint64_t)
						(unit_data.desc_p.unit_size);
						lcl_unit_no = unit_data.unit;
					}
				}
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
				"%s: cy_as_storage_create_p_partition "
				"created successfully\n", __func__);
				#endif

				disk_cap = (uint64_t)
				(unit_data.desc_p.unit_size -
				private_partition_size);

				lcl_unit_no = 1;
			}
		}
		#ifndef WESTBRIDGE_NDEBUG
		else {
			cy_as_hal_print_message(
			"%s: invalid partition_size%d\n", __func__,
			private_partition_size);

			disk_cap = (uint64_t)
				(unit_data.desc_p.unit_size);
			lcl_unit_no = 0;
		}
		#endif
	} else {
		disk_cap = (uint64_t)
			(unit_data.desc_p.unit_size);
		lcl_unit_no = 0;
	}

	if ((bus_num == 0) ||
		(total_media_count == 1)) {
		sprintf(bd->user_disk_0->disk_name,
			"cyasblkdevblk%d", devidx);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: disk unit_sz:%lu blk_sz:%d, "
			"start_blk:%lu, capacity:%llu\n",
			__func__, (unsigned long)
			unit_data.desc_p.unit_size,
			unit_data.desc_p.block_size,
			(unsigned long)
			unit_data.desc_p.start_block,
			(uint64_t)disk_cap
		);
		#endif

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: setting gendisk disk "
			"capacity to %d\n", __func__, (int) disk_cap);
		#endif

		/* initializing bd->queue */
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: init bd->queue\n",
			__func__);
		#endif

		/* this will create a
		 * queue kernel thread */
		cyasblkdev_init_queue(
			&bd->queue, &bd->lock);

		bd->queue.prep_fn = cyasblkdev_blk_prep_rq;
		bd->queue.issue_fn = cyasblkdev_blk_issue_rq;
		bd->queue.data = bd;

		/*blk_size should always
		 * be a multiple of 512,
		 * set to the max to ensure
		 * that all accesses aligned
		 * to the greatest multiple,
		 * can adjust request to
		 * smaller block sizes
		 * dynamically*/

		bd->user_disk_0_read_only = !dev_data.desc_p.writeable;
		bd->user_disk_0_blk_size = dev_data.desc_p.block_size;
		bd->user_disk_0_type = dev_data.desc_p.type;
		bd->user_disk_0_bus_num = bus_num;
		bd->user_disk_0->major = major;
		bd->user_disk_0->first_minor = devidx << CYASBLKDEV_SHIFT;
		bd->user_disk_0->minors = 8;
		bd->user_disk_0->fops = &cyasblkdev_bdops;
		bd->user_disk_0->private_data = bd;
		bd->user_disk_0->queue = bd->queue.queue;
		bd->dbgprn_flags = DBGPRN_RD_RQ;
		bd->user_disk_0_unit_no = lcl_unit_no;

		blk_queue_logical_block_size(bd->queue.queue,
			bd->user_disk_0_blk_size);

		set_capacity(bd->user_disk_0,
			disk_cap);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: returned from set_capacity %d\n",
			__func__, (int) disk_cap);
		#endif

		/* need to start search from
		 * public partition beginning */
		if (vfat_search) {
			bd->user_disk_0_first_sector =
				cyasblkdev_get_vfat_offset(
					bd->user_disk_0_bus_num,
					bd->user_disk_0_unit_no);
		} else {
			bd->user_disk_0_first_sector = 0;
		}

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: set user_disk_0_first "
			"sector to %d\n", __func__,
			 bd->user_disk_0_first_sector);
		cy_as_hal_print_message(
			"%s: add_disk: disk->major=0x%x\n",
			__func__,
			bd->user_disk_0->major);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->first_minor=0x%x\n", __func__,
			bd->user_disk_0->first_minor);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->minors=0x%x\n", __func__,
			bd->user_disk_0->minors);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->disk_name=%s\n",
			__func__,
			bd->user_disk_0->disk_name);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->part_tbl=0x%x\n", __func__,
			(unsigned int)
			bd->user_disk_0->part_tbl);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->queue=0x%x\n", __func__,
			(unsigned int)
			bd->user_disk_0->queue);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->flags=0x%x\n",
			__func__, (unsigned int)
			bd->user_disk_0->flags);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->driverfs_dev=0x%x\n",
			__func__, (unsigned int)
			bd->user_disk_0->driverfs_dev);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->slave_dir=0x%x\n",
			__func__, (unsigned int)
			bd->user_disk_0->slave_dir);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->random=0x%x\n",
			__func__, (unsigned int)
			bd->user_disk_0->random);
		cy_as_hal_print_message(
			"%s: add_disk: "
			"disk->node_id=0x%x\n",
			__func__, (unsigned int)
			bd->user_disk_0->node_id);

		#endif

		add_disk(bd->user_disk_0);

	} else if ((bus_num == 1) &&
		(total_media_count == 2)) {
		bd->user_disk_1_read_only = !dev_data.desc_p.writeable;
		bd->user_disk_1_blk_size = dev_data.desc_p.block_size;
		bd->user_disk_1_type = dev_data.desc_p.type;
		bd->user_disk_1_bus_num = bus_num;
		bd->user_disk_1->major	= major;
		bd->user_disk_1->first_minor = (devidx + 1) << CYASBLKDEV_SHIFT;
		bd->user_disk_1->minors = 8;
		bd->user_disk_1->fops = &cyasblkdev_bdops;
		bd->user_disk_1->private_data = bd;
		bd->user_disk_1->queue = bd->queue.queue;
		bd->dbgprn_flags = DBGPRN_RD_RQ;
		bd->user_disk_1_unit_no = lcl_unit_no;

		sprintf(bd->user_disk_1->disk_name,
			"cyasblkdevblk%d", (devidx + 1));

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: disk unit_sz:%lu "
			"blk_sz:%d, "
			"start_blk:%lu, "
			"capacity:%llu\n",
			__func__,
			(unsigned long)
			unit_data.desc_p.unit_size,
			unit_data.desc_p.block_size,
			(unsigned long)
			unit_data.desc_p.start_block,
			(uint64_t)disk_cap
		);
		#endif

		/*blk_size should always be a
		 * multiple of 512, set to the max
		 * to ensure that all accesses
		 * aligned to the greatest multiple,
		 * can adjust request to smaller
		 * block sizes dynamically*/
		if (bd->user_disk_0_blk_size >
		bd->user_disk_1_blk_size) {
			blk_queue_logical_block_size(bd->queue.queue,
				bd->user_disk_0_blk_size);
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
			"%s: set hard sect_sz:%d\n",
			__func__,
			bd->user_disk_0_blk_size);
			#endif
		} else {
			blk_queue_logical_block_size(bd->queue.queue,
				bd->user_disk_1_blk_size);
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
			"%s: set hard sect_sz:%d\n",
			__func__,
			bd->user_disk_1_blk_size);
			#endif
		}

		set_capacity(bd->user_disk_1, disk_cap);
		if (vfat_search) {
			bd->user_disk_1_first_sector =
				cyasblkdev_get_vfat_offset(
					bd->user_disk_1_bus_num,
					bd->user_disk_1_unit_no);
		} else {
			bd->user_disk_1_first_sector
				= 0;
		}

		add_disk(bd->user_disk_1);
	}

	if (lcl_unit_no > 0) {
		if (bd->system_disk == NULL) {
			bd->system_disk =
				alloc_disk(8);

			if (bd->system_disk == NULL) {
				kfree(bd);
				bd = ERR_PTR(-ENOMEM);
				return bd;
			}
			disk_cap = (uint64_t)
				(private_partition_size);

			/* set properties of
			 * system disk */
			bd->system_disk_read_only = !dev_data.desc_p.writeable;
			bd->system_disk_blk_size = dev_data.desc_p.block_size;
			bd->system_disk_bus_num = bus_num;
			bd->system_disk->major = major;
			bd->system_disk->first_minor =
				(devidx + 2) << CYASBLKDEV_SHIFT;
			bd->system_disk->minors = 8;
			bd->system_disk->fops = &cyasblkdev_bdops;
			bd->system_disk->private_data = bd;
			bd->system_disk->queue = bd->queue.queue;
			/* don't search for vfat
			 * with system disk */
			bd->system_disk_first_sector = 0;
			sprintf(
				bd->system_disk->disk_name,
				"cyasblkdevblk%d", (devidx + 2));

			set_capacity(bd->system_disk,
				disk_cap);

			add_disk(bd->system_disk);
		}
		#ifndef WESTBRIDGE_NDEBUG
		else {
			cy_as_hal_print_message(
				"%s: system disk already allocated %d\n",
				__func__, bus_num);
		}
		#endif
	}
out:
	return ret;
}

static struct cyasblkdev_blk_data *cyasblkdev_blk_alloc(void)
{
	struct cyasblkdev_blk_data *bd;
	int ret = 0;
	cy_as_return_status_t stat = -1;
	int bus_num = 0;
	int total_media_count = 0;
	int devidx = 0;
	DBGPRN_FUNC_NAME;

	total_media_count = 0;
	devidx = find_first_zero_bit(dev_use, CYASBLKDEV_NUM_MINORS);
	if (devidx >= CYASBLKDEV_NUM_MINORS)
		return ERR_PTR(-ENOSPC);

	__set_bit(devidx, dev_use);
	__set_bit(devidx + 1, dev_use);

	bd = kzalloc(sizeof(struct cyasblkdev_blk_data), GFP_KERNEL);
	if (bd) {
		gl_bd = bd;

		spin_lock_init(&bd->lock);
		bd->usage = 1;

		/* setup the block_dev_ops pointer*/
		bd->blkops = &cyasblkdev_bdops;

		/* Get the device handle */
		bd->dev_handle = cyasdevice_getdevhandle();
		if (0 == bd->dev_handle) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: get device failed\n", __func__);
			#endif
			ret = ENODEV;
			goto out;
		}

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s west bridge device handle:%x\n",
			__func__, (uint32_t)bd->dev_handle);
		#endif

		/* start the storage api and get a handle to the
		 * device we are interested in. */

		/* Error code to use if the conditions are not satisfied. */
		ret = ENOMEDIUM;

		stat = cy_as_misc_release_resource(bd->dev_handle, cy_as_bus_0);
		if ((stat != CY_AS_ERROR_SUCCESS) &&
		(stat != CY_AS_ERROR_RESOURCE_NOT_OWNED)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot release "
				"resource bus 0 - reason code %d\n",
				__func__, stat);
			#endif
		}

		stat = cy_as_misc_release_resource(bd->dev_handle, cy_as_bus_1);
		if ((stat != CY_AS_ERROR_SUCCESS) &&
		(stat != CY_AS_ERROR_RESOURCE_NOT_OWNED)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot release "
				"resource bus 0 - reason code %d\n",
				__func__, stat);
			#endif
		}

		/* start storage stack*/
		stat = cy_as_storage_start(bd->dev_handle, 0, 0x101);
		if (stat != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot start storage "
				"stack - reason code %d\n", __func__, stat);
			#endif
			goto out;
		}

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: storage started:%d ok\n",
			__func__, stat);
		#endif

		stat = cy_as_storage_register_callback(bd->dev_handle,
			cyasblkdev_storage_callback);
		if (stat != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot register callback "
				"- reason code %d\n", __func__, stat);
			#endif
			goto out;
		}

		for (bus_num = 0; bus_num < 2; bus_num++) {
			stat = cy_as_storage_query_bus(bd->dev_handle,
				bus_num, &bd->media_count[bus_num],  0, 0);
			if (stat == CY_AS_ERROR_SUCCESS) {
				total_media_count = total_media_count +
					bd->media_count[bus_num];
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message("%s: cannot query %d, "
					"reason code: %d\n",
					__func__, bus_num, stat);
				#endif
				goto out;
			}
		}

		if (total_media_count == 0) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: no storage media was found\n", __func__);
			#endif
			goto out;
		} else if (total_media_count >= 1) {
			if (bd->user_disk_0 == NULL) {

				bd->user_disk_0 =
					alloc_disk(8);
				if (bd->user_disk_0 == NULL) {
					kfree(bd);
					bd = ERR_PTR(-ENOMEM);
					return bd;
				}
			}
			#ifndef WESTBRIDGE_NDEBUG
			else {
				cy_as_hal_print_message("%s: no available "
					"gen_disk for disk 0, "
					"physically inconsistent\n", __func__);
			}
			#endif
		}

		if (total_media_count == 2) {
			if (bd->user_disk_1 == NULL) {
				bd->user_disk_1 =
					alloc_disk(8);
				if (bd->user_disk_1 == NULL) {
					kfree(bd);
					bd = ERR_PTR(-ENOMEM);
					return bd;
				}
			}
			#ifndef WESTBRIDGE_NDEBUG
			else {
				cy_as_hal_print_message("%s: no available "
					"gen_disk for media, "
					"physically inconsistent\n", __func__);
			}
			#endif
		}
		#ifndef WESTBRIDGE_NDEBUG
		else if (total_media_count > 2) {
			cy_as_hal_print_message("%s: count corrupted = 0x%d\n",
				__func__, total_media_count);
		}
		#endif

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: %d device(s) found\n",
			__func__, total_media_count);
		#endif

		for (bus_num = 0; bus_num <= 1; bus_num++) {
			/*claim storage for cpu */
			stat = cy_as_storage_claim(bd->dev_handle,
				bus_num, 0, 0, 0);
			if (stat != CY_AS_ERROR_SUCCESS) {
				cy_as_hal_print_message("%s: cannot claim "
					"%d bus - reason code %d\n",
					__func__, bus_num, stat);
				goto out;
			}

			dev_data.bus = bus_num;
			dev_data.device = 0;

			stat = cy_as_storage_query_device(bd->dev_handle,
				&dev_data, 0, 0);
			if (stat == CY_AS_ERROR_SUCCESS) {
				cyasblkdev_add_disks(bus_num, bd,
					total_media_count, devidx);
			} else if (stat == CY_AS_ERROR_NO_SUCH_DEVICE) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: no device on bus %d\n",
					__func__, bus_num);
				#endif
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: cannot query %d device "
					"- reason code %d\n",
					__func__, bus_num, stat);
				#endif
				goto out;
			}
		} /* end for (bus_num = 0; bus_num <= 1; bus_num++)*/

		return bd;
	}
out:
	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s: bd failed to initialize\n", __func__);
	#endif

	kfree(bd);
	bd = ERR_PTR(-ret);
	return bd;
}


/*init west bridge block device */
static int cyasblkdev_blk_initialize(void)
{
	struct cyasblkdev_blk_data *bd;
	int res;

	DBGPRN_FUNC_NAME;

	res = register_blkdev(major, "cyasblkdev");

	if (res < 0) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(KERN_WARNING
			"%s unable to get major %d for cyasblkdev media: %d\n",
			__func__, major, res);
		#endif
		return res;
	}

	if (major == 0)
		major = res;

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s cyasblkdev registered with major number: %d\n",
		__func__, major);
	#endif

	bd = cyasblkdev_blk_alloc();
	if (IS_ERR(bd))
		return PTR_ERR(bd);

	return 0;
}

/* start block device */
static int __init cyasblkdev_blk_init(void)
{
	int res = -ENOMEM;

	DBGPRN_FUNC_NAME;

	/* get the cyasdev handle for future use*/
	cyas_dev_handle = cyasdevice_getdevhandle();

	if  (cyasblkdev_blk_initialize() == 0)
		return 0;

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message("cyasblkdev init error:%d\n", res);
	#endif
	return res;
}


static void cyasblkdev_blk_deinit(struct cyasblkdev_blk_data *bd)
{
	DBGPRN_FUNC_NAME;

	if (bd) {
		int devidx;

		if (bd->user_disk_0 != NULL) {
			del_gendisk(bd->user_disk_0);
			devidx = bd->user_disk_0->first_minor
				>> CYASBLKDEV_SHIFT;
			__clear_bit(devidx, dev_use);
		}

		if (bd->user_disk_1 != NULL) {
			del_gendisk(bd->user_disk_1);
			devidx = bd->user_disk_1->first_minor
				>> CYASBLKDEV_SHIFT;
			__clear_bit(devidx, dev_use);
		}

		if (bd->system_disk != NULL) {
			del_gendisk(bd->system_disk);
			devidx = bd->system_disk->first_minor
				>> CYASBLKDEV_SHIFT;
			__clear_bit(devidx, dev_use);
		}

		cyasblkdev_blk_put(bd);
	}
}

/* block device exit */
static void __exit cyasblkdev_blk_exit(void)
{
	DBGPRN_FUNC_NAME;

	cyasblkdev_blk_deinit(gl_bd);
	unregister_blkdev(major, "cyasblkdev");

}

module_init(cyasblkdev_blk_init);
module_exit(cyasblkdev_blk_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("antioch (cyasblkdev) block device driver");
MODULE_AUTHOR("cypress semiconductor");

/*[]*/