1 //---------------------------------------------------------------------------
2 // FT1000 driver for Flarion Flash OFDM NIC Device
4 // Copyright (C) 2006 Flarion Technologies, All rights reserved.
6 // This program is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by the Free
8 // Software Foundation; either version 2 of the License, or (at your option) any
9 // later version. This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details. You should have received a copy of the GNU General Public
13 // License along with this program; if not, write to the
14 // Free Software Foundation, Inc., 59 Temple Place -
15 // Suite 330, Boston, MA 02111-1307, USA.
16 //---------------------------------------------------------------------------
18 // File: ft1000_chdev.c
20 // Description: Custom character device dispatch routines.
23 // 8/29/02 Whc Ported to Linux.
24 // 6/05/06 Whc Porting to Linux 2.6.9
26 //---------------------------------------------------------------------------
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/poll.h>
32 #include <linux/netdevice.h>
33 #include <linux/delay.h>
35 #include <linux/ioctl.h>
36 #include <linux/debugfs.h>
37 #include "ft1000_usb.h"
39 static int ft1000_flarion_cnt = 0;
41 static int ft1000_open (struct inode *inode, struct file *file);
42 static unsigned int ft1000_poll_dev(struct file *file, poll_table *wait);
43 static long ft1000_ioctl(struct file *file, unsigned int command,
44 unsigned long argument);
45 static int ft1000_release (struct inode *inode, struct file *file);
47 // List to free receive command buffer pool
48 struct list_head freercvpool;
50 // lock to arbitrate free buffer list for receive command data
51 spinlock_t free_buff_lock;
56 // Table of entry-point routines for char device
58 static struct file_operations ft1000fops =
60 .unlocked_ioctl = ft1000_ioctl,
61 .poll = ft1000_poll_dev,
63 .release = ft1000_release,
67 //---------------------------------------------------------------------------
68 // Function: ft1000_get_buffer
78 //---------------------------------------------------------------------------
79 struct dpram_blk *ft1000_get_buffer(struct list_head *bufflist)
82 struct dpram_blk *ptr;
84 spin_lock_irqsave(&free_buff_lock, flags);
85 // Check if buffer is available
86 if ( list_empty(bufflist) ) {
87 DEBUG("ft1000_get_buffer: No more buffer - %d\n", numofmsgbuf);
92 ptr = list_entry(bufflist->next, struct dpram_blk, list);
94 //DEBUG("ft1000_get_buffer: number of free msg buffers = %d\n", numofmsgbuf);
96 spin_unlock_irqrestore(&free_buff_lock, flags);
104 //---------------------------------------------------------------------------
105 // Function: ft1000_free_buffer
115 //---------------------------------------------------------------------------
116 void ft1000_free_buffer(struct dpram_blk *pdpram_blk, struct list_head *plist)
120 spin_lock_irqsave(&free_buff_lock, flags);
121 // Put memory back to list
122 list_add_tail(&pdpram_blk->list, plist);
124 //DEBUG("ft1000_free_buffer: number of free msg buffers = %d\n", numofmsgbuf);
125 spin_unlock_irqrestore(&free_buff_lock, flags);
128 //---------------------------------------------------------------------------
129 // Function: ft1000_CreateDevice
131 // Parameters: dev - pointer to adapter object
133 // Returns: 0 if successful
135 // Description: Creates a private char device.
137 // Notes: Only called by init_module().
139 //---------------------------------------------------------------------------
140 int ft1000_create_dev(struct ft1000_device *dev)
142 struct ft1000_info *info = netdev_priv(dev->net);
145 struct dentry *dir, *file;
146 struct ft1000_debug_dirs *tmp;
148 // make a new device name
149 sprintf(info->DeviceName, "%s%d", "FT1000_", info->CardNumber);
151 DEBUG("%s: number of instance = %d\n", __func__, ft1000_flarion_cnt);
152 DEBUG("DeviceCreated = %x\n", info->DeviceCreated);
154 if (info->DeviceCreated)
156 DEBUG("%s: \"%s\" already registered\n", __func__, info->DeviceName);
161 // register the device
162 DEBUG("%s: \"%s\" debugfs device registration\n", __func__, info->DeviceName);
164 tmp = kmalloc(sizeof(struct ft1000_debug_dirs), GFP_KERNEL);
170 dir = debugfs_create_dir(info->DeviceName, 0);
172 result = PTR_ERR(dir);
176 file = debugfs_create_file("device", S_IRUGO | S_IWUSR, dir,
179 result = PTR_ERR(file);
180 goto debug_file_fail;
185 tmp->int_number = info->CardNumber;
186 list_add(&(tmp->list), &(info->nodes.list));
188 DEBUG("%s: registered debugfs directory \"%s\"\n", __func__, info->DeviceName);
190 // initialize application information
192 for (i=0; i<MAX_NUM_APP; i++) {
193 info->app_info[i].nTxMsg = 0;
194 info->app_info[i].nRxMsg = 0;
195 info->app_info[i].nTxMsgReject = 0;
196 info->app_info[i].nRxMsgMiss = 0;
197 info->app_info[i].fileobject = NULL;
198 info->app_info[i].app_id = i+1;
199 info->app_info[i].DspBCMsgFlag = 0;
200 info->app_info[i].NumOfMsg = 0;
201 init_waitqueue_head(&info->app_info[i].wait_dpram_msg);
202 INIT_LIST_HEAD (&info->app_info[i].app_sqlist);
205 info->DeviceCreated = TRUE;
206 ft1000_flarion_cnt++;
218 //---------------------------------------------------------------------------
219 // Function: ft1000_DestroyDeviceDEBUG
221 // Parameters: dev - pointer to adapter object
223 // Description: Destroys a private char device.
225 // Notes: Only called by cleanup_module().
227 //---------------------------------------------------------------------------
228 void ft1000_destroy_dev(struct net_device *dev)
230 struct ft1000_info *info = netdev_priv(dev);
232 struct dpram_blk *pdpram_blk;
233 struct dpram_blk *ptr;
234 struct list_head *pos, *q;
235 struct ft1000_debug_dirs *dir;
237 DEBUG("%s called\n", __func__);
241 if (info->DeviceCreated)
243 ft1000_flarion_cnt--;
244 list_for_each_safe(pos, q, &info->nodes.list) {
245 dir = list_entry(pos, struct ft1000_debug_dirs, list);
246 if (dir->int_number == info->CardNumber) {
247 debugfs_remove(dir->file);
248 debugfs_remove(dir->dent);
253 DEBUG("%s: unregistered device \"%s\"\n", __func__,
256 // Make sure we free any memory reserve for slow Queue
257 for (i=0; i<MAX_NUM_APP; i++) {
258 while (list_empty(&info->app_info[i].app_sqlist) == 0) {
259 pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
260 list_del(&pdpram_blk->list);
261 ft1000_free_buffer(pdpram_blk, &freercvpool);
264 wake_up_interruptible(&info->app_info[i].wait_dpram_msg);
267 // Remove buffer allocated for receive command data
268 if (ft1000_flarion_cnt == 0) {
269 while (list_empty(&freercvpool) == 0) {
270 ptr = list_entry(freercvpool.next, struct dpram_blk, list);
271 list_del(&ptr->list);
276 info->DeviceCreated = FALSE;
282 //---------------------------------------------------------------------------
283 // Function: ft1000_open
291 //---------------------------------------------------------------------------
292 static int ft1000_open (struct inode *inode, struct file *file)
294 struct ft1000_info *info;
295 struct ft1000_device *dev = (struct ft1000_device *)inode->i_private;
298 DEBUG("%s called\n", __func__);
299 num = (MINOR(inode->i_rdev) & 0xf);
300 DEBUG("ft1000_open: minor number=%d\n", num);
302 info = file->private_data = netdev_priv(dev->net);
304 DEBUG("f_owner = %p number of application = %d\n", (&file->f_owner), info->appcnt );
306 // Check if maximum number of application exceeded
307 if (info->appcnt > MAX_NUM_APP) {
308 DEBUG("Maximum number of application exceeded\n");
312 // Search for available application info block
313 for (i=0; i<MAX_NUM_APP; i++) {
314 if ( (info->app_info[i].fileobject == NULL) ) {
319 // Fail due to lack of application info block
320 if (i == MAX_NUM_APP) {
321 DEBUG("Could not find an application info block\n");
326 info->app_info[i].fileobject = &file->f_owner;
327 info->app_info[i].nTxMsg = 0;
328 info->app_info[i].nRxMsg = 0;
329 info->app_info[i].nTxMsgReject = 0;
330 info->app_info[i].nRxMsgMiss = 0;
332 nonseekable_open(inode, file);
337 //---------------------------------------------------------------------------
338 // Function: ft1000_poll_dev
346 //---------------------------------------------------------------------------
348 static unsigned int ft1000_poll_dev(struct file *file, poll_table *wait)
350 struct net_device *dev = file->private_data;
351 struct ft1000_info *info;
354 //DEBUG("ft1000_poll_dev called\n");
355 if (ft1000_flarion_cnt == 0) {
356 DEBUG("FT1000:ft1000_poll_dev called when ft1000_flarion_cnt is zero\n");
360 info = netdev_priv(dev);
362 // Search for matching file object
363 for (i=0; i<MAX_NUM_APP; i++) {
364 if ( info->app_info[i].fileobject == &file->f_owner) {
365 //DEBUG("FT1000:ft1000_ioctl: Message is for AppId = %d\n", info->app_info[i].app_id);
370 // Could not find application info block
371 if (i == MAX_NUM_APP) {
372 DEBUG("FT1000:ft1000_ioctl:Could not find application info block\n");
376 if (list_empty(&info->app_info[i].app_sqlist) == 0) {
377 DEBUG("FT1000:ft1000_poll_dev:Message detected in slow queue\n");
378 return(POLLIN | POLLRDNORM | POLLPRI);
381 poll_wait (file, &info->app_info[i].wait_dpram_msg, wait);
382 //DEBUG("FT1000:ft1000_poll_dev:Polling for data from DSP\n");
387 //---------------------------------------------------------------------------
388 // Function: ft1000_ioctl
396 //---------------------------------------------------------------------------
397 static long ft1000_ioctl (struct file *file, unsigned int command,
398 unsigned long argument)
400 void __user *argp = (void __user *)argument;
401 struct ft1000_info *info;
402 struct ft1000_device *ft1000dev;
409 IOCTL_GET_VER get_ver_data;
410 IOCTL_GET_DSP_STAT get_stat_data;
411 u8 ConnectionMsg[] = {0x00,0x44,0x10,0x20,0x80,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x93,0x64,
412 0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0a,
413 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
414 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
415 0x00,0x00,0x02,0x37,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x01,0x00,0x01,0x7f,0x00,
416 0x00,0x01,0x00,0x00};
418 unsigned short ledStat=0;
419 unsigned short conStat=0;
421 //DEBUG("ft1000_ioctl called\n");
423 if (ft1000_flarion_cnt == 0) {
424 DEBUG("FT1000:ft1000_ioctl called when ft1000_flarion_cnt is zero\n");
428 //DEBUG("FT1000:ft1000_ioctl:command = 0x%x argument = 0x%8x\n", command, (u32)argument);
430 info = file->private_data;
431 ft1000dev = info->pFt1000Dev;
432 cmd = _IOC_NR(command);
433 //DEBUG("FT1000:ft1000_ioctl:cmd = 0x%x\n", cmd);
435 // process the command
437 case IOCTL_REGISTER_CMD:
438 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_REGISTER called\n");
439 result = get_user(tempword, (__u16 __user*)argp);
441 DEBUG("result = %d failed to get_user\n", result);
444 if (tempword == DSPBCMSGID) {
445 // Search for matching file object
446 for (i=0; i<MAX_NUM_APP; i++) {
447 if ( info->app_info[i].fileobject == &file->f_owner) {
448 info->app_info[i].DspBCMsgFlag = 1;
449 DEBUG("FT1000:ft1000_ioctl:Registered for broadcast messages\n");
456 case IOCTL_GET_VER_CMD:
457 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_GET_VER called\n");
459 get_ver_data.drv_ver = FT1000_DRV_VER;
461 if (copy_to_user(argp, &get_ver_data, sizeof(get_ver_data)) ) {
462 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
467 DEBUG("FT1000:ft1000_ioctl:driver version = 0x%x\n",(unsigned int)get_ver_data.drv_ver);
472 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_CONNECT\n");
473 ConnectionMsg[79] = 0xfc;
474 card_send_command(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
477 case IOCTL_DISCONNECT:
478 // Disconnect Message
479 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_DISCONNECT\n");
480 ConnectionMsg[79] = 0xfd;
481 card_send_command(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
483 case IOCTL_GET_DSP_STAT_CMD:
484 //DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_GET_DSP_STAT called\n");
485 memset(&get_stat_data, 0, sizeof(get_stat_data));
486 memcpy(get_stat_data.DspVer, info->DspVer, DSPVERSZ);
487 memcpy(get_stat_data.HwSerNum, info->HwSerNum, HWSERNUMSZ);
488 memcpy(get_stat_data.Sku, info->Sku, SKUSZ);
489 memcpy(get_stat_data.eui64, info->eui64, EUISZ);
491 if (info->ProgConStat != 0xFF) {
492 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_LED, (u8 *)&ledStat, FT1000_MAG_DSP_LED_INDX);
493 get_stat_data.LedStat = ntohs(ledStat);
494 DEBUG("FT1000:ft1000_ioctl: LedStat = 0x%x\n", get_stat_data.LedStat);
495 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_CON_STATE, (u8 *)&conStat, FT1000_MAG_DSP_CON_STATE_INDX);
496 get_stat_data.ConStat = ntohs(conStat);
497 DEBUG("FT1000:ft1000_ioctl: ConStat = 0x%x\n", get_stat_data.ConStat);
500 get_stat_data.ConStat = 0x0f;
504 get_stat_data.nTxPkts = info->stats.tx_packets;
505 get_stat_data.nRxPkts = info->stats.rx_packets;
506 get_stat_data.nTxBytes = info->stats.tx_bytes;
507 get_stat_data.nRxBytes = info->stats.rx_bytes;
508 do_gettimeofday ( &tv );
509 get_stat_data.ConTm = (u32)(tv.tv_sec - info->ConTm);
510 DEBUG("Connection Time = %d\n", (int)get_stat_data.ConTm);
511 if (copy_to_user(argp, &get_stat_data, sizeof(get_stat_data)) ) {
512 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
516 DEBUG("ft1000_chioctl: GET_DSP_STAT succeed\n");
518 case IOCTL_SET_DPRAM_CMD:
520 IOCTL_DPRAM_BLK *dpram_data = NULL;
521 //IOCTL_DPRAM_COMMAND dpram_command;
524 struct pseudo_hdr *ppseudo_hdr;
530 //DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_SET_DPRAM called\n");
533 if (ft1000_flarion_cnt == 0) {
537 if (info->DrvMsgPend) {
541 if ( (info->DspAsicReset) || (info->fProvComplete == 0) ) {
545 info->fAppMsgPend = 1;
547 if (info->CardReady) {
549 //DEBUG("FT1000:ft1000_ioctl: try to SET_DPRAM \n");
551 // Get the length field to see how many bytes to copy
552 result = get_user(msgsz, (__u16 __user *)argp);
553 msgsz = ntohs (msgsz);
554 //DEBUG("FT1000:ft1000_ioctl: length of message = %d\n", msgsz);
556 if (msgsz > MAX_CMD_SQSIZE) {
557 DEBUG("FT1000:ft1000_ioctl: bad message length = %d\n", msgsz);
563 dpram_data = kmalloc(msgsz + 2, GFP_KERNEL);
567 if ( copy_from_user(dpram_data, argp, msgsz+2) ) {
568 DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
572 // Check if this message came from a registered application
573 for (i=0; i<MAX_NUM_APP; i++) {
574 if ( info->app_info[i].fileobject == &file->f_owner) {
578 if (i==MAX_NUM_APP) {
579 DEBUG("FT1000:No matching application fileobject\n");
586 // Check message qtype type which is the lower byte within qos_class
587 qtype = ntohs(dpram_data->pseudohdr.qos_class) & 0xff;
588 //DEBUG("FT1000_ft1000_ioctl: qtype = %d\n", qtype);
592 // Put message into Slow Queue
593 // Only put a message into the DPRAM if msg doorbell is available
594 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
595 //DEBUG("FT1000_ft1000_ioctl: READ REGISTER tempword=%x\n", tempword);
596 if (tempword & FT1000_DB_DPRAM_TX) {
597 // Suspend for 2ms and try again due to DSP doorbell busy
599 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
600 if (tempword & FT1000_DB_DPRAM_TX) {
601 // Suspend for 1ms and try again due to DSP doorbell busy
603 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
604 if (tempword & FT1000_DB_DPRAM_TX) {
605 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
606 if (tempword & FT1000_DB_DPRAM_TX) {
607 // Suspend for 3ms and try again due to DSP doorbell busy
609 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
610 if (tempword & FT1000_DB_DPRAM_TX) {
611 DEBUG("FT1000:ft1000_ioctl:Doorbell not available\n");
621 //DEBUG("FT1000_ft1000_ioctl: finished reading register\n");
623 // Make sure we are within the limits of the slow queue memory limitation
624 if ( (msgsz < MAX_CMD_SQSIZE) && (msgsz > PSEUDOSZ) ) {
625 // Need to put sequence number plus new checksum for message
626 pmsg = (u16 *)&dpram_data->pseudohdr;
627 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
629 if (total_len & 0x1) {
633 // Insert slow queue sequence number
634 ppseudo_hdr->seq_num = info->squeseqnum++;
635 ppseudo_hdr->portsrc = info->app_info[app_index].app_id;
636 // Calculate new checksum
637 ppseudo_hdr->checksum = *pmsg++;
638 //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
639 for (i=1; i<7; i++) {
640 ppseudo_hdr->checksum ^= *pmsg++;
641 //DEBUG("checksum = 0x%x\n", ppseudo_hdr->checksum);
644 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
645 card_send_command(ft1000dev,(unsigned short*)dpram_data,total_len+2);
648 info->app_info[app_index].nTxMsg++;
657 DEBUG("FT1000:ft1000_ioctl: Card not ready take messages\n");
664 case IOCTL_GET_DPRAM_CMD:
666 struct dpram_blk *pdpram_blk;
667 IOCTL_DPRAM_BLK __user *pioctl_dpram;
670 //DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_GET_DPRAM called\n");
672 if (ft1000_flarion_cnt == 0) {
676 // Search for matching file object
677 for (i=0; i<MAX_NUM_APP; i++) {
678 if ( info->app_info[i].fileobject == &file->f_owner) {
679 //DEBUG("FT1000:ft1000_ioctl: Message is for AppId = %d\n", info->app_info[i].app_id);
684 // Could not find application info block
685 if (i == MAX_NUM_APP) {
686 DEBUG("FT1000:ft1000_ioctl:Could not find application info block\n");
693 if (list_empty(&info->app_info[i].app_sqlist) == 0) {
694 //DEBUG("FT1000:ft1000_ioctl:Message detected in slow queue\n");
695 spin_lock_irqsave(&free_buff_lock, flags);
696 pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
697 list_del(&pdpram_blk->list);
698 info->app_info[i].NumOfMsg--;
699 //DEBUG("FT1000:ft1000_ioctl:NumOfMsg for app %d = %d\n", i, info->app_info[i].NumOfMsg);
700 spin_unlock_irqrestore(&free_buff_lock, flags);
701 msglen = ntohs(*(u16 *)pdpram_blk->pbuffer) + PSEUDOSZ;
702 result = get_user(msglen, &pioctl_dpram->total_len);
705 msglen = htons(msglen);
706 //DEBUG("FT1000:ft1000_ioctl:msg length = %x\n", msglen);
707 if(copy_to_user (&pioctl_dpram->pseudohdr, pdpram_blk->pbuffer, msglen))
709 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
714 ft1000_free_buffer(pdpram_blk, &freercvpool);
717 //DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_GET_DPRAM no message\n");
722 DEBUG("FT1000:ft1000_ioctl:unknown command: 0x%x\n", command);
726 info->fAppMsgPend = 0;
730 //---------------------------------------------------------------------------
731 // Function: ft1000_release
739 //---------------------------------------------------------------------------
740 static int ft1000_release (struct inode *inode, struct file *file)
742 struct ft1000_info *info;
743 struct net_device *dev;
745 struct dpram_blk *pdpram_blk;
747 DEBUG("ft1000_release called\n");
749 dev = file->private_data;
750 info = netdev_priv(dev);
752 if (ft1000_flarion_cnt == 0) {
757 // Search for matching file object
758 for (i=0; i<MAX_NUM_APP; i++) {
759 if ( info->app_info[i].fileobject == &file->f_owner) {
760 //DEBUG("FT1000:ft1000_ioctl: Message is for AppId = %d\n", info->app_info[i].app_id);
768 while (list_empty(&info->app_info[i].app_sqlist) == 0) {
769 DEBUG("Remove and free memory queue up on slow queue\n");
770 pdpram_blk = list_entry(info->app_info[i].app_sqlist.next, struct dpram_blk, list);
771 list_del(&pdpram_blk->list);
772 ft1000_free_buffer(pdpram_blk, &freercvpool);
775 // initialize application information
777 DEBUG("ft1000_chdev:%s:appcnt = %d\n", __FUNCTION__, info->appcnt);
778 info->app_info[i].fileobject = NULL;