2 * Siano core API module
4 * This file contains implementation for the interface to sms core component
6 * author: Anatoly Greenblat
8 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
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 3 as
12 * published by the Free Software Foundation;
14 * Software distributed under the License is distributed on an "AS IS"
15 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
17 * See the GNU General Public License for more details.
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.
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>
32 #include <linux/firmware.h>
34 #include "smscoreapi.h"
36 #define PERROR(fmt, args...) printk( KERN_ERR "smscore error: line %d- %s(): " fmt,__LINE__, __func__, ## args)
41 # define PWARNING(fmt, args...) printk( KERN_INFO "smscore warning: line %d- %s(): " fmt,__LINE__, __func__, ## args)
42 #undef PDEBUG /* undef it, just in case */
43 # define PDEBUG(fmt, args...) printk( KERN_INFO "smscore - %s(): " fmt, __func__, ## args)
45 #else /*SMSCORE_DEBUG*/
47 #define PDEBUG(fmt, args...)
48 #define PWARNING(fmt, args...)
53 typedef struct _smscore_device_notifyee
55 struct list_head entry;
57 } smscore_device_notifyee_t;
59 typedef struct _smscore_subclient
61 struct list_head entry;
66 typedef struct _smscore_client
68 struct list_head entry;
69 smscore_device_t *coredev;
71 struct list_head idlist;
72 onresponse_t onresponse_handler;
73 onremove_t onremove_handler;
78 typedef struct _smscore_device
80 struct list_head entry;
82 struct list_head clients;
83 struct list_head subclients;
84 spinlock_t clientslock;
86 struct list_head buffers;
87 spinlock_t bufferslock;
91 int common_buffer_size;
92 dma_addr_t common_buffer_phys;
95 struct device *device;
98 unsigned long device_flags;
100 setmode_t setmode_handler;
101 detectmode_t detectmode_handler;
102 sendrequest_t sendrequest_handler;
103 preload_t preload_handler;
104 postload_t postload_handler;
106 int mode, modes_supported;
108 struct completion version_ex_done, data_download_done, trigger_done;
109 struct completion init_device_done, reload_start_done, resume_done;
110 } *psmscore_device_t;
112 typedef struct _smscore_registry_entry
114 struct list_head entry;
117 sms_device_type_st type;
118 } smscore_registry_entry_t;
120 struct list_head g_smscore_notifyees;
121 struct list_head g_smscore_devices;
122 kmutex_t g_smscore_deviceslock;
124 struct list_head g_smscore_registry;
125 kmutex_t g_smscore_registrylock;
127 static int default_mode = 1;
129 module_param(default_mode, int, 0644);
130 MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
132 static smscore_registry_entry_t *smscore_find_registry ( char *devpath )
134 smscore_registry_entry_t *entry;
135 struct list_head *next;
137 kmutex_lock(&g_smscore_registrylock);
138 for (next = g_smscore_registry.next; next != &g_smscore_registry; next = next->next)
140 entry = (smscore_registry_entry_t *) next;
141 if (!strcmp(entry->devpath, devpath))
143 kmutex_unlock(&g_smscore_registrylock);
147 entry = (smscore_registry_entry_t *) kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
150 entry->mode = default_mode;
151 strcpy(entry->devpath, devpath);
152 list_add(&entry->entry, &g_smscore_registry);
155 printk ( KERN_ERR "%s failed to create smscore_registry.\n", __func__ );
156 kmutex_unlock(&g_smscore_registrylock);
160 int smscore_registry_getmode ( char *devpath )
162 smscore_registry_entry_t *entry;
164 entry = smscore_find_registry ( devpath );
171 printk ( KERN_ERR "%s No registry found.\n", __func__ );
176 sms_device_type_st smscore_registry_gettype ( char *devpath )
178 smscore_registry_entry_t *entry;
180 entry = smscore_find_registry ( devpath );
187 printk ( KERN_ERR "%s No registry found.\n", __func__ );
192 void smscore_registry_setmode ( char *devpath, int mode )
194 smscore_registry_entry_t *entry;
196 entry = smscore_find_registry ( devpath );
203 printk ( KERN_ERR "%s No registry found.\n", __func__ );
207 void smscore_registry_settype ( char *devpath, sms_device_type_st type )
209 smscore_registry_entry_t *entry;
211 entry = smscore_find_registry ( devpath );
218 printk ( KERN_ERR "%s No registry found.\n", __func__ );
224 void list_add_locked(struct list_head *new, struct list_head *head,
229 spin_lock_irqsave(lock, flags);
233 spin_unlock_irqrestore(lock, flags);
237 * register a client callback that called when device plugged in/unplugged
238 * NOTE: if devices exist callback is called immediately for each device
240 * @param hotplug callback
242 * @return 0 on success, <0 on error.
244 int smscore_register_hotplug(hotplug_t hotplug)
246 smscore_device_notifyee_t *notifyee;
247 struct list_head *next, *first;
250 kmutex_lock(&g_smscore_deviceslock);
252 notifyee = kmalloc(sizeof(smscore_device_notifyee_t), GFP_KERNEL);
255 // now notify callback about existing devices
256 first = &g_smscore_devices;
257 for (next = first->next; next != first && !rc; next = next->next)
259 smscore_device_t *coredev = (smscore_device_t *) next;
260 rc = hotplug(coredev, coredev->device, 1);
265 notifyee->hotplug = hotplug;
266 list_add(¬ifyee->entry, &g_smscore_notifyees);
274 kmutex_unlock(&g_smscore_deviceslock);
280 * unregister a client callback that called when device plugged in/unplugged
282 * @param hotplug callback
285 void smscore_unregister_hotplug(hotplug_t hotplug)
287 struct list_head *next, *first;
289 kmutex_lock(&g_smscore_deviceslock);
291 first = &g_smscore_notifyees;
293 for (next = first->next; next != first;)
295 smscore_device_notifyee_t *notifyee = (smscore_device_notifyee_t *) next;
298 if (notifyee->hotplug == hotplug)
300 list_del(¬ifyee->entry);
305 kmutex_unlock(&g_smscore_deviceslock);
308 void smscore_notify_clients(smscore_device_t *coredev)
310 smscore_client_t *client;
312 // the client must call smscore_unregister_client from remove handler
313 while (!list_empty(&coredev->clients))
315 client = (smscore_client_t *) coredev->clients.next;
316 client->onremove_handler(client->context);
320 int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device, int arrival)
322 struct list_head *next, *first;
325 // note: must be called under g_deviceslock
327 first = &g_smscore_notifyees;
329 for (next = first->next; next != first; next = next->next)
331 rc = ((smscore_device_notifyee_t *) next)->hotplug(coredev, device, arrival);
339 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
340 dma_addr_t common_buffer_phys)
342 smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
345 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
350 cb->offset_in_common = buffer - (u8 *) common_buffer;
351 cb->phys = common_buffer_phys + cb->offset_in_common;
357 * creates coredev object for a device, prepares buffers, creates buffer mappings, notifies
358 * registered hotplugs about new device.
360 * @param params device pointer to struct with device specific parameters and handlers
361 * @param coredev pointer to a value that receives created coredev object
363 * @return 0 on success, <0 on error.
365 int smscore_register_device(smsdevice_params_t *params, smscore_device_t **coredev)
367 smscore_device_t *dev;
370 dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
373 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
377 // init list entry so it could be safe in smscore_unregister_device
378 INIT_LIST_HEAD(&dev->entry);
381 INIT_LIST_HEAD(&dev->clients);
382 INIT_LIST_HEAD(&dev->buffers);
385 spin_lock_init(&dev->clientslock);
386 spin_lock_init(&dev->bufferslock);
388 // init completion events
389 init_completion(&dev->version_ex_done);
390 init_completion(&dev->data_download_done);
391 init_completion(&dev->trigger_done);
392 init_completion(&dev->init_device_done);
393 init_completion(&dev->reload_start_done);
394 init_completion(&dev->resume_done);
396 // alloc common buffer
397 dev->common_buffer_size = params->buffer_size * params->num_buffers;
398 dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size, &dev->common_buffer_phys, GFP_KERNEL | GFP_DMA);
399 if (!dev->common_buffer)
401 smscore_unregister_device(dev);
405 // prepare dma buffers
406 for (buffer = dev->common_buffer; dev->num_buffers < params->num_buffers; dev->num_buffers ++, buffer += params->buffer_size)
408 smscore_buffer_t *cb = smscore_createbuffer(buffer, dev->common_buffer, dev->common_buffer_phys);
411 smscore_unregister_device(dev);
415 smscore_putbuffer(dev, cb);
418 printk(KERN_INFO "%s allocated %d buffers\n", __func__, dev->num_buffers);
420 dev->mode = DEVICE_MODE_NONE;
421 dev->context = params->context;
422 dev->device = params->device;
423 dev->setmode_handler = params->setmode_handler;
424 dev->detectmode_handler = params->detectmode_handler;
425 dev->sendrequest_handler = params->sendrequest_handler;
426 dev->preload_handler = params->preload_handler;
427 dev->postload_handler = params->postload_handler;
429 dev->device_flags = params->flags;
430 strcpy(dev->devpath, params->devpath);
432 smscore_registry_settype ( dev->devpath, params->device_type );
434 // add device to devices list
435 kmutex_lock(&g_smscore_deviceslock);
436 list_add(&dev->entry, &g_smscore_devices);
437 kmutex_unlock(&g_smscore_deviceslock);
441 printk(KERN_INFO "%s device %p created\n", __func__, dev);
447 * sets initial device mode and notifies client hotplugs that device is ready
449 * @param coredev pointer to a coredev object returned by smscore_register_device
451 * @return 0 on success, <0 on error.
453 int smscore_start_device(smscore_device_t *coredev)
455 int rc = smscore_set_device_mode(coredev, smscore_registry_getmode(coredev->devpath));
458 printk ( KERN_INFO "%s set device mode faile , rc %d\n", __func__, rc );
462 kmutex_lock(&g_smscore_deviceslock);
464 rc = smscore_notify_callbacks(coredev, coredev->device, 1);
466 printk(KERN_INFO "%s device %p started, rc %d\n", __func__, coredev, rc);
468 kmutex_unlock(&g_smscore_deviceslock);
473 int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
474 size_t size, struct completion *completion)
476 int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
479 printk(KERN_INFO "%s sendrequest returned error %d\n", __func__, rc);
483 return wait_for_completion_timeout(completion, msecs_to_jiffies(10000)) ? 0 : -ETIME;
486 int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer, size_t size)
488 SmsFirmware_ST* firmware = (SmsFirmware_ST*) buffer;
490 UINT32 mem_address = firmware->StartAddress;
491 u8 *payload = firmware->Payload;
494 printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n", __func__, mem_address,firmware->Length);
495 if (coredev->preload_handler)
497 rc = coredev->preload_handler(coredev->context);
502 // PAGE_SIZE buffer shall be enough and dma aligned
503 msg = (SmsMsgHdr_ST *) kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
507 if (coredev->mode != DEVICE_MODE_NONE)
509 PDEBUG("Sending reload command\n");
510 SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ, sizeof(SmsMsgHdr_ST));
511 rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->reload_start_done);
512 mem_address = *(UINT32*) &payload[20];
515 while (size && rc >= 0)
517 SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
518 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
520 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ, (UINT16)(sizeof(SmsMsgHdr_ST) + sizeof(UINT32) + payload_size));
522 DataMsg->MemAddr = mem_address;
523 memcpy(DataMsg->Payload, payload, payload_size);
525 if (coredev->device_flags & SMS_ROM_NO_RESPONSE && coredev->mode == DEVICE_MODE_NONE)
526 rc = coredev->sendrequest_handler(coredev->context, DataMsg, DataMsg->xMsgHeader.msgLength);
528 rc = smscore_sendrequest_and_wait(coredev, DataMsg, DataMsg->xMsgHeader.msgLength, &coredev->data_download_done);
530 payload += payload_size;
531 size -= payload_size;
532 mem_address += payload_size;
537 if (coredev->mode == DEVICE_MODE_NONE)
539 SmsMsgData_ST* TriggerMsg = (SmsMsgData_ST*) msg;
541 SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ, sizeof(SmsMsgHdr_ST) + sizeof(UINT32) * 5);
543 TriggerMsg->msgData[0] = firmware->StartAddress; // Entry point
544 TriggerMsg->msgData[1] = 5; // Priority
545 TriggerMsg->msgData[2] = 0x200; // Stack size
546 TriggerMsg->msgData[3] = 0; // Parameter
547 TriggerMsg->msgData[4] = 4; // Task ID
549 if (coredev->device_flags & SMS_ROM_NO_RESPONSE)
551 rc = coredev->sendrequest_handler(coredev->context, TriggerMsg, TriggerMsg->xMsgHeader.msgLength);
555 rc = smscore_sendrequest_and_wait(coredev, TriggerMsg, TriggerMsg->xMsgHeader.msgLength, &coredev->trigger_done);
559 SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ, sizeof(SmsMsgHdr_ST));
561 rc = coredev->sendrequest_handler(coredev->context, msg, msg->msgLength);
566 printk("%s rc=%d, postload=%p \n", __func__, rc, coredev->postload_handler);
570 return ((rc >= 0) && coredev->postload_handler) ?
571 coredev->postload_handler(coredev->context) :
576 * loads specified firmware into a buffer and calls device loadfirmware_handler
578 * @param coredev pointer to a coredev object returned by smscore_register_device
579 * @param filename null-terminated string specifies firmware file name
580 * @param loadfirmware_handler device handler that loads firmware
582 * @return 0 on success, <0 on error.
584 int smscore_load_firmware_from_file(smscore_device_t *coredev, char *filename, loadfirmware_t loadfirmware_handler)
588 const struct firmware *fw;
591 if (loadfirmware_handler == NULL && !(coredev->device_flags & SMS_DEVICE_FAMILY2))
594 rc = request_firmware(&fw, filename, coredev->device);
597 printk(KERN_INFO "%s failed to open \"%s\"\n", __func__, filename);
600 printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__, filename, fw->size);
601 fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT), GFP_KERNEL | GFP_DMA);
604 memcpy(fw_buffer, fw->data, fw->size);
606 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
607 smscore_load_firmware_family2(coredev, fw_buffer, fw->size) :
608 loadfirmware_handler(coredev->context, fw_buffer, fw->size);
614 printk(KERN_INFO "%s failed to allocate firmware buffer\n", __func__);
618 release_firmware(fw);
623 int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer, int size, int new_mode)
625 PERROR("Feature not implemented yet\n");
630 * notifies all clients registered with the device, notifies hotplugs, frees all buffers and coredev object
632 * @param coredev pointer to a coredev object returned by smscore_register_device
634 * @return 0 on success, <0 on error.
636 void smscore_unregister_device(smscore_device_t *coredev)
638 smscore_buffer_t *cb;
642 kmutex_lock(&g_smscore_deviceslock);
644 smscore_notify_clients(coredev);
645 smscore_notify_callbacks(coredev, NULL, 0);
647 // at this point all buffers should be back
648 // onresponse must no longer be called
652 while ((cb = smscore_getbuffer(coredev)))
657 if (num_buffers == coredev->num_buffers)
661 printk(KERN_INFO "%s exiting although not all buffers released.\n", __func__);
665 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__, coredev->num_buffers - num_buffers);
669 printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
671 if (coredev->common_buffer)
672 dma_free_coherent(NULL, coredev->common_buffer_size, coredev->common_buffer, coredev->common_buffer_phys);
674 list_del(&coredev->entry);
677 kmutex_unlock(&g_smscore_deviceslock);
679 printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
682 int smscore_detect_mode(smscore_device_t *coredev)
684 void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
685 SmsMsgHdr_ST *msg = (SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
691 SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));
693 rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
696 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed first try\n", __func__);
698 if (wait_for_completion_timeout(&coredev->resume_done, msecs_to_jiffies(5000)))
700 rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
703 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d\n", __func__, rc);
715 char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] =
717 /*Stellar NOVA A0 Nova B0 VEGA*/
718 /*DVBT*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
719 /*DVBH*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
720 /*TDMB*/ {"none", "tdmb_nova_12mhz.inp", "none" "none"},
721 /*DABIP*/ {"none", "none", "none", "none"},
722 /*BDA*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
723 /*ISDBT*/ {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
724 /*ISDBTBDA*/{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
725 /*CMMB*/ {"none", "none", "none", "cmmb_vega_12mhz.inp"}
730 * calls device handler to change mode of operation
731 * NOTE: stellar/usb may disconnect when changing mode
733 * @param coredev pointer to a coredev object returned by smscore_register_device
734 * @param mode requested mode of operation
736 * @return 0 on success, <0 on error.
738 int smscore_set_device_mode(smscore_device_t *coredev, int mode)
742 sms_device_type_st type;
744 PDEBUG("set device mode to %d\n", mode );
745 if (coredev->device_flags & SMS_DEVICE_FAMILY2)
747 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER)
749 printk(KERN_INFO "%s invalid mode specified %d\n", __func__, mode);
753 smscore_registry_setmode(coredev->devpath, mode);
755 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY))
757 rc = smscore_detect_mode(coredev);
760 printk(KERN_INFO "%s mode detect failed %d\n", __func__, rc);
765 if (coredev->mode == mode)
767 printk(KERN_INFO "%s device mode %d already set\n", __func__, mode);
771 if (!(coredev->modes_supported & (1 << mode)))
773 type = smscore_registry_gettype ( coredev->devpath );
774 rc = smscore_load_firmware_from_file ( coredev, smscore_fw_lkup[mode][type], NULL );
777 printk(KERN_INFO "%s load firmware failed %d\n", __func__, rc);
783 printk(KERN_INFO "%s mode %d supported by running firmware\n", __func__, mode);
786 buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
789 SmsMsgData_ST *msg = (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
791 SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ, sizeof(SmsMsgData_ST));
792 msg->msgData[0] = mode;
794 rc = smscore_sendrequest_and_wait(coredev, msg, msg->xMsgHeader.msgLength, &coredev->init_device_done);
800 printk(KERN_INFO "%s Could not allocate buffer for init device message.\n", __func__);
806 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA)
808 printk(KERN_INFO "%s invalid mode specified %d\n", __func__, mode);
812 smscore_registry_setmode(coredev->devpath, mode);
814 if (coredev->detectmode_handler)
815 coredev->detectmode_handler(coredev->context, &coredev->mode);
817 if (coredev->mode != mode && coredev->setmode_handler)
818 rc = coredev->setmode_handler(coredev->context, mode);
823 coredev->mode = mode;
824 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
828 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
833 * calls device handler to get current mode of operation
835 * @param coredev pointer to a coredev object returned by smscore_register_device
837 * @return current mode
839 int smscore_get_device_mode(smscore_device_t *coredev)
841 return coredev->mode;
845 * find client by response id & type within the clients list.
846 * return client handle or NULL.
848 * @param coredev pointer to a coredev object returned by smscore_register_device
849 * @param data_type client data type (SMS_DONT_CARE for all types)
850 * @param id client id (SMS_DONT_CARE for all id )
853 smscore_client_t *smscore_find_client(smscore_device_t *coredev, int data_type, int id)
855 smscore_client_t *client = NULL;
856 struct list_head *next, *first;
858 struct list_head *firstid, *nextid;
861 spin_lock_irqsave(&coredev->clientslock, flags);
862 first = &coredev->clients;
863 for (next = first->next; (next != first) && !client; next = next->next)
865 firstid = &((smscore_client_t*)next )->idlist;
866 for (nextid = firstid->next ; nextid != firstid ; nextid = nextid->next)
868 if ((((smscore_idlist_t*)nextid)->id == id) &&
869 (((smscore_idlist_t*)nextid)->data_type == data_type ||
870 (((smscore_idlist_t*)nextid)->data_type == 0)))
872 client = (smscore_client_t*) next;
877 spin_unlock_irqrestore(&coredev->clientslock, flags);
882 * find client by response id/type, call clients onresponse handler
883 * return buffer to pool on error
885 * @param coredev pointer to a coredev object returned by smscore_register_device
886 * @param cb pointer to response buffer descriptor
889 void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
891 SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
892 smscore_client_t *client = smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
895 static unsigned long last_sample_time = 0;
896 static int data_total = 0;
897 unsigned long time_now = jiffies_to_msecs(jiffies);
899 if (!last_sample_time)
900 last_sample_time = time_now;
902 if (time_now - last_sample_time > 10000)
904 printk("\n%s data rate %d bytes/secs\n", __func__, (int)((data_total * 1000) / (time_now - last_sample_time)));
906 last_sample_time = time_now;
910 data_total += cb->size;
911 /* If no client registered for type & id, check for control client where type is not registered*/
913 // client = smscore_find_client( coredev, 0, phdr->msgDstId);
915 rc = client->onresponse_handler(client->context, cb);
919 switch (phdr->msgType)
921 case MSG_SMS_GET_VERSION_EX_RES:
923 SmsVersionRes_ST *ver = (SmsVersionRes_ST*) phdr;
924 printk("%s: MSG_SMS_GET_VERSION_EX_RES id %d prots 0x%x ver %d.%d\n", __func__, ver->FirmwareId, ver->SupportedProtocols, ver->RomVersionMajor, ver->RomVersionMinor);
926 coredev->mode = ver->FirmwareId == 255 ? DEVICE_MODE_NONE : ver->FirmwareId;
927 coredev->modes_supported = ver->SupportedProtocols;
929 complete(&coredev->version_ex_done);
933 case MSG_SMS_INIT_DEVICE_RES:
934 printk("%s: MSG_SMS_INIT_DEVICE_RES\n", __func__);
935 complete(&coredev->init_device_done);
938 case MSG_SW_RELOAD_START_RES:
939 printk("%s: MSG_SW_RELOAD_START_RES\n", __func__);
940 complete(&coredev->reload_start_done);
943 case MSG_SMS_DATA_DOWNLOAD_RES:
944 complete(&coredev->data_download_done);
947 case MSG_SW_RELOAD_EXEC_RES:
948 printk("%s: MSG_SW_RELOAD_EXEC_RES\n", __func__);
951 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
952 printk("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n", __func__);
953 complete(&coredev->trigger_done);
956 case MSG_SMS_SLEEP_RESUME_COMP_IND:
957 complete(&coredev->resume_done);
961 //printk(KERN_INFO "%s no client (%p) or error (%d), type:%d dstid:%d\n", __func__, client, rc, phdr->msgType, phdr->msgDstId);
965 smscore_putbuffer(coredev, cb);
970 * return pointer to next free buffer descriptor from core pool
972 * @param coredev pointer to a coredev object returned by smscore_register_device
974 * @return pointer to descriptor on success, NULL on error.
976 smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
978 smscore_buffer_t *cb = NULL;
981 spin_lock_irqsave(&coredev->bufferslock, flags);
983 if (!list_empty(&coredev->buffers))
985 cb = (smscore_buffer_t *) coredev->buffers.next;
986 list_del(&cb->entry);
989 spin_unlock_irqrestore(&coredev->bufferslock, flags);
995 * return buffer descriptor to a pool
997 * @param coredev pointer to a coredev object returned by smscore_register_device
998 * @param cb pointer buffer descriptor
1001 void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
1003 list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
1006 int smscore_validate_client(smscore_device_t *coredev, smscore_client_t *client, int data_type, int id)
1008 smscore_idlist_t *listentry;
1009 smscore_client_t *registered_client;
1013 PERROR("bad parameter.\n");
1016 registered_client = smscore_find_client(coredev, data_type, id);
1017 if (registered_client == client)
1021 if (registered_client)
1023 PERROR("The msg ID already registered to another client.\n");
1026 listentry = kzalloc ( sizeof ( smscore_idlist_t ), GFP_KERNEL );
1029 PERROR("Can't allocate memory for client id.\n");
1033 listentry->data_type = data_type;
1034 list_add_locked ( &listentry->entry, &client->idlist, &coredev->clientslock );
1039 * creates smsclient object, check that id is taken by another client
1041 * @param coredev pointer to a coredev object from clients hotplug
1042 * @param initial_id all messages with this id would be sent to this client
1043 * @param data_type all messages of this type would be sent to this client
1044 * @param onresponse_handler client handler that is called to process incoming messages
1045 * @param onremove_handler client handler that is called when device is removed
1046 * @param context client-specific context
1047 * @param client pointer to a value that receives created smsclient object
1049 * @return 0 on success, <0 on error.
1051 int smscore_register_client(smscore_device_t *coredev, smsclient_params_t *params, smscore_client_t **client)
1053 smscore_client_t *newclient;
1054 // check that no other channel with same parameters exists
1055 if (smscore_find_client(coredev, params->data_type, params->initial_id))
1057 PERROR("Client already exist.\n");
1061 newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1064 PERROR("Failed to allocate memory for client.\n");
1068 INIT_LIST_HEAD ( &newclient->idlist);
1069 newclient->coredev = coredev;
1070 newclient->onresponse_handler = params->onresponse_handler;
1071 newclient->onremove_handler = params->onremove_handler;
1072 newclient->context = params->context;
1073 list_add_locked(&newclient->entry, &coredev->clients, &coredev->clientslock);
1074 smscore_validate_client(coredev, newclient, params->data_type, params->initial_id);
1075 *client = newclient;
1076 PDEBUG ( "%p %d %d\n", params->context, params->data_type, params->initial_id );
1082 * frees smsclient object and all subclients associated with it
1084 * @param client pointer to smsclient object returned by smscore_register_client
1087 void smscore_unregister_client(smscore_client_t *client)
1089 smscore_device_t *coredev = client->coredev;
1090 unsigned long flags;
1092 spin_lock_irqsave(&coredev->clientslock, flags);
1095 while (!list_empty( &client->idlist))
1097 smscore_idlist_t *identry = (smscore_idlist_t*)client->idlist.next;
1098 list_del ( &identry->entry );
1102 printk(KERN_INFO "%s %p\n", __func__, client->context);
1104 list_del(&client->entry);
1107 spin_unlock_irqrestore(&coredev->clientslock, flags);
1111 * verifies that source id is not taken by another client,
1112 * calls device handler to send requests to the device
1114 * @param client pointer to smsclient object returned by smscore_register_client
1115 * @param buffer pointer to a request buffer
1116 * @param size size (in bytes) of request buffer
1118 * @return 0 on success, <0 on error.
1120 int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
1122 smscore_device_t *coredev;
1123 SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1126 if ( client == NULL )
1128 printk(KERN_ERR "%s Got NULL client\n", __func__ );
1132 coredev = client->coredev;
1134 // check that no other channel with same id exists
1135 if ( coredev == NULL )
1137 printk(KERN_ERR "%s Got NULL coredev\n", __func__ );
1141 rc = smscore_validate_client(client->coredev, client, 0, phdr->msgSrcId);
1145 return coredev->sendrequest_handler(coredev->context, buffer, size);
1149 * return the size of large (common) buffer
1151 * @param coredev pointer to a coredev object from clients hotplug
1153 * @return size (in bytes) of the buffer
1155 int smscore_get_common_buffer_size(smscore_device_t *coredev)
1157 return coredev->common_buffer_size;
1161 * maps common buffer (if supported by platform)
1163 * @param coredev pointer to a coredev object from clients hotplug
1164 * @param vma pointer to vma struct from mmap handler
1166 * @return 0 on success, <0 on error.
1168 int smscore_map_common_buffer(smscore_device_t *coredev,
1169 struct vm_area_struct *vma)
1171 unsigned long end = vma->vm_end, start = vma->vm_start, size = PAGE_ALIGN(coredev->common_buffer_size);
1173 if (!(vma->vm_flags & (VM_READ | VM_SHARED)) || (vma->vm_flags & VM_WRITE))
1175 printk(KERN_INFO "%s invalid vm flags\n", __func__);
1179 if ((end - start) != size)
1181 printk(KERN_INFO "%s invalid size %d expected %d\n", __func__, (int)(end - start), (int) size);
1185 if (remap_pfn_range(vma, start, coredev->common_buffer_phys >> PAGE_SHIFT, size, pgprot_noncached(vma->vm_page_prot)))
1187 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1194 int smscore_module_init(void)
1198 INIT_LIST_HEAD(&g_smscore_notifyees);
1199 INIT_LIST_HEAD(&g_smscore_devices);
1200 kmutex_init(&g_smscore_deviceslock);
1202 INIT_LIST_HEAD(&g_smscore_registry);
1203 kmutex_init(&g_smscore_registrylock);
1206 rc = smsusb_register();
1209 rc = smsdvb_register();
1211 printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1216 void smscore_module_exit(void)
1219 kmutex_lock(&g_smscore_deviceslock);
1220 while (!list_empty(&g_smscore_notifyees))
1222 smscore_device_notifyee_t *notifyee = (smscore_device_notifyee_t *) g_smscore_notifyees.next;
1224 list_del(¬ifyee->entry);
1227 kmutex_unlock(&g_smscore_deviceslock);
1229 kmutex_lock(&g_smscore_registrylock);
1230 while (!list_empty(&g_smscore_registry))
1232 smscore_registry_entry_t *entry = (smscore_registry_entry_t *) g_smscore_registry.next;
1234 list_del(&entry->entry);
1237 kmutex_unlock(&g_smscore_registrylock);
1239 /* DVB UnRegister */
1240 smsdvb_unregister();
1242 /* Unregister USB */
1243 smsusb_unregister();
1245 printk(KERN_INFO "%s\n", __func__);
1248 module_init(smscore_module_init);
1249 module_exit(smscore_module_exit);
1251 MODULE_DESCRIPTION("smscore");
1252 MODULE_AUTHOR ( "Siano Mobile Silicon,,, (doronc@siano-ms.com)" );
1253 MODULE_LICENSE("GPL");