V4L/DVB (8282): sms1xxx: more codingstyle cleanups
[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)->hotplug(coredev, device, arrival);
313                 if (rc < 0)
314                         break;
315         }
316
317         return rc;
318 }
319
320 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
321                                        dma_addr_t common_buffer_phys)
322 {
323         smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
324         if (!cb) {
325                 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
326                 return NULL;
327         }
328
329         cb->p = buffer;
330         cb->offset_in_common = buffer - (u8 *) common_buffer;
331         cb->phys = common_buffer_phys + cb->offset_in_common;
332
333         return cb;
334 }
335
336 /**
337  * creates coredev object for a device, prepares buffers,
338  * creates buffer mappings, notifies registered hotplugs about new device.
339  *
340  * @param params device pointer to struct with device specific parameters and handlers
341  * @param coredev pointer to a value that receives created coredev object
342  *
343  * @return 0 on success, <0 on error.
344  */
345 int smscore_register_device(smsdevice_params_t *params,
346                             smscore_device_t **coredev)
347 {
348         smscore_device_t *dev;
349         u8 *buffer;
350
351         dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
352         if (!dev) {
353                 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
354                 return -ENOMEM;
355         }
356
357         /* init list entry so it could be safe in smscore_unregister_device */
358         INIT_LIST_HEAD(&dev->entry);
359
360         /* init queues */
361         INIT_LIST_HEAD(&dev->clients);
362         INIT_LIST_HEAD(&dev->buffers);
363
364         /* init locks */
365         spin_lock_init(&dev->clientslock);
366         spin_lock_init(&dev->bufferslock);
367
368         /* init completion events */
369         init_completion(&dev->version_ex_done);
370         init_completion(&dev->data_download_done);
371         init_completion(&dev->trigger_done);
372         init_completion(&dev->init_device_done);
373         init_completion(&dev->reload_start_done);
374         init_completion(&dev->resume_done);
375
376         /* alloc common buffer */
377         dev->common_buffer_size = params->buffer_size * params->num_buffers;
378         dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
379                                                 &dev->common_buffer_phys,
380                                                 GFP_KERNEL | GFP_DMA);
381         if (!dev->common_buffer) {
382                 smscore_unregister_device(dev);
383                 return -ENOMEM;
384         }
385
386         /* prepare dma buffers */
387         for (buffer = dev->common_buffer;
388              dev->num_buffers < params->num_buffers;
389              dev->num_buffers++, buffer += params->buffer_size) {
390                 smscore_buffer_t *cb = smscore_createbuffer(buffer, dev->common_buffer, dev->common_buffer_phys);
391                 if (!cb) {
392                         smscore_unregister_device(dev);
393                         return -ENOMEM;
394                 }
395
396                 smscore_putbuffer(dev, cb);
397         }
398
399         printk(KERN_INFO "%s allocated %d buffers\n",
400                __func__, dev->num_buffers);
401
402         dev->mode = DEVICE_MODE_NONE;
403         dev->context = params->context;
404         dev->device = params->device;
405         dev->setmode_handler = params->setmode_handler;
406         dev->detectmode_handler = params->detectmode_handler;
407         dev->sendrequest_handler = params->sendrequest_handler;
408         dev->preload_handler = params->preload_handler;
409         dev->postload_handler = params->postload_handler;
410
411         dev->device_flags = params->flags;
412         strcpy(dev->devpath, params->devpath);
413
414         smscore_registry_settype(dev->devpath, params->device_type);
415
416         /* add device to devices list */
417         kmutex_lock(&g_smscore_deviceslock);
418         list_add(&dev->entry, &g_smscore_devices);
419         kmutex_unlock(&g_smscore_deviceslock);
420
421         *coredev = dev;
422
423         printk(KERN_INFO "%s device %p created\n", __func__, dev);
424
425         return 0;
426 }
427
428 /**
429  * sets initial device mode and notifies client hotplugs that device is ready
430  *
431  * @param coredev pointer to a coredev object returned by smscore_register_device
432  *
433  * @return 0 on success, <0 on error.
434  */
435 int smscore_start_device(smscore_device_t *coredev)
436 {
437         int rc = smscore_set_device_mode(coredev, smscore_registry_getmode(coredev->devpath));
438         if (rc < 0) {
439                 printk(KERN_INFO "%s set device mode faile , rc %d\n", __func__, rc);
440                 return rc;
441         }
442
443         kmutex_lock(&g_smscore_deviceslock);
444
445         rc = smscore_notify_callbacks(coredev, coredev->device, 1);
446
447         printk(KERN_INFO "%s device %p started, rc %d\n",
448                __func__, coredev, rc);
449
450         kmutex_unlock(&g_smscore_deviceslock);
451
452         return rc;
453 }
454
455 int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
456                                  size_t size, struct completion *completion)
457 {
458         int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
459         if (rc < 0) {
460                 printk(KERN_INFO "%s sendrequest returned error %d\n",
461                        __func__, rc);
462                 return rc;
463         }
464
465         return wait_for_completion_timeout(completion,
466                                            msecs_to_jiffies(10000)) ?
467                                                 0 : -ETIME;
468 }
469
470 int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer,
471                                   size_t size)
472 {
473         SmsFirmware_ST *firmware = (SmsFirmware_ST *) buffer;
474         SmsMsgHdr_ST *msg;
475         u32 mem_address = firmware->StartAddress;
476         u8 *payload = firmware->Payload;
477         int rc = 0;
478
479         printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
480                __func__, mem_address, firmware->Length);
481         if (coredev->preload_handler) {
482                 rc = coredev->preload_handler(coredev->context);
483                 if (rc < 0)
484                         return rc;
485         }
486
487         /* PAGE_SIZE buffer shall be enough and dma aligned */
488         msg = (SmsMsgHdr_ST *) kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
489         if (!msg)
490                 return -ENOMEM;
491
492         if (coredev->mode != DEVICE_MODE_NONE) {
493                 PDEBUG("Sending reload command\n");
494                 SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
495                              sizeof(SmsMsgHdr_ST));
496                 rc = smscore_sendrequest_and_wait(coredev, msg,
497                                                   msg->msgLength,
498                                                   &coredev->reload_start_done);
499                 mem_address = *(u32 *) &payload[20];
500         }
501
502         while (size && rc >= 0) {
503                 SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
504                 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
505
506                 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
507                              (u16)(sizeof(SmsMsgHdr_ST) +
508                                       sizeof(u32) + payload_size));
509
510                 DataMsg->MemAddr = mem_address;
511                 memcpy(DataMsg->Payload, payload, payload_size);
512
513                 if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
514                     (coredev->mode == DEVICE_MODE_NONE))
515                         rc = coredev->sendrequest_handler(coredev->context, DataMsg, DataMsg->xMsgHeader.msgLength);
516                 else
517                         rc = smscore_sendrequest_and_wait(coredev, DataMsg, DataMsg->xMsgHeader.msgLength, &coredev->data_download_done);
518
519                 payload += payload_size;
520                 size -= payload_size;
521                 mem_address += payload_size;
522         }
523
524         if (rc >= 0) {
525                 if (coredev->mode == DEVICE_MODE_NONE) {
526                         SmsMsgData_ST *TriggerMsg = (SmsMsgData_ST *) msg;
527
528                         SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
529                                      sizeof(SmsMsgHdr_ST) +
530                                      sizeof(u32) * 5);
531
532                         TriggerMsg->msgData[0] = firmware->StartAddress; /* Entry point */
533                         TriggerMsg->msgData[1] = 5; /* Priority */
534                         TriggerMsg->msgData[2] = 0x200; /* Stack size */
535                         TriggerMsg->msgData[3] = 0; /* Parameter */
536                         TriggerMsg->msgData[4] = 4; /* Task ID */
537
538                         if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
539                                 rc = coredev->sendrequest_handler(coredev->context, TriggerMsg, TriggerMsg->xMsgHeader.msgLength);
540                                 msleep(100);
541                         } else
542                                 rc = smscore_sendrequest_and_wait(coredev, TriggerMsg, TriggerMsg->xMsgHeader.msgLength, &coredev->trigger_done);
543                 } else {
544                         SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
545                                      sizeof(SmsMsgHdr_ST));
546
547                         rc = coredev->sendrequest_handler(coredev->context,
548                                                           msg, msg->msgLength);
549                 }
550                 msleep(500);
551         }
552
553         printk("%s rc=%d, postload=%p \n", __func__, rc,
554                coredev->postload_handler);
555
556         kfree(msg);
557
558         return ((rc >= 0) && coredev->postload_handler) ?
559                 coredev->postload_handler(coredev->context) :
560                 rc;
561 }
562
563 /**
564  * loads specified firmware into a buffer and calls device loadfirmware_handler
565  *
566  * @param coredev pointer to a coredev object returned by smscore_register_device
567  * @param filename null-terminated string specifies firmware file name
568  * @param loadfirmware_handler device handler that loads firmware
569  *
570  * @return 0 on success, <0 on error.
571  */
572 int smscore_load_firmware_from_file(smscore_device_t *coredev, char *filename,
573                                     loadfirmware_t loadfirmware_handler)
574 {
575         int rc = -ENOENT;
576
577         const struct firmware *fw;
578         u8 *fw_buffer;
579
580         if (loadfirmware_handler == NULL && !(coredev->device_flags &
581                                               SMS_DEVICE_FAMILY2))
582                 return -EINVAL;
583
584         rc = request_firmware(&fw, filename, coredev->device);
585         if (rc < 0) {
586                 printk(KERN_INFO "%s failed to open \"%s\"\n",
587                        __func__, filename);
588                 return rc;
589         }
590         printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
591                filename, fw->size);
592         fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
593                             GFP_KERNEL | GFP_DMA);
594         if (fw_buffer) {
595                 memcpy(fw_buffer, fw->data, fw->size);
596
597                 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
598                       smscore_load_firmware_family2(coredev, fw_buffer, fw->size) :
599                         loadfirmware_handler(coredev->context, fw_buffer, fw->size);
600
601                 kfree(fw_buffer);
602         } else {
603                 printk(KERN_INFO "%s failed to allocate firmware buffer\n",
604                        __func__);
605                 rc = -ENOMEM;
606         }
607
608         release_firmware(fw);
609
610         return rc;
611 }
612
613 int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer,
614                                       int size, int new_mode)
615 {
616         PERROR("Feature not implemented yet\n");
617         return -EFAULT;
618 }
619
620 /**
621  * notifies all clients registered with the device, notifies hotplugs, frees all buffers and coredev object
622  *
623  * @param coredev pointer to a coredev object returned by smscore_register_device
624  *
625  * @return 0 on success, <0 on error.
626  */
627 void smscore_unregister_device(smscore_device_t *coredev)
628 {
629         smscore_buffer_t *cb;
630         int num_buffers = 0;
631         int retry = 0;
632
633         kmutex_lock(&g_smscore_deviceslock);
634
635         smscore_notify_clients(coredev);
636         smscore_notify_callbacks(coredev, NULL, 0);
637
638         /* at this point all buffers should be back
639          * onresponse must no longer be called */
640
641         while (1) {
642                 while ((cb = smscore_getbuffer(coredev))) {
643                         kfree(cb);
644                         num_buffers++;
645                 }
646                 if (num_buffers == coredev->num_buffers)
647                         break;
648                 if (++retry > 10) {
649                         printk(KERN_INFO "%s exiting although "
650                                "not all buffers released.\n", __func__);
651                         break;
652                 }
653
654                 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
655                        coredev->num_buffers - num_buffers);
656                 msleep(100);
657         }
658
659         printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
660
661         if (coredev->common_buffer)
662                 dma_free_coherent(NULL, coredev->common_buffer_size,
663                                   coredev->common_buffer,
664                                   coredev->common_buffer_phys);
665
666         list_del(&coredev->entry);
667         kfree(coredev);
668
669         kmutex_unlock(&g_smscore_deviceslock);
670
671         printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
672 }
673
674 int smscore_detect_mode(smscore_device_t *coredev)
675 {
676         void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
677                                GFP_KERNEL | GFP_DMA);
678         SmsMsgHdr_ST *msg = (SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
679         int rc;
680
681         if (!buffer)
682                 return -ENOMEM;
683
684         SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));
685
686         rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
687                                           &coredev->version_ex_done);
688         if (rc == -ETIME) {
689                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed first try\n", __func__);
690
691                 if (wait_for_completion_timeout(&coredev->resume_done,
692                                                 msecs_to_jiffies(5000))) {
693                         rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
694                         if (rc < 0)
695                                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d\n", __func__, rc);
696                 } else
697                         rc = -ETIME;
698         }
699
700         kfree(buffer);
701
702         return rc;
703 }
704
705 char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
706         /*Stellar                       NOVA A0                 Nova B0                         VEGA*/
707         /*DVBT*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
708         /*DVBH*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
709         /*TDMB*/  {"none", "tdmb_nova_12mhz.inp", "none", "none"},
710         /*DABIP*/ {"none", "none", "none", "none"},
711         /*BDA*/   {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
712         /*ISDBT*/ {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
713         /*ISDBTBDA*/{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
714         /*CMMB*/  {"none", "none", "none", "cmmb_vega_12mhz.inp"}
715 };
716
717
718 /**
719  * calls device handler to change mode of operation
720  * NOTE: stellar/usb may disconnect when changing mode
721  *
722  * @param coredev pointer to a coredev object returned by smscore_register_device
723  * @param mode requested mode of operation
724  *
725  * @return 0 on success, <0 on error.
726  */
727 int smscore_set_device_mode(smscore_device_t *coredev, int mode)
728 {
729         void *buffer;
730         int rc = 0;
731         sms_device_type_st type;
732
733         PDEBUG("set device mode to %d\n", mode);
734         if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
735                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
736                         printk(KERN_INFO "%s invalid mode specified %d\n",
737                                __func__, mode);
738                         return -EINVAL;
739                 }
740
741                 smscore_registry_setmode(coredev->devpath, mode);
742
743                 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
744                         rc = smscore_detect_mode(coredev);
745                         if (rc < 0) {
746                                 printk(KERN_INFO "%s mode detect failed %d\n",
747                                        __func__, rc);
748                                 return rc;
749                         }
750                 }
751
752                 if (coredev->mode == mode) {
753                         printk(KERN_INFO "%s device mode %d already set\n",
754                                __func__, mode);
755                         return 0;
756                 }
757
758                 if (!(coredev->modes_supported & (1 << mode))) {
759                         type = smscore_registry_gettype(coredev->devpath);
760                         rc = smscore_load_firmware_from_file(coredev, smscore_fw_lkup[mode][type], NULL);
761                         if (rc < 0) {
762                                 printk(KERN_INFO "%s load firmware failed %d\n", __func__, rc);
763                                 return rc;
764                         }
765                 } else
766                         printk(KERN_INFO "%s mode %d supported by running firmware\n", __func__, mode);
767
768                 buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT,
769                                  GFP_KERNEL | GFP_DMA);
770                 if (buffer) {
771                         SmsMsgData_ST *msg = (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
772
773                         SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ, sizeof(SmsMsgData_ST));
774                         msg->msgData[0] = mode;
775
776                         rc = smscore_sendrequest_and_wait(coredev, msg, msg->xMsgHeader.msgLength, &coredev->init_device_done);
777
778                         kfree(buffer);
779                 } else {
780                         printk(KERN_INFO "%s Could not allocate buffer for init device message.\n", __func__);
781                         rc = -ENOMEM;
782                 }
783         } else {
784                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
785                         printk(KERN_INFO "%s invalid mode specified %d\n",
786                                __func__, mode);
787                         return -EINVAL;
788                 }
789
790                 smscore_registry_setmode(coredev->devpath, mode);
791
792                 if (coredev->detectmode_handler)
793                         coredev->detectmode_handler(coredev->context,
794                                                     &coredev->mode);
795
796                 if (coredev->mode != mode && coredev->setmode_handler)
797                         rc = coredev->setmode_handler(coredev->context, mode);
798         }
799
800         if (rc >= 0) {
801                 coredev->mode = mode;
802                 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
803         }
804
805         if (rc != 0)
806                 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
807         return rc;
808 }
809
810 /**
811  * calls device handler to get current mode of operation
812  *
813  * @param coredev pointer to a coredev object returned by smscore_register_device
814  *
815  * @return current mode
816  */
817 int smscore_get_device_mode(smscore_device_t *coredev)
818 {
819         return coredev->mode;
820 }
821
822 /**
823  * find client by response id & type within the clients list.
824  * return client handle or NULL.
825  *
826  * @param coredev pointer to a coredev object returned by smscore_register_device
827  * @param data_type client data type (SMS_DONT_CARE for all types)
828  * @param id client id (SMS_DONT_CARE for all id)
829  *
830  */
831 smscore_client_t *smscore_find_client(smscore_device_t *coredev, int data_type, int id)
832 {
833         smscore_client_t *client = NULL;
834         struct list_head *next, *first;
835         unsigned long flags;
836         struct list_head *firstid, *nextid;
837
838
839         spin_lock_irqsave(&coredev->clientslock, flags);
840         first = &coredev->clients;
841         for (next = first->next;
842              (next != first) && !client;
843              next = next->next) {
844                 firstid = &((smscore_client_t *)next)->idlist;
845                 for (nextid = firstid->next;
846                      nextid != firstid;
847                      nextid = nextid->next) {
848                         if ((((smscore_idlist_t *)nextid)->id  == id) &&
849                             (((smscore_idlist_t *)nextid)->data_type  == data_type ||
850                             (((smscore_idlist_t *)nextid)->data_type  == 0))) {
851                                 client = (smscore_client_t *) next;
852                                 break;
853                         }
854                 }
855         }
856         spin_unlock_irqrestore(&coredev->clientslock, flags);
857         return client;
858 }
859
860 /**
861  * find client by response id/type, call clients onresponse handler
862  * return buffer to pool on error
863  *
864  * @param coredev pointer to a coredev object returned by smscore_register_device
865  * @param cb pointer to response buffer descriptor
866  *
867  */
868 void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
869 {
870         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
871         smscore_client_t *client = smscore_find_client(coredev, phdr->msgType,
872                                                        phdr->msgDstId);
873         int rc = -EBUSY;
874
875         static unsigned long last_sample_time = 0;
876         static int data_total = 0;
877         unsigned long time_now = jiffies_to_msecs(jiffies);
878
879         if (!last_sample_time)
880                 last_sample_time = time_now;
881
882         if (time_now - last_sample_time > 10000) {
883                 printk("\n%s data rate %d bytes/secs\n", __func__,
884                        (int)((data_total * 1000) /
885                              (time_now - last_sample_time)));
886
887                 last_sample_time = time_now;
888                 data_total = 0;
889         }
890
891         data_total += cb->size;
892         /* If no client registered for type & id,
893          * check for control client where type is not registered */
894         if (client)
895                 rc = client->onresponse_handler(client->context, cb);
896
897         if (rc < 0) {
898                 switch (phdr->msgType) {
899                 case MSG_SMS_GET_VERSION_EX_RES:
900                 {
901                         SmsVersionRes_ST *ver = (SmsVersionRes_ST *) phdr;
902                         printk("%s: MSG_SMS_GET_VERSION_EX_RES id %d "
903                                "prots 0x%x ver %d.%d\n", __func__,
904                                ver->FirmwareId, ver->SupportedProtocols,
905                                ver->RomVersionMajor, ver->RomVersionMinor);
906
907                         coredev->mode = ver->FirmwareId == 255 ?
908                                 DEVICE_MODE_NONE : ver->FirmwareId;
909                         coredev->modes_supported = ver->SupportedProtocols;
910
911                         complete(&coredev->version_ex_done);
912                         break;
913                 }
914                 case MSG_SMS_INIT_DEVICE_RES:
915                         printk("%s: MSG_SMS_INIT_DEVICE_RES\n", __func__);
916                         complete(&coredev->init_device_done);
917                         break;
918                 case MSG_SW_RELOAD_START_RES:
919                         printk("%s: MSG_SW_RELOAD_START_RES\n", __func__);
920                         complete(&coredev->reload_start_done);
921                         break;
922                 case MSG_SMS_DATA_DOWNLOAD_RES:
923                         complete(&coredev->data_download_done);
924                         break;
925                 case MSG_SW_RELOAD_EXEC_RES:
926                         printk("%s: MSG_SW_RELOAD_EXEC_RES\n", __func__);
927                         break;
928                 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
929                         printk("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
930                                __func__);
931                         complete(&coredev->trigger_done);
932                         break;
933                 case MSG_SMS_SLEEP_RESUME_COMP_IND:
934                         complete(&coredev->resume_done);
935                         break;
936                 default:
937                         break;
938                 }
939                 smscore_putbuffer(coredev, cb);
940         }
941 }
942
943 /**
944  * return pointer to next free buffer descriptor from core pool
945  *
946  * @param coredev pointer to a coredev object returned by smscore_register_device
947  *
948  * @return pointer to descriptor on success, NULL on error.
949  */
950 smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
951 {
952         smscore_buffer_t *cb = NULL;
953         unsigned long flags;
954
955         spin_lock_irqsave(&coredev->bufferslock, flags);
956
957         if (!list_empty(&coredev->buffers)) {
958                 cb = (smscore_buffer_t *) coredev->buffers.next;
959                 list_del(&cb->entry);
960         }
961
962         spin_unlock_irqrestore(&coredev->bufferslock, flags);
963
964         return cb;
965 }
966
967 /**
968  * return buffer descriptor to a pool
969  *
970  * @param coredev pointer to a coredev object returned by smscore_register_device
971  * @param cb pointer buffer descriptor
972  *
973  */
974 void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
975 {
976         list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
977 }
978
979 int smscore_validate_client(smscore_device_t *coredev,
980                             smscore_client_t *client, int data_type, int id)
981 {
982         smscore_idlist_t *listentry;
983         smscore_client_t *registered_client;
984
985         if (!client) {
986                 PERROR("bad parameter.\n");
987                 return -EFAULT;
988         }
989         registered_client = smscore_find_client(coredev, data_type, id);
990         if (registered_client == client)
991                 return 0;
992
993         if (registered_client) {
994                 PERROR("The msg ID already registered to another client.\n");
995                 return -EEXIST;
996         }
997         listentry = kzalloc(sizeof(smscore_idlist_t), GFP_KERNEL);
998         if (!listentry) {
999                 PERROR("Can't allocate memory for client id.\n");
1000                 return -ENOMEM;
1001         }
1002         listentry->id = id;
1003         listentry->data_type = data_type;
1004         list_add_locked(&listentry->entry, &client->idlist,
1005                         &coredev->clientslock);
1006         return 0;
1007 }
1008
1009 /**
1010  * creates smsclient object, check that id is taken by another client
1011  *
1012  * @param coredev pointer to a coredev object from clients hotplug
1013  * @param initial_id all messages with this id would be sent to this client
1014  * @param data_type all messages of this type would be sent to this client
1015  * @param onresponse_handler client handler that is called to process incoming messages
1016  * @param onremove_handler client handler that is called when device is removed
1017  * @param context client-specific context
1018  * @param client pointer to a value that receives created smsclient object
1019  *
1020  * @return 0 on success, <0 on error.
1021  */
1022 int smscore_register_client(smscore_device_t *coredev, smsclient_params_t *params, smscore_client_t **client)
1023 {
1024         smscore_client_t *newclient;
1025         /* check that no other channel with same parameters exists */
1026         if (smscore_find_client(coredev, params->data_type,
1027                                 params->initial_id)) {
1028                 PERROR("Client already exist.\n");
1029                 return -EEXIST;
1030         }
1031
1032         newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1033         if (!newclient) {
1034                 PERROR("Failed to allocate memory for client.\n");
1035                 return -ENOMEM;
1036         }
1037
1038         INIT_LIST_HEAD(&newclient->idlist);
1039         newclient->coredev = coredev;
1040         newclient->onresponse_handler = params->onresponse_handler;
1041         newclient->onremove_handler = params->onremove_handler;
1042         newclient->context = params->context;
1043         list_add_locked(&newclient->entry, &coredev->clients,
1044                         &coredev->clientslock);
1045         smscore_validate_client(coredev, newclient, params->data_type,
1046                                 params->initial_id);
1047         *client = newclient;
1048         PDEBUG("%p %d %d\n", params->context, params->data_type,
1049                params->initial_id);
1050
1051         return 0;
1052 }
1053
1054 /**
1055  * frees smsclient object and all subclients associated with it
1056  *
1057  * @param client pointer to smsclient object returned by smscore_register_client
1058  *
1059  */
1060 void smscore_unregister_client(smscore_client_t *client)
1061 {
1062         smscore_device_t *coredev = client->coredev;
1063         unsigned long flags;
1064
1065         spin_lock_irqsave(&coredev->clientslock, flags);
1066
1067
1068         while (!list_empty(&client->idlist)) {
1069                 smscore_idlist_t *identry =
1070                         (smscore_idlist_t *) client->idlist.next;
1071                 list_del(&identry->entry);
1072                 kfree(identry);
1073         }
1074
1075         printk(KERN_INFO "%s %p\n", __func__, client->context);
1076
1077         list_del(&client->entry);
1078         kfree(client);
1079
1080         spin_unlock_irqrestore(&coredev->clientslock, flags);
1081 }
1082
1083 /**
1084  * verifies that source id is not taken by another client,
1085  * calls device handler to send requests to the device
1086  *
1087  * @param client pointer to smsclient object returned by smscore_register_client
1088  * @param buffer pointer to a request buffer
1089  * @param size size (in bytes) of request buffer
1090  *
1091  * @return 0 on success, <0 on error.
1092  */
1093 int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
1094 {
1095         smscore_device_t *coredev;
1096         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1097         int rc;
1098
1099         if (client == NULL) {
1100                 printk(KERN_ERR "%s Got NULL client\n", __func__);
1101                 return -EINVAL;
1102         }
1103
1104         coredev = client->coredev;
1105
1106         /* check that no other channel with same id exists */
1107         if (coredev == NULL) {
1108                 printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1109                 return -EINVAL;
1110         }
1111
1112         rc = smscore_validate_client(client->coredev, client, 0,
1113                                      phdr->msgSrcId);
1114         if (rc < 0)
1115                 return rc;
1116
1117         return coredev->sendrequest_handler(coredev->context, buffer, size);
1118 }
1119
1120 /**
1121  * return the size of large (common) buffer
1122  *
1123  * @param coredev pointer to a coredev object from clients hotplug
1124  *
1125  * @return size (in bytes) of the buffer
1126  */
1127 int smscore_get_common_buffer_size(smscore_device_t *coredev)
1128 {
1129         return coredev->common_buffer_size;
1130 }
1131
1132 /**
1133  * maps common buffer (if supported by platform)
1134  *
1135  * @param coredev pointer to a coredev object from clients hotplug
1136  * @param vma pointer to vma struct from mmap handler
1137  *
1138  * @return 0 on success, <0 on error.
1139  */
1140 int smscore_map_common_buffer(smscore_device_t *coredev,
1141                                struct vm_area_struct *vma)
1142 {
1143         unsigned long end = vma->vm_end,
1144                       start = vma->vm_start,
1145                       size = PAGE_ALIGN(coredev->common_buffer_size);
1146
1147         if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
1148              (vma->vm_flags & VM_WRITE)) {
1149                 printk(KERN_INFO "%s invalid vm flags\n", __func__);
1150                 return -EINVAL;
1151         }
1152
1153         if ((end - start) != size) {
1154                 printk(KERN_INFO "%s invalid size %d expected %d\n",
1155                        __func__, (int)(end - start), (int) size);
1156                 return -EINVAL;
1157         }
1158
1159         if (remap_pfn_range(vma, start,
1160                             coredev->common_buffer_phys >> PAGE_SHIFT,
1161                             size, pgprot_noncached(vma->vm_page_prot)))
1162         {
1163                 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1164                 return -EAGAIN;
1165         }
1166
1167         return 0;
1168 }
1169
1170 int smscore_module_init(void)
1171 {
1172         int rc = 0;
1173
1174         INIT_LIST_HEAD(&g_smscore_notifyees);
1175         INIT_LIST_HEAD(&g_smscore_devices);
1176         kmutex_init(&g_smscore_deviceslock);
1177
1178         INIT_LIST_HEAD(&g_smscore_registry);
1179         kmutex_init(&g_smscore_registrylock);
1180
1181         /* USB Register */
1182         rc = smsusb_register();
1183
1184         /* DVB Register */
1185         rc = smsdvb_register();
1186
1187         printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1188
1189         return rc;
1190 }
1191
1192 void smscore_module_exit(void)
1193 {
1194
1195         kmutex_lock(&g_smscore_deviceslock);
1196         while (!list_empty(&g_smscore_notifyees)) {
1197                 smscore_device_notifyee_t *notifyee =
1198                         (smscore_device_notifyee_t *) g_smscore_notifyees.next;
1199
1200                 list_del(&notifyee->entry);
1201                 kfree(notifyee);
1202         }
1203         kmutex_unlock(&g_smscore_deviceslock);
1204
1205         kmutex_lock(&g_smscore_registrylock);
1206         while (!list_empty(&g_smscore_registry)) {
1207                 smscore_registry_entry_t *entry =
1208                         (smscore_registry_entry_t *) g_smscore_registry.next;
1209
1210                 list_del(&entry->entry);
1211                 kfree(entry);
1212         }
1213         kmutex_unlock(&g_smscore_registrylock);
1214
1215         /* DVB UnRegister */
1216         smsdvb_unregister();
1217
1218         /* Unregister USB */
1219         smsusb_unregister();
1220
1221         printk(KERN_INFO "%s\n", __func__);
1222 }
1223
1224 module_init(smscore_module_init);
1225 module_exit(smscore_module_exit);
1226
1227 MODULE_DESCRIPTION("smscore");
1228 MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1229 MODULE_LICENSE("GPL");