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