smscoreapi.c 28.6 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 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
#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"

typedef struct _smscore_device_notifyee
{
	struct list_head entry;
	hotplug_t hotplug;
} smscore_device_notifyee_t;

typedef struct _smscore_client
{
	struct list_head entry;
	smscore_device_t *coredev;

	void			*context;

	int				data_type;

	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
} *psmscore_client_t;

typedef struct _smscore_subclient
{
	struct list_head entry;
	smscore_client_t *client;

	int				id;
} smscore_subclient_t;

typedef struct _smscore_device
{
	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;
} *psmscore_device_t;

typedef struct _smscore_registry_entry
{
	struct list_head entry;
	char			devpath[32];
	int				mode;
} smscore_registry_entry_t;

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;

static int default_mode = 1;
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

int smscore_registry_getmode(char* devpath)
{
	smscore_registry_entry_t *entry;
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);

	for (next = g_smscore_registry.next; next != &g_smscore_registry; next = next->next)
	{
		entry = (smscore_registry_entry_t *) next;

		if (!strcmp(entry->devpath, devpath))
		{
			kmutex_unlock(&g_smscore_registrylock);
			return entry->mode;
		}
	}

	entry = (smscore_registry_entry_t *) kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
	if (entry)
	{
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);

		list_add(&entry->entry, &g_smscore_registry);
	}

	kmutex_unlock(&g_smscore_registrylock);

	return default_mode;
}

void smscore_registry_setmode(char* devpath, int mode)
{
	smscore_registry_entry_t *entry;
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);

	for (next = g_smscore_registry.next; next != &g_smscore_registry; next = next->next)
	{
		entry = (smscore_registry_entry_t *) next;

		if (!strcmp(entry->devpath, devpath))
		{
			entry->mode = mode;
			break;
		}
	}

	kmutex_unlock(&g_smscore_registrylock);
}


void list_add_locked(struct list_head *new, struct list_head *head, spinlock_t* lock)
{
	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)
{
	smscore_device_notifyee_t *notifyee;
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

	notifyee = kmalloc(sizeof(smscore_device_notifyee_t), GFP_KERNEL);
	if (notifyee)
	{
		// now notify callback about existing devices
		first = &g_smscore_devices;
		for (next = first->next; next != first && !rc; next = next->next)
		{
			smscore_device_t *coredev = (smscore_device_t *) next;
			rc = hotplug(coredev, coredev->device, 1);
		}

		if (rc >= 0)
		{
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
		}
		else
			kfree(notifyee);
	}
	else
		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;

	for (next = first->next; next != first;)
	{
		smscore_device_notifyee_t *notifyee = (smscore_device_notifyee_t *) next;
		next = next->next;

		if (notifyee->hotplug == hotplug)
		{
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

void smscore_notify_clients(smscore_device_t *coredev)
{
	smscore_client_t* client;

	// the client must call smscore_unregister_client from remove handler
	while (!list_empty(&coredev->clients))
	{
		client = (smscore_client_t *) coredev->clients.next;
		client->onremove_handler(client->context);
	}
}

int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device, int arrival)
{
	struct list_head *next, *first;
	int rc = 0;

	// note: must be called under g_deviceslock

	first = &g_smscore_notifyees;

	for (next = first->next; next != first; next = next->next)
	{
		rc = ((smscore_device_notifyee_t *) next)->hotplug(coredev, device, arrival);
		if (rc < 0)
			break;
	}

	return rc;
}

smscore_buffer_t *smscore_createbuffer(u8* buffer, void* common_buffer, dma_addr_t common_buffer_phys)
{
	smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
	if (!cb)
	{
288
		printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
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
		return NULL;
	}

	cb->p = buffer;
	cb->offset_in_common = buffer - (u8*) common_buffer;
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
 * creates coredev object for a device, prepares buffers, creates buffer mappings, notifies
 * registered hotplugs about new device.
 *
 * @param params device pointer to struct with device specific parameters and handlers
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
int smscore_register_device(smsdevice_params_t *params, smscore_device_t **coredev)
{
	smscore_device_t* dev;
	u8 *buffer;

	dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
	if (!dev)
	{
316
		printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
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
		return -ENOMEM;
	}

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

	// init queues
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->subclients);
	INIT_LIST_HEAD(&dev->buffers);

	// init locks
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

	// init completion events
	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);

	// alloc common buffer
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size, &dev->common_buffer_phys, GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer)
	{
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

	// prepare dma buffers
	for (buffer = dev->common_buffer; dev->num_buffers < params->num_buffers; dev->num_buffers ++, buffer += params->buffer_size)
	{
		smscore_buffer_t *cb = smscore_createbuffer(buffer, dev->common_buffer, dev->common_buffer_phys);
		if (!cb)
		{
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

362
	printk(KERN_INFO "%s allocated %d buffers\n", __func__, dev->num_buffers);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	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);

	// add device to devices list
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

383
	printk(KERN_INFO "%s device %p created\n", __func__, dev);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 *
 * @return 0 on success, <0 on error.
 */
int smscore_start_device(smscore_device_t *coredev)
{
	int rc = smscore_set_device_mode(coredev, smscore_registry_getmode(coredev->devpath));
	if (rc < 0)
		return rc;

	kmutex_lock(&g_smscore_deviceslock);

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

405
	printk(KERN_INFO "%s device %p started, rc %d\n", __func__, coredev, rc);
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

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

int smscore_sendrequest_and_wait(smscore_device_t *coredev, void* buffer, size_t size, struct completion *completion)
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
	if (rc < 0)
		return rc;

	return wait_for_completion_timeout(completion, msecs_to_jiffies(1000)) ? 0 : -ETIME;
}

int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer, size_t size)
{
	SmsFirmware_ST* firmware = (SmsFirmware_ST*) buffer;
	SmsMsgHdr_ST *msg;
	UINT32 mem_address = firmware->StartAddress;
	u8* payload = firmware->Payload;
	int rc = 0;

	if (coredev->preload_handler)
	{
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

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

	if (coredev->mode != DEVICE_MODE_NONE)
	{
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ, sizeof(SmsMsgHdr_ST));
		rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->reload_start_done);
		mem_address = *(UINT32*) &payload[20];
	}

	while (size && rc >= 0)
	{
		SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ, (UINT16)(sizeof(SmsMsgHdr_ST) + sizeof(UINT32) + payload_size));

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

		if (coredev->device_flags & SMS_ROM_NO_RESPONSE && coredev->mode == DEVICE_MODE_NONE)
			rc = coredev->sendrequest_handler(coredev->context, DataMsg, DataMsg->xMsgHeader.msgLength);
		else
			rc = smscore_sendrequest_and_wait(coredev, DataMsg, DataMsg->xMsgHeader.msgLength, &coredev->data_download_done);

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

	if (rc >= 0)
	{
		if (coredev->mode == DEVICE_MODE_NONE)
		{
			SmsMsgData_ST* TriggerMsg = (SmsMsgData_ST*) msg;

			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ, sizeof(SmsMsgHdr_ST) + sizeof(UINT32) * 5);

			TriggerMsg->msgData[0] = firmware->StartAddress;	// Entry point
			TriggerMsg->msgData[1] = 5;							// Priority
			TriggerMsg->msgData[2] = 0x200;						// Stack size
			TriggerMsg->msgData[3] = 0;							// Parameter
			TriggerMsg->msgData[4] = 4;							// Task ID

			if (coredev->device_flags & SMS_ROM_NO_RESPONSE)
			{
				rc = coredev->sendrequest_handler(coredev->context, TriggerMsg, TriggerMsg->xMsgHeader.msgLength);
				msleep(100);
			}
			else
				rc = smscore_sendrequest_and_wait(coredev, TriggerMsg, TriggerMsg->xMsgHeader.msgLength, &coredev->trigger_done);
		}
		else
		{
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ, sizeof(SmsMsgHdr_ST));

			rc = coredev->sendrequest_handler(coredev->context, msg, msg->msgLength);
		}
	}

	printk("%s %d \n", __func__, rc);

	kfree(msg);

	return (rc >= 0 && coredev->postload_handler) ?
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
int smscore_load_firmware(smscore_device_t *coredev, char* filename, loadfirmware_t loadfirmware_handler)
{
	int rc = -ENOENT;

	const struct firmware *fw;
	u8* fw_buffer;

	if (loadfirmware_handler == NULL && !(coredev->device_flags & SMS_DEVICE_FAMILY2))
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
	if (rc < 0)
	{
529
		printk(KERN_INFO "%s failed to open \"%s\"\n", __func__, filename);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
		return rc;
	}

	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT), GFP_KERNEL | GFP_DMA);
	if (fw_buffer)
	{
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
			smscore_load_firmware_family2(coredev, fw_buffer, fw->size) :
			loadfirmware_handler(coredev->context, fw_buffer, fw->size);

		kfree(fw_buffer);
	}
	else
	{
546
		printk(KERN_INFO "%s failed to allocate firmware buffer\n", __func__);
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
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

/**
 * notifies all clients registered with the device, notifies hotplugs, frees all buffers and coredev object
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 *
 * @return 0 on success, <0 on error.
 */
void smscore_unregister_device(smscore_device_t *coredev)
{
	smscore_buffer_t *cb;
	int num_buffers = 0;

	kmutex_lock(&g_smscore_deviceslock);

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

	// at this point all buffers should be back
	// onresponse must no longer be called

	while (1)
	{
		while ((cb = smscore_getbuffer(coredev)))
		{
			kfree(cb);
			num_buffers ++;
		}

		if (num_buffers == coredev->num_buffers)
			break;

586
		printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__, coredev->num_buffers - num_buffers);
587 588 589
		msleep(100);
	}

590
	printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
591 592 593 594 595 596 597 598 599

	if (coredev->common_buffer)
		dma_free_coherent(NULL, coredev->common_buffer_size, coredev->common_buffer, coredev->common_buffer_phys);

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

	kmutex_unlock(&g_smscore_deviceslock);

600
	printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
}

int smscore_detect_mode(smscore_device_t *coredev)
{
	void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
	SmsMsgHdr_ST *msg = (SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
	int rc;

	if (!buffer)
		return -ENOMEM;

	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));

	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
	if (rc == -ETIME)
	{
617
		printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed first try\n", __func__);
618 619 620 621 622 623

		if (wait_for_completion_timeout(&coredev->resume_done, msecs_to_jiffies(5000)))
		{
			rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
			if (rc < 0)
			{
624
				printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d\n", __func__, rc);
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
			}
		}
		else
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

char *smscore_fw_lkup[] =
{
	"dvb_nova_12mhz.inp",
	"dvb_nova_12mhz.inp",
	"tdmb_nova.inp",
	"none",
	"dvb_nova_12mhz.inp",
	"isdbt_nova_12mhz.inp",
	"isdbt_nova_12mhz.inp",
	"cmmb_nova_12mhz.inp",
	"none",
};

/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
int smscore_set_device_mode(smscore_device_t *coredev, int mode)
{
	void *buffer;
	int rc = 0;

	if (coredev->device_flags & SMS_DEVICE_FAMILY2)
	{
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER)
		{
667
			printk(KERN_INFO "%s invalid mode specified %d\n", __func__, mode);
668 669 670 671 672 673 674 675 676 677 678 679
			return -EINVAL;
		}

		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY))
		{
			rc = smscore_detect_mode(coredev);
			if (rc < 0)
				return rc;
		}

		if (coredev->mode == mode)
		{
680
			printk(KERN_INFO "%s device mode %d already set\n", __func__, mode);
681 682 683 684 685 686 687 688 689 690 691
			return 0;
		}

		if (!(coredev->modes_supported & (1 << mode)))
		{
			rc = smscore_load_firmware(coredev, smscore_fw_lkup[mode], NULL);
			if (rc < 0)
				return rc;
		}
		else
		{
692
			printk(KERN_INFO "%s mode %d supported by running firmware\n", __func__, mode);
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
		}

		buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
		if (buffer)
		{
			SmsMsgData_ST *msg = (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);

			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ, sizeof(SmsMsgData_ST));
			msg->msgData[0] = mode;

			rc = smscore_sendrequest_and_wait(coredev, msg, msg->xMsgHeader.msgLength, &coredev->init_device_done);

			kfree(buffer);
		}
		else
			rc = -ENOMEM;
	}
	else
	{
		if (coredev->detectmode_handler)
			coredev->detectmode_handler(coredev->context, &coredev->mode);

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

	smscore_registry_setmode(coredev->devpath, mode);

	if (rc >= 0)
	{
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 *
 * @return current mode
 */
int smscore_get_device_mode(smscore_device_t *coredev)
{
	return coredev->mode;
}

smscore_client_t* smscore_getclient_by_type(smscore_device_t *coredev, int data_type)
{
	smscore_client_t *client = NULL;
	struct list_head *next, *first;
	unsigned long flags;

	if (!data_type)
		return NULL;

	spin_lock_irqsave(&coredev->clientslock, flags);

	first = &coredev->clients;

	for (next = first->next; next != first; next = next->next)
	{
		if (((smscore_client_t*) next)->data_type == data_type)
		{
			client = (smscore_client_t*) next;
			break;
		}
	}

	spin_unlock_irqrestore(&coredev->clientslock, flags);

	return client;
}

smscore_client_t* smscore_getclient_by_id(smscore_device_t *coredev, int id)
{
	smscore_client_t *client = NULL;
	struct list_head *next, *first;
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);

	first = &coredev->subclients;

	for (next = first->next; next != first; next = next->next)
	{
		if (((smscore_subclient_t*) next)->id == id)
		{
			client = ((smscore_subclient_t*) next)->client;
			break;
		}
	}

	spin_unlock_irqrestore(&coredev->clientslock, flags);

	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 * @param cb pointer to response buffer descriptor
 *
 */
void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
{
	SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8*) cb->p + cb->offset);
	smscore_client_t * client = smscore_getclient_by_type(coredev, phdr->msgType);
	int rc = -EBUSY;

	static unsigned long last_sample_time = 0;
	static int data_total = 0;
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

	if (time_now - last_sample_time > 10000)
	{
		printk("\n%s data rate %d bytes/secs\n", __func__, (int)((data_total * 1000) / (time_now - last_sample_time)));

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;

	if (!client)
		client = smscore_getclient_by_id(coredev, phdr->msgDstId);

	if (client)
		rc = client->onresponse_handler(client->context, cb);

	if (rc < 0)
	{
		switch (phdr->msgType)
		{
			case MSG_SMS_GET_VERSION_EX_RES:
			{
				SmsVersionRes_ST *ver = (SmsVersionRes_ST*) phdr;
837
				printk("%s: MSG_SMS_GET_VERSION_EX_RES id %d prots 0x%x ver %d.%d\n", __func__, ver->FirmwareId, ver->SupportedProtocols, ver->RomVersionMajor, ver->RomVersionMinor);
838 839 840 841 842 843 844 845 846

				coredev->mode = ver->FirmwareId == 255 ? DEVICE_MODE_NONE : ver->FirmwareId;
				coredev->modes_supported = ver->SupportedProtocols;

				complete(&coredev->version_ex_done);
				break;
			}

			case MSG_SMS_INIT_DEVICE_RES:
847
				printk("%s: MSG_SMS_INIT_DEVICE_RES\n", __func__);
848 849 850 851
				complete(&coredev->init_device_done);
				break;

			case MSG_SW_RELOAD_START_RES:
852
				printk("%s: MSG_SW_RELOAD_START_RES\n", __func__);
853 854 855 856 857 858 859 860
				complete(&coredev->reload_start_done);
				break;

			case MSG_SMS_DATA_DOWNLOAD_RES:
				complete(&coredev->data_download_done);
				break;

			case MSG_SW_RELOAD_EXEC_RES:
861
				printk("%s: MSG_SW_RELOAD_EXEC_RES\n", __func__);
862 863 864
				break;

			case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
865
				printk("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n", __func__);
866 867 868 869 870 871 872 873
				complete(&coredev->trigger_done);
				break;

			case MSG_SMS_SLEEP_RESUME_COMP_IND:
				complete(&coredev->resume_done);
				break;

			default:
874
				printk(KERN_INFO "%s no client (%p) or error (%d), type:%d dstid:%d\n", __func__, client, rc, phdr->msgType, phdr->msgDstId);
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
		}

		smscore_putbuffer(coredev, cb);
	}
}

/**
 * return pointer to next free buffer descriptor from core pool
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 *
 * @return pointer to descriptor on success, NULL on error.
 */
smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
{
	smscore_buffer_t *cb = NULL;
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);

	if (!list_empty(&coredev->buffers))
	{
		cb = (smscore_buffer_t *) coredev->buffers.next;
		list_del(&cb->entry);
	}

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

/**
 * return buffer descriptor to a pool
 *
 * @param coredev pointer to a coredev object returned by smscore_register_device
 * @param cb pointer buffer descriptor
 *
 */
void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
{
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}

int smscore_validate_client(smscore_device_t *coredev, smscore_client_t *client, int id)
{
	smscore_client_t *existing_client;
	smscore_subclient_t *subclient;

	if (!id)
		return 0;

	existing_client = smscore_getclient_by_id(coredev, id);
	if (existing_client == client)
		return 0;

	if (existing_client)
		return -EBUSY;

	subclient = kzalloc(sizeof(smscore_subclient_t), GFP_KERNEL);
	if (!subclient)
		return -ENOMEM;

	subclient->client = client;
	subclient->id = id;

	list_add_locked(&subclient->entry, &coredev->subclients, &coredev->clientslock);

	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
 * @param onresponse_handler client handler that is called to process incoming messages
 * @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.
 */
int smscore_register_client(smscore_device_t *coredev, smsclient_params_t *params, smscore_client_t **client)
{
	smscore_client_t* newclient;
	int rc;

	// check that no other channel with same data type exists
	if (params->data_type && smscore_getclient_by_type(coredev, params->data_type))
		return -EEXIST;

	newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
	if (!newclient)
		return -ENOMEM;

	// check that no other channel with same id exists
	rc = smscore_validate_client(coredev, newclient, params->initial_id);
	if (rc < 0)
	{
		kfree(newclient);
		return rc;
	}

	newclient->coredev = coredev;
	newclient->data_type = params->data_type;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;

	list_add_locked(&newclient->entry, &coredev->clients, &coredev->clientslock);

	*client = newclient;

989
	printk(KERN_INFO "%s %p %d %d\n", __func__, params->context, params->data_type, params->initial_id);
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

	return 0;
}

/**
 * frees smsclient object and all subclients associated with it
 *
 * @param client pointer to smsclient object returned by smscore_register_client
 *
 */
void smscore_unregister_client(smscore_client_t *client)
{
	smscore_device_t *coredev = client->coredev;
	struct list_head *next, *first;
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);

	first = &coredev->subclients;

	for (next = first->next; next != first;)
	{
		smscore_subclient_t *subclient = (smscore_subclient_t *) next;
		next = next->next;

		if (subclient->client == client)
		{
			list_del(&subclient->entry);
			kfree(subclient);
		}
	}

1022
	printk(KERN_INFO "%s %p %d\n", __func__, client->context, client->data_type);
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

	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
 *
 * @param client pointer to smsclient object returned by smscore_register_client
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
{
	smscore_device_t* coredev = client->coredev;
	SmsMsgHdr_ST* phdr = (SmsMsgHdr_ST*) buffer;

	// check that no other channel with same id exists
	int rc = smscore_validate_client(client->coredev, client, phdr->msgSrcId);
	if (rc < 0)
		return rc;

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

/**
 * return the size of large (common) buffer
 *
 * @param coredev pointer to a coredev object from clients hotplug
 *
 * @return size (in bytes) of the buffer
 */
int smscore_get_common_buffer_size(smscore_device_t *coredev)
{
	return coredev->common_buffer_size;
}

/**
 * maps common buffer (if supported by platform)
 *
 * @param coredev pointer to a coredev object from clients hotplug
 * @param vma pointer to vma struct from mmap handler
 *
 * @return 0 on success, <0 on error.
 */
int smscore_map_common_buffer(smscore_device_t *coredev, struct vm_area_struct * vma)
{
	unsigned long end = vma->vm_end, start = vma->vm_start, size = PAGE_ALIGN(coredev->common_buffer_size);

	if (!(vma->vm_flags & (VM_READ | VM_SHARED)) || (vma->vm_flags & VM_WRITE))
	{
1079
		printk(KERN_INFO "%s invalid vm flags\n", __func__);
1080 1081 1082 1083 1084
		return -EINVAL;
	}

	if ((end - start) != size)
	{
1085
		printk(KERN_INFO "%s invalid size %d expected %d\n", __func__, (int)(end - start), (int) size);
1086 1087 1088 1089 1090
		return -EINVAL;
	}

	if (remap_pfn_range(vma, start, coredev->common_buffer_phys >> PAGE_SHIFT, size, pgprot_noncached(vma->vm_page_prot)))
	{
1091
		printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1092 1093 1094 1095 1096 1097 1098 1099
		return -EAGAIN;
	}

	return 0;
}

int smscore_module_init(void)
{
1100
	int rc = 0;
1101 1102 1103 1104 1105 1106 1107 1108

	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);

1109 1110 1111 1112 1113 1114
	/* USB Register */
	rc = smsusb_register();

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

1115
	printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1116 1117 1118 1119 1120 1121

	return rc;
}

void smscore_module_exit(void)
{
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	kmutex_lock(&g_smscore_deviceslock);
	while (!list_empty(&g_smscore_notifyees))
	{
		smscore_device_notifyee_t *notifyee = (smscore_device_notifyee_t *) g_smscore_notifyees.next;

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

	kmutex_lock(&g_smscore_registrylock);
	while (!list_empty(&g_smscore_registry))
	{
		smscore_registry_entry_t *entry = (smscore_registry_entry_t *) g_smscore_registry.next;

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

1143 1144 1145 1146 1147 1148
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1149
	printk(KERN_INFO "%s\n", __func__);
1150 1151 1152 1153 1154 1155 1156 1157 1158
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

MODULE_DESCRIPTION("smscore");
MODULE_AUTHOR("Anatoly Greenblatt,,, (anatolyg@siano-ms.com)");
MODULE_LICENSE("GPL");