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