]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/dvb/siano/smscoreapi.c
V4L/DVB (8292): sms1xxx: add code to allow device-specific functionality
[mirror_ubuntu-artful-kernel.git] / drivers / media / dvb / siano / smscoreapi.c
CommitLineData
8d4f9d0e 1/*
85447060
MK
2 * Siano core API module
3 *
4 * This file contains implementation for the interface to sms core component
5 *
6 * author: Anatoly Greenblat
8d4f9d0e 7 *
85447060 8 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
8d4f9d0e
ST
9 *
10 * This program is free software; you can redistribute it and/or modify
85447060
MK
11 * it under the terms of the GNU General Public License version 3 as
12 * published by the Free Software Foundation;
8d4f9d0e 13 *
85447060
MK
14 * Software distributed under the License is distributed on an "AS IS"
15 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
8d4f9d0e 16 *
85447060 17 * See the GNU General Public License for more details.
8d4f9d0e
ST
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
2e5c1ec8
MK
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/dma-mapping.h>
29#include <linux/delay.h>
30#include <asm/io.h>
31
2e5c1ec8
MK
32#include <linux/firmware.h>
33
34#include "smscoreapi.h"
2e5c1ec8 35
82237416
MK
36#define PERROR(fmt, args...)\
37 printk(KERN_ERR "smscore error: line %d- %s(): " fmt, \
38 __LINE__, __func__, ## args)
f17407a8
MK
39
40#ifdef SMSCORE_DEBUG
f17407a8 41#undef PWARNING
82237416
MK
42# define PWARNING(fmt, args...) printk(KERN_INFO "smscore warning: " \
43 "line %d- %s(): " fmt, \
44 __LINE__, __func__, ## args)
f17407a8 45#undef PDEBUG /* undef it, just in case */
82237416
MK
46# define PDEBUG(fmt, args...) printk(KERN_INFO "smscore - %s(): " fmt, \
47 __func__, ## args)
f17407a8 48#else /*SMSCORE_DEBUG*/
f17407a8
MK
49#define PDEBUG(fmt, args...)
50#define PWARNING(fmt, args...)
f17407a8
MK
51#endif
52
18245e18 53struct smscore_device_notifyee_t {
2e5c1ec8
MK
54 struct list_head entry;
55 hotplug_t hotplug;
18245e18 56};
2e5c1ec8 57
18245e18 58struct smscore_idlist_t {
f17407a8
MK
59 struct list_head entry;
60 int id;
61 int data_type;
18245e18 62};
f17407a8 63
18245e18 64struct smscore_client_t {
2e5c1ec8 65 struct list_head entry;
18245e18 66 struct smscore_device_t *coredev;
2e5c1ec8 67 void *context;
f17407a8 68 struct list_head idlist;
2e5c1ec8
MK
69 onresponse_t onresponse_handler;
70 onremove_t onremove_handler;
18245e18 71};
2e5c1ec8 72
18245e18 73struct smscore_device_t {
2e5c1ec8
MK
74 struct list_head entry;
75
76 struct list_head clients;
77 struct list_head subclients;
78 spinlock_t clientslock;
79
80 struct list_head buffers;
81 spinlock_t bufferslock;
82 int num_buffers;
83
84 void *common_buffer;
85 int common_buffer_size;
86 dma_addr_t common_buffer_phys;
87
88 void *context;
89 struct device *device;
90
91 char devpath[32];
92 unsigned long device_flags;
93
94 setmode_t setmode_handler;
95 detectmode_t detectmode_handler;
96 sendrequest_t sendrequest_handler;
97 preload_t preload_handler;
98 postload_t postload_handler;
99
100 int mode, modes_supported;
101
102 struct completion version_ex_done, data_download_done, trigger_done;
103 struct completion init_device_done, reload_start_done, resume_done;
1c11d546
MK
104
105 int board_id;
18245e18 106};
2e5c1ec8 107
1c11d546
MK
108void smscore_set_board_id(struct smscore_device_t *core, int id)
109{
110 core->board_id = id;
111}
112
113int smscore_get_board_id(struct smscore_device_t *core)
114{
115 return core->board_id;
116}
117
18245e18 118struct smscore_registry_entry_t {
2e5c1ec8
MK
119 struct list_head entry;
120 char devpath[32];
121 int mode;
18245e18
MK
122 enum sms_device_type_st type;
123};
2e5c1ec8
MK
124
125struct list_head g_smscore_notifyees;
126struct list_head g_smscore_devices;
127kmutex_t g_smscore_deviceslock;
128
129struct list_head g_smscore_registry;
130kmutex_t g_smscore_registrylock;
131
dd5b2a5c 132static int default_mode = 4;
f17407a8 133
2e5c1ec8
MK
134module_param(default_mode, int, 0644);
135MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
136
18245e18 137static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
2e5c1ec8 138{
18245e18 139 struct smscore_registry_entry_t *entry;
2e5c1ec8
MK
140 struct list_head *next;
141
142 kmutex_lock(&g_smscore_registrylock);
82237416
MK
143 for (next = g_smscore_registry.next;
144 next != &g_smscore_registry;
145 next = next->next) {
18245e18 146 entry = (struct smscore_registry_entry_t *) next;
82237416 147 if (!strcmp(entry->devpath, devpath)) {
2e5c1ec8 148 kmutex_unlock(&g_smscore_registrylock);
f17407a8 149 return entry;
2e5c1ec8
MK
150 }
151 }
18245e18
MK
152 entry = (struct smscore_registry_entry_t *)
153 kmalloc(sizeof(struct smscore_registry_entry_t),
154 GFP_KERNEL);
82237416 155 if (entry) {
2e5c1ec8
MK
156 entry->mode = default_mode;
157 strcpy(entry->devpath, devpath);
2e5c1ec8 158 list_add(&entry->entry, &g_smscore_registry);
82237416
MK
159 } else
160 printk(KERN_ERR "%s failed to create smscore_registry.\n",
161 __func__);
2e5c1ec8 162 kmutex_unlock(&g_smscore_registrylock);
f17407a8
MK
163 return entry;
164}
2e5c1ec8 165
82237416 166int smscore_registry_getmode(char *devpath)
f17407a8 167{
18245e18 168 struct smscore_registry_entry_t *entry;
f17407a8 169
82237416
MK
170 entry = smscore_find_registry(devpath);
171 if (entry)
f17407a8 172 return entry->mode;
f17407a8 173 else
82237416
MK
174 printk(KERN_ERR "%s No registry found.\n", __func__);
175
2e5c1ec8
MK
176 return default_mode;
177}
178
18245e18 179enum sms_device_type_st smscore_registry_gettype(char *devpath)
2e5c1ec8 180{
18245e18 181 struct smscore_registry_entry_t *entry;
2e5c1ec8 182
82237416
MK
183 entry = smscore_find_registry(devpath);
184 if (entry)
f17407a8 185 return entry->type;
f17407a8 186 else
82237416
MK
187 printk(KERN_ERR "%s No registry found.\n", __func__);
188
f17407a8
MK
189 return -1;
190}
2e5c1ec8 191
82237416
MK
192void smscore_registry_setmode(char *devpath, int mode)
193{
18245e18 194 struct smscore_registry_entry_t *entry;
2e5c1ec8 195
82237416
MK
196 entry = smscore_find_registry(devpath);
197 if (entry)
198 entry->mode = mode;
f17407a8 199 else
82237416
MK
200 printk(KERN_ERR "%s No registry found.\n", __func__);
201}
2e5c1ec8 202
18245e18 203void smscore_registry_settype(char *devpath, enum sms_device_type_st type)
f17407a8 204{
18245e18 205 struct smscore_registry_entry_t *entry;
f17407a8 206
82237416
MK
207 entry = smscore_find_registry(devpath);
208 if (entry)
f17407a8 209 entry->type = type;
f17407a8 210 else
82237416 211 printk(KERN_ERR "%s No registry found.\n", __func__);
2e5c1ec8
MK
212}
213
214
a83ccdd6 215void list_add_locked(struct list_head *new, struct list_head *head,
82237416 216 spinlock_t *lock)
2e5c1ec8
MK
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(lock, flags);
221
222 list_add(new, head);
223
224 spin_unlock_irqrestore(lock, flags);
225}
226
227/**
228 * register a client callback that called when device plugged in/unplugged
229 * NOTE: if devices exist callback is called immediately for each device
230 *
231 * @param hotplug callback
232 *
233 * @return 0 on success, <0 on error.
234 */
235int smscore_register_hotplug(hotplug_t hotplug)
236{
18245e18 237 struct smscore_device_notifyee_t *notifyee;
2e5c1ec8
MK
238 struct list_head *next, *first;
239 int rc = 0;
240
241 kmutex_lock(&g_smscore_deviceslock);
242
18245e18
MK
243 notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
244 GFP_KERNEL);
82237416
MK
245 if (notifyee) {
246 /* now notify callback about existing devices */
2e5c1ec8 247 first = &g_smscore_devices;
82237416
MK
248 for (next = first->next;
249 next != first && !rc;
250 next = next->next) {
18245e18
MK
251 struct smscore_device_t *coredev =
252 (struct smscore_device_t *) next;
2e5c1ec8
MK
253 rc = hotplug(coredev, coredev->device, 1);
254 }
255
82237416 256 if (rc >= 0) {
2e5c1ec8
MK
257 notifyee->hotplug = hotplug;
258 list_add(&notifyee->entry, &g_smscore_notifyees);
82237416 259 } else
2e5c1ec8 260 kfree(notifyee);
82237416 261 } else
2e5c1ec8
MK
262 rc = -ENOMEM;
263
264 kmutex_unlock(&g_smscore_deviceslock);
265
266 return rc;
267}
268
269/**
270 * unregister a client callback that called when device plugged in/unplugged
271 *
272 * @param hotplug callback
273 *
274 */
275void smscore_unregister_hotplug(hotplug_t hotplug)
276{
277 struct list_head *next, *first;
278
279 kmutex_lock(&g_smscore_deviceslock);
280
281 first = &g_smscore_notifyees;
282
82237416 283 for (next = first->next; next != first;) {
18245e18
MK
284 struct smscore_device_notifyee_t *notifyee =
285 (struct smscore_device_notifyee_t *) next;
2e5c1ec8
MK
286 next = next->next;
287
82237416 288 if (notifyee->hotplug == hotplug) {
2e5c1ec8
MK
289 list_del(&notifyee->entry);
290 kfree(notifyee);
291 }
292 }
293
294 kmutex_unlock(&g_smscore_deviceslock);
295}
296
18245e18 297void smscore_notify_clients(struct smscore_device_t *coredev)
2e5c1ec8 298{
18245e18 299 struct smscore_client_t *client;
2e5c1ec8 300
82237416
MK
301 /* the client must call smscore_unregister_client from remove handler */
302 while (!list_empty(&coredev->clients)) {
18245e18 303 client = (struct smscore_client_t *) coredev->clients.next;
2e5c1ec8
MK
304 client->onremove_handler(client->context);
305 }
306}
307
18245e18
MK
308int smscore_notify_callbacks(struct smscore_device_t *coredev,
309 struct device *device, int arrival)
2e5c1ec8
MK
310{
311 struct list_head *next, *first;
312 int rc = 0;
313
82237416 314 /* note: must be called under g_deviceslock */
2e5c1ec8
MK
315
316 first = &g_smscore_notifyees;
317
82237416 318 for (next = first->next; next != first; next = next->next) {
18245e18 319 rc = ((struct smscore_device_notifyee_t *) next)->
59bf6b8e 320 hotplug(coredev, device, arrival);
2e5c1ec8
MK
321 if (rc < 0)
322 break;
323 }
324
325 return rc;
326}
327
18245e18 328struct smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
a83ccdd6 329 dma_addr_t common_buffer_phys)
2e5c1ec8 330{
18245e18
MK
331 struct smscore_buffer_t *cb =
332 kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
82237416 333 if (!cb) {
18658117 334 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
2e5c1ec8
MK
335 return NULL;
336 }
337
338 cb->p = buffer;
494d24c5 339 cb->offset_in_common = buffer - (u8 *) common_buffer;
2e5c1ec8
MK
340 cb->phys = common_buffer_phys + cb->offset_in_common;
341
342 return cb;
343}
344
345/**
82237416
MK
346 * creates coredev object for a device, prepares buffers,
347 * creates buffer mappings, notifies registered hotplugs about new device.
2e5c1ec8 348 *
59bf6b8e
MK
349 * @param params device pointer to struct with device specific parameters
350 * and handlers
2e5c1ec8
MK
351 * @param coredev pointer to a value that receives created coredev object
352 *
353 * @return 0 on success, <0 on error.
354 */
18245e18
MK
355int smscore_register_device(struct smsdevice_params_t *params,
356 struct smscore_device_t **coredev)
2e5c1ec8 357{
18245e18 358 struct smscore_device_t *dev;
2e5c1ec8
MK
359 u8 *buffer;
360
18245e18 361 dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
82237416 362 if (!dev) {
18658117 363 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
2e5c1ec8
MK
364 return -ENOMEM;
365 }
366
82237416 367 /* init list entry so it could be safe in smscore_unregister_device */
2e5c1ec8
MK
368 INIT_LIST_HEAD(&dev->entry);
369
82237416 370 /* init queues */
2e5c1ec8 371 INIT_LIST_HEAD(&dev->clients);
2e5c1ec8
MK
372 INIT_LIST_HEAD(&dev->buffers);
373
82237416 374 /* init locks */
2e5c1ec8
MK
375 spin_lock_init(&dev->clientslock);
376 spin_lock_init(&dev->bufferslock);
377
82237416 378 /* init completion events */
2e5c1ec8
MK
379 init_completion(&dev->version_ex_done);
380 init_completion(&dev->data_download_done);
381 init_completion(&dev->trigger_done);
382 init_completion(&dev->init_device_done);
383 init_completion(&dev->reload_start_done);
384 init_completion(&dev->resume_done);
385
82237416 386 /* alloc common buffer */
2e5c1ec8 387 dev->common_buffer_size = params->buffer_size * params->num_buffers;
82237416
MK
388 dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
389 &dev->common_buffer_phys,
390 GFP_KERNEL | GFP_DMA);
391 if (!dev->common_buffer) {
2e5c1ec8
MK
392 smscore_unregister_device(dev);
393 return -ENOMEM;
394 }
395
82237416
MK
396 /* prepare dma buffers */
397 for (buffer = dev->common_buffer;
398 dev->num_buffers < params->num_buffers;
fa830e8a 399 dev->num_buffers++, buffer += params->buffer_size) {
18245e18 400 struct smscore_buffer_t *cb =
59bf6b8e
MK
401 smscore_createbuffer(buffer, dev->common_buffer,
402 dev->common_buffer_phys);
82237416 403 if (!cb) {
2e5c1ec8
MK
404 smscore_unregister_device(dev);
405 return -ENOMEM;
406 }
407
408 smscore_putbuffer(dev, cb);
409 }
410
82237416
MK
411 printk(KERN_INFO "%s allocated %d buffers\n",
412 __func__, dev->num_buffers);
2e5c1ec8
MK
413
414 dev->mode = DEVICE_MODE_NONE;
415 dev->context = params->context;
416 dev->device = params->device;
417 dev->setmode_handler = params->setmode_handler;
418 dev->detectmode_handler = params->detectmode_handler;
419 dev->sendrequest_handler = params->sendrequest_handler;
420 dev->preload_handler = params->preload_handler;
421 dev->postload_handler = params->postload_handler;
422
423 dev->device_flags = params->flags;
424 strcpy(dev->devpath, params->devpath);
425
82237416 426 smscore_registry_settype(dev->devpath, params->device_type);
f17407a8 427
82237416 428 /* add device to devices list */
2e5c1ec8
MK
429 kmutex_lock(&g_smscore_deviceslock);
430 list_add(&dev->entry, &g_smscore_devices);
431 kmutex_unlock(&g_smscore_deviceslock);
432
433 *coredev = dev;
434
18658117 435 printk(KERN_INFO "%s device %p created\n", __func__, dev);
2e5c1ec8
MK
436
437 return 0;
438}
439
440/**
441 * sets initial device mode and notifies client hotplugs that device is ready
442 *
59bf6b8e
MK
443 * @param coredev pointer to a coredev object returned by
444 * smscore_register_device
2e5c1ec8
MK
445 *
446 * @return 0 on success, <0 on error.
447 */
18245e18 448int smscore_start_device(struct smscore_device_t *coredev)
2e5c1ec8 449{
59bf6b8e
MK
450 int rc = smscore_set_device_mode(
451 coredev, smscore_registry_getmode(coredev->devpath));
82237416 452 if (rc < 0) {
59bf6b8e
MK
453 printk(KERN_INFO "%s set device mode faile , rc %d\n",
454 __func__, rc);
2e5c1ec8 455 return rc;
f17407a8 456 }
2e5c1ec8
MK
457
458 kmutex_lock(&g_smscore_deviceslock);
459
460 rc = smscore_notify_callbacks(coredev, coredev->device, 1);
461
82237416
MK
462 printk(KERN_INFO "%s device %p started, rc %d\n",
463 __func__, coredev, rc);
2e5c1ec8
MK
464
465 kmutex_unlock(&g_smscore_deviceslock);
466
467 return rc;
468}
469
18245e18 470int smscore_sendrequest_and_wait(struct smscore_device_t *coredev, void *buffer,
82237416 471 size_t size, struct completion *completion)
2e5c1ec8
MK
472{
473 int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
82237416
MK
474 if (rc < 0) {
475 printk(KERN_INFO "%s sendrequest returned error %d\n",
476 __func__, rc);
2e5c1ec8 477 return rc;
f17407a8 478 }
2e5c1ec8 479
82237416
MK
480 return wait_for_completion_timeout(completion,
481 msecs_to_jiffies(10000)) ?
482 0 : -ETIME;
2e5c1ec8
MK
483}
484
18245e18
MK
485int smscore_load_firmware_family2(struct smscore_device_t *coredev,
486 void *buffer, size_t size)
2e5c1ec8 487{
18245e18
MK
488 struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
489 struct SmsMsgHdr_ST *msg;
f0333e3d 490 u32 mem_address = firmware->StartAddress;
a83ccdd6 491 u8 *payload = firmware->Payload;
2e5c1ec8
MK
492 int rc = 0;
493
82237416
MK
494 printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
495 __func__, mem_address, firmware->Length);
496 if (coredev->preload_handler) {
2e5c1ec8
MK
497 rc = coredev->preload_handler(coredev->context);
498 if (rc < 0)
499 return rc;
500 }
501
82237416 502 /* PAGE_SIZE buffer shall be enough and dma aligned */
e080842c 503 msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
2e5c1ec8
MK
504 if (!msg)
505 return -ENOMEM;
506
82237416 507 if (coredev->mode != DEVICE_MODE_NONE) {
f17407a8 508 PDEBUG("Sending reload command\n");
82237416 509 SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
18245e18 510 sizeof(struct SmsMsgHdr_ST));
82237416
MK
511 rc = smscore_sendrequest_and_wait(coredev, msg,
512 msg->msgLength,
513 &coredev->reload_start_done);
f0333e3d 514 mem_address = *(u32 *) &payload[20];
2e5c1ec8
MK
515 }
516
fa830e8a 517 while (size && rc >= 0) {
18245e18
MK
518 struct SmsDataDownload_ST *DataMsg =
519 (struct SmsDataDownload_ST *) msg;
2e5c1ec8
MK
520 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
521
82237416 522 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
18245e18 523 (u16)(sizeof(struct SmsMsgHdr_ST) +
f0333e3d 524 sizeof(u32) + payload_size));
2e5c1ec8
MK
525
526 DataMsg->MemAddr = mem_address;
527 memcpy(DataMsg->Payload, payload, payload_size);
528
82237416
MK
529 if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
530 (coredev->mode == DEVICE_MODE_NONE))
59bf6b8e
MK
531 rc = coredev->sendrequest_handler(
532 coredev->context, DataMsg,
533 DataMsg->xMsgHeader.msgLength);
2e5c1ec8 534 else
59bf6b8e
MK
535 rc = smscore_sendrequest_and_wait(
536 coredev, DataMsg,
537 DataMsg->xMsgHeader.msgLength,
538 &coredev->data_download_done);
2e5c1ec8
MK
539
540 payload += payload_size;
541 size -= payload_size;
542 mem_address += payload_size;
543 }
544
82237416
MK
545 if (rc >= 0) {
546 if (coredev->mode == DEVICE_MODE_NONE) {
18245e18
MK
547 struct SmsMsgData_ST *TriggerMsg =
548 (struct SmsMsgData_ST *) msg;
2e5c1ec8 549
82237416 550 SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
18245e18 551 sizeof(struct SmsMsgHdr_ST) +
f0333e3d 552 sizeof(u32) * 5);
2e5c1ec8 553
59bf6b8e
MK
554 TriggerMsg->msgData[0] = firmware->StartAddress;
555 /* Entry point */
fa830e8a
MK
556 TriggerMsg->msgData[1] = 5; /* Priority */
557 TriggerMsg->msgData[2] = 0x200; /* Stack size */
558 TriggerMsg->msgData[3] = 0; /* Parameter */
559 TriggerMsg->msgData[4] = 4; /* Task ID */
2e5c1ec8 560
82237416 561 if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
59bf6b8e
MK
562 rc = coredev->sendrequest_handler(
563 coredev->context, TriggerMsg,
564 TriggerMsg->xMsgHeader.msgLength);
2e5c1ec8 565 msleep(100);
82237416 566 } else
59bf6b8e
MK
567 rc = smscore_sendrequest_and_wait(
568 coredev, TriggerMsg,
569 TriggerMsg->xMsgHeader.msgLength,
570 &coredev->trigger_done);
82237416
MK
571 } else {
572 SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
18245e18 573 sizeof(struct SmsMsgHdr_ST));
2e5c1ec8 574
82237416
MK
575 rc = coredev->sendrequest_handler(coredev->context,
576 msg, msg->msgLength);
2e5c1ec8 577 }
82237416 578 msleep(500);
2e5c1ec8
MK
579 }
580
9f211397 581 printk(KERN_DEBUG "%s rc=%d, postload=%p \n", __func__, rc,
82237416 582 coredev->postload_handler);
2e5c1ec8
MK
583
584 kfree(msg);
585
f17407a8 586 return ((rc >= 0) && coredev->postload_handler) ?
2e5c1ec8
MK
587 coredev->postload_handler(coredev->context) :
588 rc;
589}
590
591/**
592 * loads specified firmware into a buffer and calls device loadfirmware_handler
593 *
59bf6b8e
MK
594 * @param coredev pointer to a coredev object returned by
595 * smscore_register_device
2e5c1ec8
MK
596 * @param filename null-terminated string specifies firmware file name
597 * @param loadfirmware_handler device handler that loads firmware
598 *
599 * @return 0 on success, <0 on error.
600 */
18245e18
MK
601int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
602 char *filename,
82237416 603 loadfirmware_t loadfirmware_handler)
2e5c1ec8
MK
604{
605 int rc = -ENOENT;
2e5c1ec8 606 const struct firmware *fw;
a83ccdd6 607 u8 *fw_buffer;
2e5c1ec8 608
82237416
MK
609 if (loadfirmware_handler == NULL && !(coredev->device_flags &
610 SMS_DEVICE_FAMILY2))
2e5c1ec8
MK
611 return -EINVAL;
612
613 rc = request_firmware(&fw, filename, coredev->device);
82237416
MK
614 if (rc < 0) {
615 printk(KERN_INFO "%s failed to open \"%s\"\n",
616 __func__, filename);
2e5c1ec8
MK
617 return rc;
618 }
82237416
MK
619 printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
620 filename, fw->size);
621 fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
622 GFP_KERNEL | GFP_DMA);
623 if (fw_buffer) {
2e5c1ec8
MK
624 memcpy(fw_buffer, fw->data, fw->size);
625
626 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
59bf6b8e
MK
627 smscore_load_firmware_family2(coredev,
628 fw_buffer,
629 fw->size) :
630 loadfirmware_handler(coredev->context,
631 fw_buffer, fw->size);
2e5c1ec8
MK
632
633 kfree(fw_buffer);
82237416
MK
634 } else {
635 printk(KERN_INFO "%s failed to allocate firmware buffer\n",
636 __func__);
2e5c1ec8
MK
637 rc = -ENOMEM;
638 }
639
640 release_firmware(fw);
641
642 return rc;
643}
644
18245e18
MK
645int smscore_load_firmware_from_buffer(struct smscore_device_t *coredev,
646 u8 *buffer, int size, int new_mode)
f17407a8
MK
647{
648 PERROR("Feature not implemented yet\n");
649 return -EFAULT;
650}
651
2e5c1ec8 652/**
59bf6b8e
MK
653 * notifies all clients registered with the device, notifies hotplugs,
654 * frees all buffers and coredev object
2e5c1ec8 655 *
59bf6b8e
MK
656 * @param coredev pointer to a coredev object returned by
657 * smscore_register_device
2e5c1ec8
MK
658 *
659 * @return 0 on success, <0 on error.
660 */
18245e18 661void smscore_unregister_device(struct smscore_device_t *coredev)
2e5c1ec8 662{
18245e18 663 struct smscore_buffer_t *cb;
2e5c1ec8 664 int num_buffers = 0;
f17407a8 665 int retry = 0;
2e5c1ec8
MK
666
667 kmutex_lock(&g_smscore_deviceslock);
668
669 smscore_notify_clients(coredev);
670 smscore_notify_callbacks(coredev, NULL, 0);
671
82237416
MK
672 /* at this point all buffers should be back
673 * onresponse must no longer be called */
2e5c1ec8 674
82237416
MK
675 while (1) {
676 while ((cb = smscore_getbuffer(coredev))) {
2e5c1ec8 677 kfree(cb);
fa830e8a 678 num_buffers++;
2e5c1ec8 679 }
2e5c1ec8
MK
680 if (num_buffers == coredev->num_buffers)
681 break;
82237416
MK
682 if (++retry > 10) {
683 printk(KERN_INFO "%s exiting although "
684 "not all buffers released.\n", __func__);
f17407a8
MK
685 break;
686 }
2e5c1ec8 687
82237416
MK
688 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
689 coredev->num_buffers - num_buffers);
2e5c1ec8
MK
690 msleep(100);
691 }
692
18658117 693 printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
2e5c1ec8
MK
694
695 if (coredev->common_buffer)
82237416
MK
696 dma_free_coherent(NULL, coredev->common_buffer_size,
697 coredev->common_buffer,
698 coredev->common_buffer_phys);
2e5c1ec8
MK
699
700 list_del(&coredev->entry);
701 kfree(coredev);
702
703 kmutex_unlock(&g_smscore_deviceslock);
704
18658117 705 printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
2e5c1ec8
MK
706}
707
18245e18 708int smscore_detect_mode(struct smscore_device_t *coredev)
2e5c1ec8 709{
18245e18 710 void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
82237416 711 GFP_KERNEL | GFP_DMA);
18245e18
MK
712 struct SmsMsgHdr_ST *msg =
713 (struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
2e5c1ec8
MK
714 int rc;
715
716 if (!buffer)
717 return -ENOMEM;
718
18245e18
MK
719 SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
720 sizeof(struct SmsMsgHdr_ST));
2e5c1ec8 721
82237416
MK
722 rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
723 &coredev->version_ex_done);
724 if (rc == -ETIME) {
9f211397
MK
725 printk(KERN_ERR "%s: MSG_SMS_GET_VERSION_EX_REQ "
726 "failed first try\n", __func__);
2e5c1ec8 727
82237416
MK
728 if (wait_for_completion_timeout(&coredev->resume_done,
729 msecs_to_jiffies(5000))) {
59bf6b8e
MK
730 rc = smscore_sendrequest_and_wait(
731 coredev, msg, msg->msgLength,
732 &coredev->version_ex_done);
2e5c1ec8 733 if (rc < 0)
9f211397
MK
734 printk(KERN_ERR "%s: "
735 "MSG_SMS_GET_VERSION_EX_REQ failed "
59bf6b8e 736 "second try, rc %d\n", __func__, rc);
82237416 737 } else
2e5c1ec8
MK
738 rc = -ETIME;
739 }
740
741 kfree(buffer);
742
743 return rc;
744}
745
82237416 746char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
fbd05c82
MK
747 /*Stellar NOVA A0 Nova B0 VEGA*/
748 /*DVBT*/
749 {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
750 /*DVBH*/
751 {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
752 /*TDMB*/
753 {"none", "tdmb_nova_12mhz.inp", "none", "none"},
754 /*DABIP*/
755 {"none", "none", "none", "none"},
756 /*BDA*/
757 {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
758 /*ISDBT*/
759 {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
760 /*ISDBTBDA*/
761 {"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
762 /*CMMB*/
763 {"none", "none", "none", "cmmb_vega_12mhz.inp"}
2e5c1ec8
MK
764};
765
f17407a8 766
2e5c1ec8
MK
767/**
768 * calls device handler to change mode of operation
769 * NOTE: stellar/usb may disconnect when changing mode
770 *
59bf6b8e
MK
771 * @param coredev pointer to a coredev object returned by
772 * smscore_register_device
2e5c1ec8
MK
773 * @param mode requested mode of operation
774 *
775 * @return 0 on success, <0 on error.
776 */
18245e18 777int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
2e5c1ec8
MK
778{
779 void *buffer;
780 int rc = 0;
18245e18 781 enum sms_device_type_st type;
2e5c1ec8 782
82237416
MK
783 PDEBUG("set device mode to %d\n", mode);
784 if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
785 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
786 printk(KERN_INFO "%s invalid mode specified %d\n",
787 __func__, mode);
2e5c1ec8
MK
788 return -EINVAL;
789 }
790
f17407a8
MK
791 smscore_registry_setmode(coredev->devpath, mode);
792
82237416 793 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
2e5c1ec8 794 rc = smscore_detect_mode(coredev);
82237416
MK
795 if (rc < 0) {
796 printk(KERN_INFO "%s mode detect failed %d\n",
797 __func__, rc);
2e5c1ec8 798 return rc;
82237416 799 }
f17407a8 800 }
2e5c1ec8 801
82237416
MK
802 if (coredev->mode == mode) {
803 printk(KERN_INFO "%s device mode %d already set\n",
804 __func__, mode);
2e5c1ec8
MK
805 return 0;
806 }
807
82237416
MK
808 if (!(coredev->modes_supported & (1 << mode))) {
809 type = smscore_registry_gettype(coredev->devpath);
59bf6b8e
MK
810 rc = smscore_load_firmware_from_file(
811 coredev, smscore_fw_lkup[mode][type], NULL);
82237416 812 if (rc < 0) {
59bf6b8e
MK
813 printk(KERN_INFO "%s load firmware "
814 "failed %d\n", __func__, rc);
2e5c1ec8 815 return rc;
82237416
MK
816 }
817 } else
59bf6b8e
MK
818 printk(KERN_INFO "%s mode %d supported by running "
819 "firmware\n", __func__, mode);
2e5c1ec8 820
18245e18
MK
821 buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
822 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
82237416 823 if (buffer) {
18245e18
MK
824 struct SmsMsgData_ST *msg =
825 (struct SmsMsgData_ST *)
826 SMS_ALIGN_ADDRESS(buffer);
2e5c1ec8 827
59bf6b8e 828 SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
18245e18 829 sizeof(struct SmsMsgData_ST));
2e5c1ec8
MK
830 msg->msgData[0] = mode;
831
59bf6b8e
MK
832 rc = smscore_sendrequest_and_wait(
833 coredev, msg, msg->xMsgHeader.msgLength,
834 &coredev->init_device_done);
2e5c1ec8
MK
835
836 kfree(buffer);
82237416 837 } else {
59bf6b8e
MK
838 printk(KERN_INFO "%s Could not allocate buffer for "
839 "init device message.\n", __func__);
2e5c1ec8 840 rc = -ENOMEM;
82237416
MK
841 }
842 } else {
843 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
844 printk(KERN_INFO "%s invalid mode specified %d\n",
845 __func__, mode);
f17407a8
MK
846 return -EINVAL;
847 }
848
849 smscore_registry_setmode(coredev->devpath, mode);
850
2e5c1ec8 851 if (coredev->detectmode_handler)
82237416
MK
852 coredev->detectmode_handler(coredev->context,
853 &coredev->mode);
2e5c1ec8
MK
854
855 if (coredev->mode != mode && coredev->setmode_handler)
856 rc = coredev->setmode_handler(coredev->context, mode);
857 }
858
82237416 859 if (rc >= 0) {
2e5c1ec8
MK
860 coredev->mode = mode;
861 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
862 }
863
f17407a8
MK
864 if (rc != 0)
865 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
2e5c1ec8
MK
866 return rc;
867}
868
869/**
870 * calls device handler to get current mode of operation
871 *
59bf6b8e
MK
872 * @param coredev pointer to a coredev object returned by
873 * smscore_register_device
2e5c1ec8
MK
874 *
875 * @return current mode
876 */
18245e18 877int smscore_get_device_mode(struct smscore_device_t *coredev)
2e5c1ec8
MK
878{
879 return coredev->mode;
880}
881
f17407a8
MK
882/**
883 * find client by response id & type within the clients list.
884 * return client handle or NULL.
885 *
59bf6b8e
MK
886 * @param coredev pointer to a coredev object returned by
887 * smscore_register_device
f17407a8 888 * @param data_type client data type (SMS_DONT_CARE for all types)
82237416 889 * @param id client id (SMS_DONT_CARE for all id)
f17407a8
MK
890 *
891 */
18245e18 892struct smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
59bf6b8e 893 int data_type, int id)
2e5c1ec8 894{
18245e18 895 struct smscore_client_t *client = NULL;
2e5c1ec8
MK
896 struct list_head *next, *first;
897 unsigned long flags;
f17407a8 898 struct list_head *firstid, *nextid;
2e5c1ec8 899
2e5c1ec8
MK
900
901 spin_lock_irqsave(&coredev->clientslock, flags);
2e5c1ec8 902 first = &coredev->clients;
82237416
MK
903 for (next = first->next;
904 (next != first) && !client;
905 next = next->next) {
18245e18 906 firstid = &((struct smscore_client_t *)next)->idlist;
82237416
MK
907 for (nextid = firstid->next;
908 nextid != firstid;
909 nextid = nextid->next) {
18245e18
MK
910 if ((((struct smscore_idlist_t *)nextid)->id == id) &&
911 (((struct smscore_idlist_t *)nextid)->data_type == data_type ||
912 (((struct smscore_idlist_t *)nextid)->data_type == 0))) {
913 client = (struct smscore_client_t *) next;
82237416
MK
914 break;
915 }
2e5c1ec8
MK
916 }
917 }
2e5c1ec8 918 spin_unlock_irqrestore(&coredev->clientslock, flags);
2e5c1ec8
MK
919 return client;
920}
921
922/**
923 * find client by response id/type, call clients onresponse handler
924 * return buffer to pool on error
925 *
59bf6b8e
MK
926 * @param coredev pointer to a coredev object returned by
927 * smscore_register_device
2e5c1ec8
MK
928 * @param cb pointer to response buffer descriptor
929 *
930 */
18245e18
MK
931void smscore_onresponse(struct smscore_device_t *coredev,
932 struct smscore_buffer_t *cb)
2e5c1ec8 933{
18245e18
MK
934 struct SmsMsgHdr_ST *phdr =
935 (struct SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
936 struct smscore_client_t *client =
59bf6b8e 937 smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
2e5c1ec8
MK
938 int rc = -EBUSY;
939
fbd05c82
MK
940 static unsigned long last_sample_time; /* = 0; */
941 static int data_total; /* = 0; */
2e5c1ec8
MK
942 unsigned long time_now = jiffies_to_msecs(jiffies);
943
944 if (!last_sample_time)
945 last_sample_time = time_now;
946
fa830e8a 947 if (time_now - last_sample_time > 10000) {
9f211397 948 printk(KERN_DEBUG "\n%s data rate %d bytes/secs\n", __func__,
82237416
MK
949 (int)((data_total * 1000) /
950 (time_now - last_sample_time)));
2e5c1ec8
MK
951
952 last_sample_time = time_now;
953 data_total = 0;
954 }
955
956 data_total += cb->size;
82237416
MK
957 /* If no client registered for type & id,
958 * check for control client where type is not registered */
2e5c1ec8
MK
959 if (client)
960 rc = client->onresponse_handler(client->context, cb);
961
82237416
MK
962 if (rc < 0) {
963 switch (phdr->msgType) {
964 case MSG_SMS_GET_VERSION_EX_RES:
2e5c1ec8 965 {
18245e18
MK
966 struct SmsVersionRes_ST *ver =
967 (struct SmsVersionRes_ST *) phdr;
9f211397
MK
968 printk(KERN_DEBUG "%s: MSG_SMS_GET_VERSION_EX_RES "
969 "id %d prots 0x%x ver %d.%d\n", __func__,
82237416
MK
970 ver->FirmwareId, ver->SupportedProtocols,
971 ver->RomVersionMajor, ver->RomVersionMinor);
2e5c1ec8 972
82237416
MK
973 coredev->mode = ver->FirmwareId == 255 ?
974 DEVICE_MODE_NONE : ver->FirmwareId;
975 coredev->modes_supported = ver->SupportedProtocols;
2e5c1ec8 976
82237416
MK
977 complete(&coredev->version_ex_done);
978 break;
979 }
980 case MSG_SMS_INIT_DEVICE_RES:
9f211397
MK
981 printk(KERN_DEBUG "%s: MSG_SMS_INIT_DEVICE_RES\n",
982 __func__);
82237416
MK
983 complete(&coredev->init_device_done);
984 break;
985 case MSG_SW_RELOAD_START_RES:
9f211397
MK
986 printk(KERN_DEBUG "%s: MSG_SW_RELOAD_START_RES\n",
987 __func__);
82237416
MK
988 complete(&coredev->reload_start_done);
989 break;
990 case MSG_SMS_DATA_DOWNLOAD_RES:
991 complete(&coredev->data_download_done);
992 break;
993 case MSG_SW_RELOAD_EXEC_RES:
9f211397
MK
994 printk(KERN_DEBUG "%s: MSG_SW_RELOAD_EXEC_RES\n",
995 __func__);
82237416
MK
996 break;
997 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
ca783736
MK
998 printk(KERN_DEBUG
999 "%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
82237416
MK
1000 __func__);
1001 complete(&coredev->trigger_done);
1002 break;
1003 case MSG_SMS_SLEEP_RESUME_COMP_IND:
1004 complete(&coredev->resume_done);
1005 break;
1006 default:
1007 break;
2e5c1ec8 1008 }
2e5c1ec8
MK
1009 smscore_putbuffer(coredev, cb);
1010 }
1011}
1012
1013/**
1014 * return pointer to next free buffer descriptor from core pool
1015 *
59bf6b8e
MK
1016 * @param coredev pointer to a coredev object returned by
1017 * smscore_register_device
2e5c1ec8
MK
1018 *
1019 * @return pointer to descriptor on success, NULL on error.
1020 */
18245e18 1021struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
2e5c1ec8 1022{
18245e18 1023 struct smscore_buffer_t *cb = NULL;
2e5c1ec8
MK
1024 unsigned long flags;
1025
1026 spin_lock_irqsave(&coredev->bufferslock, flags);
1027
82237416 1028 if (!list_empty(&coredev->buffers)) {
18245e18 1029 cb = (struct smscore_buffer_t *) coredev->buffers.next;
2e5c1ec8
MK
1030 list_del(&cb->entry);
1031 }
1032
1033 spin_unlock_irqrestore(&coredev->bufferslock, flags);
1034
1035 return cb;
1036}
1037
1038/**
1039 * return buffer descriptor to a pool
1040 *
59bf6b8e
MK
1041 * @param coredev pointer to a coredev object returned by
1042 * smscore_register_device
2e5c1ec8
MK
1043 * @param cb pointer buffer descriptor
1044 *
1045 */
18245e18
MK
1046void smscore_putbuffer(struct smscore_device_t *coredev,
1047 struct smscore_buffer_t *cb)
2e5c1ec8
MK
1048{
1049 list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
1050}
1051
18245e18
MK
1052int smscore_validate_client(struct smscore_device_t *coredev,
1053 struct smscore_client_t *client,
1054 int data_type, int id)
2e5c1ec8 1055{
18245e18
MK
1056 struct smscore_idlist_t *listentry;
1057 struct smscore_client_t *registered_client;
2e5c1ec8 1058
82237416 1059 if (!client) {
f17407a8
MK
1060 PERROR("bad parameter.\n");
1061 return -EFAULT;
1062 }
1063 registered_client = smscore_find_client(coredev, data_type, id);
fa830e8a 1064 if (registered_client == client)
2e5c1ec8 1065 return 0;
fa830e8a 1066
82237416 1067 if (registered_client) {
f17407a8
MK
1068 PERROR("The msg ID already registered to another client.\n");
1069 return -EEXIST;
1070 }
18245e18 1071 listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
82237416 1072 if (!listentry) {
f17407a8 1073 PERROR("Can't allocate memory for client id.\n");
2e5c1ec8 1074 return -ENOMEM;
f17407a8
MK
1075 }
1076 listentry->id = id;
1077 listentry->data_type = data_type;
82237416
MK
1078 list_add_locked(&listentry->entry, &client->idlist,
1079 &coredev->clientslock);
2e5c1ec8
MK
1080 return 0;
1081}
1082
1083/**
1084 * creates smsclient object, check that id is taken by another client
1085 *
1086 * @param coredev pointer to a coredev object from clients hotplug
1087 * @param initial_id all messages with this id would be sent to this client
1088 * @param data_type all messages of this type would be sent to this client
ca783736
MK
1089 * @param onresponse_handler client handler that is called to
1090 * process incoming messages
2e5c1ec8
MK
1091 * @param onremove_handler client handler that is called when device is removed
1092 * @param context client-specific context
1093 * @param client pointer to a value that receives created smsclient object
1094 *
1095 * @return 0 on success, <0 on error.
1096 */
18245e18
MK
1097int smscore_register_client(struct smscore_device_t *coredev,
1098 struct smsclient_params_t *params,
1099 struct smscore_client_t **client)
2e5c1ec8 1100{
18245e18 1101 struct smscore_client_t *newclient;
82237416
MK
1102 /* check that no other channel with same parameters exists */
1103 if (smscore_find_client(coredev, params->data_type,
1104 params->initial_id)) {
f17407a8 1105 PERROR("Client already exist.\n");
2e5c1ec8 1106 return -EEXIST;
f17407a8 1107 }
2e5c1ec8 1108
18245e18 1109 newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
82237416 1110 if (!newclient) {
f17407a8
MK
1111 PERROR("Failed to allocate memory for client.\n");
1112 return -ENOMEM;
2e5c1ec8
MK
1113 }
1114
82237416 1115 INIT_LIST_HEAD(&newclient->idlist);
2e5c1ec8 1116 newclient->coredev = coredev;
2e5c1ec8
MK
1117 newclient->onresponse_handler = params->onresponse_handler;
1118 newclient->onremove_handler = params->onremove_handler;
1119 newclient->context = params->context;
82237416
MK
1120 list_add_locked(&newclient->entry, &coredev->clients,
1121 &coredev->clientslock);
1122 smscore_validate_client(coredev, newclient, params->data_type,
1123 params->initial_id);
2e5c1ec8 1124 *client = newclient;
82237416
MK
1125 PDEBUG("%p %d %d\n", params->context, params->data_type,
1126 params->initial_id);
2e5c1ec8
MK
1127
1128 return 0;
1129}
1130
1131/**
1132 * frees smsclient object and all subclients associated with it
1133 *
59bf6b8e
MK
1134 * @param client pointer to smsclient object returned by
1135 * smscore_register_client
2e5c1ec8
MK
1136 *
1137 */
18245e18 1138void smscore_unregister_client(struct smscore_client_t *client)
2e5c1ec8 1139{
18245e18 1140 struct smscore_device_t *coredev = client->coredev;
2e5c1ec8
MK
1141 unsigned long flags;
1142
1143 spin_lock_irqsave(&coredev->clientslock, flags);
1144
2e5c1ec8 1145
82237416 1146 while (!list_empty(&client->idlist)) {
18245e18
MK
1147 struct smscore_idlist_t *identry =
1148 (struct smscore_idlist_t *) client->idlist.next;
82237416
MK
1149 list_del(&identry->entry);
1150 kfree(identry);
2e5c1ec8
MK
1151 }
1152
f17407a8 1153 printk(KERN_INFO "%s %p\n", __func__, client->context);
2e5c1ec8
MK
1154
1155 list_del(&client->entry);
1156 kfree(client);
1157
1158 spin_unlock_irqrestore(&coredev->clientslock, flags);
1159}
1160
1161/**
1162 * verifies that source id is not taken by another client,
1163 * calls device handler to send requests to the device
1164 *
59bf6b8e
MK
1165 * @param client pointer to smsclient object returned by
1166 * smscore_register_client
2e5c1ec8
MK
1167 * @param buffer pointer to a request buffer
1168 * @param size size (in bytes) of request buffer
1169 *
1170 * @return 0 on success, <0 on error.
1171 */
18245e18
MK
1172int smsclient_sendrequest(struct smscore_client_t *client,
1173 void *buffer, size_t size)
2e5c1ec8 1174{
18245e18
MK
1175 struct smscore_device_t *coredev;
1176 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
f17407a8
MK
1177 int rc;
1178
82237416
MK
1179 if (client == NULL) {
1180 printk(KERN_ERR "%s Got NULL client\n", __func__);
f17407a8
MK
1181 return -EINVAL;
1182 }
1183
1184 coredev = client->coredev;
2e5c1ec8 1185
82237416
MK
1186 /* check that no other channel with same id exists */
1187 if (coredev == NULL) {
1188 printk(KERN_ERR "%s Got NULL coredev\n", __func__);
f17407a8
MK
1189 return -EINVAL;
1190 }
1191
82237416
MK
1192 rc = smscore_validate_client(client->coredev, client, 0,
1193 phdr->msgSrcId);
2e5c1ec8
MK
1194 if (rc < 0)
1195 return rc;
1196
1197 return coredev->sendrequest_handler(coredev->context, buffer, size);
1198}
1199
1200/**
1201 * return the size of large (common) buffer
1202 *
1203 * @param coredev pointer to a coredev object from clients hotplug
1204 *
1205 * @return size (in bytes) of the buffer
1206 */
18245e18 1207int smscore_get_common_buffer_size(struct smscore_device_t *coredev)
2e5c1ec8
MK
1208{
1209 return coredev->common_buffer_size;
1210}
1211
1212/**
1213 * maps common buffer (if supported by platform)
1214 *
1215 * @param coredev pointer to a coredev object from clients hotplug
1216 * @param vma pointer to vma struct from mmap handler
1217 *
1218 * @return 0 on success, <0 on error.
1219 */
18245e18 1220int smscore_map_common_buffer(struct smscore_device_t *coredev,
a83ccdd6 1221 struct vm_area_struct *vma)
2e5c1ec8 1222{
82237416
MK
1223 unsigned long end = vma->vm_end,
1224 start = vma->vm_start,
1225 size = PAGE_ALIGN(coredev->common_buffer_size);
2e5c1ec8 1226
82237416
MK
1227 if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
1228 (vma->vm_flags & VM_WRITE)) {
18658117 1229 printk(KERN_INFO "%s invalid vm flags\n", __func__);
2e5c1ec8
MK
1230 return -EINVAL;
1231 }
1232
82237416
MK
1233 if ((end - start) != size) {
1234 printk(KERN_INFO "%s invalid size %d expected %d\n",
1235 __func__, (int)(end - start), (int) size);
2e5c1ec8
MK
1236 return -EINVAL;
1237 }
1238
82237416
MK
1239 if (remap_pfn_range(vma, start,
1240 coredev->common_buffer_phys >> PAGE_SHIFT,
df0462e7 1241 size, pgprot_noncached(vma->vm_page_prot))) {
18658117 1242 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
2e5c1ec8
MK
1243 return -EAGAIN;
1244 }
1245
1246 return 0;
1247}
1248
1249int smscore_module_init(void)
1250{
c6465799 1251 int rc = 0;
2e5c1ec8
MK
1252
1253 INIT_LIST_HEAD(&g_smscore_notifyees);
1254 INIT_LIST_HEAD(&g_smscore_devices);
1255 kmutex_init(&g_smscore_deviceslock);
1256
1257 INIT_LIST_HEAD(&g_smscore_registry);
1258 kmutex_init(&g_smscore_registrylock);
1259
eae55660
ST
1260 /* USB Register */
1261 rc = smsusb_register();
1262
1263 /* DVB Register */
1264 rc = smsdvb_register();
1265
18658117 1266 printk(KERN_INFO "%s, rc %d\n", __func__, rc);
2e5c1ec8
MK
1267
1268 return rc;
1269}
1270
1271void smscore_module_exit(void)
1272{
eae55660 1273
2e5c1ec8 1274 kmutex_lock(&g_smscore_deviceslock);
82237416 1275 while (!list_empty(&g_smscore_notifyees)) {
18245e18
MK
1276 struct smscore_device_notifyee_t *notifyee =
1277 (struct smscore_device_notifyee_t *)
1278 g_smscore_notifyees.next;
2e5c1ec8
MK
1279
1280 list_del(&notifyee->entry);
1281 kfree(notifyee);
1282 }
1283 kmutex_unlock(&g_smscore_deviceslock);
1284
1285 kmutex_lock(&g_smscore_registrylock);
82237416 1286 while (!list_empty(&g_smscore_registry)) {
18245e18
MK
1287 struct smscore_registry_entry_t *entry =
1288 (struct smscore_registry_entry_t *)
1289 g_smscore_registry.next;
2e5c1ec8
MK
1290
1291 list_del(&entry->entry);
1292 kfree(entry);
1293 }
1294 kmutex_unlock(&g_smscore_registrylock);
1295
eae55660
ST
1296 /* DVB UnRegister */
1297 smsdvb_unregister();
1298
1299 /* Unregister USB */
1300 smsusb_unregister();
1301
18658117 1302 printk(KERN_INFO "%s\n", __func__);
2e5c1ec8
MK
1303}
1304
1305module_init(smscore_module_init);
1306module_exit(smscore_module_exit);
1307
1308MODULE_DESCRIPTION("smscore");
82237416 1309MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
2e5c1ec8 1310MODULE_LICENSE("GPL");