1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>
34
35#include <linux/ioctl.h>
36#include <linux/debugfs.h>
37#include "ft1000_usb.h"
38
39static int ft1000_flarion_cnt = 0;
40
41static int ft1000_open (struct inode *inode, struct file *file);
42static unsigned int ft1000_poll_dev(struct file *file, poll_table *wait);
43static long ft1000_ioctl(struct file *file, unsigned int command,
44 unsigned long argument);
45static int ft1000_release (struct inode *inode, struct file *file);
46
47
48struct list_head freercvpool;
49
50
51spinlock_t free_buff_lock;
52
53int numofmsgbuf = 0;
54
55
56
57
58static const struct file_operations ft1000fops =
59{
60 .unlocked_ioctl = ft1000_ioctl,
61 .poll = ft1000_poll_dev,
62 .open = ft1000_open,
63 .release = ft1000_release,
64 .llseek = no_llseek,
65};
66
67
68
69
70
71
72
73
74
75
76
77
78
79struct dpram_blk *ft1000_get_buffer(struct list_head *bufflist)
80{
81 unsigned long flags;
82 struct dpram_blk *ptr;
83
84 spin_lock_irqsave(&free_buff_lock, flags);
85
86 if ( list_empty(bufflist) ) {
87 DEBUG("ft1000_get_buffer: No more buffer - %d\n", numofmsgbuf);
88 ptr = NULL;
89 }
90 else {
91 numofmsgbuf--;
92 ptr = list_entry(bufflist->next, struct dpram_blk, list);
93 list_del(&ptr->list);
94
95 }
96 spin_unlock_irqrestore(&free_buff_lock, flags);
97
98 return ptr;
99}
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116void ft1000_free_buffer(struct dpram_blk *pdpram_blk, struct list_head *plist)
117{
118 unsigned long flags;
119
120 spin_lock_irqsave(&free_buff_lock, flags);
121
122 list_add_tail(&pdpram_blk->list, plist);
123 numofmsgbuf++;
124
125 spin_unlock_irqrestore(&free_buff_lock, flags);
126}
127
128
129
130
131
132
133
134
135
136
137
138
139
140int ft1000_create_dev(struct ft1000_usb *dev)
141{
142 int result;
143 int i;
144 struct dentry *dir, *file;
145 struct ft1000_debug_dirs *tmp;
146
147
148 sprintf(dev->DeviceName, "%s%d", "FT1000_", dev->CardNumber);
149
150 DEBUG("%s: number of instance = %d\n", __func__, ft1000_flarion_cnt);
151 DEBUG("DeviceCreated = %x\n", dev->DeviceCreated);
152
153 if (dev->DeviceCreated)
154 {
155 DEBUG("%s: \"%s\" already registered\n", __func__, dev->DeviceName);
156 return -EIO;
157 }
158
159
160
161 DEBUG("%s: \"%s\" debugfs device registration\n", __func__, dev->DeviceName);
162
163 tmp = kmalloc(sizeof(struct ft1000_debug_dirs), GFP_KERNEL);
164 if (tmp == NULL) {
165 result = -1;
166 goto fail;
167 }
168
169 dir = debugfs_create_dir(dev->DeviceName, NULL);
170 if (IS_ERR(dir)) {
171 result = PTR_ERR(dir);
172 goto debug_dir_fail;
173 }
174
175 file = debugfs_create_file("device", S_IRUGO | S_IWUSR, dir,
176 dev, &ft1000fops);
177 if (IS_ERR(file)) {
178 result = PTR_ERR(file);
179 goto debug_file_fail;
180 }
181
182 tmp->dent = dir;
183 tmp->file = file;
184 tmp->int_number = dev->CardNumber;
185 list_add(&(tmp->list), &(dev->nodes.list));
186
187 DEBUG("%s: registered debugfs directory \"%s\"\n", __func__, dev->DeviceName);
188
189
190 dev->appcnt = 0;
191 for (i=0; i<MAX_NUM_APP; i++) {
192 dev->app_info[i].nTxMsg = 0;
193 dev->app_info[i].nRxMsg = 0;
194 dev->app_info[i].nTxMsgReject = 0;
195 dev->app_info[i].nRxMsgMiss = 0;
196 dev->app_info[i].fileobject = NULL;
197 dev->app_info[i].app_id = i+1;
198 dev->app_info[i].DspBCMsgFlag = 0;
199 dev->app_info[i].NumOfMsg = 0;
200 init_waitqueue_head(&dev->app_info[i].wait_dpram_msg);
201 INIT_LIST_HEAD (&dev->app_info[i].app_sqlist);
202 }
203
204 dev->DeviceCreated = TRUE;
205 ft1000_flarion_cnt++;
206
207 return 0;
208
209debug_file_fail:
210 debugfs_remove(dir);
211debug_dir_fail:
212 kfree(tmp);
213fail:
214 return result;
215}
216
217
218
219
220
221
222
223
224
225
226
227void ft1000_destroy_dev(struct net_device *netdev)
228{
229 struct ft1000_info *info = netdev_priv(netdev);
230 struct ft1000_usb *dev = info->priv;
231 int i;
232 struct dpram_blk *pdpram_blk;
233 struct dpram_blk *ptr;
234 struct list_head *pos, *q;
235 struct ft1000_debug_dirs *dir;
236
237 DEBUG("%s called\n", __func__);
238
239
240
241 if (dev->DeviceCreated)
242 {
243 ft1000_flarion_cnt--;
244 list_for_each_safe(pos, q, &dev->nodes.list) {
245 dir = list_entry(pos, struct ft1000_debug_dirs, list);
246 if (dir->int_number == dev->CardNumber) {
247 debugfs_remove(dir->file);
248 debugfs_remove(dir->dent);
249 list_del(pos);
250 kfree(dir);
251 }
252 }
253 DEBUG("%s: unregistered device \"%s\"\n", __func__,
254 dev->DeviceName);
255
256
257 for (i=0; i<MAX_NUM_APP; i++) {
258 while (list_empty(&dev->app_info[i].app_sqlist) == 0) {
259 pdpram_blk = list_entry(dev->app_info[i].app_sqlist.next, struct dpram_blk, list);
260 list_del(&pdpram_blk->list);
261 ft1000_free_buffer(pdpram_blk, &freercvpool);
262
263 }
264 wake_up_interruptible(&dev->app_info[i].wait_dpram_msg);
265 }
266
267
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);
272 kfree(ptr->pbuffer);
273 kfree(ptr);
274 }
275 }
276 dev->DeviceCreated = FALSE;
277 }
278
279
280}
281
282
283
284
285
286
287
288
289
290
291
292static int ft1000_open (struct inode *inode, struct file *file)
293{
294 struct ft1000_info *info;
295 struct ft1000_usb *dev = (struct ft1000_usb *)inode->i_private;
296 int i,num;
297
298 DEBUG("%s called\n", __func__);
299 num = (MINOR(inode->i_rdev) & 0xf);
300 DEBUG("ft1000_open: minor number=%d\n", num);
301
302 info = file->private_data = netdev_priv(dev->net);
303
304 DEBUG("f_owner = %p number of application = %d\n", (&file->f_owner), dev->appcnt );
305
306
307 if (dev->appcnt > MAX_NUM_APP) {
308 DEBUG("Maximum number of application exceeded\n");
309 return -EACCES;
310 }
311
312
313 for (i=0; i<MAX_NUM_APP; i++) {
314 if ( (dev->app_info[i].fileobject == NULL) ) {
315 break;
316 }
317 }
318
319
320 if (i == MAX_NUM_APP) {
321 DEBUG("Could not find an application info block\n");
322 return -EACCES;
323 }
324
325 dev->appcnt++;
326 dev->app_info[i].fileobject = &file->f_owner;
327 dev->app_info[i].nTxMsg = 0;
328 dev->app_info[i].nRxMsg = 0;
329 dev->app_info[i].nTxMsgReject = 0;
330 dev->app_info[i].nRxMsgMiss = 0;
331
332 nonseekable_open(inode, file);
333 return 0;
334}
335
336
337
338
339
340
341
342
343
344
345
346
347
348static unsigned int ft1000_poll_dev(struct file *file, poll_table *wait)
349{
350 struct net_device *netdev = file->private_data;
351 struct ft1000_info *info = netdev_priv(netdev);
352 struct ft1000_usb *dev = info->priv;
353 int i;
354
355
356 if (ft1000_flarion_cnt == 0) {
357 DEBUG("FT1000:ft1000_poll_dev called when ft1000_flarion_cnt is zero\n");
358 return (-EBADF);
359 }
360
361
362 for (i=0; i<MAX_NUM_APP; i++) {
363 if ( dev->app_info[i].fileobject == &file->f_owner) {
364
365 break;
366 }
367 }
368
369
370 if (i == MAX_NUM_APP) {
371 DEBUG("FT1000:ft1000_ioctl:Could not find application info block\n");
372 return ( -EACCES );
373 }
374
375 if (list_empty(&dev->app_info[i].app_sqlist) == 0) {
376 DEBUG("FT1000:ft1000_poll_dev:Message detected in slow queue\n");
377 return(POLLIN | POLLRDNORM | POLLPRI);
378 }
379
380 poll_wait (file, &dev->app_info[i].wait_dpram_msg, wait);
381
382
383 return (0);
384}
385
386
387
388
389
390
391
392
393
394
395
396static long ft1000_ioctl (struct file *file, unsigned int command,
397 unsigned long argument)
398{
399 void __user *argp = (void __user *)argument;
400 struct ft1000_info *info;
401 struct ft1000_usb *ft1000dev;
402 int result=0;
403 int cmd;
404 int i;
405 u16 tempword;
406 unsigned long flags;
407 struct timeval tv;
408 IOCTL_GET_VER get_ver_data;
409 IOCTL_GET_DSP_STAT get_stat_data;
410 u8 ConnectionMsg[] = {0x00,0x44,0x10,0x20,0x80,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x93,0x64,
411 0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0a,
412 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
413 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
414 0x00,0x00,0x02,0x37,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x01,0x00,0x01,0x7f,0x00,
415 0x00,0x01,0x00,0x00};
416
417 unsigned short ledStat=0;
418 unsigned short conStat=0;
419
420
421
422 if (ft1000_flarion_cnt == 0) {
423 DEBUG("FT1000:ft1000_ioctl called when ft1000_flarion_cnt is zero\n");
424 return (-EBADF);
425 }
426
427
428
429 info = file->private_data;
430 ft1000dev = info->priv;
431 cmd = _IOC_NR(command);
432
433
434
435 switch (cmd) {
436 case IOCTL_REGISTER_CMD:
437 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_REGISTER called\n");
438 result = get_user(tempword, (__u16 __user*)argp);
439 if (result) {
440 DEBUG("result = %d failed to get_user\n", result);
441 break;
442 }
443 if (tempword == DSPBCMSGID) {
444
445 for (i=0; i<MAX_NUM_APP; i++) {
446 if (ft1000dev->app_info[i].fileobject == &file->f_owner) {
447 ft1000dev->app_info[i].DspBCMsgFlag = 1;
448 DEBUG("FT1000:ft1000_ioctl:Registered for broadcast messages\n");
449 break;
450 }
451 }
452 }
453 break;
454
455 case IOCTL_GET_VER_CMD:
456 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_GET_VER called\n");
457
458 get_ver_data.drv_ver = FT1000_DRV_VER;
459
460 if (copy_to_user(argp, &get_ver_data, sizeof(get_ver_data)) ) {
461 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
462 result = -EFAULT;
463 break;
464 }
465
466 DEBUG("FT1000:ft1000_ioctl:driver version = 0x%x\n",(unsigned int)get_ver_data.drv_ver);
467
468 break;
469 case IOCTL_CONNECT:
470
471 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_CONNECT\n");
472 ConnectionMsg[79] = 0xfc;
473 card_send_command(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
474
475 break;
476 case IOCTL_DISCONNECT:
477
478 DEBUG("FT1000:ft1000_ioctl: IOCTL_FT1000_DISCONNECT\n");
479 ConnectionMsg[79] = 0xfd;
480 card_send_command(ft1000dev, (unsigned short *)ConnectionMsg, 0x4c);
481 break;
482 case IOCTL_GET_DSP_STAT_CMD:
483
484 memset(&get_stat_data, 0, sizeof(get_stat_data));
485 memcpy(get_stat_data.DspVer, info->DspVer, DSPVERSZ);
486 memcpy(get_stat_data.HwSerNum, info->HwSerNum, HWSERNUMSZ);
487 memcpy(get_stat_data.Sku, info->Sku, SKUSZ);
488 memcpy(get_stat_data.eui64, info->eui64, EUISZ);
489
490 if (info->ProgConStat != 0xFF) {
491 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_LED, (u8 *)&ledStat, FT1000_MAG_DSP_LED_INDX);
492 get_stat_data.LedStat = ntohs(ledStat);
493 DEBUG("FT1000:ft1000_ioctl: LedStat = 0x%x\n", get_stat_data.LedStat);
494 ft1000_read_dpram16(ft1000dev, FT1000_MAG_DSP_CON_STATE, (u8 *)&conStat, FT1000_MAG_DSP_CON_STATE_INDX);
495 get_stat_data.ConStat = ntohs(conStat);
496 DEBUG("FT1000:ft1000_ioctl: ConStat = 0x%x\n", get_stat_data.ConStat);
497 }
498 else {
499 get_stat_data.ConStat = 0x0f;
500 }
501
502
503 get_stat_data.nTxPkts = info->stats.tx_packets;
504 get_stat_data.nRxPkts = info->stats.rx_packets;
505 get_stat_data.nTxBytes = info->stats.tx_bytes;
506 get_stat_data.nRxBytes = info->stats.rx_bytes;
507 do_gettimeofday ( &tv );
508 get_stat_data.ConTm = (u32)(tv.tv_sec - info->ConTm);
509 DEBUG("Connection Time = %d\n", (int)get_stat_data.ConTm);
510 if (copy_to_user(argp, &get_stat_data, sizeof(get_stat_data)) ) {
511 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
512 result = -EFAULT;
513 break;
514 }
515 DEBUG("ft1000_chioctl: GET_DSP_STAT succeed\n");
516 break;
517 case IOCTL_SET_DPRAM_CMD:
518 {
519 IOCTL_DPRAM_BLK *dpram_data = NULL;
520
521 u16 qtype;
522 u16 msgsz;
523 struct pseudo_hdr *ppseudo_hdr;
524 u16 *pmsg;
525 u16 total_len;
526 u16 app_index;
527 u16 status;
528
529
530
531
532 if (ft1000_flarion_cnt == 0) {
533 return (-EBADF);
534 }
535
536 if (ft1000dev->DrvMsgPend) {
537 return (-ENOTTY);
538 }
539
540 if (ft1000dev->fProvComplete == 0) {
541 return (-EACCES);
542 }
543
544 ft1000dev->fAppMsgPend = 1;
545
546 if (info->CardReady) {
547
548
549
550
551 result = get_user(msgsz, (__u16 __user *)argp);
552 msgsz = ntohs (msgsz);
553
554
555 if (msgsz > MAX_CMD_SQSIZE) {
556 DEBUG("FT1000:ft1000_ioctl: bad message length = %d\n", msgsz);
557 result = -EINVAL;
558 break;
559 }
560
561 result = -ENOMEM;
562 dpram_data = kmalloc(msgsz + 2, GFP_KERNEL);
563 if (!dpram_data)
564 break;
565
566 if ( copy_from_user(dpram_data, argp, msgsz+2) ) {
567 DEBUG("FT1000:ft1000_ChIoctl: copy fault occurred\n");
568 result = -EFAULT;
569 }
570 else {
571
572 for (i=0; i<MAX_NUM_APP; i++) {
573 if (ft1000dev->app_info[i].fileobject == &file->f_owner) {
574 break;
575 }
576 }
577 if (i==MAX_NUM_APP) {
578 DEBUG("FT1000:No matching application fileobject\n");
579 result = -EINVAL;
580 kfree(dpram_data);
581 break;
582 }
583 app_index = i;
584
585
586 qtype = ntohs(dpram_data->pseudohdr.qos_class) & 0xff;
587
588 if (qtype) {
589 }
590 else {
591
592
593 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
594
595 if (tempword & FT1000_DB_DPRAM_TX) {
596
597 mdelay(2);
598 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
599 if (tempword & FT1000_DB_DPRAM_TX) {
600
601 mdelay(1);
602 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
603 if (tempword & FT1000_DB_DPRAM_TX) {
604 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
605 if (tempword & FT1000_DB_DPRAM_TX) {
606
607 mdelay(3);
608 status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_DOORBELL);
609 if (tempword & FT1000_DB_DPRAM_TX) {
610 DEBUG("FT1000:ft1000_ioctl:Doorbell not available\n");
611 result = -ENOTTY;
612 kfree(dpram_data);
613 break;
614 }
615 }
616 }
617 }
618 }
619
620
621
622
623 if ( (msgsz < MAX_CMD_SQSIZE) && (msgsz > PSEUDOSZ) ) {
624
625 pmsg = (u16 *)&dpram_data->pseudohdr;
626 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
627 total_len = msgsz+2;
628 if (total_len & 0x1) {
629 total_len++;
630 }
631
632
633 ppseudo_hdr->seq_num = info->squeseqnum++;
634 ppseudo_hdr->portsrc = ft1000dev->app_info[app_index].app_id;
635
636 ppseudo_hdr->checksum = *pmsg++;
637
638 for (i=1; i<7; i++) {
639 ppseudo_hdr->checksum ^= *pmsg++;
640
641 }
642 pmsg++;
643 ppseudo_hdr = (struct pseudo_hdr *)pmsg;
644 card_send_command(ft1000dev,(unsigned short*)dpram_data,total_len+2);
645
646
647 ft1000dev->app_info[app_index].nTxMsg++;
648 }
649 else {
650 result = -EINVAL;
651 }
652 }
653 }
654 }
655 else {
656 DEBUG("FT1000:ft1000_ioctl: Card not ready take messages\n");
657 result = -EACCES;
658 }
659 kfree(dpram_data);
660
661 }
662 break;
663 case IOCTL_GET_DPRAM_CMD:
664 {
665 struct dpram_blk *pdpram_blk;
666 IOCTL_DPRAM_BLK __user *pioctl_dpram;
667 int msglen;
668
669
670
671 if (ft1000_flarion_cnt == 0) {
672 return (-EBADF);
673 }
674
675
676 for (i=0; i<MAX_NUM_APP; i++) {
677 if (ft1000dev->app_info[i].fileobject == &file->f_owner) {
678
679 break;
680 }
681 }
682
683
684 if (i == MAX_NUM_APP) {
685 DEBUG("FT1000:ft1000_ioctl:Could not find application info block\n");
686 result = -EBADF;
687 break;
688 }
689
690 result = 0;
691 pioctl_dpram = argp;
692 if (list_empty(&ft1000dev->app_info[i].app_sqlist) == 0) {
693
694 spin_lock_irqsave(&free_buff_lock, flags);
695 pdpram_blk = list_entry(ft1000dev->app_info[i].app_sqlist.next, struct dpram_blk, list);
696 list_del(&pdpram_blk->list);
697 ft1000dev->app_info[i].NumOfMsg--;
698
699 spin_unlock_irqrestore(&free_buff_lock, flags);
700 msglen = ntohs(*(u16 *)pdpram_blk->pbuffer) + PSEUDOSZ;
701 result = get_user(msglen, &pioctl_dpram->total_len);
702 if (result)
703 break;
704 msglen = htons(msglen);
705
706 if(copy_to_user (&pioctl_dpram->pseudohdr, pdpram_blk->pbuffer, msglen))
707 {
708 DEBUG("FT1000:ft1000_ioctl: copy fault occurred\n");
709 result = -EFAULT;
710 break;
711 }
712
713 ft1000_free_buffer(pdpram_blk, &freercvpool);
714 result = msglen;
715 }
716
717 }
718 break;
719
720 default:
721 DEBUG("FT1000:ft1000_ioctl:unknown command: 0x%x\n", command);
722 result = -ENOTTY;
723 break;
724 }
725 ft1000dev->fAppMsgPend = 0;
726 return result;
727}
728
729
730
731
732
733
734
735
736
737
738
739static int ft1000_release (struct inode *inode, struct file *file)
740{
741 struct ft1000_info *info;
742 struct net_device *dev;
743 struct ft1000_usb *ft1000dev;
744 int i;
745 struct dpram_blk *pdpram_blk;
746
747 DEBUG("ft1000_release called\n");
748
749 dev = file->private_data;
750 info = netdev_priv(dev);
751 ft1000dev = info->priv;
752
753 if (ft1000_flarion_cnt == 0) {
754 ft1000dev->appcnt--;
755 return (-EBADF);
756 }
757
758
759 for (i=0; i<MAX_NUM_APP; i++) {
760 if ( ft1000dev->app_info[i].fileobject == &file->f_owner) {
761
762 break;
763 }
764 }
765
766 if (i==MAX_NUM_APP)
767 return 0;
768
769 while (list_empty(&ft1000dev->app_info[i].app_sqlist) == 0) {
770 DEBUG("Remove and free memory queue up on slow queue\n");
771 pdpram_blk = list_entry(ft1000dev->app_info[i].app_sqlist.next, struct dpram_blk, list);
772 list_del(&pdpram_blk->list);
773 ft1000_free_buffer(pdpram_blk, &freercvpool);
774 }
775
776
777 ft1000dev->appcnt--;
778 DEBUG("ft1000_chdev:%s:appcnt = %d\n", __FUNCTION__, ft1000dev->appcnt);
779 ft1000dev->app_info[i].fileobject = NULL;
780
781 return 0;
782}
783
784