smscoreapi.c 33.9 KB
Newer Older
1
/*
2 3 4 5
 *  Siano core API module
 *
 *  This file contains implementation for the interface to sms core component
 *
6
 *  author: Uri Shkolnik
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
 *  it under the terms of the GNU General Public License version 2 as
12
 *  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
#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>
30
#include <linux/io.h>
31 32 33 34

#include <linux/firmware.h>

#include "smscoreapi.h"
35
#include "sms-cards.h"
36

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

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

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

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

61
struct smscore_device_t {
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
	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;
92 93

	int board_id;
94
	int led_state;
95
};
96

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

102 103 104 105 106 107
int smscore_led_state(struct smscore_device_t *core, int led)
{
	if (led >= 0)
		core->led_state = led;
	return core->led_state;
}
108
EXPORT_SYMBOL_GPL(smscore_set_board_id);
109

110 111 112 113
int smscore_get_board_id(struct smscore_device_t *core)
{
	return core->board_id;
}
114
EXPORT_SYMBOL_GPL(smscore_get_board_id);
115

116
struct smscore_registry_entry_t {
117 118 119
	struct list_head entry;
	char			devpath[32];
	int				mode;
120 121
	enum sms_device_type_st	type;
};
122

123 124 125
static struct list_head g_smscore_notifyees;
static struct list_head g_smscore_devices;
static struct mutex g_smscore_deviceslock;
126

127 128
static struct list_head g_smscore_registry;
static struct mutex g_smscore_registrylock;
129

130
static int default_mode = 4;
131

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

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

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

163
int smscore_registry_getmode(char *devpath)
164
{
165
	struct smscore_registry_entry_t *entry;
166

167 168
	entry = smscore_find_registry(devpath);
	if (entry)
169 170
		return entry->mode;
	else
171
		sms_err("No registry found.");
172

173 174
	return default_mode;
}
175
EXPORT_SYMBOL_GPL(smscore_registry_getmode);
176

177
static enum sms_device_type_st smscore_registry_gettype(char *devpath)
178
{
179
	struct smscore_registry_entry_t *entry;
180

181 182
	entry = smscore_find_registry(devpath);
	if (entry)
183 184
		return entry->type;
	else
185
		sms_err("No registry found.");
186

187 188
	return -1;
}
189

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

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

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

206 207
	entry = smscore_find_registry(devpath);
	if (entry)
208 209
		entry->type = type;
	else
210
		sms_err("No registry found.");
211 212 213
}


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

	kmutex_lock(&g_smscore_deviceslock);

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

255
		if (rc >= 0) {
256 257
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
258
		} else
259
			kfree(notifyee);
260
	} else
261 262 263 264 265 266
		rc = -ENOMEM;

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
267
EXPORT_SYMBOL_GPL(smscore_register_hotplug);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

/**
 * 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
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}
296
EXPORT_SYMBOL_GPL(smscore_unregister_hotplug);
297

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

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

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

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

	first = &g_smscore_notifyees;

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

	return rc;
}

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

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

	return cb;
}

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

363
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
364
	if (!dev) {
365
		sms_info("kzalloc(...) failed");
366 367 368
		return -ENOMEM;
	}

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

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

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

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

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

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

		smscore_putbuffer(dev, cb);
	}

413
	sms_info("allocated %d buffers", dev->num_buffers);
414 415 416 417 418 419 420 421 422 423 424 425 426

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

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

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

	*coredev = dev;

436
	sms_info("device %p created", dev);
437 438 439

	return 0;
}
440
EXPORT_SYMBOL_GPL(smscore_register_device);
441 442 443 444

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

	kmutex_lock(&g_smscore_deviceslock);

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

463
	sms_info("device %p started, rc %d", coredev, rc);
464 465 466 467 468

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
469
EXPORT_SYMBOL_GPL(smscore_start_device);
470

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

582
	sms_debug("rc=%d, postload=%p ", rc,
583
		  coredev->postload_handler);
584 585 586

	kfree(msg);

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

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

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

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

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

		kfree(fw_buffer);
633
	} else {
634
		sms_info("failed to allocate firmware buffer");
635 636 637 638 639 640 641 642 643
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

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

	kmutex_lock(&g_smscore_deviceslock);

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

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

666 667
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
668
			kfree(cb);
669
			num_buffers++;
670 671 672
		}
		if (num_buffers == coredev->num_buffers)
			break;
673
		if (++retry > 10) {
674 675
			sms_info("exiting although "
				 "not all buffers released.");
676 677
			break;
		}
678

679
		sms_info("waiting for %d buffer(s)",
680
			 coredev->num_buffers - num_buffers);
681 682 683
		msleep(100);
	}

684
	sms_info("freed %d buffers", num_buffers);
685 686

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

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

	kmutex_unlock(&g_smscore_deviceslock);

696
	sms_info("device %p destroyed", coredev);
697
}
698
EXPORT_SYMBOL_GPL(smscore_unregister_device);
699

700
static int smscore_detect_mode(struct smscore_device_t *coredev)
701
{
702
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
703
			       GFP_KERNEL | GFP_DMA);
704 705
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
706 707 708 709 710
	int rc;

	if (!buffer)
		return -ENOMEM;

711 712
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
713

714 715 716
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
717
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
718

719 720
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
721 722 723
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
724
			if (rc < 0)
725 726
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d", rc);
727
		} else
728 729 730 731 732 733 734 735
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

736
static char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
737 738 739 740 741 742
	/*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*/
743
	{"none", "tdmb_nova_12mhz.inp", "tdmb_nova_12mhz_b0.inp", "none"},
744 745 746 747 748 749 750 751 752 753
	/*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"}
754 755
};

756 757 758 759 760 761
static inline char *sms_get_fw_name(struct smscore_device_t *coredev,
				    int mode, enum sms_device_type_st type)
{
	char **fw = sms_get_board(smscore_get_board_id(coredev))->fw;
	return (fw && fw[mode]) ? fw[mode] : smscore_fw_lkup[mode][type];
}
762

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

779
	sms_debug("set device mode to %d", mode);
780 781
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
782
			sms_err("invalid mode specified %d", mode);
783 784 785
			return -EINVAL;
		}

786 787
		smscore_registry_setmode(coredev->devpath, mode);

788
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
789
			rc = smscore_detect_mode(coredev);
790
			if (rc < 0) {
791
				sms_err("mode detect failed %d", rc);
792
				return rc;
793
			}
794
		}
795

796
		if (coredev->mode == mode) {
797
			sms_info("device mode %d already set", mode);
798 799 800
			return 0;
		}

801
		if (!(coredev->modes_supported & (1 << mode))) {
802 803
			char *fw_filename;

804
			type = smscore_registry_gettype(coredev->devpath);
805 806 807 808
			fw_filename = sms_get_fw_name(coredev, mode, type);

			rc = smscore_load_firmware_from_file(coredev,
							     fw_filename, NULL);
809
			if (rc < 0) {
810 811 812
				sms_warn("error %d loading firmware: %s, "
					 "trying again with default firmware",
					 rc, fw_filename);
813 814

				/* try again with the default firmware */
815
				fw_filename = smscore_fw_lkup[mode][type];
816
				rc = smscore_load_firmware_from_file(coredev,
817
							     fw_filename, NULL);
818 819

				if (rc < 0) {
820 821 822
					sms_warn("error %d loading "
						 "firmware: %s", rc,
						 fw_filename);
823 824
					return rc;
				}
825
			}
826
			sms_log("firmware download success: %s", fw_filename);
827
		} else
828 829
			sms_info("mode %d supported by running "
				 "firmware", mode);
830

831 832
		buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
833
		if (buffer) {
834 835 836
			struct SmsMsgData_ST *msg =
				(struct SmsMsgData_ST *)
					SMS_ALIGN_ADDRESS(buffer);
837

838
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
839
				     sizeof(struct SmsMsgData_ST));
840 841
			msg->msgData[0] = mode;

842 843 844
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
845 846

			kfree(buffer);
847
		} else {
848 849
			sms_err("Could not allocate buffer for "
				"init device message.");
850
			rc = -ENOMEM;
851 852 853
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
854
			sms_err("invalid mode specified %d", mode);
855 856 857 858 859
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

860
		if (coredev->detectmode_handler)
861 862
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
863 864 865 866 867

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

868
	if (rc >= 0) {
869 870 871 872
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

873
	if (rc != 0)
874
		sms_err("return error code %d.", rc);
875 876 877 878 879 880
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
881 882
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
883 884 885
 *
 * @return current mode
 */
886
int smscore_get_device_mode(struct smscore_device_t *coredev)
887 888 889
{
	return coredev->mode;
}
890
EXPORT_SYMBOL_GPL(smscore_get_device_mode);
891

892 893 894 895
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
896 897
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
898
 * @param data_type client data type (SMS_DONT_CARE for all types)
899
 * @param id client id (SMS_DONT_CARE for all id)
900 901
 *
 */
902 903
static struct
smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
904
				      int data_type, int id)
905
{
906
	struct smscore_client_t *client = NULL;
907 908
	struct list_head *next, *first;
	unsigned long flags;
909
	struct list_head *firstid, *nextid;
910 911 912 913


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
914 915 916
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
917
		firstid = &((struct smscore_client_t *)next)->idlist;
918 919 920
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
921 922 923 924
			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;
925 926
				break;
			}
927 928 929 930 931 932 933 934 935 936
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
937 938
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
939 940 941
 * @param cb pointer to response buffer descriptor
 *
 */
942 943
void smscore_onresponse(struct smscore_device_t *coredev,
			struct smscore_buffer_t *cb)
944
{
945 946 947
	struct SmsMsgHdr_ST *phdr =
		(struct SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
	struct smscore_client_t *client =
948
		smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
949 950
	int rc = -EBUSY;

951 952
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
953 954 955 956 957
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

958
	if (time_now - last_sample_time > 10000) {
959
		sms_debug("\ndata rate %d bytes/secs",
960 961
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
962 963 964 965 966 967

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
968 969
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
970 971 972
	if (client)
		rc = client->onresponse_handler(client->context, cb);

973 974 975
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
976
		{
977 978
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
979 980
			sms_debug("MSG_SMS_GET_VERSION_EX_RES "
				  "id %d prots 0x%x ver %d.%d",
981 982
				  ver->FirmwareId, ver->SupportedProtocols,
				  ver->RomVersionMajor, ver->RomVersionMinor);
983

984 985 986
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
987

988 989 990 991
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
992
			sms_debug("MSG_SMS_INIT_DEVICE_RES");
993 994 995
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
996
			sms_debug("MSG_SW_RELOAD_START_RES");
997 998 999 1000 1001 1002
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
1003
			sms_debug("MSG_SW_RELOAD_EXEC_RES");
1004 1005
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
1006
			sms_debug("MSG_SMS_SWDOWNLOAD_TRIGGER_RES");
1007 1008 1009 1010 1011 1012 1013
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
1014 1015 1016 1017
		}
		smscore_putbuffer(coredev, cb);
	}
}
1018
EXPORT_SYMBOL_GPL(smscore_onresponse);
1019 1020 1021 1022

/**
 * return pointer to next free buffer descriptor from core pool
 *
1023 1024
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1025 1026 1027
 *
 * @return pointer to descriptor on success, NULL on error.
 */
1028
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1029
{
1030
	struct smscore_buffer_t *cb = NULL;
1031 1032 1033 1034
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);

1035
	if (!list_empty(&coredev->buffers)) {
1036
		cb = (struct smscore_buffer_t *) coredev->buffers.next;
1037 1038 1039 1040 1041 1042 1043
		list_del(&cb->entry);
	}

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}
1044
EXPORT_SYMBOL_GPL(smscore_getbuffer);
1045 1046 1047 1048

/**
 * return buffer descriptor to a pool
 *
1049 1050
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1051 1052 1053
 * @param cb pointer buffer descriptor
 *
 */
1054 1055
void smscore_putbuffer(struct smscore_device_t *coredev,
		       struct smscore_buffer_t *cb)
1056 1057 1058
{
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}
1059
EXPORT_SYMBOL_GPL(smscore_putbuffer);
1060

1061 1062 1063
static int smscore_validate_client(struct smscore_device_t *coredev,
				   struct smscore_client_t *client,
				   int data_type, int id)
1064
{
1065 1066
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1067

1068
	if (!client) {
1069
		sms_err("bad parameter.");
1070 1071 1072
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1073
	if (registered_client == client)
1074
		return 0;
1075

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

1118
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1119
	if (!newclient) {
1120
		sms_err("Failed to allocate memory for client.");
1121
		return -ENOMEM;
1122 1123
	}

1124
	INIT_LIST_HEAD(&newclient->idlist);
1125 1126 1127 1128
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1129 1130 1131 1132
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1133
	*client = newclient;
1134 1135
	sms_debug("%p %d %d", params->context, params->data_type,
		  params->initial_id);
1136 1137 1138

	return 0;
}
1139
EXPORT_SYMBOL_GPL(smscore_register_client);
1140 1141 1142 1143

/**
 * frees smsclient object and all subclients associated with it
 *
1144 1145
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1146 1147
 *
 */
1148
void smscore_unregister_client(struct smscore_client_t *client)
1149
{
1150
	struct smscore_device_t *coredev = client->coredev;
1151 1152 1153 1154 1155
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1156
	while (!list_empty(&client->idlist)) {
1157 1158
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1159 1160
		list_del(&identry->entry);
		kfree(identry);
1161 1162
	}

1163
	sms_info("%p", client->context);
1164 1165 1166 1167 1168 1169

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

	spin_unlock_irqrestore(&coredev->clientslock, flags);
}
1170
EXPORT_SYMBOL_GPL(smscore_unregister_client);
1171 1172 1173 1174 1175

/**
 * verifies that source id is not taken by another client,
 * calls device handler to send requests to the device
 *
1176 1177
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1178 1179 1180 1181 1182
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1183 1184
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1185
{
1186 1187
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1188 1189
	int rc;

1190
	if (client == NULL) {
1191
		sms_err("Got NULL client");
1192 1193 1194 1195
		return -EINVAL;
	}

	coredev = client->coredev;
1196

1197 1198
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1199
		sms_err("Got NULL coredev");
1200 1201 1202
		return -EINVAL;
	}

1203 1204
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1205 1206 1207 1208 1209
	if (rc < 0)
		return rc;

	return coredev->sendrequest_handler(coredev->context, buffer, size);
}
1210
EXPORT_SYMBOL_GPL(smsclient_sendrequest);
1211 1212


1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
int smscore_configure_gpio(struct smscore_device_t *coredev, u32 pin,
			   struct smscore_gpio_config *pinconfig)
{
	struct {
		struct SmsMsgHdr_ST hdr;
		u32 data[6];
	} msg;

	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		msg.hdr.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
		msg.hdr.msgDstId = HIF_TASK;
		msg.hdr.msgFlags = 0;
		msg.hdr.msgType  = MSG_SMS_GPIO_CONFIG_EX_REQ;
		msg.hdr.msgLength = sizeof(msg);

		msg.data[0] = pin;
		msg.data[1] = pinconfig->pullupdown;

		/* Convert slew rate for Nova: Fast(0) = 3 / Slow(1) = 0; */
		msg.data[2] = pinconfig->outputslewrate == 0 ? 3 : 0;

		switch (pinconfig->outputdriving) {
		case SMS_GPIO_OUTPUTDRIVING_16mA:
			msg.data[3] = 7; /* Nova - 16mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_12mA:
			msg.data[3] = 5; /* Nova - 11mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_8mA:
			msg.data[3] = 3; /* Nova - 7mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_4mA:
		default:
			msg.data[3] = 2; /* Nova - 4mA */
			break;
		}

		msg.data[4] = pinconfig->direction;
		msg.data[5] = 0;
	} else /* TODO: SMS_DEVICE_FAMILY1 */
		return -EINVAL;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

int smscore_set_gpio(struct smscore_device_t *coredev, u32 pin, int level)
{
	struct {
		struct SmsMsgHdr_ST hdr;
		u32 data[3];
	} msg;

	if (pin > MAX_GPIO_PIN_NUMBER)
		return -EINVAL;

	msg.hdr.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	msg.hdr.msgDstId = HIF_TASK;
	msg.hdr.msgFlags = 0;
	msg.hdr.msgType  = MSG_SMS_GPIO_SET_LEVEL_REQ;
	msg.hdr.msgLength = sizeof(msg);

	msg.data[0] = pin;
	msg.data[1] = level ? 1 : 0;
	msg.data[2] = 0;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

1283
static int __init smscore_module_init(void)
1284
{
1285
	int rc = 0;
1286 1287 1288 1289 1290 1291 1292 1293

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

1294 1295


1296 1297 1298 1299



	return rc;
1300
	sms_debug("rc %d", rc);
1301 1302 1303 1304

	return rc;
}

1305
static void __exit smscore_module_exit(void)
1306
{
1307

1308 1309 1310 1311




1312
	kmutex_lock(&g_smscore_deviceslock);
1313
	while (!list_empty(&g_smscore_notifyees)) {
1314 1315 1316
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1317 1318 1319 1320 1321 1322 1323

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

	kmutex_lock(&g_smscore_registrylock);
1324
	while (!list_empty(&g_smscore_registry)) {
1325 1326 1327
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1328 1329 1330 1331 1332 1333

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

1334
	sms_debug("");
1335 1336 1337 1338 1339
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

1340 1341
MODULE_DESCRIPTION("Siano MDTV Core module");
MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1342
MODULE_LICENSE("GPL");