smscoreapi.c 32.9 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
#define PERROR(fmt, args...)\
	printk(KERN_ERR "smscore error: line %d- %s(): " fmt, \
		__LINE__,  __func__, ## args)
39 40 41

#ifdef SMSCORE_DEBUG
#undef PWARNING
42 43 44
#  define PWARNING(fmt, args...) printk(KERN_INFO "smscore warning: " \
					"line %d- %s(): " fmt, \
					__LINE__, __func__, ## args)
45
#undef PDEBUG					/* undef it, just in case */
46 47
#  define PDEBUG(fmt, args...)   printk(KERN_INFO "smscore - %s(): " fmt, \
					__func__, ## args)
48 49 50 51 52
#else /*SMSCORE_DEBUG*/
#define PDEBUG(fmt, args...)
#define PWARNING(fmt, args...)
#endif

53 54 55 56 57 58
typedef struct _smscore_device_notifyee
{
	struct list_head entry;
	hotplug_t hotplug;
} smscore_device_notifyee_t;

59 60 61 62 63 64 65
typedef struct _smscore_subclient
{
	struct list_head entry;
	int		id;
	int		data_type;
} smscore_idlist_t;

66 67 68 69 70
typedef struct _smscore_client
{
	struct list_head entry;
	smscore_device_t *coredev;
	void			*context;
71
	struct list_head 	idlist;
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
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
} *psmscore_client_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;
117
	sms_device_type_st	type;
118 119 120 121 122 123 124 125 126 127
} 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;
128

129 130 131
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

132
static smscore_registry_entry_t *smscore_find_registry(char *devpath)
133 134 135 136 137
{
	smscore_registry_entry_t *entry;
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
138 139 140
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
141
		entry = (smscore_registry_entry_t *) next;
142
		if (!strcmp(entry->devpath, devpath)) {
143
			kmutex_unlock(&g_smscore_registrylock);
144
			return entry;
145 146
		}
	}
147 148 149
	entry = (smscore_registry_entry_t *)
			kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
	if (entry) {
150 151 152
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
153 154 155
	} else
		printk(KERN_ERR "%s failed to create smscore_registry.\n",
		       __func__);
156
	kmutex_unlock(&g_smscore_registrylock);
157 158
	return entry;
}
159

160
int smscore_registry_getmode(char *devpath)
161 162 163
{
	smscore_registry_entry_t *entry;

164 165
	entry = smscore_find_registry(devpath);
	if (entry)
166 167
		return entry->mode;
	else
168 169
		printk(KERN_ERR "%s No registry found.\n", __func__);

170 171 172
	return default_mode;
}

173
sms_device_type_st smscore_registry_gettype(char *devpath)
174 175 176
{
	smscore_registry_entry_t *entry;

177 178
	entry = smscore_find_registry(devpath);
	if (entry)
179 180
		return entry->type;
	else
181 182
		printk(KERN_ERR "%s No registry found.\n", __func__);

183 184
	return -1;
}
185

186 187
void smscore_registry_setmode(char *devpath, int mode)
{
188
	smscore_registry_entry_t *entry;
189

190 191 192
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
193
	else
194 195
		printk(KERN_ERR "%s No registry found.\n", __func__);
}
196

197
void smscore_registry_settype(char *devpath, sms_device_type_st type)
198 199 200
{
	smscore_registry_entry_t *entry;

201 202
	entry = smscore_find_registry(devpath);
	if (entry)
203 204
		entry->type = type;
	else
205
		printk(KERN_ERR "%s No registry found.\n", __func__);
206 207 208
}


209

210
void list_add_locked(struct list_head *new, struct list_head *head,
211
		     spinlock_t *lock)
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
{
	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);
239 240
	if (notifyee) {
		/* now notify callback about existing devices */
241
		first = &g_smscore_devices;
242 243 244
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
245 246 247 248
			smscore_device_t *coredev = (smscore_device_t *) next;
			rc = hotplug(coredev, coredev->device, 1);
		}

249
		if (rc >= 0) {
250 251
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
252
		} else
253
			kfree(notifyee);
254
	} else
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
		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;

276 277 278
	for (next = first->next; next != first;) {
		smscore_device_notifyee_t *notifyee =
			(smscore_device_notifyee_t *) next;
279 280
		next = next->next;

281
		if (notifyee->hotplug == hotplug) {
282 283 284 285 286 287 288 289 290 291
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

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

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

301 302
int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device,
			     int arrival)
303 304 305 306
{
	struct list_head *next, *first;
	int rc = 0;

307
	/* note: must be called under g_deviceslock */
308 309 310

	first = &g_smscore_notifyees;

311
	for (next = first->next; next != first; next = next->next) {
312 313
		rc = ((smscore_device_notifyee_t *) next)->
				hotplug(coredev, device, arrival);
314 315 316 317 318 319 320
		if (rc < 0)
			break;
	}

	return rc;
}

321 322
smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
				       dma_addr_t common_buffer_phys)
323 324
{
	smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
325
	if (!cb) {
326
		printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
327 328 329 330
		return NULL;
	}

	cb->p = buffer;
331
	cb->offset_in_common = buffer - (u8 *) common_buffer;
332 333 334 335 336 337
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

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

	dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
354
	if (!dev) {
355
		printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
356 357 358
		return -ENOMEM;
	}

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

362
	/* init queues */
363 364 365
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

366
	/* init locks */
367 368 369
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

370
	/* init completion events */
371 372 373 374 375 376 377
	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);

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

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

		smscore_putbuffer(dev, cb);
	}

403 404
	printk(KERN_INFO "%s allocated %d buffers\n",
	       __func__, dev->num_buffers);
405 406 407 408 409 410 411 412 413 414 415 416 417

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

418
	smscore_registry_settype(dev->devpath, params->device_type);
419

420
	/* add device to devices list */
421 422 423 424 425 426
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

427
	printk(KERN_INFO "%s device %p created\n", __func__, dev);
428 429 430 431 432 433 434

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
435 436
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
437 438 439 440 441
 *
 * @return 0 on success, <0 on error.
 */
int smscore_start_device(smscore_device_t *coredev)
{
442 443
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
444
	if (rc < 0) {
445 446
		printk(KERN_INFO "%s set device mode faile , rc %d\n",
		       __func__, rc);
447
		return rc;
448
	}
449 450 451 452 453

	kmutex_lock(&g_smscore_deviceslock);

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

454 455
	printk(KERN_INFO "%s device %p started, rc %d\n",
	       __func__, coredev, rc);
456 457 458 459 460 461

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

462
int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
463
				 size_t size, struct completion *completion)
464 465
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
466 467 468
	if (rc < 0) {
		printk(KERN_INFO "%s sendrequest returned error %d\n",
		       __func__, rc);
469
		return rc;
470
	}
471

472 473 474
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(10000)) ?
						0 : -ETIME;
475 476
}

477 478
int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer,
				  size_t size)
479
{
480
	SmsFirmware_ST *firmware = (SmsFirmware_ST *) buffer;
481
	SmsMsgHdr_ST *msg;
482
	u32 mem_address = firmware->StartAddress;
483
	u8 *payload = firmware->Payload;
484 485
	int rc = 0;

486 487 488
	printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
	       __func__, mem_address, firmware->Length);
	if (coredev->preload_handler) {
489 490 491 492 493
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

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

499
	if (coredev->mode != DEVICE_MODE_NONE) {
500
		PDEBUG("Sending reload command\n");
501 502 503 504 505
		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);
506
		mem_address = *(u32 *) &payload[20];
507 508
	}

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

513
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
514 515
			     (u16)(sizeof(SmsMsgHdr_ST) +
				      sizeof(u32) + payload_size));
516 517 518 519

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

520 521
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
522 523 524
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
525
		else
526 527 528 529
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
530 531 532 533 534 535

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

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

540 541
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
				     sizeof(SmsMsgHdr_ST) +
542
				     sizeof(u32) * 5);
543

544 545
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
546 547 548 549
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
550

551
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
552 553 554
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
555
				msleep(100);
556
			} else
557 558 559 560
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
561 562 563
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
				     sizeof(SmsMsgHdr_ST));
564

565 566
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
567
		}
568
		msleep(500);
569 570
	}

571
	printk(KERN_DEBUG "%s rc=%d, postload=%p \n", __func__, rc,
572
	       coredev->postload_handler);
573 574 575

	kfree(msg);

576
	return ((rc >= 0) && coredev->postload_handler) ?
577 578 579 580 581 582 583
		coredev->postload_handler(coredev->context) :
		rc;
}

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

	const struct firmware *fw;
597
	u8 *fw_buffer;
598

599 600
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
601 602 603
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
604 605 606
	if (rc < 0) {
		printk(KERN_INFO "%s failed to open \"%s\"\n",
		       __func__, filename);
607 608
		return rc;
	}
609 610 611 612 613
	printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
	       filename, fw->size);
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
614 615 616
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
617 618 619 620 621
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
622 623

		kfree(fw_buffer);
624 625 626
	} else {
		printk(KERN_INFO "%s failed to allocate firmware buffer\n",
		       __func__);
627 628 629 630 631 632 633 634
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

635 636
int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer,
				      int size, int new_mode)
637 638 639 640 641
{
	PERROR("Feature not implemented yet\n");
	return -EFAULT;
}

642
/**
643 644
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
645
 *
646 647
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
648 649 650 651 652 653 654
 *
 * @return 0 on success, <0 on error.
 */
void smscore_unregister_device(smscore_device_t *coredev)
{
	smscore_buffer_t *cb;
	int num_buffers = 0;
655
	int retry = 0;
656 657 658 659 660 661

	kmutex_lock(&g_smscore_deviceslock);

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

662 663
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
664

665 666
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
667
			kfree(cb);
668
			num_buffers++;
669 670 671
		}
		if (num_buffers == coredev->num_buffers)
			break;
672 673 674
		if (++retry > 10) {
			printk(KERN_INFO "%s exiting although "
			       "not all buffers released.\n", __func__);
675 676
			break;
		}
677

678 679
		printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
		       coredev->num_buffers - num_buffers);
680 681 682
		msleep(100);
	}

683
	printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
684 685

	if (coredev->common_buffer)
686 687 688
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
689 690 691 692 693 694

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

	kmutex_unlock(&g_smscore_deviceslock);

695
	printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
696 697 698 699
}

int smscore_detect_mode(smscore_device_t *coredev)
{
700 701
	void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
			       GFP_KERNEL | GFP_DMA);
702 703 704 705 706 707 708 709
	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));

710 711 712
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
713 714
		printk(KERN_ERR "%s: MSG_SMS_GET_VERSION_EX_REQ "
		       "failed first try\n", __func__);
715

716 717
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
718 719 720
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
721
			if (rc < 0)
722 723
				printk(KERN_ERR "%s: "
				       "MSG_SMS_GET_VERSION_EX_REQ failed "
724
				       "second try, rc %d\n", __func__, rc);
725
		} else
726 727 728 729 730 731 732 733
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

734
char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
735
	/*Stellar			NOVA A0			Nova B0				VEGA*/
736 737 738 739 740 741 742 743
	/*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"}
744 745
};

746

747 748 749 750
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
751 752
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
753 754 755 756 757 758 759 760
 * @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;
761
	sms_device_type_st type;
762

763 764 765 766 767
	PDEBUG("set device mode to %d\n", mode);
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
			printk(KERN_INFO "%s invalid mode specified %d\n",
			       __func__, mode);
768 769 770
			return -EINVAL;
		}

771 772
		smscore_registry_setmode(coredev->devpath, mode);

773
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
774
			rc = smscore_detect_mode(coredev);
775 776 777
			if (rc < 0) {
				printk(KERN_INFO "%s mode detect failed %d\n",
				       __func__, rc);
778
				return rc;
779
			}
780
		}
781

782 783 784
		if (coredev->mode == mode) {
			printk(KERN_INFO "%s device mode %d already set\n",
			       __func__, mode);
785 786 787
			return 0;
		}

788 789
		if (!(coredev->modes_supported & (1 << mode))) {
			type = smscore_registry_gettype(coredev->devpath);
790 791
			rc = smscore_load_firmware_from_file(
				coredev, smscore_fw_lkup[mode][type], NULL);
792
			if (rc < 0) {
793 794
				printk(KERN_INFO "%s load firmware "
				       "failed %d\n", __func__, rc);
795
				return rc;
796 797
			}
		} else
798 799
			printk(KERN_INFO "%s mode %d supported by running "
			       "firmware\n", __func__, mode);
800

801 802 803
		buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT,
				 GFP_KERNEL | GFP_DMA);
		if (buffer) {
804 805
			SmsMsgData_ST *msg =
				(SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
806

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

811 812 813
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
814 815

			kfree(buffer);
816
		} else {
817 818
			printk(KERN_INFO "%s Could not allocate buffer for "
			       "init device message.\n", __func__);
819
			rc = -ENOMEM;
820 821 822 823 824
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
			printk(KERN_INFO "%s invalid mode specified %d\n",
			       __func__, mode);
825 826 827 828 829
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

830
		if (coredev->detectmode_handler)
831 832
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
833 834 835 836 837

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

838
	if (rc >= 0) {
839 840 841 842
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

843 844
	if (rc != 0)
		printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
845 846 847 848 849 850
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
851 852
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
853 854 855 856 857 858 859 860
 *
 * @return current mode
 */
int smscore_get_device_mode(smscore_device_t *coredev)
{
	return coredev->mode;
}

861 862 863 864
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
865 866
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
867
 * @param data_type client data type (SMS_DONT_CARE for all types)
868
 * @param id client id (SMS_DONT_CARE for all id)
869 870
 *
 */
871 872
smscore_client_t *smscore_find_client(smscore_device_t *coredev,
				      int data_type, int id)
873 874 875 876
{
	smscore_client_t *client = NULL;
	struct list_head *next, *first;
	unsigned long flags;
877
	struct list_head *firstid, *nextid;
878 879 880 881


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
882 883 884
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
885
		firstid = &((smscore_client_t *)next)->idlist;
886 887 888
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
889 890 891 892
			if ((((smscore_idlist_t *)nextid)->id  == id) &&
			    (((smscore_idlist_t *)nextid)->data_type  == data_type ||
			    (((smscore_idlist_t *)nextid)->data_type  == 0))) {
				client = (smscore_client_t *) next;
893 894
				break;
			}
895 896 897 898 899 900 901 902 903 904
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
905 906
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
907 908 909 910 911
 * @param cb pointer to response buffer descriptor
 *
 */
void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
{
912
	SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
913 914
	smscore_client_t *client =
		smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
915 916 917 918 919 920 921 922 923
	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;

924
	if (time_now - last_sample_time > 10000) {
925
		printk(KERN_DEBUG "\n%s data rate %d bytes/secs\n", __func__,
926 927
		       (int)((data_total * 1000) /
			     (time_now - last_sample_time)));
928 929 930 931 932 933

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
934 935
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
936 937 938
	if (client)
		rc = client->onresponse_handler(client->context, cb);

939 940 941
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
942
		{
943
			SmsVersionRes_ST *ver = (SmsVersionRes_ST *) phdr;
944 945
			printk(KERN_DEBUG "%s: MSG_SMS_GET_VERSION_EX_RES "
			       "id %d prots 0x%x ver %d.%d\n", __func__,
946 947
			       ver->FirmwareId, ver->SupportedProtocols,
			       ver->RomVersionMajor, ver->RomVersionMinor);
948

949 950 951
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
952

953 954 955 956
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
957 958
			printk(KERN_DEBUG "%s: MSG_SMS_INIT_DEVICE_RES\n",
			       __func__);
959 960 961
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
962 963
			printk(KERN_DEBUG "%s: MSG_SW_RELOAD_START_RES\n",
			       __func__);
964 965 966 967 968 969
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
970 971
			printk(KERN_DEBUG "%s: MSG_SW_RELOAD_EXEC_RES\n",
			       __func__);
972 973
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
974 975
			printk(KERN_DEBUG
			       "%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
976 977 978 979 980 981 982 983
			       __func__);
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
984 985 986 987 988 989 990 991
		}
		smscore_putbuffer(coredev, cb);
	}
}

/**
 * return pointer to next free buffer descriptor from core pool
 *
992 993
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
994 995 996 997 998 999 1000 1001 1002 1003
 *
 * @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);

1004
	if (!list_empty(&coredev->buffers)) {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		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
 *
1017 1018
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1019 1020 1021 1022 1023 1024 1025 1026
 * @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);
}

1027 1028
int smscore_validate_client(smscore_device_t *coredev,
			    smscore_client_t *client, int data_type, int id)
1029
{
1030 1031
	smscore_idlist_t *listentry;
	smscore_client_t *registered_client;
1032

1033
	if (!client) {
1034 1035 1036 1037
		PERROR("bad parameter.\n");
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1038
	if (registered_client == client)
1039
		return 0;
1040

1041
	if (registered_client) {
1042 1043 1044
		PERROR("The msg ID already registered to another client.\n");
		return -EEXIST;
	}
1045 1046
	listentry = kzalloc(sizeof(smscore_idlist_t), GFP_KERNEL);
	if (!listentry) {
1047
		PERROR("Can't allocate memory for client id.\n");
1048
		return -ENOMEM;
1049 1050 1051
	}
	listentry->id = id;
	listentry->data_type = data_type;
1052 1053
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1054 1055 1056 1057 1058 1059 1060 1061 1062
	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
1063 1064
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1065 1066 1067 1068 1069 1070
 * @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.
 */
1071 1072 1073
int smscore_register_client(smscore_device_t *coredev,
			    smsclient_params_t *params,
			    smscore_client_t **client)
1074
{
1075
	smscore_client_t *newclient;
1076 1077 1078
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1079
		PERROR("Client already exist.\n");
1080
		return -EEXIST;
1081
	}
1082 1083

	newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1084
	if (!newclient) {
1085 1086
		PERROR("Failed to allocate memory for client.\n");
		return -ENOMEM;
1087 1088
	}

1089
	INIT_LIST_HEAD(&newclient->idlist);
1090 1091 1092 1093
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1094 1095 1096 1097
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1098
	*client = newclient;
1099 1100
	PDEBUG("%p %d %d\n", params->context, params->data_type,
	       params->initial_id);
1101 1102 1103 1104 1105 1106 1107

	return 0;
}

/**
 * frees smsclient object and all subclients associated with it
 *
1108 1109
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
 *
 */
void smscore_unregister_client(smscore_client_t *client)
{
	smscore_device_t *coredev = client->coredev;
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1120 1121
	while (!list_empty(&client->idlist)) {
		smscore_idlist_t *identry =
1122
			(smscore_idlist_t *) client->idlist.next;
1123 1124
		list_del(&identry->entry);
		kfree(identry);
1125 1126
	}

1127
	printk(KERN_INFO "%s %p\n", __func__, client->context);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138

	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
 *
1139 1140
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1141 1142 1143 1144 1145 1146 1147
 * @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)
{
1148
	smscore_device_t *coredev;
1149
	SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1150 1151
	int rc;

1152 1153
	if (client == NULL) {
		printk(KERN_ERR "%s Got NULL client\n", __func__);
1154 1155 1156 1157
		return -EINVAL;
	}

	coredev = client->coredev;
1158

1159 1160 1161
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
		printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1162 1163 1164
		return -EINVAL;
	}

1165 1166
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
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
	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.
 */
1193 1194
int smscore_map_common_buffer(smscore_device_t *coredev,
			       struct vm_area_struct *vma)
1195
{
1196 1197 1198
	unsigned long end = vma->vm_end,
		      start = vma->vm_start,
		      size = PAGE_ALIGN(coredev->common_buffer_size);
1199

1200 1201
	if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
	     (vma->vm_flags & VM_WRITE)) {
1202
		printk(KERN_INFO "%s invalid vm flags\n", __func__);
1203 1204 1205
		return -EINVAL;
	}

1206 1207 1208
	if ((end - start) != size) {
		printk(KERN_INFO "%s invalid size %d expected %d\n",
		       __func__, (int)(end - start), (int) size);
1209 1210 1211
		return -EINVAL;
	}

1212 1213 1214
	if (remap_pfn_range(vma, start,
			    coredev->common_buffer_phys >> PAGE_SHIFT,
			    size, pgprot_noncached(vma->vm_page_prot)))
1215
	{
1216
		printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1217 1218 1219 1220 1221 1222 1223 1224
		return -EAGAIN;
	}

	return 0;
}

int smscore_module_init(void)
{
1225
	int rc = 0;
1226 1227 1228 1229 1230 1231 1232 1233

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

1234 1235 1236 1237 1238 1239
	/* USB Register */
	rc = smsusb_register();

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

1240
	printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1241 1242 1243 1244 1245 1246

	return rc;
}

void smscore_module_exit(void)
{
1247

1248
	kmutex_lock(&g_smscore_deviceslock);
1249 1250 1251
	while (!list_empty(&g_smscore_notifyees)) {
		smscore_device_notifyee_t *notifyee =
			(smscore_device_notifyee_t *) g_smscore_notifyees.next;
1252 1253 1254 1255 1256 1257 1258

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

	kmutex_lock(&g_smscore_registrylock);
1259 1260 1261
	while (!list_empty(&g_smscore_registry)) {
		smscore_registry_entry_t *entry =
			(smscore_registry_entry_t *) g_smscore_registry.next;
1262 1263 1264 1265 1266 1267

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

1268 1269 1270 1271 1272 1273
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1274
	printk(KERN_INFO "%s\n", __func__);
1275 1276 1277 1278 1279 1280
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

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