staging/ft1000-usb: fix problems found by sparse
[pandora-kernel.git] / drivers / staging / ft1000 / ft1000-usb / ft1000_hw.c
1 //=====================================================
2 // CopyRight (C) 2007 Qualcomm Inc. All Rights Reserved.
3 //
4 //
5 // This file is part of Express Card USB Driver
6 //
7 // $Id:
8 //====================================================
9 // 20090926; aelias; removed compiler warnings & errors; ubuntu 9.04; 2.6.28-15-generic
10
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/usb.h>
17 #include "ft1000_usb.h"
18 #include <linux/types.h>
19 //#include <asm/semaphore.h>            //aelias [-] reason : file moved
20 //#include <linux/semaphore.h>          //aelias [+] reason : file moved
21 //#include <asm/io.h>
22 //#include <linux/kthread.h>
23
24 #define HARLEY_READ_REGISTER     0x0
25 #define HARLEY_WRITE_REGISTER    0x01
26 #define HARLEY_READ_DPRAM_32     0x02
27 #define HARLEY_READ_DPRAM_LOW    0x03
28 #define HARLEY_READ_DPRAM_HIGH   0x04
29 #define HARLEY_WRITE_DPRAM_32    0x05
30 #define HARLEY_WRITE_DPRAM_LOW   0x06
31 #define HARLEY_WRITE_DPRAM_HIGH  0x07
32
33 #define HARLEY_READ_OPERATION    0xc1
34 #define HARLEY_WRITE_OPERATION   0x41
35
36 //#define JDEBUG
37
38 static int ft1000_reset(struct net_device *ft1000dev);
39 static int ft1000_submit_rx_urb(PFT1000_INFO info);
40 static void ft1000_hbchk(u_long data);
41 static int ft1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
42 static int ft1000_open (struct net_device *dev);
43 static struct net_device_stats *ft1000_netdev_stats(struct net_device *dev);
44 static struct timer_list poll_timer[MAX_NUM_CARDS];
45 static int ft1000_chkcard (struct ft1000_device *dev);
46 /*
47 static const struct net_device_ops ft1000net_ops = {
48     .ndo_start_xmit = ft1000_start_xmit,
49     .ndo_get_stats = ft1000_netdev_stats,
50     .ndo_open = ft1000_open,
51     .ndo_stop = ft1000_close,
52 };
53 */
54
55 //Jim
56
57 static u8 tempbuffer[1600];
58 static int gCardIndex;
59
60 #define MAX_RCV_LOOP   100
61
62
63 static int atoi(const char *s)
64 {
65         int k = 0;
66     int cnt;
67
68         k = 0;
69     cnt = 0;
70         while (*s != '\0' && *s >= '0' && *s <= '9') {
71                 k = 10 * k + (*s - '0');
72                 s++;
73         // Let's put a limit on this while loop to avoid deadlock scenario
74         if (cnt > 100)
75            break;
76         cnt++;
77         }
78         return k;
79 }
80 /****************************************************************
81  *     ft1000_control_complete
82  ****************************************************************/
83 static void ft1000_control_complete(struct urb *urb)
84 {
85     struct ft1000_device *ft1000dev = (struct ft1000_device *)urb->context;
86
87     //DEBUG("FT1000_CONTROL_COMPLETE ENTERED\n");
88     if (ft1000dev == NULL )
89     {
90         DEBUG("NULL ft1000dev, failure\n");
91         return ;
92     }
93     else if ( ft1000dev->dev == NULL )
94     {
95         DEBUG("NULL ft1000dev->dev, failure\n");
96         return ;
97     }
98     //spin_lock(&ft1000dev->device_lock);
99
100     if(waitqueue_active(&ft1000dev->control_wait))
101     {
102         wake_up(&ft1000dev->control_wait);
103     }
104
105     //DEBUG("FT1000_CONTROL_COMPLETE RETURNED\n");
106     //spin_unlock(&ft1000dev->device_lock);
107 }
108
109 //---------------------------------------------------------------------------
110 // Function:    ft1000_control
111 //
112 // Parameters:  ft1000_device  - device structure
113 //              pipe - usb control message pipe
114 //              request - control request
115 //              requesttype - control message request type
116 //              value - value to be written or 0
117 //              index - register index
118 //              data - data buffer to hold the read/write values
119 //              size - data size
120 //              timeout - control message time out value
121 //
122 // Returns:     STATUS_SUCCESS - success
123 //              STATUS_FAILURE - failure
124 //
125 // Description: This function sends a control message via USB interface synchronously
126 //
127 // Notes:
128 //
129 //---------------------------------------------------------------------------
130 static int ft1000_control(struct ft1000_device *ft1000dev,unsigned int pipe,
131                           u8 request,
132                           u8 requesttype,
133                           u16 value,
134                           u16 index,
135                           void *data,
136                           u16 size,
137                           int timeout)
138 {
139         u16 ret;
140
141     if (ft1000dev == NULL )
142     {
143         DEBUG("NULL ft1000dev, failure\n");
144         return STATUS_FAILURE;
145     }
146     else if ( ft1000dev->dev == NULL )
147     {
148         DEBUG("NULL ft1000dev->dev, failure\n");
149         return STATUS_FAILURE;
150     }
151
152     ret = usb_control_msg(ft1000dev->dev,
153                           pipe,
154                           request,
155                           requesttype,
156                           value,
157                           index,
158                           data,
159                           size,
160                           LARGE_TIMEOUT);
161
162     if (ret>0)
163         ret = STATUS_SUCCESS;
164     else
165         ret = STATUS_FAILURE;
166
167
168     return ret;
169
170
171 }
172 //---------------------------------------------------------------------------
173 // Function:    ft1000_read_register
174 //
175 // Parameters:  ft1000_device  - device structure
176 //              Data - data buffer to hold the value read
177 //              nRegIndex - register index
178 //
179 // Returns:     STATUS_SUCCESS - success
180 //              STATUS_FAILURE - failure
181 //
182 // Description: This function returns the value in a register
183 //
184 // Notes:
185 //
186 //---------------------------------------------------------------------------
187
188 u16 ft1000_read_register(struct ft1000_device *ft1000dev, u16* Data, u16 nRegIndx)
189 {
190     u16 ret = STATUS_SUCCESS;
191
192     //DEBUG("ft1000_read_register: reg index is %d\n", nRegIndx);
193     //DEBUG("ft1000_read_register: spin_lock locked\n");
194     ret = ft1000_control(ft1000dev,
195                          usb_rcvctrlpipe(ft1000dev->dev,0),
196                          HARLEY_READ_REGISTER,   //request --READ_REGISTER
197                          HARLEY_READ_OPERATION,  //requestType
198                          0,                      //value
199                          nRegIndx,               //index
200                          Data,                   //data
201                          2,                      //data size
202                          LARGE_TIMEOUT );        //timeout
203
204    //DEBUG("ft1000_read_register: ret is  %d \n", ret);
205
206    //DEBUG("ft1000_read_register: data is  %x \n", *Data);
207    if ( ret != STATUS_SUCCESS )
208        return STATUS_FAILURE;
209
210    return ret;
211
212 }
213
214 //---------------------------------------------------------------------------
215 // Function:    ft1000_write_register
216 //
217 // Parameters:  ft1000_device  - device structure
218 //              value - value to write into a register
219 //              nRegIndex - register index
220 //
221 // Returns:     STATUS_SUCCESS - success
222 //              STATUS_FAILURE - failure
223 //
224 // Description: This function writes the value in a register
225 //
226 // Notes:
227 //
228 //---------------------------------------------------------------------------
229 u16 ft1000_write_register(struct ft1000_device *ft1000dev, USHORT value, u16 nRegIndx)
230 {
231      u16 ret = STATUS_SUCCESS;
232
233      //DEBUG("ft1000_write_register: value is: %d, reg index is: %d\n", value, nRegIndx);
234
235      ret = ft1000_control(ft1000dev,
236                            usb_sndctrlpipe(ft1000dev->dev, 0),
237                            HARLEY_WRITE_REGISTER,       //request -- WRITE_REGISTER
238                            HARLEY_WRITE_OPERATION,      //requestType
239                            value,
240                            nRegIndx,
241                            NULL,
242                            0,
243                            LARGE_TIMEOUT );
244
245    if ( ret != STATUS_SUCCESS )
246        return STATUS_FAILURE;
247
248     return ret;
249 }
250
251 //---------------------------------------------------------------------------
252 // Function:    ft1000_read_dpram32
253 //
254 // Parameters:  ft1000_device  - device structure
255 //              indx - starting address to read
256 //              buffer - data buffer to hold the data read
257 //              cnt - number of byte read from DPRAM
258 //
259 // Returns:     STATUS_SUCCESS - success
260 //              STATUS_FAILURE - failure
261 //
262 // Description: This function read a number of bytes from DPRAM
263 //
264 // Notes:
265 //
266 //---------------------------------------------------------------------------
267
268 u16 ft1000_read_dpram32(struct ft1000_device *ft1000dev, USHORT indx, PUCHAR buffer, USHORT cnt)
269 {
270     u16 ret = STATUS_SUCCESS;
271
272     //DEBUG("ft1000_read_dpram32: indx: %d  cnt: %d\n", indx, cnt);
273     ret =ft1000_control(ft1000dev,
274                          usb_rcvctrlpipe(ft1000dev->dev,0),
275                          HARLEY_READ_DPRAM_32,                //request --READ_DPRAM_32
276                          HARLEY_READ_OPERATION,               //requestType
277                          0,                                   //value
278                          indx,                                //index
279                          buffer,                              //data
280                          cnt,                                 //data size
281                          LARGE_TIMEOUT );                     //timeout
282
283    //DEBUG("ft1000_read_dpram32: ret is  %d \n", ret);
284
285    //DEBUG("ft1000_read_dpram32: ret=%d \n", ret);
286    if ( ret != STATUS_SUCCESS )
287        return STATUS_FAILURE;
288
289    return ret;
290
291 }
292
293 //---------------------------------------------------------------------------
294 // Function:    ft1000_write_dpram32
295 //
296 // Parameters:  ft1000_device  - device structure
297 //              indx - starting address to write the data
298 //              buffer - data buffer to write into DPRAM
299 //              cnt - number of bytes to write
300 //
301 // Returns:     STATUS_SUCCESS - success
302 //              STATUS_FAILURE - failure
303 //
304 // Description: This function writes into DPRAM a number of bytes
305 //
306 // Notes:
307 //
308 //---------------------------------------------------------------------------
309 u16 ft1000_write_dpram32(struct ft1000_device *ft1000dev, USHORT indx, PUCHAR buffer, USHORT cnt)
310 {
311      u16 ret = STATUS_SUCCESS;
312
313      //DEBUG("ft1000_write_dpram32: indx: %d   buffer: %x cnt: %d\n", indx, buffer, cnt);
314      if ( cnt % 4)
315          cnt += cnt - (cnt % 4);
316
317      ret = ft1000_control(ft1000dev,
318                            usb_sndctrlpipe(ft1000dev->dev, 0),
319                            HARLEY_WRITE_DPRAM_32,              //request -- WRITE_DPRAM_32
320                            HARLEY_WRITE_OPERATION,             //requestType
321                            0,                                  //value
322                            indx,                               //index
323                            buffer,                             //buffer
324                            cnt,                                //buffer size
325                            LARGE_TIMEOUT );
326
327
328    if ( ret != STATUS_SUCCESS )
329        return STATUS_FAILURE;
330
331
332     return ret;
333 }
334
335 //---------------------------------------------------------------------------
336 // Function:    ft1000_read_dpram16
337 //
338 // Parameters:  ft1000_device  - device structure
339 //              indx - starting address to read
340 //              buffer - data buffer to hold the data read
341 //              hightlow - high or low 16 bit word
342 //
343 // Returns:     STATUS_SUCCESS - success
344 //              STATUS_FAILURE - failure
345 //
346 // Description: This function read 16 bits from DPRAM
347 //
348 // Notes:
349 //
350 //---------------------------------------------------------------------------
351 u16 ft1000_read_dpram16(struct ft1000_device *ft1000dev, USHORT indx, PUCHAR buffer, u8 highlow)
352 {
353     u16 ret = STATUS_SUCCESS;
354
355     //DEBUG("ft1000_read_dpram16: indx: %d  hightlow: %d\n", indx, highlow);
356
357     u8 request;
358
359     if (highlow == 0 )
360         request = HARLEY_READ_DPRAM_LOW;
361     else
362         request = HARLEY_READ_DPRAM_HIGH;
363
364     ret = ft1000_control(ft1000dev,
365                          usb_rcvctrlpipe(ft1000dev->dev,0),
366                          request,                     //request --READ_DPRAM_H/L
367                          HARLEY_READ_OPERATION,       //requestType
368                          0,                           //value
369                          indx,                        //index
370                          buffer,                      //data
371                          2,                           //data size
372                          LARGE_TIMEOUT );             //timeout
373
374    //DEBUG("ft1000_read_dpram16: ret is  %d \n", ret);
375
376
377    //DEBUG("ft1000_read_dpram16: data is  %x \n", *buffer);
378    if ( ret != STATUS_SUCCESS )
379        return STATUS_FAILURE;
380
381    return ret;
382
383 }
384
385 //---------------------------------------------------------------------------
386 // Function:    ft1000_write_dpram16
387 //
388 // Parameters:  ft1000_device  - device structure
389 //              indx - starting address to write the data
390 //              value - 16bits value to write
391 //              hightlow - high or low 16 bit word
392 //
393 // Returns:     STATUS_SUCCESS - success
394 //              STATUS_FAILURE - failure
395 //
396 // Description: This function writes into DPRAM a number of bytes
397 //
398 // Notes:
399 //
400 //---------------------------------------------------------------------------
401 u16 ft1000_write_dpram16(struct ft1000_device *ft1000dev, USHORT indx, USHORT value, u8 highlow)
402 {
403      u16 ret = STATUS_SUCCESS;
404
405
406
407      //DEBUG("ft1000_write_dpram16: indx: %d  value: %d  highlow: %d\n", indx, value, highlow);
408
409      u8 request;
410
411
412      if ( highlow == 0 )
413          request = HARLEY_WRITE_DPRAM_LOW;
414      else
415          request = HARLEY_WRITE_DPRAM_HIGH;
416
417      ret = ft1000_control(ft1000dev,
418                            usb_sndctrlpipe(ft1000dev->dev, 0),
419                            request,                             //request -- WRITE_DPRAM_H/L
420                            HARLEY_WRITE_OPERATION,              //requestType
421                            value,                                   //value
422                            indx,                                //index
423                            NULL,                               //buffer
424                            0,                                   //buffer size
425                            LARGE_TIMEOUT );
426
427
428    if ( ret != STATUS_SUCCESS )
429        return STATUS_FAILURE;
430
431
432     return ret;
433 }
434
435 //---------------------------------------------------------------------------
436 // Function:    fix_ft1000_read_dpram32
437 //
438 // Parameters:  ft1000_device  - device structure
439 //              indx - starting address to read
440 //              buffer - data buffer to hold the data read
441 //
442 //
443 // Returns:     STATUS_SUCCESS - success
444 //              STATUS_FAILURE - failure
445 //
446 // Description: This function read DPRAM 4 words at a time
447 //
448 // Notes:
449 //
450 //---------------------------------------------------------------------------
451 u16 fix_ft1000_read_dpram32(struct ft1000_device *ft1000dev, USHORT indx, PUCHAR buffer)
452 {
453     UCHAR buf[16];
454     USHORT pos;
455     u16 ret = STATUS_SUCCESS;
456
457     //DEBUG("fix_ft1000_read_dpram32: indx: %d  \n", indx);
458     pos = (indx / 4)*4;
459     ret = ft1000_read_dpram32(ft1000dev, pos, buf, 16);
460     if (ret == STATUS_SUCCESS)
461     {
462         pos = (indx % 4)*4;
463         *buffer++ = buf[pos++];
464         *buffer++ = buf[pos++];
465         *buffer++ = buf[pos++];
466         *buffer++ = buf[pos++];
467     }
468     else
469     {
470         DEBUG("fix_ft1000_read_dpram32: DPRAM32 Read failed\n");
471         *buffer++ = 0;
472         *buffer++ = 0;
473         *buffer++ = 0;
474         *buffer++ = 0;
475
476     }
477
478    //DEBUG("fix_ft1000_read_dpram32: data is  %x \n", *buffer);
479    return ret;
480
481 }
482
483
484 //---------------------------------------------------------------------------
485 // Function:    fix_ft1000_write_dpram32
486 //
487 // Parameters:  ft1000_device  - device structure
488 //              indx - starting address to write
489 //              buffer - data buffer to write
490 //
491 //
492 // Returns:     STATUS_SUCCESS - success
493 //              STATUS_FAILURE - failure
494 //
495 // Description: This function write to DPRAM 4 words at a time
496 //
497 // Notes:
498 //
499 //---------------------------------------------------------------------------
500 u16 fix_ft1000_write_dpram32(struct ft1000_device *ft1000dev, USHORT indx, PUCHAR buffer)
501 {
502     USHORT pos1;
503     USHORT pos2;
504     USHORT i;
505     UCHAR buf[32];
506     UCHAR resultbuffer[32];
507     PUCHAR pdata;
508     u16 ret  = STATUS_SUCCESS;
509
510     //DEBUG("fix_ft1000_write_dpram32: Entered:\n");
511
512     pos1 = (indx / 4)*4;
513     pdata = buffer;
514     ret = ft1000_read_dpram32(ft1000dev, pos1, buf, 16);
515     if (ret == STATUS_SUCCESS)
516     {
517         pos2 = (indx % 4)*4;
518         buf[pos2++] = *buffer++;
519         buf[pos2++] = *buffer++;
520         buf[pos2++] = *buffer++;
521         buf[pos2++] = *buffer++;
522         ret = ft1000_write_dpram32(ft1000dev, pos1, buf, 16);
523     }
524     else
525     {
526         DEBUG("fix_ft1000_write_dpram32: DPRAM32 Read failed\n");
527
528         return ret;
529     }
530
531     ret = ft1000_read_dpram32(ft1000dev, pos1, (PUCHAR)&resultbuffer[0], 16);
532     if (ret == STATUS_SUCCESS)
533     {
534         buffer = pdata;
535         for (i=0; i<16; i++)
536         {
537             if (buf[i] != resultbuffer[i]){
538
539                 ret = STATUS_FAILURE;
540             }
541         }
542     }
543
544     if (ret == STATUS_FAILURE)
545     {
546         ret = ft1000_write_dpram32(ft1000dev, pos1, (PUCHAR)&tempbuffer[0], 16);
547         ret = ft1000_read_dpram32(ft1000dev, pos1, (PUCHAR)&resultbuffer[0], 16);
548         if (ret == STATUS_SUCCESS)
549         {
550             buffer = pdata;
551             for (i=0; i<16; i++)
552             {
553                 if (tempbuffer[i] != resultbuffer[i])
554                 {
555                     ret = STATUS_FAILURE;
556                     DEBUG("fix_ft1000_write_dpram32 Failed to write\n");
557                 }
558             }
559          }
560     }
561
562     return ret;
563
564 }
565
566
567 //------------------------------------------------------------------------
568 //
569 //  Function:   card_reset_dsp
570 //
571 //  Synopsis:   This function is called to reset or activate the DSP
572 //
573 //  Arguments:  value                  - reset or activate
574 //
575 //  Returns:    None
576 //-----------------------------------------------------------------------
577 static void card_reset_dsp (struct ft1000_device *ft1000dev, BOOLEAN value)
578 {
579     u16 status = STATUS_SUCCESS;
580     USHORT tempword;
581
582     status = ft1000_write_register (ft1000dev, HOST_INTF_BE, FT1000_REG_SUP_CTRL);
583     status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_SUP_CTRL);
584     if (value)
585     {
586         DEBUG("Reset DSP\n");
587         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
588         tempword |= DSP_RESET_BIT;
589         status = ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
590     }
591     else
592     {
593         DEBUG("Activate DSP\n");
594         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
595 #if 1
596         tempword |= DSP_ENCRYPTED;
597         tempword &= ~DSP_UNENCRYPTED;
598 #else
599         tempword |= DSP_UNENCRYPTED;
600         tempword &= ~DSP_ENCRYPTED;
601 #endif
602         status = ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
603         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
604         tempword &= ~EFUSE_MEM_DISABLE;
605         tempword &= ~DSP_RESET_BIT;
606         status = ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
607         status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
608     }
609 }
610
611 //---------------------------------------------------------------------------
612 // Function:    CardSendCommand
613 //
614 // Parameters:  ft1000_device  - device structure
615 //              ptempbuffer - command buffer
616 //              size - command buffer size
617 //
618 // Returns:     STATUS_SUCCESS - success
619 //              STATUS_FAILURE - failure
620 //
621 // Description: This function sends a command to ASIC
622 //
623 // Notes:
624 //
625 //---------------------------------------------------------------------------
626 void CardSendCommand(struct ft1000_device *ft1000dev, void *ptempbuffer, int size)
627 {
628     unsigned short temp;
629     unsigned char *commandbuf;
630
631     DEBUG("CardSendCommand: enter CardSendCommand... size=%d\n", size);
632
633     commandbuf =(unsigned char*) kmalloc(size+2, GFP_KERNEL);
634     //memset((void*)commandbuf, 0, size+2);
635     memcpy((void*)commandbuf+2, (void*)ptempbuffer, size);
636
637     //DEBUG("CardSendCommand: Command Send\n");
638     /***
639     for (i=0; i<size+2; i++)
640     {
641         DEBUG("FT1000:ft1000_ChIoctl: data %d = 0x%x\n", i, *ptr++);
642     }
643     ***/
644
645     ft1000_read_register(ft1000dev, &temp, FT1000_REG_DOORBELL);
646
647     if (temp & 0x0100)
648     {
649        msleep(10);
650     }
651
652     // check for odd word
653     size = size + 2;
654     if (size % 4)
655     {
656        // Must force to be 32 bit aligned
657        size += 4 - (size % 4);
658     }
659
660
661     //DEBUG("CardSendCommand: write dpram ... size=%d\n", size);
662     ft1000_write_dpram32(ft1000dev, 0,commandbuf, size);
663     msleep(1);
664     //DEBUG("CardSendCommand: write into doorbell ...\n");
665     ft1000_write_register(ft1000dev,  FT1000_DB_DPRAM_TX ,FT1000_REG_DOORBELL) ;
666     msleep(1);
667
668     ft1000_read_register(ft1000dev, &temp, FT1000_REG_DOORBELL);
669     //DEBUG("CardSendCommand: read doorbell ...temp=%x\n", temp);
670     if ( (temp & 0x0100) == 0)
671     {
672        //DEBUG("CardSendCommand: Message sent\n");
673     }
674
675 }
676
677
678 //--------------------------------------------------------------------------
679 //
680 //  Function:   dsp_reload
681 //
682 //  Synopsis:   This function is called to load or reload the DSP
683 //
684 //  Arguments:  ft1000dev - device structure
685 //
686 //  Returns:    None
687 //-----------------------------------------------------------------------
688 void dsp_reload (struct ft1000_device *ft1000dev)
689 {
690     u16 status;
691     USHORT tempword;
692     ULONG templong;
693
694     PFT1000_INFO pft1000info;
695
696     pft1000info = netdev_priv(ft1000dev->net);
697
698     pft1000info->CardReady = 0;
699     pft1000info->DSP_loading= 1;
700
701     // Program Interrupt Mask register
702     status = ft1000_write_register (ft1000dev, 0xffff, FT1000_REG_SUP_IMASK);
703
704     status = ft1000_read_register (ft1000dev, &tempword, FT1000_REG_RESET);
705     tempword |= ASIC_RESET_BIT;
706     status = ft1000_write_register (ft1000dev, tempword, FT1000_REG_RESET);
707     msleep(1000);
708     status = ft1000_read_register (ft1000dev, &tempword, FT1000_REG_RESET);
709     DEBUG("Reset Register = 0x%x\n", tempword);
710
711     // Toggle DSP reset
712     card_reset_dsp (ft1000dev, 1);
713     msleep(1000);
714     card_reset_dsp (ft1000dev, 0);
715     msleep(1000);
716
717     status = ft1000_write_register (ft1000dev, HOST_INTF_BE, FT1000_REG_SUP_CTRL);
718
719     // Let's check for FEFE
720     status = ft1000_read_dpram32 (ft1000dev, FT1000_MAG_DPRAM_FEFE_INDX, (PUCHAR)&templong, 4);
721     DEBUG("templong (fefe) = 0x%8x\n", templong);
722
723     // call codeloader
724     status = scram_dnldr(ft1000dev, pFileStart, FileLength);
725
726     if ( status != STATUS_SUCCESS)
727        return;
728
729     msleep(1000);
730     pft1000info->DSP_loading= 0;
731
732     DEBUG("dsp_reload returned\n");
733
734
735 }
736
737 //---------------------------------------------------------------------------
738 //
739 // Function:   ft1000_reset_asic
740 // Descripton: This function will call the Card Service function to reset the
741 //             ASIC.
742 // Input:
743 //     dev    - device structure
744 // Output:
745 //     none
746 //
747 //---------------------------------------------------------------------------
748 static void ft1000_reset_asic (struct net_device *dev)
749 {
750     FT1000_INFO *info = netdev_priv(dev);
751     struct ft1000_device *ft1000dev = info->pFt1000Dev;
752     u16 tempword;
753
754     DEBUG("ft1000_hw:ft1000_reset_asic called\n");
755
756     info->ASICResetNum++;
757
758     // Let's use the register provided by the Magnemite ASIC to reset the
759     // ASIC and DSP.
760     ft1000_write_register(ft1000dev,  (DSP_RESET_BIT | ASIC_RESET_BIT), FT1000_REG_RESET );
761
762     mdelay(1);
763
764     // set watermark to -1 in order to not generate an interrrupt
765     ft1000_write_register(ft1000dev, 0xffff, FT1000_REG_MAG_WATERMARK);
766
767     // clear interrupts
768     ft1000_read_register (ft1000dev, &tempword, FT1000_REG_SUP_ISR);
769     DEBUG("ft1000_hw: interrupt status register = 0x%x\n",tempword);
770     ft1000_write_register (ft1000dev,  tempword, FT1000_REG_SUP_ISR);
771     ft1000_read_register (ft1000dev, &tempword, FT1000_REG_SUP_ISR);
772     DEBUG("ft1000_hw: interrupt status register = 0x%x\n",tempword);
773
774 }
775 /*
776 //---------------------------------------------------------------------------
777 //
778 // Function:   ft1000_disable_interrupts
779 // Descripton: This function will disable all interrupts.
780 // Input:
781 //     dev    - device structure
782 // Output:
783 //     None.
784 //
785 //---------------------------------------------------------------------------
786 static void ft1000_disable_interrupts(struct net_device *dev) {
787     FT1000_INFO *info = netdev_priv(dev);
788     struct ft1000_device *ft1000dev = info->pFt1000Dev;
789     u16 tempword;
790
791     DEBUG("ft1000_hw: ft1000_disable_interrupts()\n");
792     ft1000_write_register (ft1000dev, ISR_MASK_ALL, FT1000_REG_SUP_IMASK);
793     ft1000_read_register (ft1000dev, &tempword, FT1000_REG_SUP_IMASK);
794     DEBUG("ft1000_hw:ft1000_disable_interrupts:current interrupt enable mask = 0x%x\n", tempword);
795     info->InterruptsEnabled = FALSE;
796 }
797
798 //---------------------------------------------------------------------------
799 //
800 // Function:   ft1000_enable_interrupts
801 // Descripton: This function will enable interrupts base on the current interrupt mask.
802 // Input:
803 //     dev    - device structure
804 // Output:
805 //     None.
806 //
807 //---------------------------------------------------------------------------
808 static void ft1000_enable_interrupts(struct net_device *dev) {
809     FT1000_INFO *info = netdev_priv(dev);
810     struct ft1000_device *ft1000dev = info->pFt1000Dev;
811     u16 tempword;
812
813     DEBUG("ft1000_hw:ft1000_enable_interrupts()\n");
814     ft1000_write_register (ft1000dev, info->CurrentInterruptEnableMask, FT1000_REG_SUP_IMASK);
815     ft1000_read_register (ft1000dev, &tempword, FT1000_REG_SUP_IMASK);
816     DEBUG("ft1000_hw:ft1000_enable_interrupts:current interrupt enable mask = 0x%x\n", tempword);
817     info->InterruptsEnabled = TRUE;
818 }
819 */
820
821 //---------------------------------------------------------------------------
822 //
823 // Function:   ft1000_reset_card
824 // Descripton: This function will reset the card
825 // Input:
826 //     dev    - device structure
827 // Output:
828 //     status - FALSE (card reset fail)
829 //              TRUE  (card reset successful)
830 //
831 //---------------------------------------------------------------------------
832 static int ft1000_reset_card (struct net_device *dev)
833 {
834     FT1000_INFO *info = netdev_priv(dev);
835     struct ft1000_device *ft1000dev = info->pFt1000Dev;
836     u16 tempword;
837     PPROV_RECORD ptr;
838
839     DEBUG("ft1000_hw:ft1000_reset_card called.....\n");
840
841     info->fCondResetPend = 1;
842     info->CardReady = 0;
843     info->fProvComplete = 0;
844     //ft1000_disable_interrupts(dev);
845
846     // Cancel heartbeat task since we are reloading the dsp
847     //del_timer(&poll_timer[info->CardNumber]);
848
849     // Make sure we free any memory reserve for provisioning
850     while (list_empty(&info->prov_list) == 0) {
851         DEBUG("ft1000_hw:ft1000_reset_card:deleting provisioning record\n");
852         ptr = list_entry(info->prov_list.next, PROV_RECORD, list);
853         list_del(&ptr->list);
854         kfree(ptr->pprov_data);
855         kfree(ptr);
856     }
857
858     DEBUG("ft1000_hw:ft1000_reset_card: reset asic\n");
859     //reset ASIC
860     ft1000_reset_asic(dev);
861
862     info->DSPResetNum++;
863
864 #if 0
865     DEBUG("ft1000_hw:ft1000_reset_card:resetting ASIC and DSP\n");
866     ft1000_write_register (ft1000dev, (DSP_RESET_BIT | ASIC_RESET_BIT), FT1000_REG_RESET );
867
868
869     // Copy DSP session record into info block if this is not a coldstart
870     //if (ft1000_card_present == 1) {
871         spin_lock_irqsave(&info->dpram_lock, flags);
872
873             ft1000_write_register(ft1000dev,  FT1000_DPRAM_MAG_RX_BASE, FT1000_REG_DPRAM_ADDR);
874             for (i=0;i<MAX_DSP_SESS_REC/2; i++) {
875                 //info->DSPSess.MagRec[i] = inl(dev->base_addr+FT1000_REG_MAG_DPDATA);
876                 ft1000_read_dpram32(ft1000dev, FT1000_REG_MAG_DPDATA, (PCHAR)&(info->DSPSess.MagRec[i]), 4);
877             }
878
879         spin_unlock_irqrestore(&info->dpram_lock, flags);
880     //}
881     info->squeseqnum = 0;
882
883     DEBUG("ft1000_hw:ft1000_reset_card:resetting ASIC\n");
884     mdelay(10);
885     //reset ASIC
886     ft1000_reset_asic(dev);
887
888     info->DSPResetNum++;
889
890     DEBUG("ft1000_hw:ft1000_reset_card:downloading dsp image\n");
891
892
893         // Put dsp in reset and take ASIC out of reset
894         DEBUG("ft1000_hw:ft1000_reset_card:Put DSP in reset and take ASIC out of reset\n");
895         ft1000_write_register (ft1000dev, DSP_RESET_BIT, FT1000_REG_RESET);
896
897         // Setting MAGNEMITE ASIC to big endian mode
898         ft1000_write_register (ft1000dev, HOST_INTF_BE, FT1000_REG_SUP_CTRL);
899
900         // Take DSP out of reset
901
902            ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
903            tempword |= DSP_ENCRYPTED;
904            tempword &= ~DSP_UNENCRYPTED;
905            ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
906            tempword &= ~EFUSE_MEM_DISABLE;
907            ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
908            tempword &= ~DSP_RESET_BIT;
909            ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
910
911
912         // FLARION_DSP_ACTIVE;
913         mdelay(10);
914         DEBUG("ft1000_hw:ft1000_reset_card:Take DSP out of reset\n");
915
916         // Wait for 0xfefe indicating dsp ready before starting download
917         for (i=0; i<50; i++) {
918             //tempword = ft1000_read_dpram_mag_16(dev, FT1000_MAG_DPRAM_FEFE, FT1000_MAG_DPRAM_FEFE_INDX);
919             ft1000_read_dpram32 (ft1000dev, FT1000_MAG_DPRAM_FEFE_INDX, (PUCHAR)&templong, 4);
920             if (tempword == 0xfefe) {
921                 break;
922             }
923             mdelay(20);
924         }
925
926         if (i==50) {
927             DEBUG("ft1000_hw:ft1000_reset_card:No FEFE detected from DSP\n");
928             return FALSE;
929         }
930
931
932 #endif
933
934     DEBUG("ft1000_hw:ft1000_reset_card: call dsp_reload\n");
935     dsp_reload(ft1000dev);
936
937     DEBUG("dsp reload successful\n");
938
939
940     mdelay(10);
941
942     // Initialize DSP heartbeat area to ho
943     ft1000_write_dpram16(ft1000dev, FT1000_MAG_HI_HO, ho_mag, FT1000_MAG_HI_HO_INDX);
944     ft1000_read_dpram16(ft1000dev, FT1000_MAG_HI_HO, (PCHAR)&tempword, FT1000_MAG_HI_HO_INDX);
945     DEBUG("ft1000_hw:ft1000_reset_card:hi_ho value = 0x%x\n", tempword);
946
947
948
949     info->CardReady = 1;
950     //ft1000_enable_interrupts(dev);
951     /* Schedule heartbeat process to run every 2 seconds */
952     //poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
953     //poll_timer[info->CardNumber].data = (u_long)dev;
954     //add_timer(&poll_timer[info->CardNumber]);
955
956     info->fCondResetPend = 0;
957     return TRUE;
958
959 }
960
961
962 //mbelian
963 #ifdef HAVE_NET_DEVICE_OPS
964 static const struct net_device_ops ftnet_ops =
965 {
966 .ndo_open = &ft1000_open,
967 .ndo_stop = &ft1000_close,
968 .ndo_start_xmit = &ft1000_start_xmit,
969 .ndo_get_stats = &ft1000_netdev_stats,
970 };
971 #endif
972
973
974 //---------------------------------------------------------------------------
975 // Function:    init_ft1000_netdev
976 //
977 // Parameters:  ft1000dev  - device structure
978 //
979 //
980 // Returns:     STATUS_SUCCESS - success
981 //              STATUS_FAILURE - failure
982 //
983 // Description: This function initialize the network device
984 //
985 // Notes:
986 //
987 //---------------------------------------------------------------------------
988 u16 init_ft1000_netdev(struct ft1000_device *ft1000dev)
989 {
990     struct net_device *netdev;
991     FT1000_INFO *pInfo = NULL;
992     PDPRAM_BLK pdpram_blk;
993     int i;
994
995         gCardIndex=0; //mbelian
996
997     DEBUG("Enter init_ft1000_netdev...\n");
998
999
1000     netdev = alloc_etherdev( sizeof(FT1000_INFO));
1001     if (!netdev )
1002     {
1003         DEBUG("init_ft1000_netdev: can not allocate network device\n");
1004         return STATUS_FAILURE;
1005     }
1006
1007     //pInfo = (PFT1000_INFO)netdev->priv;
1008         pInfo = (FT1000_INFO *) netdev_priv (netdev);
1009
1010     //DEBUG("init_ft1000_netdev: gFt1000Info=%x, netdev=%x, ft1000dev=%x\n", gFt1000Info, netdev, ft1000dev);
1011
1012     memset (pInfo, 0, sizeof(FT1000_INFO));
1013
1014     dev_alloc_name(netdev, netdev->name);
1015
1016     //for the first inserted card, decide the card index beginning number, in case there are existing network interfaces
1017     if ( gCardIndex == 0 )
1018     {
1019         DEBUG("init_ft1000_netdev: network device name is %s\n", netdev->name);
1020
1021         if ( strncmp(netdev->name,"eth", 3) == 0) {
1022             //pInfo->CardNumber = atoi(&netdev->name[3]);
1023             gCardIndex = atoi(&netdev->name[3]);
1024             pInfo->CardNumber = gCardIndex;
1025             DEBUG("card number = %d\n", pInfo->CardNumber);
1026         }
1027         else {
1028             printk(KERN_ERR "ft1000: Invalid device name\n");
1029             kfree(netdev);
1030             return STATUS_FAILURE;
1031         }
1032     }
1033     else
1034     {
1035         //not the first inserted card, increase card number by 1
1036         /*gCardIndex ++;*/
1037         pInfo->CardNumber = gCardIndex;
1038         /*DEBUG("card number = %d\n", pInfo->CardNumber);*/ //mbelian
1039     }
1040
1041     memset(&pInfo->stats, 0, sizeof(struct net_device_stats) );
1042
1043    spin_lock_init(&pInfo->dpram_lock);
1044     pInfo->pFt1000Dev = ft1000dev;
1045     pInfo->DrvErrNum = 0;
1046     pInfo->ASICResetNum = 0;
1047     pInfo->registered = 1;
1048     pInfo->ft1000_reset = ft1000_reset;
1049     pInfo->mediastate = 0;
1050     pInfo->fifo_cnt = 0;
1051     pInfo->DeviceCreated = FALSE;
1052     pInfo->DeviceMajor = 0;
1053     pInfo->CurrentInterruptEnableMask = ISR_DEFAULT_MASK;
1054     pInfo->InterruptsEnabled = FALSE;
1055     pInfo->CardReady = 0;
1056     pInfo->DSP_loading = 0;
1057     pInfo->DSP_TIME[0] = 0;
1058     pInfo->DSP_TIME[1] = 0;
1059     pInfo->DSP_TIME[2] = 0;
1060     pInfo->DSP_TIME[3] = 0;
1061     pInfo->fAppMsgPend = 0;
1062     pInfo->fCondResetPend = 0;
1063         pInfo->usbboot = 0;
1064         pInfo->dspalive = 0;
1065         for (i=0;i<32 ;i++ )
1066         {
1067                 pInfo->tempbuf[i] = 0;
1068         }
1069
1070     INIT_LIST_HEAD(&pInfo->prov_list);
1071
1072 //mbelian
1073 #ifdef HAVE_NET_DEVICE_OPS
1074         netdev->netdev_ops = &ftnet_ops;
1075 #else
1076     netdev->hard_start_xmit = &ft1000_start_xmit;
1077     netdev->get_stats = &ft1000_netdev_stats;
1078     netdev->open = &ft1000_open;
1079     netdev->stop = &ft1000_close;
1080 #endif
1081
1082     //netif_stop_queue(netdev); //mbelian
1083
1084
1085     ft1000dev->net = netdev;
1086
1087
1088
1089 //init free_buff_lock, freercvpool, numofmsgbuf, pdpram_blk
1090 //only init once per card
1091 //Jim
1092           DEBUG("Initialize free_buff_lock and freercvpool\n");
1093         spin_lock_init(&free_buff_lock);
1094
1095         // initialize a list of buffers to be use for queuing up receive command data
1096         INIT_LIST_HEAD (&freercvpool);
1097
1098         // create list of free buffers
1099         for (i=0; i<NUM_OF_FREE_BUFFERS; i++) {
1100             // Get memory for DPRAM_DATA link list
1101             pdpram_blk = kmalloc ( sizeof(DPRAM_BLK), GFP_KERNEL );
1102             // Get a block of memory to store command data
1103             pdpram_blk->pbuffer = kmalloc ( MAX_CMD_SQSIZE, GFP_KERNEL );
1104             // link provisioning data
1105             list_add_tail (&pdpram_blk->list, &freercvpool);
1106         }
1107         numofmsgbuf = NUM_OF_FREE_BUFFERS;
1108
1109
1110     return STATUS_SUCCESS;
1111
1112 }
1113
1114
1115
1116 //---------------------------------------------------------------------------
1117 // Function:    reg_ft1000_netdev
1118 //
1119 // Parameters:  ft1000dev  - device structure
1120 //
1121 //
1122 // Returns:     STATUS_SUCCESS - success
1123 //              STATUS_FAILURE - failure
1124 //
1125 // Description: This function register the network driver
1126 //
1127 // Notes:
1128 //
1129 //---------------------------------------------------------------------------
1130 u16 reg_ft1000_netdev(struct ft1000_device *ft1000dev, struct usb_interface *intf)
1131 {
1132     struct net_device *netdev;
1133     FT1000_INFO *pInfo;
1134     int i, rc;
1135
1136     netdev = ft1000dev->net;
1137     pInfo = netdev_priv(ft1000dev->net);
1138     DEBUG("Enter reg_ft1000_netdev...\n");
1139
1140
1141     ft1000_read_register(ft1000dev, &pInfo->AsicID, FT1000_REG_ASIC_ID);
1142
1143     usb_set_intfdata(intf, pInfo);
1144     SET_NETDEV_DEV(netdev, &intf->dev);
1145
1146     rc = register_netdev(netdev);
1147     if (rc)
1148     {
1149         DEBUG("reg_ft1000_netdev: could not register network device\n");
1150         free_netdev(netdev);
1151         return STATUS_FAILURE;
1152     }
1153
1154
1155     //Create character device, implemented by Jim
1156     ft1000_CreateDevice(ft1000dev);
1157
1158     //INIT_LIST_HEAD(&pInfo->prov_list);
1159
1160     for (i=0; i<MAX_NUM_CARDS; i++) {
1161         poll_timer[i].function = ft1000_hbchk;
1162     }
1163
1164
1165     //hard code MAC address for now
1166 /**
1167     netdev->dev_addr[0] = 0;
1168     netdev->dev_addr[1] = 7;
1169     netdev->dev_addr[2] = 0x35;
1170     netdev->dev_addr[3] = 0x84;
1171     netdev->dev_addr[4] = 0;
1172     netdev->dev_addr[5] = 0x20 + pInfo->CardNumber;
1173 **/
1174
1175     DEBUG ("reg_ft1000_netdev returned\n");
1176
1177     pInfo->CardReady = 1;
1178
1179
1180    return STATUS_SUCCESS;
1181 }
1182
1183 static int ft1000_reset(struct net_device *dev)
1184 {
1185     ft1000_reset_card(dev);
1186     return 0;
1187 }
1188
1189 //---------------------------------------------------------------------------
1190 // Function:    ft1000_usb_transmit_complete
1191 //
1192 // Parameters:  urb  - transmitted usb urb
1193 //
1194 //
1195 // Returns:     none
1196 //
1197 // Description: This is the callback function when a urb is transmitted
1198 //
1199 // Notes:
1200 //
1201 //---------------------------------------------------------------------------
1202 static void ft1000_usb_transmit_complete(struct urb *urb)
1203 {
1204
1205     struct ft1000_device *ft1000dev = urb->context;
1206
1207     //DEBUG("ft1000_usb_transmit_complete entered\n");
1208 // Jim   spin_lock(&ft1000dev->device_lock);
1209
1210     if (urb->status)
1211         printk("%s: TX status %d\n", ft1000dev->net->name, urb->status);
1212
1213     netif_wake_queue(ft1000dev->net);
1214
1215 //Jim    spin_unlock(&ft1000dev->device_lock);
1216     //DEBUG("Return from ft1000_usb_transmit_complete\n");
1217 }
1218
1219
1220 /****************************************************************
1221  *     ft1000_control
1222  ****************************************************************/
1223 static int ft1000_read_fifo_reg(struct ft1000_device *ft1000dev,unsigned int pipe,
1224                           u8 request,
1225                           u8 requesttype,
1226                           u16 value,
1227                           u16 index,
1228                           void *data,
1229                           u16 size,
1230                           int timeout)
1231 {
1232     u16 ret;
1233
1234     DECLARE_WAITQUEUE(wait, current);
1235     struct urb *urb;
1236     struct usb_ctrlrequest *dr;
1237     int status;
1238
1239     if (ft1000dev == NULL )
1240     {
1241         DEBUG("NULL ft1000dev, failure\n");
1242         return STATUS_FAILURE;
1243     }
1244     else if ( ft1000dev->dev == NULL )
1245     {
1246         DEBUG("NULL ft1000dev->dev, failure\n");
1247         return STATUS_FAILURE;
1248     }
1249
1250     spin_lock(&ft1000dev->device_lock);
1251
1252     /*DECLARE_WAITQUEUE(wait, current);
1253     struct urb *urb;
1254     struct usb_ctrlrequest *dr;
1255     int status;*/
1256
1257     if(in_interrupt())
1258     {
1259         spin_unlock(&ft1000dev->device_lock);
1260         return -EBUSY;
1261     }
1262
1263     urb = usb_alloc_urb(0, GFP_KERNEL);
1264     dr = kmalloc(sizeof(struct usb_ctrlrequest), in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1265
1266     if(!urb || !dr)
1267     {
1268         if(urb) kfree(urb);
1269         spin_unlock(&ft1000dev->device_lock);
1270         return -ENOMEM;
1271     }
1272
1273
1274
1275     dr->bRequestType = requesttype;
1276     dr->bRequest = request;
1277     dr->wValue = value;
1278     dr->wIndex = index;
1279     dr->wLength = size;
1280
1281     usb_fill_control_urb(urb, ft1000dev->dev, pipe, (char*)dr, (void*)data, size, (void *)ft1000_control_complete, (void*)ft1000dev);
1282
1283
1284     init_waitqueue_head(&ft1000dev->control_wait);
1285
1286     //current->state = TASK_INTERRUPTIBLE; //mbelian
1287         set_current_state(TASK_INTERRUPTIBLE);
1288
1289     add_wait_queue(&ft1000dev->control_wait, &wait);
1290
1291
1292
1293
1294     status = usb_submit_urb(urb, GFP_KERNEL);
1295
1296     if(status)
1297     {
1298         usb_free_urb(urb);
1299         kfree(dr);
1300         remove_wait_queue(&ft1000dev->control_wait, &wait);
1301         spin_unlock(&ft1000dev->device_lock);
1302         return status;
1303     }
1304
1305     if(urb->status == -EINPROGRESS)
1306     {
1307         while(timeout && urb->status == -EINPROGRESS)
1308         {
1309             status = timeout = schedule_timeout(timeout);
1310         }
1311     }
1312     else
1313     {
1314         status = 1;
1315     }
1316
1317     remove_wait_queue(&ft1000dev->control_wait, &wait);
1318
1319     if(!status)
1320     {
1321         usb_unlink_urb(urb);
1322         printk("ft1000 timeout\n");
1323         status = -ETIMEDOUT;
1324     }
1325     else
1326     {
1327         status = urb->status;
1328
1329         if(urb->status)
1330         {
1331             printk("ft1000 control message failed (urb addr: %p) with error number: %i\n", urb, (int)status);
1332
1333             usb_clear_halt(ft1000dev->dev, usb_rcvctrlpipe(ft1000dev->dev, 0));
1334             usb_clear_halt(ft1000dev->dev, usb_sndctrlpipe(ft1000dev->dev, 0));
1335             usb_unlink_urb(urb);
1336         }
1337     }
1338
1339
1340
1341     usb_free_urb(urb);
1342     kfree(dr);
1343     spin_unlock(&ft1000dev->device_lock);
1344     return ret;
1345
1346
1347 }
1348
1349 //---------------------------------------------------------------------------
1350 // Function:    ft1000_read_fifo_len
1351 //
1352 // Parameters:  ft1000dev - device structure
1353 //
1354 //
1355 // Returns:     none
1356 //
1357 // Description: read the fifo length register content
1358 //
1359 // Notes:
1360 //
1361 //---------------------------------------------------------------------------
1362 static inline u16 ft1000_read_fifo_len (struct net_device *dev)
1363 {
1364     u16 temp;
1365     u16 ret;
1366
1367     //FT1000_INFO *info = (PFT1000_INFO)dev->priv;
1368         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev);
1369     struct ft1000_device *ft1000dev = info->pFt1000Dev;
1370 //    DEBUG("ft1000_read_fifo_len: enter ft1000dev %x\n", ft1000dev);                   //aelias [-] reason: warning: format ???%x??? expects type ???unsigned int???, but argument 2 has type ???struct ft1000_device *???
1371     DEBUG("ft1000_read_fifo_len: enter ft1000dev %p\n", ft1000dev);     //aelias [+] reason: up
1372     //ft1000_read_register(ft1000dev, &temp, FT1000_REG_MAG_UFSR);
1373
1374     ret = STATUS_SUCCESS;
1375
1376     ret = ft1000_read_fifo_reg(ft1000dev,
1377                           usb_rcvctrlpipe(ft1000dev->dev,0),
1378                           HARLEY_READ_REGISTER,
1379                           HARLEY_READ_OPERATION,
1380                           0,
1381                           FT1000_REG_MAG_UFSR,
1382                           &temp,
1383                           2,
1384                           LARGE_TIMEOUT);
1385
1386     if (ret>0)
1387         ret = STATUS_SUCCESS;
1388     else
1389         ret = STATUS_FAILURE;
1390
1391     DEBUG("ft1000_read_fifo_len: returned %d\n", temp);
1392
1393     return (temp- 16);
1394
1395 }
1396
1397
1398 //---------------------------------------------------------------------------
1399 //
1400 // Function:   ft1000_copy_down_pkt
1401 // Descripton: This function will take an ethernet packet and convert it to
1402 //             a Flarion packet prior to sending it to the ASIC Downlink
1403 //             FIFO.
1404 // Input:
1405 //     dev    - device structure
1406 //     packet - address of ethernet packet
1407 //     len    - length of IP packet
1408 // Output:
1409 //     status - FAILURE
1410 //              SUCCESS
1411 //
1412 //---------------------------------------------------------------------------
1413 static int ft1000_copy_down_pkt (struct net_device *netdev, u8 *packet, u16 len)
1414 {
1415     FT1000_INFO *pInfo = netdev_priv(netdev);
1416     struct ft1000_device *pFt1000Dev = pInfo->pFt1000Dev;
1417
1418
1419     int i, count, ret;
1420     USHORT *pTemp;
1421     USHORT checksum;
1422     u8 *t;
1423
1424     if (!pInfo->CardReady)
1425     {
1426
1427         DEBUG("ft1000_copy_down_pkt::Card Not Ready\n");
1428         return STATUS_FAILURE;
1429
1430     }
1431
1432
1433     //DEBUG("ft1000_copy_down_pkt() entered, len = %d\n", len);
1434
1435 #if 0
1436     // Check if there is room on the FIFO
1437     if ( len > ft1000_read_fifo_len (netdev) )
1438     {
1439          udelay(10);
1440          if ( len > ft1000_read_fifo_len (netdev) )
1441          {
1442              udelay(20);
1443          }
1444
1445          if ( len > ft1000_read_fifo_len (netdev) )
1446          {
1447              udelay(20);
1448          }
1449
1450          if ( len > ft1000_read_fifo_len (netdev) )
1451          {
1452              udelay(20);
1453          }
1454
1455          if ( len > ft1000_read_fifo_len (netdev) )
1456          {
1457              udelay(20);
1458          }
1459
1460          if ( len > ft1000_read_fifo_len (netdev) )
1461          {
1462              udelay(20);
1463          }
1464
1465          if ( len > ft1000_read_fifo_len (netdev) )
1466          {
1467             DEBUG("ft1000_hw:ft1000_copy_down_pkt:Transmit FIFO is fulli - pkt drop\n");
1468             pInfo->stats.tx_errors++;
1469             return STATUS_SUCCESS;
1470          }
1471     }
1472 #endif
1473
1474     count = sizeof (PSEUDO_HDR) + len;
1475     if(count > MAX_BUF_SIZE)
1476     {
1477         DEBUG("Error:ft1000_copy_down_pkt:Message Size Overflow!\n");
1478         DEBUG("size = %d\n", count);
1479         return STATUS_FAILURE;
1480     }
1481
1482     if ( count % 4)
1483         count = count + (4- (count %4) );
1484
1485     pTemp = (PUSHORT)&(pFt1000Dev->tx_buf[0]);
1486     *pTemp ++ = ntohs(count);
1487     *pTemp ++ = 0x1020;
1488     *pTemp ++ = 0x2010;
1489     *pTemp ++ = 0x9100;
1490     *pTemp ++ = 0;
1491     *pTemp ++ = 0;
1492     *pTemp ++ = 0;
1493     pTemp = (PUSHORT)&(pFt1000Dev->tx_buf[0]);
1494     checksum = *pTemp ++;
1495     for (i=1; i<7; i++)
1496     {
1497         checksum ^= *pTemp ++;
1498     }
1499     *pTemp++ = checksum;
1500     memcpy (&(pFt1000Dev->tx_buf[sizeof(PSEUDO_HDR)]), packet, len);
1501
1502     //usb_init_urb(pFt1000Dev->tx_urb); //mbelian
1503
1504     netif_stop_queue(netdev);
1505
1506     //DEBUG ("ft1000_copy_down_pkt: count = %d\n", count);
1507
1508     usb_fill_bulk_urb(pFt1000Dev->tx_urb,
1509                       pFt1000Dev->dev,
1510                       usb_sndbulkpipe(pFt1000Dev->dev, pFt1000Dev->bulk_out_endpointAddr),
1511                       pFt1000Dev->tx_buf,
1512                       count,
1513                       ft1000_usb_transmit_complete,
1514                       (void*)pFt1000Dev);
1515
1516     t = (u8 *)pFt1000Dev->tx_urb->transfer_buffer;
1517     //DEBUG("transfer_length=%d\n", pFt1000Dev->tx_urb->transfer_buffer_length);
1518     /*for (i=0; i<count; i++ )
1519     {
1520        DEBUG("%x    ", *t++ );
1521     }*/
1522
1523
1524     ret = usb_submit_urb(pFt1000Dev->tx_urb, GFP_ATOMIC);
1525     if(ret)
1526     {
1527                 DEBUG("ft1000 failed tx_urb %d\n", ret);
1528
1529    /*     pInfo->stats.tx_errors++;
1530
1531         netif_start_queue(netdev);  */  //mbelian
1532                 return STATUS_FAILURE;
1533
1534     }
1535     else
1536     {
1537         //DEBUG("ft1000 sucess tx_urb %d\n", ret);
1538
1539         pInfo->stats.tx_packets++;
1540         pInfo->stats.tx_bytes += (len+14);
1541     }
1542
1543     //DEBUG("ft1000_copy_down_pkt() exit\n");
1544
1545     return STATUS_SUCCESS;
1546 }
1547
1548 //---------------------------------------------------------------------------
1549 // Function:    ft1000_start_xmit
1550 //
1551 // Parameters:  skb - socket buffer to be sent
1552 //              dev - network device
1553 //
1554 //
1555 // Returns:     none
1556 //
1557 // Description: transmit a ethernet packet
1558 //
1559 // Notes:
1560 //
1561 //---------------------------------------------------------------------------
1562 static int ft1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1563 {
1564     FT1000_INFO *pInfo = netdev_priv(dev);
1565     struct ft1000_device *pFt1000Dev= pInfo->pFt1000Dev;
1566     u8 *pdata;
1567     int maxlen, pipe;
1568
1569
1570     //DEBUG(" ft1000_start_xmit() entered\n");
1571
1572     if ( skb == NULL )
1573     {
1574         DEBUG ("ft1000_hw: ft1000_start_xmit:skb == NULL!!!\n" );
1575         return STATUS_FAILURE;
1576     }
1577
1578     if ( pFt1000Dev->status & FT1000_STATUS_CLOSING)
1579     {
1580         DEBUG("network driver is closed, return\n");
1581         dev_kfree_skb(skb);
1582         //usb_kill_urb(pFt1000Dev->tx_urb); //mbelian
1583         return STATUS_SUCCESS;
1584     }
1585
1586     //DEBUG("ft1000_start_xmit 1:length of packet = %d\n", skb->len);
1587     pipe = usb_sndbulkpipe(pFt1000Dev->dev, pFt1000Dev->bulk_out_endpointAddr);
1588     maxlen = usb_maxpacket(pFt1000Dev->dev, pipe, usb_pipeout(pipe));
1589     //DEBUG("ft1000_start_xmit 2: pipe=%d dev->maxpacket  = %d\n", pipe, maxlen);
1590
1591     pdata = (u8 *)skb->data;
1592     /*for (i=0; i<skb->len; i++)
1593         DEBUG("skb->data[%d]=%x    ", i, *(skb->data+i));
1594
1595     DEBUG("\n");*/
1596
1597
1598     if (pInfo->mediastate == 0)
1599     {
1600         /* Drop packet is mediastate is down */
1601         DEBUG("ft1000_hw:ft1000_start_xmit:mediastate is down\n");
1602         dev_kfree_skb(skb);
1603         return STATUS_SUCCESS;
1604     }
1605
1606     if ( (skb->len < ENET_HEADER_SIZE) || (skb->len > ENET_MAX_SIZE) )
1607     {
1608         /* Drop packet which has invalid size */
1609         DEBUG("ft1000_hw:ft1000_start_xmit:invalid ethernet length\n");
1610         dev_kfree_skb(skb);
1611         return STATUS_SUCCESS;
1612     }
1613 //mbelian
1614     if(ft1000_copy_down_pkt (dev, (pdata+ENET_HEADER_SIZE-2), skb->len - ENET_HEADER_SIZE + 2) == STATUS_FAILURE)
1615         {
1616         dev_kfree_skb(skb);
1617                 return STATUS_SUCCESS;
1618         }
1619
1620     dev_kfree_skb(skb);
1621     //DEBUG(" ft1000_start_xmit() exit\n");
1622
1623     return 0;
1624 }
1625
1626 //---------------------------------------------------------------------------
1627 //
1628 // Function:   ft1000_copy_up_pkt
1629 // Descripton: This function will take a packet from the FIFO up link and
1630 //             convert it into an ethernet packet and deliver it to the IP stack
1631 // Input:
1632 //     urb - the receving usb urb
1633 //
1634 // Output:
1635 //     status - FAILURE
1636 //              SUCCESS
1637 //
1638 //---------------------------------------------------------------------------
1639 static int ft1000_copy_up_pkt (struct urb *urb)
1640 {
1641     PFT1000_INFO info = urb->context;
1642     struct ft1000_device *ft1000dev = info->pFt1000Dev;
1643     struct net_device *net = ft1000dev->net;
1644
1645     u16 tempword;
1646     u16 len;
1647     u16 lena; //mbelian
1648     struct sk_buff *skb;
1649     u16 i;
1650     u8 *pbuffer=NULL;
1651     u8 *ptemp=NULL;
1652     u16 *chksum;
1653
1654
1655     //DEBUG("ft1000_copy_up_pkt entered\n");
1656
1657     if ( ft1000dev->status & FT1000_STATUS_CLOSING)
1658     {
1659         DEBUG("network driver is closed, return\n");
1660         return STATUS_SUCCESS;
1661     }
1662
1663     // Read length
1664     len = urb->transfer_buffer_length;
1665     lena = urb->actual_length; //mbelian
1666     //DEBUG("ft1000_copy_up_pkt: transfer_buffer_length=%d, actual_buffer_len=%d\n",
1667       //       urb->transfer_buffer_length, urb->actual_length);
1668
1669     chksum = (PUSHORT)ft1000dev->rx_buf;
1670
1671     tempword = *chksum++;
1672     for (i=1; i<7; i++)
1673     {
1674         tempword ^= *chksum++;
1675     }
1676
1677     if  (tempword != *chksum)
1678     {
1679         info->stats.rx_errors ++;
1680         ft1000_submit_rx_urb(info);
1681         return STATUS_FAILURE;
1682     }
1683
1684
1685     //DEBUG("ft1000_copy_up_pkt: checksum is correct %x\n", *chksum);
1686
1687     skb = dev_alloc_skb(len+12+2);
1688
1689     if (skb == NULL)
1690     {
1691         DEBUG("ft1000_copy_up_pkt: No Network buffers available\n");
1692         info->stats.rx_errors++;
1693         ft1000_submit_rx_urb(info);
1694         return STATUS_FAILURE;
1695     }
1696
1697     pbuffer = (u8 *)skb_put(skb, len+12);
1698
1699     //subtract the number of bytes read already
1700     ptemp = pbuffer;
1701
1702     // fake MAC address
1703     *pbuffer++ = net->dev_addr[0];
1704     *pbuffer++ = net->dev_addr[1];
1705     *pbuffer++ = net->dev_addr[2];
1706     *pbuffer++ = net->dev_addr[3];
1707     *pbuffer++ = net->dev_addr[4];
1708     *pbuffer++ = net->dev_addr[5];
1709     *pbuffer++ = 0x00;
1710     *pbuffer++ = 0x07;
1711     *pbuffer++ = 0x35;
1712     *pbuffer++ = 0xff;
1713     *pbuffer++ = 0xff;
1714     *pbuffer++ = 0xfe;
1715
1716
1717
1718
1719     memcpy(pbuffer, ft1000dev->rx_buf+sizeof(PSEUDO_HDR), len-sizeof(PSEUDO_HDR));
1720
1721     //DEBUG("ft1000_copy_up_pkt: Data passed to Protocol layer\n");
1722     /*for (i=0; i<len+12; i++)
1723     {
1724         DEBUG("ft1000_copy_up_pkt: Protocol Data: 0x%x\n ", *ptemp++);
1725     }*/
1726
1727     skb->dev = net;
1728
1729     skb->protocol = eth_type_trans(skb, net);
1730     skb->ip_summed = CHECKSUM_UNNECESSARY;
1731     netif_rx(skb);
1732
1733     info->stats.rx_packets++;
1734     // Add on 12 bytes for MAC address which was removed
1735     info->stats.rx_bytes += (lena+12); //mbelian
1736
1737     ft1000_submit_rx_urb(info);
1738     //DEBUG("ft1000_copy_up_pkt exited\n");
1739     return SUCCESS;
1740 }
1741
1742 //---------------------------------------------------------------------------
1743 //
1744 // Function:   ft1000_submit_rx_urb
1745 // Descripton: the receiving function of the network driver
1746 //
1747 // Input:
1748 //     info - a private structure contains the device information
1749 //
1750 // Output:
1751 //     status - FAILURE
1752 //              SUCCESS
1753 //
1754 //---------------------------------------------------------------------------
1755 static int ft1000_submit_rx_urb(PFT1000_INFO info)
1756 {
1757     int result;
1758     struct ft1000_device *pFt1000Dev = info->pFt1000Dev;
1759
1760     //netif_carrier_on(pFt1000Dev->net);
1761
1762     //DEBUG ("ft1000_submit_rx_urb entered: sizeof rx_urb is %d\n", sizeof(*pFt1000Dev->rx_urb));
1763     if ( pFt1000Dev->status & FT1000_STATUS_CLOSING)
1764     {
1765         DEBUG("network driver is closed, return\n");
1766         //usb_kill_urb(pFt1000Dev->rx_urb); //mbelian
1767         return STATUS_SUCCESS;
1768     }
1769     //memset(pFt1000Dev->rx_urb, 0, sizeof(*pFt1000Dev->rx_urb));
1770     //usb_init_urb(pFt1000Dev->rx_urb);//mbelian
1771
1772     //spin_lock_init(&pFt1000Dev->rx_urb->lock);
1773
1774     usb_fill_bulk_urb(pFt1000Dev->rx_urb,
1775             pFt1000Dev->dev,
1776             usb_rcvbulkpipe(pFt1000Dev->dev, pFt1000Dev->bulk_in_endpointAddr),
1777             pFt1000Dev->rx_buf,
1778             MAX_BUF_SIZE,
1779             (usb_complete_t)ft1000_copy_up_pkt,
1780             info);
1781
1782
1783     if((result = usb_submit_urb(pFt1000Dev->rx_urb, GFP_ATOMIC)))
1784     {
1785         printk("ft1000_submit_rx_urb: submitting rx_urb %d failed\n", result);
1786         return STATUS_FAILURE;
1787     }
1788
1789     //DEBUG("ft1000_submit_rx_urb exit: result=%d\n", result);
1790
1791     return STATUS_SUCCESS;
1792 }
1793
1794 //---------------------------------------------------------------------------
1795 // Function:    ft1000_open
1796 //
1797 // Parameters:
1798 //              dev - network device
1799 //
1800 //
1801 // Returns:     none
1802 //
1803 // Description: open the network driver
1804 //
1805 // Notes:
1806 //
1807 //---------------------------------------------------------------------------
1808 static int ft1000_open (struct net_device *dev)
1809 {
1810         FT1000_INFO *pInfo = (FT1000_INFO *)netdev_priv(dev);
1811     struct timeval tv; //mbelian
1812
1813     DEBUG("ft1000_open is called for card %d\n", pInfo->CardNumber);
1814     //DEBUG("ft1000_open: dev->addr=%x, dev->addr_len=%d\n", dev->addr, dev->addr_len);
1815
1816         pInfo->stats.rx_bytes = 0; //mbelian
1817         pInfo->stats.tx_bytes = 0; //mbelian
1818         pInfo->stats.rx_packets = 0; //mbelian
1819         pInfo->stats.tx_packets = 0; //mbelian
1820         do_gettimeofday(&tv);
1821     pInfo->ConTm = tv.tv_sec;
1822         pInfo->ProgConStat = 0; //mbelian
1823
1824
1825     netif_start_queue(dev);
1826
1827     //netif_device_attach(dev);
1828
1829     netif_carrier_on(dev); //mbelian
1830
1831     ft1000_submit_rx_urb(pInfo);
1832     return 0;
1833 }
1834
1835 //---------------------------------------------------------------------------
1836 // Function:    ft1000_close
1837 //
1838 // Parameters:
1839 //              net - network device
1840 //
1841 //
1842 // Returns:     none
1843 //
1844 // Description: close the network driver
1845 //
1846 // Notes:
1847 //
1848 //---------------------------------------------------------------------------
1849 int ft1000_close(struct net_device *net)
1850 {
1851         FT1000_INFO *pInfo = (FT1000_INFO *) netdev_priv (net);
1852     struct ft1000_device *ft1000dev = pInfo->pFt1000Dev;
1853
1854     //DEBUG ("ft1000_close: netdev->refcnt=%d\n", net->refcnt);
1855
1856     ft1000dev->status |= FT1000_STATUS_CLOSING;
1857
1858     //DEBUG("ft1000_close: calling usb_kill_urb \n");
1859     //usb_kill_urb(ft1000dev->rx_urb);
1860     //usb_kill_urb(ft1000dev->tx_urb);
1861
1862
1863     DEBUG("ft1000_close: pInfo=%p, ft1000dev=%p\n", pInfo, ft1000dev);
1864     netif_carrier_off(net);//mbelian
1865     netif_stop_queue(net);
1866     //DEBUG("ft1000_close: netif_stop_queue called\n");
1867     ft1000dev->status &= ~FT1000_STATUS_CLOSING;
1868
1869    pInfo->ProgConStat = 0xff; //mbelian
1870
1871
1872     return 0;
1873 }
1874
1875 static struct net_device_stats *ft1000_netdev_stats(struct net_device *dev)
1876 {
1877         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev);
1878     //struct ft1000_device *ft1000dev = info->pFt1000Dev;
1879
1880     //return &(ft1000dev->stats);//mbelian
1881         return &(info->stats); //mbelian
1882 }
1883
1884
1885 /*********************************************************************************
1886 Jim
1887 */
1888
1889
1890 //---------------------------------------------------------------------------
1891 //
1892 // Function:   ft1000_chkcard
1893 // Descripton: This function will check if the device is presently available on
1894 //             the system.
1895 // Input:
1896 //     dev    - device structure
1897 // Output:
1898 //     status - FALSE (device is not present)
1899 //              TRUE  (device is present)
1900 //
1901 //---------------------------------------------------------------------------
1902 static int ft1000_chkcard (struct ft1000_device *dev) {
1903     u16 tempword;
1904     u16 status;
1905         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev->net);
1906
1907     if (info->fCondResetPend)
1908     {
1909         DEBUG("ft1000_hw:ft1000_chkcard:Card is being reset, return FALSE\n");
1910         return TRUE;
1911     }
1912
1913     // Mask register is used to check for device presence since it is never
1914     // set to zero.
1915     status = ft1000_read_register(dev, &tempword, FT1000_REG_SUP_IMASK);
1916     //DEBUG("ft1000_hw:ft1000_chkcard: read FT1000_REG_SUP_IMASK = %x\n", tempword);
1917     if (tempword == 0) {
1918         DEBUG("ft1000_hw:ft1000_chkcard: IMASK = 0 Card not detected\n");
1919         return FALSE;
1920     }
1921
1922     // The system will return the value of 0xffff for the version register
1923     // if the device is not present.
1924     status = ft1000_read_register(dev, &tempword, FT1000_REG_ASIC_ID);
1925     //DEBUG("ft1000_hw:ft1000_chkcard: read FT1000_REG_ASIC_ID = %x\n", tempword);
1926     //pxu if (tempword == 0xffff) {
1927     if (tempword != 0x1b01 ){
1928         dev->status |= FT1000_STATUS_CLOSING; //mbelian
1929         DEBUG("ft1000_hw:ft1000_chkcard: Version = 0xffff Card not detected\n");
1930         return FALSE;
1931     }
1932     return TRUE;
1933 }
1934
1935 //---------------------------------------------------------------------------
1936 //
1937 // Function:   ft1000_hbchk
1938 // Descripton: This function will perform the heart beat check of the DSP as
1939 //             well as the ASIC.
1940 // Input:
1941 //     dev    - device structure
1942 // Output:
1943 //     none
1944 //
1945 //---------------------------------------------------------------------------
1946 static void ft1000_hbchk(u_long data)
1947 {
1948     struct ft1000_device *dev = (struct ft1000_device *)data;
1949
1950     FT1000_INFO *info;
1951     USHORT tempword;
1952         u16 status;
1953         info = (FT1000_INFO *) netdev_priv (dev->net);
1954
1955     DEBUG("ft1000_hbchk called for CardNumber = %d CardReady = %d\n", info->CardNumber, info->CardReady);
1956
1957     if (info->fCondResetPend == 1) {
1958         // Reset ASIC and DSP
1959         status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER0, (PUCHAR)&(info->DSP_TIME[0]), FT1000_MAG_DSP_TIMER0_INDX);
1960         status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER1, (PUCHAR)&(info->DSP_TIME[1]), FT1000_MAG_DSP_TIMER1_INDX);
1961         status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER2, (PUCHAR)&(info->DSP_TIME[2]), FT1000_MAG_DSP_TIMER2_INDX);
1962         status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER3, (PUCHAR)&(info->DSP_TIME[3]), FT1000_MAG_DSP_TIMER3_INDX);
1963
1964         info->DrvErrNum = DSP_CONDRESET_INFO;
1965         DEBUG("ft1000_hw:DSP conditional reset requested\n");
1966         ft1000_reset_card(dev->net);
1967         info->fCondResetPend = 0;
1968         /* Schedule this module to run every 2 seconds */
1969
1970         poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
1971         poll_timer[info->CardNumber].data = (u_long)dev;
1972         add_timer(&poll_timer[info->CardNumber]);
1973
1974
1975
1976         return;
1977     }
1978
1979     if (info->CardReady == 1) {
1980         // Perform dsp heartbeat check
1981             status = ntohs(ft1000_read_dpram16(dev, FT1000_MAG_HI_HO, (PUCHAR)&tempword, FT1000_MAG_HI_HO_INDX));
1982         DEBUG("ft1000_hw:ft1000_hbchk:hi_ho value = 0x%x\n", tempword);
1983         // Let's perform another check if ho is not detected
1984         if (tempword != ho) {
1985               status  = ntohs(ft1000_read_dpram16(dev, FT1000_MAG_HI_HO, (PUCHAR)&tempword,FT1000_MAG_HI_HO_INDX));
1986         }
1987         if (tempword != ho) {
1988             printk(KERN_INFO "ft1000: heartbeat failed - no ho detected\n");
1989                 status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER0, (PUCHAR)&(info->DSP_TIME[0]), FT1000_MAG_DSP_TIMER0_INDX);
1990                 status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER1, (PUCHAR)&(info->DSP_TIME[1]), FT1000_MAG_DSP_TIMER1_INDX);
1991                 status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER2, (PUCHAR)&(info->DSP_TIME[2]), FT1000_MAG_DSP_TIMER2_INDX);
1992                 status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER3, (PUCHAR)&(info->DSP_TIME[3]), FT1000_MAG_DSP_TIMER3_INDX);
1993             info->DrvErrNum = DSP_HB_INFO;
1994             if (ft1000_reset_card(dev->net) == 0) {
1995                printk(KERN_INFO "ft1000: Hardware Failure Detected - PC Card disabled\n");
1996                info->ProgConStat = 0xff;
1997                return;
1998             }
1999             /* Schedule this module to run every 2 seconds */
2000             poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
2001             poll_timer[info->CardNumber].data = (u_long)dev;
2002             add_timer(&poll_timer[info->CardNumber]);
2003             return;
2004         }
2005
2006         status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2007         // Let's check doorbell again if fail
2008         if (tempword & FT1000_DB_HB) {
2009                 status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2010         }
2011         if (tempword & FT1000_DB_HB) {
2012             printk(KERN_INFO "ft1000: heartbeat doorbell not clear by firmware\n");
2013             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER0, (PUCHAR)&(info->DSP_TIME[0]), FT1000_MAG_DSP_TIMER0_INDX);
2014             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER1, (PUCHAR)&(info->DSP_TIME[1]), FT1000_MAG_DSP_TIMER1_INDX);
2015             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER2, (PUCHAR)&(info->DSP_TIME[2]), FT1000_MAG_DSP_TIMER2_INDX);
2016             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER3, (PUCHAR)&(info->DSP_TIME[3]), FT1000_MAG_DSP_TIMER3_INDX);
2017             info->DrvErrNum = DSP_HB_INFO;
2018             if (ft1000_reset_card(dev->net) == 0) {
2019                printk(KERN_INFO "ft1000: Hardware Failure Detected - PC Card disabled\n");
2020                info->ProgConStat = 0xff;
2021                return;
2022             }
2023             /* Schedule this module to run every 2 seconds */
2024             poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
2025             poll_timer[info->CardNumber].data = (u_long)dev;
2026             add_timer(&poll_timer[info->CardNumber]);
2027             return;
2028         }
2029
2030         // Set dedicated area to hi and ring appropriate doorbell according
2031         // to hi/ho heartbeat protocol
2032         ft1000_write_dpram16(dev, FT1000_MAG_HI_HO, hi_mag, FT1000_MAG_HI_HO_INDX);
2033
2034         status = ntohs(ft1000_read_dpram16(dev, FT1000_MAG_HI_HO, (PUCHAR)&tempword, FT1000_MAG_HI_HO_INDX));
2035         // Let's write hi again if fail
2036         if (tempword != hi) {
2037                ft1000_write_dpram16(dev, FT1000_MAG_HI_HO, hi_mag, FT1000_MAG_HI_HO_INDX);
2038                    status = ntohs(ft1000_read_dpram16(dev, FT1000_MAG_HI_HO, (PUCHAR)&tempword, FT1000_MAG_HI_HO_INDX));
2039
2040         }
2041         if (tempword != hi) {
2042             printk(KERN_INFO "ft1000: heartbeat failed - cannot write hi into DPRAM\n");
2043             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER0, (PUCHAR)&(info->DSP_TIME[0]), FT1000_MAG_DSP_TIMER0_INDX);
2044             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER1, (PUCHAR)&(info->DSP_TIME[1]), FT1000_MAG_DSP_TIMER1_INDX);
2045             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER2, (PUCHAR)&(info->DSP_TIME[2]), FT1000_MAG_DSP_TIMER2_INDX);
2046             status = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER3, (PUCHAR)&(info->DSP_TIME[3]), FT1000_MAG_DSP_TIMER3_INDX);
2047
2048             info->DrvErrNum = DSP_HB_INFO;
2049             if (ft1000_reset_card(dev->net) == 0) {
2050                printk(KERN_INFO "ft1000: Hardware Failure Detected - PC Card disabled\n");
2051                info->ProgConStat = 0xff;
2052                return;
2053             }
2054             /* Schedule this module to run every 2 seconds */
2055             poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
2056             poll_timer[info->CardNumber].data = (u_long)dev;
2057             add_timer(&poll_timer[info->CardNumber]);
2058             return;
2059         }
2060         ft1000_write_register(dev, FT1000_DB_HB, FT1000_REG_DOORBELL);
2061
2062     }
2063
2064     /* Schedule this module to run every 2 seconds */
2065     poll_timer[info->CardNumber].expires = jiffies + (2*HZ);
2066     poll_timer[info->CardNumber].data = (u_long)dev;
2067     add_timer(&poll_timer[info->CardNumber]);
2068 }
2069
2070 //---------------------------------------------------------------------------
2071 //
2072 // Function:   ft1000_receive_cmd
2073 // Descripton: This function will read a message from the dpram area.
2074 // Input:
2075 //    dev - network device structure
2076 //    pbuffer - caller supply address to buffer
2077 //    pnxtph - pointer to next pseudo header
2078 // Output:
2079 //   Status = 0 (unsuccessful)
2080 //          = 1 (successful)
2081 //
2082 //---------------------------------------------------------------------------
2083 static BOOLEAN ft1000_receive_cmd (struct ft1000_device *dev, u16 *pbuffer, int maxsz, u16 *pnxtph) {
2084     u16 size, ret;
2085     u16 *ppseudohdr;
2086     int i;
2087     u16 tempword;
2088
2089     ret = ft1000_read_dpram16(dev, FT1000_MAG_PH_LEN, (PUCHAR)&size, FT1000_MAG_PH_LEN_INDX);
2090     size = ntohs(size) + PSEUDOSZ;
2091     if (size > maxsz) {
2092         DEBUG("FT1000:ft1000_receive_cmd:Invalid command length = %d\n", size);
2093         return FALSE;
2094     }
2095     else {
2096         ppseudohdr = (u16 *)pbuffer;
2097         //spin_lock_irqsave (&info->dpram_lock, flags);
2098         ft1000_write_register(dev, FT1000_DPRAM_MAG_RX_BASE, FT1000_REG_DPRAM_ADDR);
2099         ret = ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAH);
2100         //DEBUG("ft1000_hw:received data = 0x%x\n", *pbuffer);
2101         pbuffer++;
2102         ft1000_write_register(dev,  FT1000_DPRAM_MAG_RX_BASE+1, FT1000_REG_DPRAM_ADDR);
2103         for (i=0; i<=(size>>2); i++) {
2104             ret = ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAL);
2105             pbuffer++;
2106             ret = ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAH);
2107             pbuffer++;
2108         }
2109         //copy odd aligned word
2110         ret = ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAL);
2111         //DEBUG("ft1000_hw:received data = 0x%x\n", *pbuffer);
2112         pbuffer++;
2113         ret = ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAH);
2114         //DEBUG("ft1000_hw:received data = 0x%x\n", *pbuffer);
2115         pbuffer++;
2116         if (size & 0x0001) {
2117             //copy odd byte from fifo
2118             ret = ft1000_read_register(dev, &tempword, FT1000_REG_DPRAM_DATA);
2119             *pbuffer = ntohs(tempword);
2120         }
2121         //spin_unlock_irqrestore(&info->dpram_lock, flags);
2122
2123         // Check if pseudo header checksum is good
2124         // Calculate pseudo header checksum
2125         tempword = *ppseudohdr++;
2126         for (i=1; i<7; i++) {
2127             tempword ^= *ppseudohdr++;
2128         }
2129         if ( (tempword != *ppseudohdr) ) {
2130             return FALSE;
2131         }
2132
2133
2134 #if 0
2135         DEBUG("ft1000_receive_cmd:pbuffer\n");
2136         for(i = 0; i < size; i+=5)
2137         {
2138             if( (i + 5) < size )
2139                 DEBUG("0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", tempbuffer[i], tempbuffer[i+1], tempbuffer[i+2], tempbuffer[i+3], tempbuffer[i+4]);
2140             else
2141             {
2142                 for (j = i; j < size; j++)
2143                 DEBUG("0x%x ", tempbuffer[j]);
2144                 DEBUG("\n");
2145                 break;
2146             }
2147         }
2148
2149 #endif
2150
2151         return TRUE;
2152     }
2153 }
2154
2155
2156 static int ft1000_dsp_prov(void *arg)
2157 {
2158     struct ft1000_device *dev = (struct ft1000_device *)arg;
2159         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev->net);
2160     u16 tempword;
2161     u16 len;
2162     u16 i=0;
2163     PPROV_RECORD ptr;
2164     PPSEUDO_HDR ppseudo_hdr;
2165     PUSHORT pmsg;
2166     u16 status;
2167     USHORT TempShortBuf [256];
2168
2169     DEBUG("*** DspProv Entered\n");
2170
2171     while (         list_empty(&info->prov_list) == 0
2172                    /*&&  !kthread_should_stop()  */)
2173     {
2174         DEBUG("DSP Provisioning List Entry\n");
2175
2176         // Check if doorbell is available
2177         DEBUG("check if doorbell is cleared\n");
2178         status = ft1000_read_register (dev, &tempword, FT1000_REG_DOORBELL);
2179         if (status)
2180         {
2181                 DEBUG("ft1000_dsp_prov::ft1000_read_register error\n");
2182             break;
2183         }
2184
2185         while (tempword & FT1000_DB_DPRAM_TX) {
2186             mdelay(10);
2187             i++;
2188             if (i==10) {
2189                DEBUG("FT1000:ft1000_dsp_prov:message drop\n");
2190                return STATUS_FAILURE;
2191             }
2192             ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2193         }
2194
2195         if ( !(tempword & FT1000_DB_DPRAM_TX) ) {
2196             DEBUG("*** Provision Data Sent to DSP\n");
2197
2198             // Send provisioning data
2199             ptr = list_entry(info->prov_list.next, PROV_RECORD, list);
2200             len = *(u16 *)ptr->pprov_data;
2201             len = htons(len);
2202             len += PSEUDOSZ;
2203             //len = htons(len);
2204
2205             pmsg = (PUSHORT)ptr->pprov_data;
2206             ppseudo_hdr = (PPSEUDO_HDR)pmsg;
2207             // Insert slow queue sequence number
2208             ppseudo_hdr->seq_num = info->squeseqnum++;
2209             ppseudo_hdr->portsrc = 0;
2210             // Calculate new checksum
2211             ppseudo_hdr->checksum = *pmsg++;
2212             //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
2213             for (i=1; i<7; i++) {
2214                 ppseudo_hdr->checksum ^= *pmsg++;
2215                 //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
2216             }
2217
2218             TempShortBuf[0] = 0;
2219             TempShortBuf[1] = htons (len);
2220             memcpy(&TempShortBuf[2], ppseudo_hdr, len);
2221
2222             status = ft1000_write_dpram32 (dev, 0, (PUCHAR)&TempShortBuf[0], (unsigned short)(len+2));
2223             status = ft1000_write_register (dev, FT1000_DB_DPRAM_TX, FT1000_REG_DOORBELL);
2224
2225             list_del(&ptr->list);
2226             kfree(ptr->pprov_data);
2227             kfree(ptr);
2228         }
2229         msleep(10);
2230     }
2231
2232     DEBUG("DSP Provisioning List Entry finished\n");
2233
2234     msleep(100);
2235
2236     info->fProvComplete = 1;
2237     info->CardReady = 1;
2238     info->DSP_loading= 0;
2239     return STATUS_SUCCESS;
2240
2241 }
2242
2243
2244 static int ft1000_proc_drvmsg (struct ft1000_device *dev, u16 size) {
2245         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev->net);
2246     u16 msgtype;
2247     u16 tempword;
2248     PMEDIAMSG pmediamsg;
2249     PDSPINITMSG pdspinitmsg;
2250     PDRVMSG pdrvmsg;
2251     u16 i;
2252     PPSEUDO_HDR ppseudo_hdr;
2253     PUSHORT pmsg;
2254     u16 status;
2255     //struct timeval tv; //mbelian
2256     union {
2257         u8  byte[2];
2258         u16 wrd;
2259     } convert;
2260
2261
2262     char *cmdbuffer = kmalloc(1600, GFP_KERNEL);
2263     if (!cmdbuffer)
2264         return STATUS_FAILURE;
2265
2266     status = ft1000_read_dpram32(dev, 0x200, cmdbuffer, size);
2267
2268
2269     //if (ft1000_receive_cmd(dev, &cmdbuffer[0], MAX_CMD_SQSIZE, &tempword))
2270     {
2271
2272 #ifdef JDEBUG
2273         DEBUG("ft1000_proc_drvmsg:cmdbuffer\n");
2274         for(i = 0; i < size; i+=5)
2275         {
2276             if( (i + 5) < size )
2277                 DEBUG("0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", cmdbuffer[i], cmdbuffer[i+1], cmdbuffer[i+2], cmdbuffer[i+3], cmdbuffer[i+4]);
2278             else
2279             {
2280                 for (j = i; j < size; j++)
2281                 DEBUG("0x%x ", cmdbuffer[j]);
2282                 DEBUG("\n");
2283                 break;
2284             }
2285         }
2286 #endif
2287         pdrvmsg = (PDRVMSG)&cmdbuffer[2];
2288         msgtype = ntohs(pdrvmsg->type);
2289         DEBUG("ft1000_proc_drvmsg:Command message type = 0x%x\n", msgtype);
2290         switch (msgtype) {
2291             case MEDIA_STATE: {
2292                 DEBUG("ft1000_proc_drvmsg:Command message type = MEDIA_STATE");
2293
2294                 pmediamsg = (PMEDIAMSG)&cmdbuffer[0];
2295                 if (info->ProgConStat != 0xFF) {
2296                     if (pmediamsg->state) {
2297                         DEBUG("Media is up\n");
2298                         if (info->mediastate == 0) {
2299                             if ( info->NetDevRegDone )
2300                             {
2301                                 //netif_carrier_on(dev->net);//mbelian
2302                                 netif_wake_queue(dev->net);
2303                             }
2304                             info->mediastate = 1;
2305                             /*do_gettimeofday(&tv);
2306                             info->ConTm = tv.tv_sec;*/ //mbelian
2307                         }
2308                     }
2309                     else {
2310                         DEBUG("Media is down\n");
2311                         if (info->mediastate == 1) {
2312                             info->mediastate = 0;
2313                             if ( info->NetDevRegDone )
2314                             {
2315                                 //netif_carrier_off(dev->net); mbelian
2316                                 //netif_stop_queue(dev->net);
2317                             }
2318                             info->ConTm = 0;
2319                         }
2320                     }
2321                 }
2322                 else {
2323                     DEBUG("Media is down\n");
2324                     if (info->mediastate == 1) {
2325                         info->mediastate = 0;
2326                         if ( info->NetDevRegDone)
2327                         {
2328                             //netif_carrier_off(dev->net); //mbelian
2329                             //netif_stop_queue(dev->net);
2330                         }
2331                         info->ConTm = 0;
2332                     }
2333                 }
2334                 break;
2335             }
2336             case DSP_INIT_MSG: {
2337                 DEBUG("ft1000_proc_drvmsg:Command message type = DSP_INIT_MSG");
2338
2339                 pdspinitmsg = (PDSPINITMSG)&cmdbuffer[2];
2340                 memcpy(info->DspVer, pdspinitmsg->DspVer, DSPVERSZ);
2341                 DEBUG("DSPVER = 0x%2x 0x%2x 0x%2x 0x%2x\n", info->DspVer[0], info->DspVer[1], info->DspVer[2], info->DspVer[3]);
2342                 memcpy(info->HwSerNum, pdspinitmsg->HwSerNum, HWSERNUMSZ);
2343                 memcpy(info->Sku, pdspinitmsg->Sku, SKUSZ);
2344                 memcpy(info->eui64, pdspinitmsg->eui64, EUISZ);
2345                 DEBUG("EUI64=%2x.%2x.%2x.%2x.%2x.%2x.%2x.%2x\n", info->eui64[0],info->eui64[1], info->eui64[2], info->eui64[3], info->eui64[4], info->eui64[5],info->eui64[6], info->eui64[7]);
2346                 dev->net->dev_addr[0] = info->eui64[0];
2347                 dev->net->dev_addr[1] = info->eui64[1];
2348                 dev->net->dev_addr[2] = info->eui64[2];
2349                 dev->net->dev_addr[3] = info->eui64[5];
2350                 dev->net->dev_addr[4] = info->eui64[6];
2351                 dev->net->dev_addr[5] = info->eui64[7];
2352
2353                 if (ntohs(pdspinitmsg->length) == (sizeof(DSPINITMSG) - 20) ) {
2354                     memcpy(info->ProductMode, pdspinitmsg->ProductMode, MODESZ);
2355                     memcpy(info->RfCalVer, pdspinitmsg->RfCalVer, CALVERSZ);
2356                     memcpy(info->RfCalDate, pdspinitmsg->RfCalDate, CALDATESZ);
2357                     DEBUG("RFCalVer = 0x%2x 0x%2x\n", info->RfCalVer[0], info->RfCalVer[1]);
2358                 }
2359                 break;
2360             }
2361             case DSP_PROVISION: {
2362                 DEBUG("ft1000_proc_drvmsg:Command message type = DSP_PROVISION\n");
2363
2364                 // kick off dspprov routine to start provisioning
2365                 // Send provisioning data to DSP
2366                 if (list_empty(&info->prov_list) == 0)
2367                 {
2368                     info->fProvComplete = 0;
2369                     status = ft1000_dsp_prov(dev);
2370                     if (status != STATUS_SUCCESS)
2371                         goto out;
2372                 }
2373                 else {
2374                     info->fProvComplete = 1;
2375                     status = ft1000_write_register (dev, FT1000_DB_HB, FT1000_REG_DOORBELL);
2376                     DEBUG("FT1000:drivermsg:No more DSP provisioning data in dsp image\n");
2377                 }
2378                 DEBUG("ft1000_proc_drvmsg:DSP PROVISION is done\n");
2379                 break;
2380             }
2381             case DSP_STORE_INFO: {
2382                 DEBUG("ft1000_proc_drvmsg:Command message type = DSP_STORE_INFO");
2383
2384                 DEBUG("FT1000:drivermsg:Got DSP_STORE_INFO\n");
2385                 tempword = ntohs(pdrvmsg->length);
2386                 info->DSPInfoBlklen = tempword;
2387                 if (tempword < (MAX_DSP_SESS_REC-4) ) {
2388                     pmsg = (PUSHORT)&pdrvmsg->data[0];
2389                     for (i=0; i<((tempword+1)/2); i++) {
2390                         DEBUG("FT1000:drivermsg:dsp info data = 0x%x\n", *pmsg);
2391                         info->DSPInfoBlk[i+10] = *pmsg++;
2392                     }
2393                 }
2394                 else {
2395                     info->DSPInfoBlklen = 0;
2396                 }
2397                 break;
2398             }
2399             case DSP_GET_INFO: {
2400                 DEBUG("FT1000:drivermsg:Got DSP_GET_INFO\n");
2401                 // copy dsp info block to dsp
2402                 info->DrvMsgPend = 1;
2403                 // allow any outstanding ioctl to finish
2404                 mdelay(10);
2405                 status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2406                 if (tempword & FT1000_DB_DPRAM_TX) {
2407                     mdelay(10);
2408                     status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2409                     if (tempword & FT1000_DB_DPRAM_TX) {
2410                         mdelay(10);
2411                             status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2412                             if (tempword & FT1000_DB_DPRAM_TX) {
2413                                 break;
2414                             }
2415                     }
2416                 }
2417
2418                 // Put message into Slow Queue
2419                 // Form Pseudo header
2420                 pmsg = (PUSHORT)info->DSPInfoBlk;
2421                 *pmsg++ = 0;
2422                 *pmsg++ = htons(info->DSPInfoBlklen+20+info->DSPInfoBlklen);
2423                 ppseudo_hdr = (PPSEUDO_HDR)(PUSHORT)&info->DSPInfoBlk[2];
2424                 ppseudo_hdr->length = htons(info->DSPInfoBlklen+4+info->DSPInfoBlklen);
2425                 ppseudo_hdr->source = 0x10;
2426                 ppseudo_hdr->destination = 0x20;
2427                 ppseudo_hdr->portdest = 0;
2428                 ppseudo_hdr->portsrc = 0;
2429                 ppseudo_hdr->sh_str_id = 0;
2430                 ppseudo_hdr->control = 0;
2431                 ppseudo_hdr->rsvd1 = 0;
2432                 ppseudo_hdr->rsvd2 = 0;
2433                 ppseudo_hdr->qos_class = 0;
2434                 // Insert slow queue sequence number
2435                 ppseudo_hdr->seq_num = info->squeseqnum++;
2436                 // Insert application id
2437                 ppseudo_hdr->portsrc = 0;
2438                 // Calculate new checksum
2439                 ppseudo_hdr->checksum = *pmsg++;
2440                 for (i=1; i<7; i++) {
2441                     ppseudo_hdr->checksum ^= *pmsg++;
2442                 }
2443                 info->DSPInfoBlk[10] = 0x7200;
2444                 info->DSPInfoBlk[11] = htons(info->DSPInfoBlklen);
2445                 status = ft1000_write_dpram32 (dev, 0, (PUCHAR)&info->DSPInfoBlk[0], (unsigned short)(info->DSPInfoBlklen+22));
2446                 status = ft1000_write_register (dev, FT1000_DB_DPRAM_TX, FT1000_REG_DOORBELL);
2447                 info->DrvMsgPend = 0;
2448
2449                 break;
2450             }
2451
2452           case GET_DRV_ERR_RPT_MSG: {
2453               DEBUG("FT1000:drivermsg:Got GET_DRV_ERR_RPT_MSG\n");
2454               // copy driver error message to dsp
2455               info->DrvMsgPend = 1;
2456               // allow any outstanding ioctl to finish
2457               mdelay(10);
2458               status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2459               if (tempword & FT1000_DB_DPRAM_TX) {
2460                   mdelay(10);
2461                   status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
2462                   if (tempword & FT1000_DB_DPRAM_TX) {
2463                       mdelay(10);
2464                   }
2465               }
2466
2467               if ( (tempword & FT1000_DB_DPRAM_TX) == 0) {
2468                   // Put message into Slow Queue
2469                   // Form Pseudo header
2470                   pmsg = (PUSHORT)&tempbuffer[0];
2471                   ppseudo_hdr = (PPSEUDO_HDR)pmsg;
2472                   ppseudo_hdr->length = htons(0x0012);
2473                   ppseudo_hdr->source = 0x10;
2474                   ppseudo_hdr->destination = 0x20;
2475                   ppseudo_hdr->portdest = 0;
2476                   ppseudo_hdr->portsrc = 0;
2477                   ppseudo_hdr->sh_str_id = 0;
2478                   ppseudo_hdr->control = 0;
2479                   ppseudo_hdr->rsvd1 = 0;
2480                   ppseudo_hdr->rsvd2 = 0;
2481                   ppseudo_hdr->qos_class = 0;
2482                   // Insert slow queue sequence number
2483                   ppseudo_hdr->seq_num = info->squeseqnum++;
2484                   // Insert application id
2485                   ppseudo_hdr->portsrc = 0;
2486                   // Calculate new checksum
2487                   ppseudo_hdr->checksum = *pmsg++;
2488                   for (i=1; i<7; i++) {
2489                       ppseudo_hdr->checksum ^= *pmsg++;
2490                   }
2491                   pmsg = (PUSHORT)&tempbuffer[16];
2492                   *pmsg++ = htons(RSP_DRV_ERR_RPT_MSG);
2493                   *pmsg++ = htons(0x000e);
2494                   *pmsg++ = htons(info->DSP_TIME[0]);
2495                   *pmsg++ = htons(info->DSP_TIME[1]);
2496                   *pmsg++ = htons(info->DSP_TIME[2]);
2497                   *pmsg++ = htons(info->DSP_TIME[3]);
2498                   convert.byte[0] = info->DspVer[0];
2499                   convert.byte[1] = info->DspVer[1];
2500                   *pmsg++ = convert.wrd;
2501                   convert.byte[0] = info->DspVer[2];
2502                   convert.byte[1] = info->DspVer[3];
2503                   *pmsg++ = convert.wrd;
2504                   *pmsg++ = htons(info->DrvErrNum);
2505
2506                   CardSendCommand (dev, (unsigned char*)&tempbuffer[0], (USHORT)(0x0012 + PSEUDOSZ));
2507                   info->DrvErrNum = 0;
2508               }
2509               info->DrvMsgPend = 0;
2510
2511           break;
2512       }
2513
2514       default:
2515           break;
2516         }
2517
2518     }
2519
2520     status = STATUS_SUCCESS;
2521 out:
2522     kfree(cmdbuffer);
2523     DEBUG("return from ft1000_proc_drvmsg\n");
2524     return status;
2525 }
2526
2527
2528
2529 int ft1000_poll(void* dev_id) {
2530
2531     //FT1000_INFO *info = (PFT1000_INFO)((struct net_device *)dev_id)->priv;
2532     //struct ft1000_device *dev = (struct ft1000_device *)info->pFt1000Dev;
2533     struct ft1000_device *dev = (struct ft1000_device *)dev_id;
2534         FT1000_INFO *info = (FT1000_INFO *) netdev_priv (dev->net);
2535
2536     u16 tempword;
2537     u16 status;
2538     u16 size;
2539     int i;
2540     USHORT data;
2541     USHORT modulo;
2542     USHORT portid;
2543     u16 nxtph;
2544     PDPRAM_BLK pdpram_blk;
2545     PPSEUDO_HDR ppseudo_hdr;
2546     unsigned long flags;
2547
2548     //DEBUG("Enter ft1000_poll...\n");
2549     if (ft1000_chkcard(dev) == FALSE) {
2550         DEBUG("ft1000_poll::ft1000_chkcard: failed\n");
2551         return STATUS_FAILURE;
2552     }
2553
2554     status = ft1000_read_register (dev, &tempword, FT1000_REG_DOORBELL);
2555    // DEBUG("ft1000_poll: read FT1000_REG_DOORBELL message 0x%x\n", tempword);
2556
2557     //while ( (tempword) && (!status) ) {
2558     if ( !status )
2559     {
2560
2561         if (tempword & FT1000_DB_DPRAM_RX) {
2562             //DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type:  FT1000_DB_DPRAM_RX\n");
2563
2564             status = ft1000_read_dpram16(dev, 0x200, (PUCHAR)&data, 0);
2565             //DEBUG("ft1000_poll:FT1000_DB_DPRAM_RX:ft1000_read_dpram16:size = 0x%x\n", data);
2566             size = ntohs(data) + 16 + 2; //wai
2567             if (size % 4) {
2568                 modulo = 4 - (size % 4);
2569                 size = size + modulo;
2570             }
2571             status = ft1000_read_dpram16(dev, 0x201, (PUCHAR)&portid, 1);
2572             portid &= 0xff;
2573             //DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type: FT1000_DB_DPRAM_RX : portid 0x%x\n", portid);
2574
2575             if (size < MAX_CMD_SQSIZE) {
2576                 switch (portid)
2577                 {
2578                     case DRIVERID:
2579                         DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type: FT1000_DB_DPRAM_RX : portid DRIVERID\n");
2580
2581                         status = ft1000_proc_drvmsg (dev, size);
2582                         if (status != STATUS_SUCCESS )
2583                             return status;
2584                         break;
2585                     case DSPBCMSGID:
2586                         // This is a dsp broadcast message
2587                         // Check which application has registered for dsp broadcast messages
2588                         //DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type: FT1000_DB_DPRAM_RX : portid DSPBCMSGID\n");
2589
2590                         for (i=0; i<MAX_NUM_APP; i++) {
2591                            if ( (info->app_info[i].DspBCMsgFlag) && (info->app_info[i].fileobject) &&
2592                                          (info->app_info[i].NumOfMsg < MAX_MSG_LIMIT)  )
2593                            {
2594                                //DEBUG("Dsp broadcast message detected for app id %d\n", i);
2595                                nxtph = FT1000_DPRAM_RX_BASE + 2;
2596                                pdpram_blk = ft1000_get_buffer (&freercvpool);
2597                                if (pdpram_blk != NULL) {
2598                                    if ( ft1000_receive_cmd(dev, pdpram_blk->pbuffer, MAX_CMD_SQSIZE, &nxtph) ) {
2599                                        ppseudo_hdr = (PPSEUDO_HDR)pdpram_blk->pbuffer;
2600                                        // Put message into the appropriate application block
2601                                        info->app_info[i].nRxMsg++;
2602                                        spin_lock_irqsave(&free_buff_lock, flags);
2603                                        list_add_tail(&pdpram_blk->list, &info->app_info[i].app_sqlist);
2604                                        info->app_info[i].NumOfMsg++;
2605                                        spin_unlock_irqrestore(&free_buff_lock, flags);
2606                                        wake_up_interruptible(&info->app_info[i].wait_dpram_msg);
2607                                    }
2608                                    else {
2609                                        info->app_info[i].nRxMsgMiss++;
2610                                        // Put memory back to free pool
2611                                        ft1000_free_buffer(pdpram_blk, &freercvpool);
2612                                        DEBUG("pdpram_blk::ft1000_get_buffer NULL\n");
2613                                    }
2614                                }
2615                                else {
2616                                    DEBUG("Out of memory in free receive command pool\n");
2617                                    info->app_info[i].nRxMsgMiss++;
2618                                }//endof if (pdpram_blk != NULL)
2619                            }//endof if
2620                            //else
2621                            //    DEBUG("app_info mismatch\n");
2622                         }// endof for
2623                         break;
2624                     default:
2625                         pdpram_blk = ft1000_get_buffer (&freercvpool);
2626                         //DEBUG("Memory allocated = 0x%8x\n", (u32)pdpram_blk);
2627                         if (pdpram_blk != NULL) {
2628                            if ( ft1000_receive_cmd(dev, pdpram_blk->pbuffer, MAX_CMD_SQSIZE, &nxtph) ) {
2629                                ppseudo_hdr = (PPSEUDO_HDR)pdpram_blk->pbuffer;
2630                                // Search for correct application block
2631                                for (i=0; i<MAX_NUM_APP; i++) {
2632                                    if (info->app_info[i].app_id == ppseudo_hdr->portdest) {
2633                                        break;
2634                                    }
2635                                }
2636
2637                                if (i==(MAX_NUM_APP-1)) {                // aelias [+] reason: was out of array boundary
2638                                    info->app_info[i].nRxMsgMiss++;
2639                                    DEBUG("FT1000:ft1000_parse_dpram_msg: No application matching id = %d\n", ppseudo_hdr->portdest);
2640                                    // Put memory back to free pool
2641                                    ft1000_free_buffer(pdpram_blk, &freercvpool);
2642                                }
2643                                else {
2644                                    if (info->app_info[i].NumOfMsg > MAX_MSG_LIMIT) {
2645                                        // Put memory back to free pool
2646                                        ft1000_free_buffer(pdpram_blk, &freercvpool);
2647                                    }
2648                                    else {
2649                                        info->app_info[i].nRxMsg++;
2650                                        // Put message into the appropriate application block
2651                                        //pxu spin_lock_irqsave(&free_buff_lock, flags);
2652                                        list_add_tail(&pdpram_blk->list, &info->app_info[i].app_sqlist);
2653                                        info->app_info[i].NumOfMsg++;
2654                                        //pxu spin_unlock_irqrestore(&free_buff_lock, flags);
2655                                        //pxu wake_up_interruptible(&info->app_info[i].wait_dpram_msg);
2656                                    }
2657                                }
2658                            }
2659                            else {
2660                                // Put memory back to free pool
2661                                ft1000_free_buffer(pdpram_blk, &freercvpool);
2662                            }
2663                         }
2664                         else {
2665                             DEBUG("Out of memory in free receive command pool\n");
2666                         }
2667                         break;
2668                 } //end of switch
2669             } //endof if (size < MAX_CMD_SQSIZE)
2670             else {
2671                 DEBUG("FT1000:dpc:Invalid total length for SlowQ = %d\n", size);
2672             }
2673             status = ft1000_write_register (dev, FT1000_DB_DPRAM_RX, FT1000_REG_DOORBELL);
2674         }
2675         else if (tempword & FT1000_DSP_ASIC_RESET) {
2676             //DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type:  FT1000_DSP_ASIC_RESET\n");
2677
2678             // Let's reset the ASIC from the Host side as well
2679             status = ft1000_write_register (dev, ASIC_RESET_BIT, FT1000_REG_RESET);
2680             status = ft1000_read_register (dev, &tempword, FT1000_REG_RESET);
2681             i = 0;
2682             while (tempword & ASIC_RESET_BIT) {
2683                 status = ft1000_read_register (dev, &tempword, FT1000_REG_RESET);
2684                 msleep(10);
2685                 i++;
2686                 if (i==100)
2687                     break;
2688             }
2689             if (i==100) {
2690                 DEBUG("Unable to reset ASIC\n");
2691                 return STATUS_SUCCESS;
2692             }
2693             msleep(10);
2694             // Program WMARK register
2695             status = ft1000_write_register (dev, 0x600, FT1000_REG_MAG_WATERMARK);
2696             // clear ASIC reset doorbell
2697             status = ft1000_write_register (dev, FT1000_DSP_ASIC_RESET, FT1000_REG_DOORBELL);
2698             msleep(10);
2699         }
2700         else if (tempword & FT1000_ASIC_RESET_REQ) {
2701             DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type:  FT1000_ASIC_RESET_REQ\n");
2702
2703             // clear ASIC reset request from DSP
2704             status = ft1000_write_register (dev, FT1000_ASIC_RESET_REQ, FT1000_REG_DOORBELL);
2705             status = ft1000_write_register (dev, HOST_INTF_BE, FT1000_REG_SUP_CTRL);
2706             // copy dsp session record from Adapter block
2707             status = ft1000_write_dpram32 (dev, 0, (PUCHAR)&info->DSPSess.Rec[0], 1024);
2708             // Program WMARK register
2709             status = ft1000_write_register (dev, 0x600, FT1000_REG_MAG_WATERMARK);
2710             // ring doorbell to tell DSP that ASIC is out of reset
2711             status = ft1000_write_register (dev, FT1000_ASIC_RESET_DSP, FT1000_REG_DOORBELL);
2712         }
2713         else if (tempword & FT1000_DB_COND_RESET) {
2714             DEBUG("ft1000_poll: FT1000_REG_DOORBELL message type:  FT1000_DB_COND_RESET\n");
2715 //By Jim
2716 // Reset ASIC and DSP
2717 //MAG
2718             if (info->fAppMsgPend == 0) {
2719                // Reset ASIC and DSP
2720
2721                 status    = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER0, (PUCHAR)&(info->DSP_TIME[0]), FT1000_MAG_DSP_TIMER0_INDX);
2722                 status    = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER1, (PUCHAR)&(info->DSP_TIME[1]), FT1000_MAG_DSP_TIMER1_INDX);
2723                 status    = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER2, (PUCHAR)&(info->DSP_TIME[2]), FT1000_MAG_DSP_TIMER2_INDX);
2724                 status    = ft1000_read_dpram16(dev, FT1000_MAG_DSP_TIMER3, (PUCHAR)&(info->DSP_TIME[3]), FT1000_MAG_DSP_TIMER3_INDX);
2725                 info->CardReady = 0;
2726                 info->DrvErrNum = DSP_CONDRESET_INFO;
2727                 DEBUG("ft1000_hw:DSP conditional reset requested\n");
2728                 info->ft1000_reset(dev->net);
2729             }
2730             else {
2731                 info->fProvComplete = 0;
2732                 info->fCondResetPend = 1;
2733             }
2734
2735             ft1000_write_register(dev, FT1000_DB_COND_RESET, FT1000_REG_DOORBELL);
2736         }
2737
2738     }//endof if ( !status )
2739
2740     //DEBUG("return from ft1000_poll.\n");
2741     return STATUS_SUCCESS;
2742
2743 }
2744
2745 /*end of Jim*/