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

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

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

58
struct smscore_idlist_t {
59 60 61
	struct list_head entry;
	int		id;
	int		data_type;
62
};
63

64
struct smscore_client_t {
65
	struct list_head entry;
66
	struct smscore_device_t *coredev;
67
	void			*context;
68
	struct list_head 	idlist;
69 70
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
71
};
72

73
struct smscore_device_t {
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
	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;
104 105

	int board_id;
106
};
107

108 109 110 111 112 113 114 115 116 117
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;
}

118
struct smscore_registry_entry_t {
119 120 121
	struct list_head entry;
	char			devpath[32];
	int				mode;
122 123
	enum sms_device_type_st	type;
};
124 125 126 127 128 129 130 131

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;

132
static int default_mode = 4;
133

134 135 136
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

137
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
138
{
139
	struct smscore_registry_entry_t *entry;
140 141 142
	struct list_head *next;

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

166
int smscore_registry_getmode(char *devpath)
167
{
168
	struct smscore_registry_entry_t *entry;
169

170 171
	entry = smscore_find_registry(devpath);
	if (entry)
172 173
		return entry->mode;
	else
174
		sms_err("%s No registry found.\n", __func__);
175

176 177 178
	return default_mode;
}

179
enum sms_device_type_st smscore_registry_gettype(char *devpath)
180
{
181
	struct smscore_registry_entry_t *entry;
182

183 184
	entry = smscore_find_registry(devpath);
	if (entry)
185 186
		return entry->type;
	else
187
		sms_err("%s No registry found.\n", __func__);
188

189 190
	return -1;
}
191

192 193
void smscore_registry_setmode(char *devpath, int mode)
{
194
	struct smscore_registry_entry_t *entry;
195

196 197 198
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
199
	else
200
		sms_err("%s No registry found.\n", __func__);
201
}
202

203
void smscore_registry_settype(char *devpath, enum sms_device_type_st type)
204
{
205
	struct smscore_registry_entry_t *entry;
206

207 208
	entry = smscore_find_registry(devpath);
	if (entry)
209 210
		entry->type = type;
	else
211
		sms_err("%s No registry found.\n", __func__);
212 213 214
}


215
void list_add_locked(struct list_head *new, struct list_head *head,
216
		     spinlock_t *lock)
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
{
	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)
{
237
	struct smscore_device_notifyee_t *notifyee;
238 239 240 241 242
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

243 244
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
245 246
	if (notifyee) {
		/* now notify callback about existing devices */
247
		first = &g_smscore_devices;
248 249 250
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
251 252
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
253 254 255
			rc = hotplug(coredev, coredev->device, 1);
		}

256
		if (rc >= 0) {
257 258
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
259
		} else
260
			kfree(notifyee);
261
	} else
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		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;

283
	for (next = first->next; next != first;) {
284 285
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
286 287
		next = next->next;

288
		if (notifyee->hotplug == hotplug) {
289 290 291 292 293 294 295 296
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

297
void smscore_notify_clients(struct smscore_device_t *coredev)
298
{
299
	struct smscore_client_t *client;
300

301 302
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
303
		client = (struct smscore_client_t *) coredev->clients.next;
304 305 306 307
		client->onremove_handler(client->context);
	}
}

308 309
int smscore_notify_callbacks(struct smscore_device_t *coredev,
			     struct device *device, int arrival)
310 311 312 313
{
	struct list_head *next, *first;
	int rc = 0;

314
	/* note: must be called under g_deviceslock */
315 316 317

	first = &g_smscore_notifyees;

318
	for (next = first->next; next != first; next = next->next) {
319
		rc = ((struct smscore_device_notifyee_t *) next)->
320
				hotplug(coredev, device, arrival);
321 322 323 324 325 326 327
		if (rc < 0)
			break;
	}

	return rc;
}

328
struct smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
329
				       dma_addr_t common_buffer_phys)
330
{
331 332
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
333
	if (!cb) {
334
		sms_info("%s kmalloc(...) failed\n", __func__);
335 336 337 338
		return NULL;
	}

	cb->p = buffer;
339
	cb->offset_in_common = buffer - (u8 *) common_buffer;
340 341 342 343 344 345
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
346 347
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
348
 *
349 350
 * @param params device pointer to struct with device specific parameters
 *               and handlers
351 352 353 354
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
355 356
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
357
{
358
	struct smscore_device_t *dev;
359 360
	u8 *buffer;

361
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
362
	if (!dev) {
363
		sms_info("%s kzalloc(...) failed\n", __func__);
364 365 366
		return -ENOMEM;
	}

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

370
	/* init queues */
371 372 373
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

374
	/* init locks */
375 376 377
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

378
	/* init completion events */
379 380 381 382 383 384 385
	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);

386
	/* alloc common buffer */
387
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
388 389 390 391
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
392 393 394 395
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

396 397 398
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
399
	     dev->num_buffers++, buffer += params->buffer_size) {
400
		struct smscore_buffer_t *cb =
401 402
			smscore_createbuffer(buffer, dev->common_buffer,
					     dev->common_buffer_phys);
403
		if (!cb) {
404 405 406 407 408 409 410
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

411 412
	sms_info("%s allocated %d buffers\n",
		 __func__, dev->num_buffers);
413 414 415 416 417 418 419 420 421 422 423 424 425

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

426
	smscore_registry_settype(dev->devpath, params->device_type);
427

428
	/* add device to devices list */
429 430 431 432 433 434
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

435
	sms_info("%s device %p created\n", __func__, dev);
436 437 438 439 440 441 442

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
443 444
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
445 446 447
 *
 * @return 0 on success, <0 on error.
 */
448
int smscore_start_device(struct smscore_device_t *coredev)
449
{
450 451
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
452
	if (rc < 0) {
453 454
		sms_info("%s set device mode faile , rc %d\n",
			 __func__, rc);
455
		return rc;
456
	}
457 458 459 460 461

	kmutex_lock(&g_smscore_deviceslock);

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

462 463
	sms_info("%s device %p started, rc %d\n",
		 __func__, coredev, rc);
464 465 466 467 468 469

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

470
int smscore_sendrequest_and_wait(struct smscore_device_t *coredev, void *buffer,
471
				 size_t size, struct completion *completion)
472 473
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
474
	if (rc < 0) {
475 476
		sms_info("%s sendrequest returned error %d\n",
			 __func__, rc);
477
		return rc;
478
	}
479

480 481 482
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(10000)) ?
						0 : -ETIME;
483 484
}

485 486
int smscore_load_firmware_family2(struct smscore_device_t *coredev,
				  void *buffer, size_t size)
487
{
488 489
	struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
	struct SmsMsgHdr_ST *msg;
490
	u32 mem_address = firmware->StartAddress;
491
	u8 *payload = firmware->Payload;
492 493
	int rc = 0;

494 495
	sms_info("%s loading FW to addr 0x%x size %d\n",
		 __func__, mem_address, firmware->Length);
496
	if (coredev->preload_handler) {
497 498 499 500 501
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

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

507
	if (coredev->mode != DEVICE_MODE_NONE) {
508
		PDEBUG("Sending reload command\n");
509
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
510
			     sizeof(struct SmsMsgHdr_ST));
511 512 513
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
514
		mem_address = *(u32 *) &payload[20];
515 516
	}

517
	while (size && rc >= 0) {
518 519
		struct SmsDataDownload_ST *DataMsg =
			(struct SmsDataDownload_ST *) msg;
520 521
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

522
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
523
			     (u16)(sizeof(struct SmsMsgHdr_ST) +
524
				      sizeof(u32) + payload_size));
525 526 527 528

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

529 530
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
531 532 533
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
534
		else
535 536 537 538
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
539 540 541 542 543 544

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

545 546
	if (rc >= 0) {
		if (coredev->mode == DEVICE_MODE_NONE) {
547 548
			struct SmsMsgData_ST *TriggerMsg =
				(struct SmsMsgData_ST *) msg;
549

550
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
551
				     sizeof(struct SmsMsgHdr_ST) +
552
				     sizeof(u32) * 5);
553

554 555
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
556 557 558 559
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
560

561
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
562 563 564
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
565
				msleep(100);
566
			} else
567 568 569 570
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
571 572
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
573
				     sizeof(struct SmsMsgHdr_ST));
574

575 576
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
577
		}
578
		msleep(500);
579 580
	}

581 582
	sms_debug("%s rc=%d, postload=%p \n", __func__, rc,
		  coredev->postload_handler);
583 584 585

	kfree(msg);

586
	return ((rc >= 0) && coredev->postload_handler) ?
587 588 589 590 591 592 593
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
594 595
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
596 597 598 599 600
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
601 602
int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
				    char *filename,
603
				    loadfirmware_t loadfirmware_handler)
604 605 606
{
	int rc = -ENOENT;
	const struct firmware *fw;
607
	u8 *fw_buffer;
608

609 610
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
611 612 613
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
614
	if (rc < 0) {
615 616
		sms_info("%s failed to open \"%s\"\n",
			 __func__, filename);
617 618
		return rc;
	}
619 620
	sms_info("%s read FW %s, size=%d\"\n", __func__,
		 filename, fw->size);
621 622 623
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
624 625 626
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
627 628 629 630 631
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
632 633

		kfree(fw_buffer);
634
	} else {
635 636
		sms_info("%s failed to allocate firmware buffer\n",
			 __func__);
637 638 639 640 641 642 643 644
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

645 646
int smscore_load_firmware_from_buffer(struct smscore_device_t *coredev,
				      u8 *buffer, int size, int new_mode)
647 648 649 650 651
{
	PERROR("Feature not implemented yet\n");
	return -EFAULT;
}

652
/**
653 654
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
655
 *
656 657
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
658 659 660
 *
 * @return 0 on success, <0 on error.
 */
661
void smscore_unregister_device(struct smscore_device_t *coredev)
662
{
663
	struct smscore_buffer_t *cb;
664
	int num_buffers = 0;
665
	int retry = 0;
666 667 668 669 670 671

	kmutex_lock(&g_smscore_deviceslock);

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

672 673
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
674

675 676
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
677
			kfree(cb);
678
			num_buffers++;
679 680 681
		}
		if (num_buffers == coredev->num_buffers)
			break;
682
		if (++retry > 10) {
683 684
			sms_info("%s exiting although "
				 "not all buffers released.\n", __func__);
685 686
			break;
		}
687

688 689
		sms_info("%s waiting for %d buffer(s)\n", __func__,
			 coredev->num_buffers - num_buffers);
690 691 692
		msleep(100);
	}

693
	sms_info("%s freed %d buffers\n", __func__, num_buffers);
694 695

	if (coredev->common_buffer)
696 697 698
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
699 700 701 702 703 704

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

	kmutex_unlock(&g_smscore_deviceslock);

705
	sms_info("%s device %p destroyed\n", __func__, coredev);
706 707
}

708
int smscore_detect_mode(struct smscore_device_t *coredev)
709
{
710
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
711
			       GFP_KERNEL | GFP_DMA);
712 713
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
714 715 716 717 718
	int rc;

	if (!buffer)
		return -ENOMEM;

719 720
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
721

722 723 724
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
725 726
		sms_err("%s: MSG_SMS_GET_VERSION_EX_REQ "
			"failed first try\n", __func__);
727

728 729
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
730 731 732
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
733
			if (rc < 0)
734 735 736
				sms_err("%s: "
					"MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d\n", __func__, rc);
737
		} else
738 739 740 741 742 743 744 745
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

746
char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	/*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"}
764 765
};

766

767 768 769 770
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
771 772
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
773 774 775 776
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
777
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
778 779 780
{
	void *buffer;
	int rc = 0;
781
	enum sms_device_type_st type;
782

783 784 785
	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) {
786 787
			sms_info("%s invalid mode specified %d\n",
				 __func__, mode);
788 789 790
			return -EINVAL;
		}

791 792
		smscore_registry_setmode(coredev->devpath, mode);

793
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
794
			rc = smscore_detect_mode(coredev);
795
			if (rc < 0) {
796 797
				sms_info("%s mode detect failed %d\n",
					 __func__, rc);
798
				return rc;
799
			}
800
		}
801

802
		if (coredev->mode == mode) {
803 804
			sms_info("%s device mode %d already set\n",
				 __func__, mode);
805 806 807
			return 0;
		}

808 809
		if (!(coredev->modes_supported & (1 << mode))) {
			type = smscore_registry_gettype(coredev->devpath);
810 811
			rc = smscore_load_firmware_from_file(
				coredev, smscore_fw_lkup[mode][type], NULL);
812
			if (rc < 0) {
813 814
				sms_info("%s load firmware "
					 "failed %d\n", __func__, rc);
815
				return rc;
816 817
			}
		} else
818 819
			sms_info("%s mode %d supported by running "
				 "firmware\n", __func__, mode);
820

821 822
		buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
823
		if (buffer) {
824 825 826
			struct SmsMsgData_ST *msg =
				(struct SmsMsgData_ST *)
					SMS_ALIGN_ADDRESS(buffer);
827

828
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
829
				     sizeof(struct SmsMsgData_ST));
830 831
			msg->msgData[0] = mode;

832 833 834
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
835 836

			kfree(buffer);
837
		} else {
838 839
			sms_info("%s Could not allocate buffer for "
				 "init device message.\n", __func__);
840
			rc = -ENOMEM;
841 842 843
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
844 845
			sms_info("%s invalid mode specified %d\n",
				 __func__, mode);
846 847 848 849 850
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

851
		if (coredev->detectmode_handler)
852 853
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
854 855 856 857 858

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

859
	if (rc >= 0) {
860 861 862 863
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

864
	if (rc != 0)
865
		sms_info("%s return error code %d.\n", __func__, rc);
866 867 868 869 870 871
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
872 873
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
874 875 876
 *
 * @return current mode
 */
877
int smscore_get_device_mode(struct smscore_device_t *coredev)
878 879 880 881
{
	return coredev->mode;
}

882 883 884 885
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
886 887
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
888
 * @param data_type client data type (SMS_DONT_CARE for all types)
889
 * @param id client id (SMS_DONT_CARE for all id)
890 891
 *
 */
892
struct smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
893
				      int data_type, int id)
894
{
895
	struct smscore_client_t *client = NULL;
896 897
	struct list_head *next, *first;
	unsigned long flags;
898
	struct list_head *firstid, *nextid;
899 900 901 902


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
903 904 905
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
906
		firstid = &((struct smscore_client_t *)next)->idlist;
907 908 909
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
910 911 912 913
			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;
914 915
				break;
			}
916 917 918 919 920 921 922 923 924 925
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
926 927
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
928 929 930
 * @param cb pointer to response buffer descriptor
 *
 */
931 932
void smscore_onresponse(struct smscore_device_t *coredev,
			struct smscore_buffer_t *cb)
933
{
934 935 936
	struct SmsMsgHdr_ST *phdr =
		(struct SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
	struct smscore_client_t *client =
937
		smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
938 939
	int rc = -EBUSY;

940 941
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
942 943 944 945 946
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

947
	if (time_now - last_sample_time > 10000) {
948 949 950
		sms_debug("\n%s data rate %d bytes/secs\n", __func__,
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
951 952 953 954 955 956

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
957 958
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
959 960 961
	if (client)
		rc = client->onresponse_handler(client->context, cb);

962 963 964
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
965
		{
966 967
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
968 969 970 971
			sms_debug("%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);
972

973 974 975
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
976

977 978 979 980
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
981 982
			sms_debug("%s: MSG_SMS_INIT_DEVICE_RES\n",
				  __func__);
983 984 985
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
986 987
			sms_debug("%s: MSG_SW_RELOAD_START_RES\n",
				  __func__);
988 989 990 991 992 993
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
994 995
			sms_debug("%s: MSG_SW_RELOAD_EXEC_RES\n",
				  __func__);
996 997
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
998 999
			sms_debug("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
				  __func__);
1000 1001 1002 1003 1004 1005 1006
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
1007 1008 1009 1010 1011 1012 1013 1014
		}
		smscore_putbuffer(coredev, cb);
	}
}

/**
 * return pointer to next free buffer descriptor from core pool
 *
1015 1016
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1017 1018 1019
 *
 * @return pointer to descriptor on success, NULL on error.
 */
1020
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1021
{
1022
	struct smscore_buffer_t *cb = NULL;
1023 1024 1025 1026
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);

1027
	if (!list_empty(&coredev->buffers)) {
1028
		cb = (struct smscore_buffer_t *) coredev->buffers.next;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
		list_del(&cb->entry);
	}

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

/**
 * return buffer descriptor to a pool
 *
1040 1041
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1042 1043 1044
 * @param cb pointer buffer descriptor
 *
 */
1045 1046
void smscore_putbuffer(struct smscore_device_t *coredev,
		       struct smscore_buffer_t *cb)
1047 1048 1049 1050
{
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}

1051 1052 1053
int smscore_validate_client(struct smscore_device_t *coredev,
			    struct smscore_client_t *client,
			    int data_type, int id)
1054
{
1055 1056
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1057

1058
	if (!client) {
1059 1060 1061 1062
		PERROR("bad parameter.\n");
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1063
	if (registered_client == client)
1064
		return 0;
1065

1066
	if (registered_client) {
1067 1068 1069
		PERROR("The msg ID already registered to another client.\n");
		return -EEXIST;
	}
1070
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1071
	if (!listentry) {
1072
		PERROR("Can't allocate memory for client id.\n");
1073
		return -ENOMEM;
1074 1075 1076
	}
	listentry->id = id;
	listentry->data_type = data_type;
1077 1078
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1079 1080 1081 1082 1083 1084 1085 1086 1087
	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
1088 1089
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1090 1091 1092 1093 1094 1095
 * @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.
 */
1096 1097 1098
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1099
{
1100
	struct smscore_client_t *newclient;
1101 1102 1103
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1104
		PERROR("Client already exist.\n");
1105
		return -EEXIST;
1106
	}
1107

1108
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1109
	if (!newclient) {
1110 1111
		PERROR("Failed to allocate memory for client.\n");
		return -ENOMEM;
1112 1113
	}

1114
	INIT_LIST_HEAD(&newclient->idlist);
1115 1116 1117 1118
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1119 1120 1121 1122
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1123
	*client = newclient;
1124 1125
	PDEBUG("%p %d %d\n", params->context, params->data_type,
	       params->initial_id);
1126 1127 1128 1129 1130 1131 1132

	return 0;
}

/**
 * frees smsclient object and all subclients associated with it
 *
1133 1134
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1135 1136
 *
 */
1137
void smscore_unregister_client(struct smscore_client_t *client)
1138
{
1139
	struct smscore_device_t *coredev = client->coredev;
1140 1141 1142 1143 1144
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1145
	while (!list_empty(&client->idlist)) {
1146 1147
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1148 1149
		list_del(&identry->entry);
		kfree(identry);
1150 1151
	}

1152
	sms_info("%s %p\n", __func__, client->context);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

	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
 *
1164 1165
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1166 1167 1168 1169 1170
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1171 1172
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1173
{
1174 1175
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1176 1177
	int rc;

1178
	if (client == NULL) {
1179
		sms_err("%s Got NULL client\n", __func__);
1180 1181 1182 1183
		return -EINVAL;
	}

	coredev = client->coredev;
1184

1185 1186
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1187
		sms_err("%s Got NULL coredev\n", __func__);
1188 1189 1190
		return -EINVAL;
	}

1191 1192
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	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
 */
1206
int smscore_get_common_buffer_size(struct smscore_device_t *coredev)
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
{
	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.
 */
1219
int smscore_map_common_buffer(struct smscore_device_t *coredev,
1220
			       struct vm_area_struct *vma)
1221
{
1222 1223 1224
	unsigned long end = vma->vm_end,
		      start = vma->vm_start,
		      size = PAGE_ALIGN(coredev->common_buffer_size);
1225

1226 1227
	if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
	     (vma->vm_flags & VM_WRITE)) {
1228
		sms_info("%s invalid vm flags\n", __func__);
1229 1230 1231
		return -EINVAL;
	}

1232
	if ((end - start) != size) {
1233 1234
		sms_info("%s invalid size %d expected %d\n",
			 __func__, (int)(end - start), (int) size);
1235 1236 1237
		return -EINVAL;
	}

1238 1239
	if (remap_pfn_range(vma, start,
			    coredev->common_buffer_phys >> PAGE_SHIFT,
1240
			    size, pgprot_noncached(vma->vm_page_prot))) {
1241
		sms_info("%s remap_page_range failed\n", __func__);
1242 1243 1244 1245 1246 1247 1248 1249
		return -EAGAIN;
	}

	return 0;
}

int smscore_module_init(void)
{
1250
	int rc = 0;
1251 1252 1253 1254 1255 1256 1257 1258

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

1259 1260 1261 1262 1263 1264
	/* USB Register */
	rc = smsusb_register();

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

1265
	sms_info("%s, rc %d\n", __func__, rc);
1266 1267 1268 1269 1270 1271

	return rc;
}

void smscore_module_exit(void)
{
1272

1273
	kmutex_lock(&g_smscore_deviceslock);
1274
	while (!list_empty(&g_smscore_notifyees)) {
1275 1276 1277
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1278 1279 1280 1281 1282 1283 1284

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

	kmutex_lock(&g_smscore_registrylock);
1285
	while (!list_empty(&g_smscore_registry)) {
1286 1287 1288
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1289 1290 1291 1292 1293 1294

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

1295 1296 1297 1298 1299 1300
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1301
	sms_info("%s\n", __func__);
1302 1303 1304 1305 1306 1307
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

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