smscoreapi.c 30.8 KB
Newer Older
1
/*
2 3 4 5 6
 *  Siano core API module
 *
 *  This file contains implementation for the interface to sms core component
 *
 *  author: Anatoly Greenblat
7
 *
8
 *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
9 10
 *
 *  This program is free software; you can redistribute it and/or modify
11 12
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation;
13
 *
14 15
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
16
 *
17
 *  See the GNU General Public License for more details.
18 19 20 21 22 23
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

24 25 26 27 28 29 30 31 32 33 34 35
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <asm/io.h>

#include <linux/firmware.h>

#include "smscoreapi.h"

36 37 38 39
int sms_debug;
module_param_named(debug, sms_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

40
struct smscore_device_notifyee_t {
41 42
	struct list_head entry;
	hotplug_t hotplug;
43
};
44

45
struct smscore_idlist_t {
46 47 48
	struct list_head entry;
	int		id;
	int		data_type;
49
};
50

51
struct smscore_client_t {
52
	struct list_head entry;
53
	struct smscore_device_t *coredev;
54
	void			*context;
55
	struct list_head 	idlist;
56 57
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
58
};
59

60
struct smscore_device_t {
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
	struct list_head entry;

	struct list_head clients;
	struct list_head subclients;
	spinlock_t		clientslock;

	struct list_head buffers;
	spinlock_t		bufferslock;
	int				num_buffers;

	void			*common_buffer;
	int				common_buffer_size;
	dma_addr_t		common_buffer_phys;

	void			*context;
	struct device	*device;

	char			devpath[32];
	unsigned long	device_flags;

	setmode_t		setmode_handler;
	detectmode_t	detectmode_handler;
	sendrequest_t	sendrequest_handler;
	preload_t		preload_handler;
	postload_t		postload_handler;

	int				mode, modes_supported;

	struct completion version_ex_done, data_download_done, trigger_done;
	struct completion init_device_done, reload_start_done, resume_done;
91 92

	int board_id;
93
};
94

95 96 97 98 99 100 101 102 103 104
void smscore_set_board_id(struct smscore_device_t *core, int id)
{
	core->board_id = id;
}

int smscore_get_board_id(struct smscore_device_t *core)
{
	return core->board_id;
}

105
struct smscore_registry_entry_t {
106 107 108
	struct list_head entry;
	char			devpath[32];
	int				mode;
109 110
	enum sms_device_type_st	type;
};
111 112 113 114 115 116 117 118

struct list_head g_smscore_notifyees;
struct list_head g_smscore_devices;
kmutex_t g_smscore_deviceslock;

struct list_head g_smscore_registry;
kmutex_t g_smscore_registrylock;

119
static int default_mode = 4;
120

121 122 123
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

124
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
125
{
126
	struct smscore_registry_entry_t *entry;
127 128 129
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
130 131 132
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
133
		entry = (struct smscore_registry_entry_t *) next;
134
		if (!strcmp(entry->devpath, devpath)) {
135
			kmutex_unlock(&g_smscore_registrylock);
136
			return entry;
137 138
		}
	}
139 140 141
	entry = (struct smscore_registry_entry_t *)
			kmalloc(sizeof(struct smscore_registry_entry_t),
				GFP_KERNEL);
142
	if (entry) {
143 144 145
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
146
	} else
147
		sms_err("failed to create smscore_registry.");
148
	kmutex_unlock(&g_smscore_registrylock);
149 150
	return entry;
}
151

152
int smscore_registry_getmode(char *devpath)
153
{
154
	struct smscore_registry_entry_t *entry;
155

156 157
	entry = smscore_find_registry(devpath);
	if (entry)
158 159
		return entry->mode;
	else
160
		sms_err("No registry found.");
161

162 163 164
	return default_mode;
}

165
static enum sms_device_type_st smscore_registry_gettype(char *devpath)
166
{
167
	struct smscore_registry_entry_t *entry;
168

169 170
	entry = smscore_find_registry(devpath);
	if (entry)
171 172
		return entry->type;
	else
173
		sms_err("No registry found.");
174

175 176
	return -1;
}
177

178 179
void smscore_registry_setmode(char *devpath, int mode)
{
180
	struct smscore_registry_entry_t *entry;
181

182 183 184
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
185
	else
186
		sms_err("No registry found.");
187
}
188

189 190
static void smscore_registry_settype(char *devpath,
				     enum sms_device_type_st type)
191
{
192
	struct smscore_registry_entry_t *entry;
193

194 195
	entry = smscore_find_registry(devpath);
	if (entry)
196 197
		entry->type = type;
	else
198
		sms_err("No registry found.");
199 200 201
}


202 203
static void list_add_locked(struct list_head *new, struct list_head *head,
			    spinlock_t *lock)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
	unsigned long flags;

	spin_lock_irqsave(lock, flags);

	list_add(new, head);

	spin_unlock_irqrestore(lock, flags);
}

/**
 * register a client callback that called when device plugged in/unplugged
 * NOTE: if devices exist callback is called immediately for each device
 *
 * @param hotplug callback
 *
 * @return 0 on success, <0 on error.
 */
int smscore_register_hotplug(hotplug_t hotplug)
{
224
	struct smscore_device_notifyee_t *notifyee;
225 226 227 228 229
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

230 231
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
232 233
	if (notifyee) {
		/* now notify callback about existing devices */
234
		first = &g_smscore_devices;
235 236 237
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
238 239
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
240 241 242
			rc = hotplug(coredev, coredev->device, 1);
		}

243
		if (rc >= 0) {
244 245
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
246
		} else
247
			kfree(notifyee);
248
	} else
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		rc = -ENOMEM;

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

/**
 * unregister a client callback that called when device plugged in/unplugged
 *
 * @param hotplug callback
 *
 */
void smscore_unregister_hotplug(hotplug_t hotplug)
{
	struct list_head *next, *first;

	kmutex_lock(&g_smscore_deviceslock);

	first = &g_smscore_notifyees;

270
	for (next = first->next; next != first;) {
271 272
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
273 274
		next = next->next;

275
		if (notifyee->hotplug == hotplug) {
276 277 278 279 280 281 282 283
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

284
static void smscore_notify_clients(struct smscore_device_t *coredev)
285
{
286
	struct smscore_client_t *client;
287

288 289
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
290
		client = (struct smscore_client_t *) coredev->clients.next;
291 292 293 294
		client->onremove_handler(client->context);
	}
}

295 296
static int smscore_notify_callbacks(struct smscore_device_t *coredev,
				    struct device *device, int arrival)
297 298 299 300
{
	struct list_head *next, *first;
	int rc = 0;

301
	/* note: must be called under g_deviceslock */
302 303 304

	first = &g_smscore_notifyees;

305
	for (next = first->next; next != first; next = next->next) {
306
		rc = ((struct smscore_device_notifyee_t *) next)->
307
				hotplug(coredev, device, arrival);
308 309 310 311 312 313 314
		if (rc < 0)
			break;
	}

	return rc;
}

315 316
static struct
smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
317
				       dma_addr_t common_buffer_phys)
318
{
319 320
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
321
	if (!cb) {
322
		sms_info("kmalloc(...) failed");
323 324 325 326
		return NULL;
	}

	cb->p = buffer;
327
	cb->offset_in_common = buffer - (u8 *) common_buffer;
328 329 330 331 332 333
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
334 335
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
336
 *
337 338
 * @param params device pointer to struct with device specific parameters
 *               and handlers
339 340 341 342
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
343 344
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
345
{
346
	struct smscore_device_t *dev;
347 348
	u8 *buffer;

349
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
350
	if (!dev) {
351
		sms_info("kzalloc(...) failed");
352 353 354
		return -ENOMEM;
	}

355
	/* init list entry so it could be safe in smscore_unregister_device */
356 357
	INIT_LIST_HEAD(&dev->entry);

358
	/* init queues */
359 360 361
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

362
	/* init locks */
363 364 365
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

366
	/* init completion events */
367 368 369 370 371 372 373
	init_completion(&dev->version_ex_done);
	init_completion(&dev->data_download_done);
	init_completion(&dev->trigger_done);
	init_completion(&dev->init_device_done);
	init_completion(&dev->reload_start_done);
	init_completion(&dev->resume_done);

374
	/* alloc common buffer */
375
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
376 377 378 379
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
380 381 382 383
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

384 385 386
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
387
	     dev->num_buffers++, buffer += params->buffer_size) {
388
		struct smscore_buffer_t *cb =
389 390
			smscore_createbuffer(buffer, dev->common_buffer,
					     dev->common_buffer_phys);
391
		if (!cb) {
392 393 394 395 396 397 398
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

399
	sms_info("allocated %d buffers", dev->num_buffers);
400 401 402 403 404 405 406 407 408 409 410 411 412

	dev->mode = DEVICE_MODE_NONE;
	dev->context = params->context;
	dev->device = params->device;
	dev->setmode_handler = params->setmode_handler;
	dev->detectmode_handler = params->detectmode_handler;
	dev->sendrequest_handler = params->sendrequest_handler;
	dev->preload_handler = params->preload_handler;
	dev->postload_handler = params->postload_handler;

	dev->device_flags = params->flags;
	strcpy(dev->devpath, params->devpath);

413
	smscore_registry_settype(dev->devpath, params->device_type);
414

415
	/* add device to devices list */
416 417 418 419 420 421
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

422
	sms_info("device %p created", dev);
423 424 425 426 427 428 429

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
430 431
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
432 433 434
 *
 * @return 0 on success, <0 on error.
 */
435
int smscore_start_device(struct smscore_device_t *coredev)
436
{
437 438
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
439
	if (rc < 0) {
440
		sms_info("set device mode faile , rc %d", rc);
441
		return rc;
442
	}
443 444 445 446 447

	kmutex_lock(&g_smscore_deviceslock);

	rc = smscore_notify_callbacks(coredev, coredev->device, 1);

448
	sms_info("device %p started, rc %d", coredev, rc);
449 450 451 452 453 454

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

455 456 457
static int smscore_sendrequest_and_wait(struct smscore_device_t *coredev,
					void *buffer, size_t size,
					struct completion *completion)
458 459
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
460
	if (rc < 0) {
461
		sms_info("sendrequest returned error %d", rc);
462
		return rc;
463
	}
464

465 466 467
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(10000)) ?
						0 : -ETIME;
468 469
}

470 471
static int smscore_load_firmware_family2(struct smscore_device_t *coredev,
					 void *buffer, size_t size)
472
{
473 474
	struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
	struct SmsMsgHdr_ST *msg;
475
	u32 mem_address = firmware->StartAddress;
476
	u8 *payload = firmware->Payload;
477 478
	int rc = 0;

479 480
	sms_info("loading FW to addr 0x%x size %d",
		 mem_address, firmware->Length);
481
	if (coredev->preload_handler) {
482 483 484 485 486
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

487
	/* PAGE_SIZE buffer shall be enough and dma aligned */
488
	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
489 490 491
	if (!msg)
		return -ENOMEM;

492
	if (coredev->mode != DEVICE_MODE_NONE) {
493
		sms_debug("sending reload command.");
494
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
495
			     sizeof(struct SmsMsgHdr_ST));
496 497 498
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
499
		mem_address = *(u32 *) &payload[20];
500 501
	}

502
	while (size && rc >= 0) {
503 504
		struct SmsDataDownload_ST *DataMsg =
			(struct SmsDataDownload_ST *) msg;
505 506
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

507
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
508
			     (u16)(sizeof(struct SmsMsgHdr_ST) +
509
				      sizeof(u32) + payload_size));
510 511 512 513

		DataMsg->MemAddr = mem_address;
		memcpy(DataMsg->Payload, payload, payload_size);

514 515
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
516 517 518
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
519
		else
520 521 522 523
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
524 525 526 527 528 529

		payload += payload_size;
		size -= payload_size;
		mem_address += payload_size;
	}

530 531
	if (rc >= 0) {
		if (coredev->mode == DEVICE_MODE_NONE) {
532 533
			struct SmsMsgData_ST *TriggerMsg =
				(struct SmsMsgData_ST *) msg;
534

535
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
536
				     sizeof(struct SmsMsgHdr_ST) +
537
				     sizeof(u32) * 5);
538

539 540
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
541 542 543 544
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
545

546
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
547 548 549
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
550
				msleep(100);
551
			} else
552 553 554 555
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
556 557
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
558
				     sizeof(struct SmsMsgHdr_ST));
559

560 561
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
562
		}
563
		msleep(500);
564 565
	}

566
	sms_debug("rc=%d, postload=%p ", rc,
567
		  coredev->postload_handler);
568 569 570

	kfree(msg);

571
	return ((rc >= 0) && coredev->postload_handler) ?
572 573 574 575 576 577 578
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
579 580
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
581 582 583 584 585
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
586 587 588
static int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
					   char *filename,
					   loadfirmware_t loadfirmware_handler)
589 590 591
{
	int rc = -ENOENT;
	const struct firmware *fw;
592
	u8 *fw_buffer;
593

594 595
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
596 597 598
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
599
	if (rc < 0) {
600
		sms_info("failed to open \"%s\"", filename);
601 602
		return rc;
	}
603
	sms_info("read FW %s, size=%d\"", filename, fw->size);
604 605 606
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
607 608 609
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
610 611 612 613 614
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
615 616

		kfree(fw_buffer);
617
	} else {
618
		sms_info("failed to allocate firmware buffer");
619 620 621 622 623 624 625 626 627
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

/**
628 629
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
630
 *
631 632
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
633 634 635
 *
 * @return 0 on success, <0 on error.
 */
636
void smscore_unregister_device(struct smscore_device_t *coredev)
637
{
638
	struct smscore_buffer_t *cb;
639
	int num_buffers = 0;
640
	int retry = 0;
641 642 643 644 645 646

	kmutex_lock(&g_smscore_deviceslock);

	smscore_notify_clients(coredev);
	smscore_notify_callbacks(coredev, NULL, 0);

647 648
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
649

650 651
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
652
			kfree(cb);
653
			num_buffers++;
654 655 656
		}
		if (num_buffers == coredev->num_buffers)
			break;
657
		if (++retry > 10) {
658 659
			sms_info("exiting although "
				 "not all buffers released.");
660 661
			break;
		}
662

663
		sms_info("waiting for %d buffer(s)",
664
			 coredev->num_buffers - num_buffers);
665 666 667
		msleep(100);
	}

668
	sms_info("freed %d buffers", num_buffers);
669 670

	if (coredev->common_buffer)
671 672 673
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
674 675 676 677 678 679

	list_del(&coredev->entry);
	kfree(coredev);

	kmutex_unlock(&g_smscore_deviceslock);

680
	sms_info("device %p destroyed", coredev);
681 682
}

683
static int smscore_detect_mode(struct smscore_device_t *coredev)
684
{
685
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
686
			       GFP_KERNEL | GFP_DMA);
687 688
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
689 690 691 692 693
	int rc;

	if (!buffer)
		return -ENOMEM;

694 695
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
696

697 698 699
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
700
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
701

702 703
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
704 705 706
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
707
			if (rc < 0)
708 709
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d", rc);
710
		} else
711 712 713 714 715 716 717 718
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

719
static char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	/*Stellar		NOVA A0		Nova B0		VEGA*/
	/*DVBT*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*DVBH*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*TDMB*/
	{"none", "tdmb_nova_12mhz.inp", "none", "none"},
	/*DABIP*/
	{"none", "none", "none", "none"},
	/*BDA*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*ISDBT*/
	{"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
	/*ISDBTBDA*/
	{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
	/*CMMB*/
	{"none", "none", "none", "cmmb_vega_12mhz.inp"}
737 738
};

739

740 741 742 743
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
744 745
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
746 747 748 749
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
750
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
751 752 753
{
	void *buffer;
	int rc = 0;
754
	enum sms_device_type_st type;
755

756
	sms_debug("set device mode to %d", mode);
757 758
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
759
			sms_err("invalid mode specified %d", mode);
760 761 762
			return -EINVAL;
		}

763 764
		smscore_registry_setmode(coredev->devpath, mode);

765
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
766
			rc = smscore_detect_mode(coredev);
767
			if (rc < 0) {
768
				sms_err("mode detect failed %d", rc);
769
				return rc;
770
			}
771
		}
772

773
		if (coredev->mode == mode) {
774
			sms_info("device mode %d already set", mode);
775 776 777
			return 0;
		}

778 779
		if (!(coredev->modes_supported & (1 << mode))) {
			type = smscore_registry_gettype(coredev->devpath);
780 781
			rc = smscore_load_firmware_from_file(
				coredev, smscore_fw_lkup[mode][type], NULL);
782
			if (rc < 0) {
783
				sms_err("load firmware failed %d", rc);
784
				return rc;
785 786
			}
		} else
787 788
			sms_info("mode %d supported by running "
				 "firmware", mode);
789

790 791
		buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
792
		if (buffer) {
793 794 795
			struct SmsMsgData_ST *msg =
				(struct SmsMsgData_ST *)
					SMS_ALIGN_ADDRESS(buffer);
796

797
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
798
				     sizeof(struct SmsMsgData_ST));
799 800
			msg->msgData[0] = mode;

801 802 803
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
804 805

			kfree(buffer);
806
		} else {
807 808
			sms_err("Could not allocate buffer for "
				"init device message.");
809
			rc = -ENOMEM;
810 811 812
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
813
			sms_err("invalid mode specified %d", mode);
814 815 816 817 818
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

819
		if (coredev->detectmode_handler)
820 821
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
822 823 824 825 826

		if (coredev->mode != mode && coredev->setmode_handler)
			rc = coredev->setmode_handler(coredev->context, mode);
	}

827
	if (rc >= 0) {
828 829 830 831
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

832
	if (rc != 0)
833
		sms_err("return error code %d.", rc);
834 835 836 837 838 839
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
840 841
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
842 843 844
 *
 * @return current mode
 */
845
int smscore_get_device_mode(struct smscore_device_t *coredev)
846 847 848 849
{
	return coredev->mode;
}

850 851 852 853
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
854 855
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
856
 * @param data_type client data type (SMS_DONT_CARE for all types)
857
 * @param id client id (SMS_DONT_CARE for all id)
858 859
 *
 */
860 861
static struct
smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
862
				      int data_type, int id)
863
{
864
	struct smscore_client_t *client = NULL;
865 866
	struct list_head *next, *first;
	unsigned long flags;
867
	struct list_head *firstid, *nextid;
868 869 870 871


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
872 873 874
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
875
		firstid = &((struct smscore_client_t *)next)->idlist;
876 877 878
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
879 880 881 882
			if ((((struct smscore_idlist_t *)nextid)->id == id) &&
			    (((struct smscore_idlist_t *)nextid)->data_type == data_type ||
			    (((struct smscore_idlist_t *)nextid)->data_type == 0))) {
				client = (struct smscore_client_t *) next;
883 884
				break;
			}
885 886 887 888 889 890 891 892 893 894
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
895 896
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
897 898 899
 * @param cb pointer to response buffer descriptor
 *
 */
900 901
void smscore_onresponse(struct smscore_device_t *coredev,
			struct smscore_buffer_t *cb)
902
{
903 904 905
	struct SmsMsgHdr_ST *phdr =
		(struct SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
	struct smscore_client_t *client =
906
		smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
907 908
	int rc = -EBUSY;

909 910
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
911 912 913 914 915
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

916
	if (time_now - last_sample_time > 10000) {
917
		sms_debug("\ndata rate %d bytes/secs",
918 919
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
920 921 922 923 924 925

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
926 927
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
928 929 930
	if (client)
		rc = client->onresponse_handler(client->context, cb);

931 932 933
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
934
		{
935 936
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
937 938
			sms_debug("MSG_SMS_GET_VERSION_EX_RES "
				  "id %d prots 0x%x ver %d.%d",
939 940
				  ver->FirmwareId, ver->SupportedProtocols,
				  ver->RomVersionMajor, ver->RomVersionMinor);
941

942 943 944
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
945

946 947 948 949
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
950
			sms_debug("MSG_SMS_INIT_DEVICE_RES");
951 952 953
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
954
			sms_debug("MSG_SW_RELOAD_START_RES");
955 956 957 958 959 960
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
961
			sms_debug("MSG_SW_RELOAD_EXEC_RES");
962 963
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
964
			sms_debug("MSG_SMS_SWDOWNLOAD_TRIGGER_RES");
965 966 967 968 969 970 971
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
972 973 974 975 976 977 978 979
		}
		smscore_putbuffer(coredev, cb);
	}
}

/**
 * return pointer to next free buffer descriptor from core pool
 *
980 981
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
982 983 984
 *
 * @return pointer to descriptor on success, NULL on error.
 */
985
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
986
{
987
	struct smscore_buffer_t *cb = NULL;
988 989 990 991
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);

992
	if (!list_empty(&coredev->buffers)) {
993
		cb = (struct smscore_buffer_t *) coredev->buffers.next;
994 995 996 997 998 999 1000 1001 1002 1003 1004
		list_del(&cb->entry);
	}

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

/**
 * return buffer descriptor to a pool
 *
1005 1006
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1007 1008 1009
 * @param cb pointer buffer descriptor
 *
 */
1010 1011
void smscore_putbuffer(struct smscore_device_t *coredev,
		       struct smscore_buffer_t *cb)
1012 1013 1014 1015
{
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}

1016 1017 1018
static int smscore_validate_client(struct smscore_device_t *coredev,
				   struct smscore_client_t *client,
				   int data_type, int id)
1019
{
1020 1021
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1022

1023
	if (!client) {
1024
		sms_err("bad parameter.");
1025 1026 1027
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1028
	if (registered_client == client)
1029
		return 0;
1030

1031
	if (registered_client) {
1032
		sms_err("The msg ID already registered to another client.");
1033 1034
		return -EEXIST;
	}
1035
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1036
	if (!listentry) {
1037
		sms_err("Can't allocate memory for client id.");
1038
		return -ENOMEM;
1039 1040 1041
	}
	listentry->id = id;
	listentry->data_type = data_type;
1042 1043
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1044 1045 1046 1047 1048 1049 1050 1051 1052
	return 0;
}

/**
 * creates smsclient object, check that id is taken by another client
 *
 * @param coredev pointer to a coredev object from clients hotplug
 * @param initial_id all messages with this id would be sent to this client
 * @param data_type all messages of this type would be sent to this client
1053 1054
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1055 1056 1057 1058 1059 1060
 * @param onremove_handler client handler that is called when device is removed
 * @param context client-specific context
 * @param client pointer to a value that receives created smsclient object
 *
 * @return 0 on success, <0 on error.
 */
1061 1062 1063
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1064
{
1065
	struct smscore_client_t *newclient;
1066 1067 1068
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1069
		sms_err("Client already exist.");
1070
		return -EEXIST;
1071
	}
1072

1073
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1074
	if (!newclient) {
1075
		sms_err("Failed to allocate memory for client.");
1076
		return -ENOMEM;
1077 1078
	}

1079
	INIT_LIST_HEAD(&newclient->idlist);
1080 1081 1082 1083
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1084 1085 1086 1087
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1088
	*client = newclient;
1089 1090
	sms_debug("%p %d %d", params->context, params->data_type,
		  params->initial_id);
1091 1092 1093 1094 1095 1096 1097

	return 0;
}

/**
 * frees smsclient object and all subclients associated with it
 *
1098 1099
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1100 1101
 *
 */
1102
void smscore_unregister_client(struct smscore_client_t *client)
1103
{
1104
	struct smscore_device_t *coredev = client->coredev;
1105 1106 1107 1108 1109
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1110
	while (!list_empty(&client->idlist)) {
1111 1112
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1113 1114
		list_del(&identry->entry);
		kfree(identry);
1115 1116
	}

1117
	sms_info("%p", client->context);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

	list_del(&client->entry);
	kfree(client);

	spin_unlock_irqrestore(&coredev->clientslock, flags);
}

/**
 * verifies that source id is not taken by another client,
 * calls device handler to send requests to the device
 *
1129 1130
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1131 1132 1133 1134 1135
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1136 1137
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1138
{
1139 1140
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1141 1142
	int rc;

1143
	if (client == NULL) {
1144
		sms_err("Got NULL client");
1145 1146 1147 1148
		return -EINVAL;
	}

	coredev = client->coredev;
1149

1150 1151
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1152
		sms_err("Got NULL coredev");
1153 1154 1155
		return -EINVAL;
	}

1156 1157
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1158 1159 1160 1161 1162 1163 1164 1165 1166
	if (rc < 0)
		return rc;

	return coredev->sendrequest_handler(coredev->context, buffer, size);
}


int smscore_module_init(void)
{
1167
	int rc = 0;
1168 1169 1170 1171 1172 1173 1174 1175

	INIT_LIST_HEAD(&g_smscore_notifyees);
	INIT_LIST_HEAD(&g_smscore_devices);
	kmutex_init(&g_smscore_deviceslock);

	INIT_LIST_HEAD(&g_smscore_registry);
	kmutex_init(&g_smscore_registrylock);

1176 1177 1178 1179 1180 1181
	/* USB Register */
	rc = smsusb_register();

	/* DVB Register */
	rc = smsdvb_register();

1182
	sms_debug("rc %d", rc);
1183 1184 1185 1186 1187 1188

	return rc;
}

void smscore_module_exit(void)
{
1189

1190
	kmutex_lock(&g_smscore_deviceslock);
1191
	while (!list_empty(&g_smscore_notifyees)) {
1192 1193 1194
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1195 1196 1197 1198 1199 1200 1201

		list_del(&notifyee->entry);
		kfree(notifyee);
	}
	kmutex_unlock(&g_smscore_deviceslock);

	kmutex_lock(&g_smscore_registrylock);
1202
	while (!list_empty(&g_smscore_registry)) {
1203 1204 1205
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1206 1207 1208 1209 1210 1211

		list_del(&entry->entry);
		kfree(entry);
	}
	kmutex_unlock(&g_smscore_registrylock);

1212 1213 1214 1215 1216 1217
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1218
	sms_debug("");
1219 1220 1221 1222 1223 1224
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

MODULE_DESCRIPTION("smscore");
1225
MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1226
MODULE_LICENSE("GPL");