Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / ft1000 / ft1000-usb / ft1000_chdev.c
1 //---------------------------------------------------------------------------
2 // FT1000 driver for Flarion Flash OFDM NIC Device
3 //
4 // Copyright (C) 2006 Flarion Technologies, All rights reserved.
5 //
6 // This program is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by the Free
8 // Software Foundation; either version 2 of the License, or (at your option) any
9 // later version. This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 // or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details. You should have received a copy of the GNU General Public
13 // License along with this program; if not, write to the
14 // Free Software Foundation, Inc., 59 Temple Place -
15 // Suite 330, Boston, MA 02111-1307, USA.
16 //---------------------------------------------------------------------------
17 //
18 // File:         ft1000_chdev.c
19 //
20 // Description:  Custom character device dispatch routines.
21 //
22 // History:
23 // 8/29/02    Whc                Ported to Linux.
24 // 6/05/06    Whc                Porting to Linux 2.6.9
25 //
26 //---------------------------------------------------------------------------
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/signal.h>
31 #include <linux/errno.h>
32 #include <linux/poll.h>
33 #include <linux/netdevice.h>
34 #include <linux/delay.h>
35
36 #include <linux/fs.h>
37 #include <linux/kmod.h>
38 #include <linux/ioctl.h>
39 #include <linux/unistd.h>
40
41 #include "ft1000_usb.h"
42 //#include "ft1000_ioctl.h"
43
44 static int ft1000_flarion_cnt = 0;
45
46 //need to looking usage of ft1000Handle
47
48 static int ft1000_ChOpen (struct inode *Inode, struct file *File);
49 static unsigned int ft1000_ChPoll(struct file *file, poll_table *wait);
50 static long ft1000_ChIoctl(struct file *File, unsigned int Command,
51                            unsigned long Argument);
52 static int ft1000_ChRelease (struct inode *Inode, struct file *File);
53
54 // Global pointer to device object
55 static struct ft1000_device *pdevobj[MAX_NUM_CARDS + 2];
56 //static devfs_handle_t ft1000Handle[MAX_NUM_CARDS];
57
58 // List to free receive command buffer pool
59 struct list_head freercvpool;
60
61 // lock to arbitrate free buffer list for receive command data
62 spinlock_t free_buff_lock;
63
64 int numofmsgbuf = 0;
65
66 // Global variable to indicate that all provisioning data is sent to DSP
67 //BOOLEAN fProvComplete;
68
69 //
70 // Table of entry-point routines for char device
71 //
72 static struct file_operations ft1000fops =
73 {
74         .unlocked_ioctl = ft1000_ChIoctl,
75         .poll           = ft1000_ChPoll,
76         .open           = ft1000_ChOpen,
77         .release        = ft1000_ChRelease,
78         .llseek         = no_llseek,
79 };
80
81
82
83
84 //---------------------------------------------------------------------------
85 // Function:    exec_mknod
86 //
87 // Parameters:
88 //
89 // Returns:
90 //
91 // Description:
92 //
93 // Notes:
94 //
95 //---------------------------------------------------------------------------
96 static int exec_mknod (void *pdata)
97 {
98         struct ft1000_info *info;
99     char mjnum[4];
100     char minornum[4];
101     char temp[32];
102     int retcode;
103 //    int i;                                    //aelias [-] reason : unused variable
104     char *envp[] = { "HOME=/", "PATH=/usr/bin:/bin", NULL };
105     char *argv[]={"-m 666",temp,"c",mjnum,minornum,NULL};
106
107     info = pdata;
108     DEBUG("ft1000_chdev:exec_mknod is called with major number = %d\n", info->DeviceMajor);
109     sprintf(temp, "%s%s", "/dev/", info->DeviceName) ;
110     sprintf(mjnum, "%d", info->DeviceMajor);
111     sprintf(minornum, "%d", info->CardNumber);
112
113     //char *argv[]={"mknod","-m 666",temp,"c",mjnum,minornum,NULL};
114 //    char *argv[]={"-m 666",temp,"c",mjnum,minornum,NULL};
115
116     //for (i=0; i<7;i++)
117     //    DEBUG("argv[%d]=%s\n", i, argv[i]);
118
119
120     retcode = call_usermodehelper ("/bin/mknod", argv, envp, 1);
121     if (retcode) {
122         DEBUG("ft1000_chdev:exec_mknod failed to make the node: retcode = %d\n", retcode);
123     }
124
125
126
127     return retcode;
128
129 }
130
131 //---------------------------------------------------------------------------
132 // Function:    rm_mknod
133 //
134 // Description: This module removes the FT1000 device file
135 //
136 //---------------------------------------------------------------------------
137 static int rm_mknod (void *pdata)
138 {
139
140         struct ft1000_info *info;
141     //char *argv[4]={"rm", "-f", "/dev/FT1000", NULL};
142     int retcode;
143     char temp[32];
144     char *argv[]={"rm", "-f", temp, NULL};
145
146         info = (struct ft1000_info *)pdata;
147     DEBUG("ft1000_chdev:rm_mknod is called for device %s\n", info->DeviceName);
148     sprintf(temp, "%s%s", "/dev/", info->DeviceName) ;
149
150 //    char *argv[]={"rm", "-f", temp, NULL};
151
152     retcode = call_usermodehelper ("/bin/rm", argv, NULL, 1);
153     if (retcode) {
154         DEBUG("ft1000_chdev:rm_mknod failed to remove the node: retcode = %d\n", retcode);
155     }
156     else
157         DEBUG("ft1000_chdev:rm_mknod done!\n");
158
159
160     return retcode;
161
162 }
163 //---------------------------------------------------------------------------
164 // Function:    ft1000_get_buffer
165 //
166 // Parameters:
167 //
168 // Returns:
169 //
170 // Description:
171 //
172 // Notes:
173 //
174 //---------------------------------------------------------------------------
175 struct dpram_blk *ft1000_get_buffer(struct list_head *bufflist)
176 {
177     unsigned long flags;
178         struct dpram_blk *ptr;
179
180     spin_lock_irqsave(&free_buff_lock, flags);
181     // Check if buffer is available
182     if ( list_empty(bufflist) ) {
183         DEBUG("ft1000_get_buffer:  No more buffer - %d\n", numofmsgbuf);
184         ptr = NULL;
185     }
186     else {
187         numofmsgbuf--;
188         ptr = list_entry(bufflist->next, struct dpram_blk, list);
189         list_del(&ptr->list);
190         //DEBUG("ft1000_get_buffer: number of free msg buffers = %d\n", numofmsgbuf);
191     }
192     spin_unlock_irqrestore(&free_buff_lock, flags);
193
194     return ptr;
195 }
196
197
198
199
200 //---------------------------------------------------------------------------
201 // Function:    ft1000_free_buffer
202 //
203 // Parameters:
204 //
205 // Returns:
206 //
207 // Description:
208 //
209 // Notes:
210 //
211 //---------------------------------------------------------------------------
212 void ft1000_free_buffer(struct dpram_blk *pdpram_blk, struct list_head *plist)
213 {
214     unsigned long flags;
215
216     spin_lock_irqsave(&free_buff_lock, flags);
217     // Put memory back to list
218     list_add_tail(&pdpram_blk->list, plist);
219     numofmsgbuf++;
220     //DEBUG("ft1000_free_buffer: number of free msg buffers = %d\n", numofmsgbuf);
221     spin_unlock_irqrestore(&free_buff_lock, flags);
222 }
223
224 //---------------------------------------------------------------------------
225 // Function:    ft1000_CreateDevice
226 //
227 // Parameters:  dev - pointer to adapter object
228 //
229 // Returns:     0 if successful
230 //
231 // Description: Creates a private char device.
232 //
233 // Notes:       Only called by init_module().
234 //
235 //---------------------------------------------------------------------------
236 int ft1000_CreateDevice(struct ft1000_device *dev)
237 {
238         struct ft1000_info *info = netdev_priv(dev->net);
239     int result;
240     int i;
241     pid_t pid;
242
243     // make a new device name
244     sprintf(info->DeviceName, "%s%d", "FT100", info->CardNumber);
245
246     // Delete any existing FT1000 node
247     pid = kernel_thread (rm_mknod,(void *)info, 0);
248     msleep(1000);
249
250     DEBUG("ft1000_CreateDevice: number of instance = %d\n", ft1000_flarion_cnt);
251     DEBUG("DeviceCreated = %x\n", info->DeviceCreated);
252
253     //save the device info to global array
254     pdevobj[info->CardNumber] = dev;
255
256     DEBUG("ft1000_CreateDevice: ******SAVED pdevobj[%d]=%p\n", info->CardNumber, pdevobj[info->CardNumber]);    //aelias [+] reason:up
257
258     if (info->DeviceCreated)
259     {
260         DEBUG("ft1000_CreateDevice: \"%s\" already registered\n", info->DeviceName);
261         return -EIO;
262     }
263
264
265     // register the device
266     DEBUG("ft1000_CreateDevice: \"%s\" device registration\n", info->DeviceName);
267     info->DeviceMajor = 0;
268
269     result = register_chrdev(info->DeviceMajor, info->DeviceName, &ft1000fops);
270     if (result < 0)
271     {
272         DEBUG("ft1000_CreateDevice: unable to get major %d\n", info->DeviceMajor);
273         return result;
274     }
275
276     DEBUG("ft1000_CreateDevice: registered char device \"%s\"\n", info->DeviceName);
277
278     // save a dynamic device major number
279     if (info->DeviceMajor == 0)
280     {
281         info->DeviceMajor = result;
282         DEBUG("ft1000_PcdCreateDevice: device major = %d\n", info->DeviceMajor);
283     }
284
285     // Create a thread to call user mode app to mknod
286     pid = kernel_thread (exec_mknod, (void *)info, 0);
287
288     // initialize application information
289     info->appcnt = 0;
290
291 //    if (ft1000_flarion_cnt == 0) {
292 //
293 //        DEBUG("Initialize free_buff_lock and freercvpool\n");
294 //        spin_lock_init(&free_buff_lock);
295 //
296 //        // initialize a list of buffers to be use for queuing up receive command data
297 //        INIT_LIST_HEAD (&freercvpool);
298 //
299 //        // create list of free buffers
300 //        for (i=0; i<NUM_OF_FREE_BUFFERS; i++) {
301 //            // Get memory for DPRAM_DATA link list
302 //            pdpram_blk = kmalloc ( sizeof(struct dpram_blk), GFP_KERNEL );
303 //            // Get a block of memory to store command data
304 //            pdpram_blk->pbuffer = kmalloc ( MAX_CMD_SQSIZE, GFP_KERNEL );
305 //            // link provisioning data
306 //            list_add_tail (&pdpram_blk->list, &freercvpool);
307 //        }
308 //        numofmsgbuf = NUM_OF_FREE_BUFFERS;
309 //    }
310
311
312     // initialize application information
313     info->appcnt = 0;
314     for (i=0; i<MAX_NUM_APP; i++) {
315         info->app_info[i].nTxMsg = 0;
316         info->app_info[i].nRxMsg = 0;
317         info->app_info[i].nTxMsgReject = 0;
318         info->app_info[i].nRxMsgMiss = 0;
319         info->app_info[i].fileobject = NULL;
320         info->app_info[i].app_id = i+1;
321         info->app_info[i].DspBCMsgFlag = 0;
322         info->app_info[i].NumOfMsg = 0;
323         init_waitqueue_head(&info->app_info[i].wait_dpram_msg);
324         INIT_LIST_HEAD (&info->app_info[i].app_sqlist);
325     }
326
327
328
329
330 //    ft1000Handle[info->CardNumber] = devfs_register(NULL, info->DeviceName, DEVFS_FL_AUTO_DEVNUM, 0, 0,
331 //                                  S_IFCHR | S_IRUGO | S_IWUGO, &ft1000fops, NULL);
332
333
334     info->DeviceCreated = TRUE;
335     ft1000_flarion_cnt++;
336
337     return result;
338 }
339
340 //---------------------------------------------------------------------------
341 // Function:    ft1000_DestroyDeviceDEBUG
342 //
343 // Parameters:  dev - pointer to adapter object
344 //
345 // Description: Destroys a private char device.
346 //
347 // Notes:       Only called by cleanup_module().
348 //
349 //---------------------------------------------------------------------------
350 void ft1000_DestroyDevice(struct net_device *dev)
351 {
352         struct ft1000_info *info = netdev_priv(dev);
353     int result = 0;
354     pid_t pid;
355                 int i;
356         struct dpram_blk *pdpram_blk;
357         struct dpram_blk *ptr;
358
359     DEBUG("ft1000_chdev:ft1000_DestroyDevice called\n");
360
361
362
363     if (info->DeviceCreated)
364         {
365         ft1000_flarion_cnt--;
366                 unregister_chrdev(info->DeviceMajor, info->DeviceName);
367                 DEBUG("ft1000_DestroyDevice: unregistered device \"%s\", result = %d\n",
368                                            info->DeviceName, result);
369
370        pid = kernel_thread (rm_mknod, (void *)info, 0);
371
372         // Make sure we free any memory reserve for slow Queue
373         for (i=0; i<MAX_NUM_APP; i++) {
374             while (list_empty(&info->app_info[i].app_sqlist) == 0) {
375                 pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
376                 list_del(&pdpram_blk->list);
377                 ft1000_free_buffer(pdpram_blk, &freercvpool);
378
379             }
380             wake_up_interruptible(&info->app_info[i].wait_dpram_msg);
381         }
382
383         // Remove buffer allocated for receive command data
384         if (ft1000_flarion_cnt == 0) {
385             while (list_empty(&freercvpool) == 0) {
386                 ptr = list_entry(freercvpool.next, struct dpram_blk, list);
387                 list_del(&ptr->list);
388                 kfree(ptr->pbuffer);
389                 kfree(ptr);
390             }
391         }
392
393 //        devfs_unregister(ft1000Handle[info->CardNumber]);
394
395                 info->DeviceCreated = FALSE;
396
397                 pdevobj[info->CardNumber] = NULL;
398         }
399
400
401 }
402
403 //---------------------------------------------------------------------------
404 // Function:    ft1000_ChOpen
405 //
406 // Parameters:
407 //
408 // Description:
409 //
410 // Notes:
411 //
412 //---------------------------------------------------------------------------
413 static int ft1000_ChOpen (struct inode *Inode, struct file *File)
414 {
415         struct ft1000_info *info;
416     int i,num;
417
418     DEBUG("ft1000_ChOpen called\n");
419     num = (MINOR(Inode->i_rdev) & 0xf);
420     DEBUG("ft1000_ChOpen: minor number=%d\n", num);
421
422     for (i=0; i<5; i++)
423         DEBUG("pdevobj[%d]=%p\n", i, pdevobj[i]); //aelias [+] reason: down
424
425     if ( pdevobj[num] != NULL )
426         //info = (struct ft1000_info *)(pdevobj[num]->net->priv);
427                 info = (struct ft1000_info *)netdev_priv(pdevobj[num]->net);
428     else
429     {
430         DEBUG("ft1000_ChOpen: can not find device object %d\n", num);
431         return -1;
432     }
433
434     DEBUG("f_owner = %p number of application = %d\n", (&File->f_owner), info->appcnt );
435
436     // Check if maximum number of application exceeded
437     if (info->appcnt > MAX_NUM_APP) {
438         DEBUG("Maximum number of application exceeded\n");
439         return -EACCES;
440     }
441
442     // Search for available application info block
443     for (i=0; i<MAX_NUM_APP; i++) {
444         if ( (info->app_info[i].fileobject == NULL) ) {
445             break;
446         }
447     }
448
449     // Fail due to lack of application info block
450     if (i == MAX_NUM_APP) {
451         DEBUG("Could not find an application info block\n");
452         return -EACCES;
453     }
454
455     info->appcnt++;
456     info->app_info[i].fileobject = &File->f_owner;
457     info->app_info[i].nTxMsg = 0;
458     info->app_info[i].nRxMsg = 0;
459     info->app_info[i].nTxMsgReject = 0;
460     info->app_info[i].nRxMsgMiss = 0;
461
462     File->private_data = pdevobj[num]->net;
463
464         nonseekable_open(Inode, File);
465     return 0;
466 }
467
468
469 //---------------------------------------------------------------------------
470 // Function:    ft1000_ChPoll
471 //
472 // Parameters:
473 //
474 // Description:
475 //
476 // Notes:
477 //
478 //---------------------------------------------------------------------------
479
480 static unsigned int ft1000_ChPoll(struct file *file, poll_table *wait)
481 {
482     struct net_device *dev = file->private_data;
483         struct ft1000_info *info;
484     int i;
485
486     //DEBUG("ft1000_ChPoll called\n");
487     if (ft1000_flarion_cnt == 0) {
488         DEBUG("FT1000:ft1000_ChPoll called when ft1000_flarion_cnt is zero\n");
489         return (-EBADF);
490     }
491
492         info = (struct ft1000_info *) netdev_priv(dev);
493
494     // Search for matching file object
495     for (i=0; i<MAX_NUM_APP; i++) {
496         if ( info->app_info[i].fileobject == &file->f_owner) {
497             //DEBUG("FT1000:ft1000_ChIoctl: Message is for AppId = %d\n", info->app_info[i].app_id);
498             break;
499         }
500     }
501
502     // Could not find application info block
503     if (i == MAX_NUM_APP) {
504         DEBUG("FT1000:ft1000_ChIoctl:Could not find application info block\n");
505         return ( -EACCES );
506     }
507
508     if (list_empty(&info->app_info[i].app_sqlist) == 0) {
509         DEBUG("FT1000:ft1000_ChPoll:Message detected in slow queue\n");
510         return(POLLIN | POLLRDNORM | POLLPRI);
511     }
512
513     poll_wait (file, &info->app_info[i].wait_dpram_msg, wait);
514     //DEBUG("FT1000:ft1000_ChPoll:Polling for data from DSP\n");
515
516     return (0);
517 }
518
519 //---------------------------------------------------------------------------
520 // Function:    ft1000_ChIoctl
521 //
522 // Parameters:
523 //
524 // Description:
525 //
526 // Notes:
527 //
528 //---------------------------------------------------------------------------
529 static long ft1000_ChIoctl (struct file *File, unsigned int Command,
530                            unsigned long Argument)
531 {
532     void __user *argp = (void __user *)Argument;
533     struct net_device *dev;
534         struct ft1000_info *info;
535     struct ft1000_device *ft1000dev;
536     int result=0;
537     int cmd;
538     int i;
539     u16 tempword;
540     unsigned long flags;
541     struct timeval tv;
542     IOCTL_GET_VER get_ver_data;
543     IOCTL_GET_DSP_STAT get_stat_data;
544     u8 ConnectionMsg[] = {0x00,0x44,0x10,0x20,0x80,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x93,0x64,
545                           0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0a,
546                           0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
547                           0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
548                           0x00,0x00,0x02,0x37,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x01,0x00,0x01,0x7f,0x00,
549                           0x00,0x01,0x00,0x00};
550
551     unsigned short ledStat=0;
552     unsigned short conStat=0;
553
554     //DEBUG("ft1000_ChIoctl called\n");
555
556     if (ft1000_flarion_cnt == 0) {
557         DEBUG("FT1000:ft1000_ChIoctl called when ft1000_flarion_cnt is zero\n");
558         return (-EBADF);
559     }
560
561     //DEBUG("FT1000:ft1000_ChIoctl:Command = 0x%x Argument = 0x%8x\n", Command, (u32)Argument);
562
563     dev = File->private_data;
564         info = (struct ft1000_info *) netdev_priv(dev);
565     ft1000dev = info->pFt1000Dev;
566     cmd = _IOC_NR(Command);
567     //DEBUG("FT1000:ft1000_ChIoctl:cmd = 0x%x\n", cmd);
568
569     // process the command
570     switch (cmd) {
571     case IOCTL_REGISTER_CMD:
572             DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_REGISTER called\n");
573             result = get_user(tempword, (__u16 __user*)argp);
574             if (result) {
575                 DEBUG("result = %d failed to get_user\n", result);
576                 break;
577             }
578             if (tempword == DSPBCMSGID) {
579                 // Search for matching file object
580                 for (i=0; i<MAX_NUM_APP; i++) {
581                     if ( info->app_info[i].fileobject == &File->f_owner) {
582                         info->app_info[i].DspBCMsgFlag = 1;
583                         DEBUG("FT1000:ft1000_ChIoctl:Registered for broadcast messages\n");
584                         break;
585                     }
586                 }
587             }
588             break;
589
590     case IOCTL_GET_VER_CMD:
591         DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_GET_VER called\n");
592
593         get_ver_data.drv_ver = FT1000_DRV_VER;
594
595         if (copy_to_user(argp, &get_ver_data, sizeof(get_ver_data)) ) {
596             DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
597             result = -EFAULT;
598             break;
599         }
600
601         DEBUG("FT1000:ft1000_ChIoctl:driver version = 0x%x\n",(unsigned int)get_ver_data.drv_ver);
602
603         break;
604     case IOCTL_CONNECT:
605         // Connect Message
606         DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_CONNECT\n");
607         ConnectionMsg[79] = 0xfc;
608                            CardSendCommand(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
609
610         break;
611     case IOCTL_DISCONNECT:
612         // Disconnect Message
613         DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_DISCONNECT\n");
614         ConnectionMsg[79] = 0xfd;
615                            CardSendCommand(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
616         break;
617     case IOCTL_GET_DSP_STAT_CMD:
618         //DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_GET_DSP_STAT called\n");
619         memset(&get_stat_data, 0, sizeof(get_stat_data));
620         memcpy(get_stat_data.DspVer, info->DspVer, DSPVERSZ);
621         memcpy(get_stat_data.HwSerNum, info->HwSerNum, HWSERNUMSZ);
622         memcpy(get_stat_data.Sku, info->Sku, SKUSZ);
623         memcpy(get_stat_data.eui64, info->eui64, EUISZ);
624
625             if (info->ProgConStat != 0xFF) {
626                 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_LED, (PUCHAR)&ledStat, FT1000_MAG_DSP_LED_INDX);
627                 get_stat_data.LedStat = ntohs(ledStat);
628                 DEBUG("FT1000:ft1000_ChIoctl: LedStat = 0x%x\n", get_stat_data.LedStat);
629                 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_CON_STATE, (PUCHAR)&conStat, FT1000_MAG_DSP_CON_STATE_INDX);
630                 get_stat_data.ConStat = ntohs(conStat);
631                 DEBUG("FT1000:ft1000_ChIoctl: ConStat = 0x%x\n", get_stat_data.ConStat);
632             }
633             else {
634                 get_stat_data.ConStat = 0x0f;
635             }
636
637
638         get_stat_data.nTxPkts = info->stats.tx_packets;
639         get_stat_data.nRxPkts = info->stats.rx_packets;
640         get_stat_data.nTxBytes = info->stats.tx_bytes;
641         get_stat_data.nRxBytes = info->stats.rx_bytes;
642         do_gettimeofday ( &tv );
643         get_stat_data.ConTm = (u32)(tv.tv_sec - info->ConTm);
644         DEBUG("Connection Time = %d\n", (int)get_stat_data.ConTm);
645         if (copy_to_user(argp, &get_stat_data, sizeof(get_stat_data)) ) {
646             DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
647             result = -EFAULT;
648             break;
649         }
650         DEBUG("ft1000_chioctl: GET_DSP_STAT succeed\n");
651         break;
652     case IOCTL_SET_DPRAM_CMD:
653         {
654             IOCTL_DPRAM_BLK *dpram_data;
655             //IOCTL_DPRAM_COMMAND dpram_command;
656             USHORT qtype;
657             USHORT msgsz;
658                 struct pseudo_hdr *ppseudo_hdr;
659             PUSHORT pmsg;
660             USHORT total_len;
661             USHORT app_index;
662             u16 status;
663
664             //DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_SET_DPRAM called\n");
665
666
667             if (ft1000_flarion_cnt == 0) {
668                 return (-EBADF);
669             }
670
671             if (info->DrvMsgPend) {
672                 return (-ENOTTY);
673             }
674
675             if ( (info->DspAsicReset) || (info->fProvComplete == 0) ) {
676                 return (-EACCES);
677             }
678
679             info->fAppMsgPend = 1;
680
681             if (info->CardReady) {
682
683                //DEBUG("FT1000:ft1000_ChIoctl: try to SET_DPRAM \n");
684
685                 // Get the length field to see how many bytes to copy
686                 result = get_user(msgsz, (__u16 __user *)argp);
687                 msgsz = ntohs (msgsz);
688                 //DEBUG("FT1000:ft1000_ChIoctl: length of message = %d\n", msgsz);
689
690                 if (msgsz > MAX_CMD_SQSIZE) {
691                     DEBUG("FT1000:ft1000_ChIoctl: bad message length = %d\n", msgsz);
692                     result = -EINVAL;
693                     break;
694                 }
695
696                 result = -ENOMEM;
697                 dpram_data = kmalloc(msgsz + 2, GFP_KERNEL);
698                 if (!dpram_data)
699                         break;
700
701                 //if ( copy_from_user(&(dpram_command.dpram_blk), (PIOCTL_DPRAM_BLK)Argument, msgsz+2) ) {
702                 if ( copy_from_user(&dpram_data, argp, msgsz+2) ) {
703                     DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
704                     result = -EFAULT;
705                 }
706                 else {
707 #if 0
708                     // whc - for debugging only
709                     ptr = (char *)&dpram_data;
710                     for (i=0; i<msgsz; i++) {
711                         DEBUG(1,"FT1000:ft1000_ChIoctl: data %d = 0x%x\n", i, *ptr++);
712                     }
713 #endif
714                     // Check if this message came from a registered application
715                     for (i=0; i<MAX_NUM_APP; i++) {
716                         if ( info->app_info[i].fileobject == &File->f_owner) {
717                             break;
718                         }
719                     }
720                     if (i==MAX_NUM_APP) {
721                         DEBUG("FT1000:No matching application fileobject\n");
722                         result = -EINVAL;
723                         kfree(dpram_data);
724                         break;
725                     }
726                     app_index = i;
727
728                     // Check message qtype type which is the lower byte within qos_class
729                     //qtype = ntohs(dpram_command.dpram_blk.pseudohdr.qos_class) & 0xff;
730                     qtype = ntohs(dpram_data->pseudohdr.qos_class) & 0xff;
731                     //DEBUG("FT1000_ft1000_ChIoctl: qtype = %d\n", qtype);
732                     if (qtype) {
733                     }
734                     else {
735                         // Put message into Slow Queue
736                         // Only put a message into the DPRAM if msg doorbell is available
737                         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
738                         //DEBUG("FT1000_ft1000_ChIoctl: READ REGISTER tempword=%x\n", tempword);
739                         if (tempword & FT1000_DB_DPRAM_TX) {
740                             // Suspend for 2ms and try again due to DSP doorbell busy
741                             mdelay(2);
742                             status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
743                             if (tempword & FT1000_DB_DPRAM_TX) {
744                                 // Suspend for 1ms and try again due to DSP doorbell busy
745                                 mdelay(1);
746                                 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
747                                 if (tempword & FT1000_DB_DPRAM_TX) {
748                                     status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
749                                     if (tempword & FT1000_DB_DPRAM_TX) {
750                                         // Suspend for 3ms and try again due to DSP doorbell busy
751                                         mdelay(3);
752                                         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
753                                         if (tempword & FT1000_DB_DPRAM_TX) {
754                                             DEBUG("FT1000:ft1000_ChIoctl:Doorbell not available\n");
755                                             result = -ENOTTY;
756                                                 kfree(dpram_data);
757                                             break;
758                                         }
759                                     }
760                                 }
761                             }
762                         }
763
764                         //DEBUG("FT1000_ft1000_ChIoctl: finished reading register\n");
765
766                         // Make sure we are within the limits of the slow queue memory limitation
767                         if ( (msgsz < MAX_CMD_SQSIZE) && (msgsz > PSEUDOSZ) ) {
768                             // Need to put sequence number plus new checksum for message
769                             //pmsg = (PUSHORT)&dpram_command.dpram_blk.pseudohdr;
770                             pmsg = (PUSHORT)&dpram_data->pseudohdr;
771                                 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
772                             total_len = msgsz+2;
773                             if (total_len & 0x1) {
774                                 total_len++;
775                             }
776
777                             // Insert slow queue sequence number
778                             ppseudo_hdr->seq_num = info->squeseqnum++;
779                             ppseudo_hdr->portsrc = info->app_info[app_index].app_id;
780                             // Calculate new checksum
781                             ppseudo_hdr->checksum = *pmsg++;
782                             //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
783                             for (i=1; i<7; i++) {
784                                 ppseudo_hdr->checksum ^= *pmsg++;
785                                 //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
786                             }
787                             pmsg++;
788                                 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
789 #if 0
790                             ptr = dpram_data;
791                             DEBUG("FT1000:ft1000_ChIoctl: Command Send\n");
792                             for (i=0; i<total_len; i++) {
793                                 DEBUG("FT1000:ft1000_ChIoctl: data %d = 0x%x\n", i, *ptr++);
794                             }
795 #endif
796                             //dpram_command.extra = 0;
797
798                             //CardSendCommand(ft1000dev,(unsigned char*)&dpram_command,total_len+2);
799                             CardSendCommand(ft1000dev,(unsigned short*)dpram_data,total_len+2);
800
801
802                             info->app_info[app_index].nTxMsg++;
803                         }
804                         else {
805                             result = -EINVAL;
806                         }
807                     }
808                 }
809             }
810             else {
811                 DEBUG("FT1000:ft1000_ChIoctl: Card not ready take messages\n");
812                 result = -EACCES;
813             }
814             kfree(dpram_data);
815
816         }
817         break;
818     case IOCTL_GET_DPRAM_CMD:
819         {
820                 struct dpram_blk *pdpram_blk;
821             IOCTL_DPRAM_BLK __user *pioctl_dpram;
822             int msglen;
823
824             //DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_GET_DPRAM called\n");
825
826             if (ft1000_flarion_cnt == 0) {
827                 return (-EBADF);
828             }
829
830             // Search for matching file object
831             for (i=0; i<MAX_NUM_APP; i++) {
832                 if ( info->app_info[i].fileobject == &File->f_owner) {
833                     //DEBUG("FT1000:ft1000_ChIoctl: Message is for AppId = %d\n", info->app_info[i].app_id);
834                     break;
835                 }
836             }
837
838             // Could not find application info block
839             if (i == MAX_NUM_APP) {
840                 DEBUG("FT1000:ft1000_ChIoctl:Could not find application info block\n");
841                 result = -EBADF;
842                 break;
843             }
844
845             result = 0;
846             pioctl_dpram = argp;
847             if (list_empty(&info->app_info[i].app_sqlist) == 0) {
848                 //DEBUG("FT1000:ft1000_ChIoctl:Message detected in slow queue\n");
849                 spin_lock_irqsave(&free_buff_lock, flags);
850                 pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
851                 list_del(&pdpram_blk->list);
852                 info->app_info[i].NumOfMsg--;
853                 //DEBUG("FT1000:ft1000_ChIoctl:NumOfMsg for app %d = %d\n", i, info->app_info[i].NumOfMsg);
854                 spin_unlock_irqrestore(&free_buff_lock, flags);
855                 msglen = ntohs(*(u16 *)pdpram_blk->pbuffer) + PSEUDOSZ;
856                 result = get_user(msglen, &pioctl_dpram->total_len);
857                 if (result)
858                         break;
859                 msglen = htons(msglen);
860                 //DEBUG("FT1000:ft1000_ChIoctl:msg length = %x\n", msglen);
861                 if(copy_to_user (&pioctl_dpram->pseudohdr, pdpram_blk->pbuffer, msglen))
862                                 {
863                                         DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
864                         result = -EFAULT;
865                         break;
866                                 }
867
868                 ft1000_free_buffer(pdpram_blk, &freercvpool);
869                 result = msglen;
870             }
871             //DEBUG("FT1000:ft1000_ChIoctl: IOCTL_FT1000_GET_DPRAM no message\n");
872         }
873         break;
874
875     default:
876         DEBUG("FT1000:ft1000_ChIoctl:unknown command: 0x%x\n", Command);
877         result = -ENOTTY;
878         break;
879     }
880     info->fAppMsgPend = 0;
881     return result;
882 }
883
884 //---------------------------------------------------------------------------
885 // Function:    ft1000_ChRelease
886 //
887 // Parameters:
888 //
889 // Description:
890 //
891 // Notes:
892 //
893 //---------------------------------------------------------------------------
894 static int ft1000_ChRelease (struct inode *Inode, struct file *File)
895 {
896         struct ft1000_info *info;
897     struct net_device *dev;
898     int i;
899         struct dpram_blk *pdpram_blk;
900
901     DEBUG("ft1000_ChRelease called\n");
902
903     dev = File->private_data;
904         info = (struct ft1000_info *) netdev_priv(dev);
905
906     if (ft1000_flarion_cnt == 0) {
907         info->appcnt--;
908         return (-EBADF);
909     }
910
911     // Search for matching file object
912     for (i=0; i<MAX_NUM_APP; i++) {
913         if ( info->app_info[i].fileobject == &File->f_owner) {
914             //DEBUG("FT1000:ft1000_ChIoctl: Message is for AppId = %d\n", info->app_info[i].app_id);
915             break;
916         }
917     }
918
919     if (i==MAX_NUM_APP)
920             return 0;
921
922     while (list_empty(&info->app_info[i].app_sqlist) == 0) {
923         DEBUG("Remove and free memory queue up on slow queue\n");
924         pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
925         list_del(&pdpram_blk->list);
926         ft1000_free_buffer(pdpram_blk, &freercvpool);
927     }
928
929     // initialize application information
930     info->appcnt--;
931     DEBUG("ft1000_chdev:%s:appcnt = %d\n", __FUNCTION__, info->appcnt);
932     info->app_info[i].fileobject = NULL;
933
934     return 0;
935 }
936