1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/kref.h>
26#include <linux/uaccess.h>
27#include <linux/usb.h>
28#include <linux/mutex.h>
29#include <linux/page-flags.h>
30#include <linux/pagemap.h>
31#include <linux/jiffies.h>
32
33#include "usb1401.h"
34
35
36
37
38
39
40void FlushOutBuff(DEVICE_EXTENSION * pdx)
41{
42 dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__,
43 pdx->sCurrentState);
44 if (pdx->sCurrentState == U14ERR_TIME)
45 return;
46
47 spin_lock_irq(&pdx->charOutLock);
48 pdx->dwNumOutput = 0;
49 pdx->dwOutBuffGet = 0;
50 pdx->dwOutBuffPut = 0;
51 spin_unlock_irq(&pdx->charOutLock);
52}
53
54
55
56
57
58
59
60void FlushInBuff(DEVICE_EXTENSION * pdx)
61{
62 dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__,
63 pdx->sCurrentState);
64 if (pdx->sCurrentState == U14ERR_TIME)
65 return;
66
67 spin_lock_irq(&pdx->charInLock);
68 pdx->dwNumInput = 0;
69 pdx->dwInBuffGet = 0;
70 pdx->dwInBuffPut = 0;
71 spin_unlock_irq(&pdx->charInLock);
72}
73
74
75
76
77
78
79
80static int PutChars(DEVICE_EXTENSION * pdx, const char *pCh,
81 unsigned int uCount)
82{
83 int iReturn;
84 spin_lock_irq(&pdx->charOutLock);
85 if ((OUTBUF_SZ - pdx->dwNumOutput) >= uCount) {
86 unsigned int u;
87 for (u = 0; u < uCount; u++) {
88 pdx->outputBuffer[pdx->dwOutBuffPut++] = pCh[u];
89 if (pdx->dwOutBuffPut >= OUTBUF_SZ)
90 pdx->dwOutBuffPut = 0;
91 }
92 pdx->dwNumOutput += uCount;
93 spin_unlock_irq(&pdx->charOutLock);
94 iReturn = SendChars(pdx);
95 } else {
96 iReturn = U14ERR_NOOUT;
97 spin_unlock_irq(&pdx->charOutLock);
98 }
99 return iReturn;
100}
101
102
103
104
105
106
107int SendString(DEVICE_EXTENSION * pdx, const char __user * pData,
108 unsigned int n)
109{
110 int iReturn = U14ERR_NOERROR;
111 char buffer[OUTBUF_SZ + 1];
112 if (n > OUTBUF_SZ)
113 return U14ERR_NOOUT;
114 if (copy_from_user(buffer, pData, n))
115 return -EFAULT;
116 buffer[n] = 0;
117
118 mutex_lock(&pdx->io_mutex);
119 if (n > 0)
120 {
121 dev_dbg(&pdx->interface->dev, "%s n=%d>%s<", __func__, n,
122 buffer);
123 iReturn = PutChars(pdx, buffer, n);
124 }
125
126 Allowi(pdx);
127 mutex_unlock(&pdx->io_mutex);
128
129 return iReturn;
130}
131
132
133
134
135
136
137int SendChar(DEVICE_EXTENSION * pdx, char c)
138{
139 int iReturn;
140 mutex_lock(&pdx->io_mutex);
141 iReturn = PutChars(pdx, &c, 1);
142 dev_dbg(&pdx->interface->dev, "SendChar >%c< (0x%02x)", c, c);
143 Allowi(pdx);
144 mutex_unlock(&pdx->io_mutex);
145 return iReturn;
146}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174int Get1401State(DEVICE_EXTENSION * pdx, __u32 * state, __u32 * error)
175{
176 int nGot;
177 dev_dbg(&pdx->interface->dev, "Get1401State() entry");
178
179 *state = 0xFFFFFFFF;
180 nGot = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
181 GET_STATUS, (D_TO_H | VENDOR | DEVREQ), 0, 0,
182 pdx->statBuf, sizeof(pdx->statBuf), HZ);
183 if (nGot != sizeof(pdx->statBuf)) {
184 dev_err(&pdx->interface->dev,
185 "Get1401State() FAILED, return code %d", nGot);
186 pdx->sCurrentState = U14ERR_TIME;
187 *state = 0;
188 *error = 0;
189 } else {
190 int nDevice;
191 dev_dbg(&pdx->interface->dev,
192 "Get1401State() Success, state: 0x%x, 0x%x",
193 pdx->statBuf[0], pdx->statBuf[1]);
194
195 *state = pdx->statBuf[0];
196 *error = pdx->statBuf[1];
197
198 nDevice = pdx->udev->descriptor.bcdDevice >> 8;
199 switch (nDevice)
200 {
201 case 0:
202 pdx->sCurrentState = U14ERR_U1401;
203 break;
204
205 default:
206 if ((nDevice >= 1) && (nDevice <= 23))
207 pdx->sCurrentState = (short)(nDevice + 6);
208 else
209 pdx->sCurrentState = U14ERR_ILL;
210 break;
211 }
212 }
213
214 return pdx->sCurrentState >= 0 ? U14ERR_NOERROR : pdx->sCurrentState;
215}
216
217
218
219
220
221
222int ReadWrite_Cancel(DEVICE_EXTENSION * pdx)
223{
224 dev_dbg(&pdx->interface->dev, "ReadWrite_Cancel entry %d",
225 pdx->bStagedUrbPending);
226#ifdef NOT_WRITTEN_YET
227 int ntStatus = STATUS_SUCCESS;
228 bool bResult = false;
229 unsigned int i;
230
231 spin_lock_irq(&pdx->stagedLock);
232
233 if (pdx->bStagedUrbPending)
234 {
235 dev_info(&pdx->interface - dev,
236 "ReadWrite_Cancel about to cancel Urb");
237
238
239 USB_ASSERT(pdx->pStagedIrp != NULL);
240
241
242
243 spin_unlock_irq(&pdx->stagedLock);
244 bResult = IoCancelIrp(pdx->pStagedIrp);
245 if (bResult) {
246 LARGE_INTEGER timeout;
247 timeout.QuadPart = -10000000;
248 dev_info(&pdx->interface - dev,
249 "ReadWrite_Cancel about to wait till done");
250 ntStatus =
251 KeWaitForSingleObject(&pdx->StagingDoneEvent,
252 Executive, KernelMode, FALSE,
253 &timeout);
254 } else {
255 dev_info(&pdx->interface - dev,
256 "ReadWrite_Cancel, cancellation failed");
257 ntStatus = U14ERR_FAIL;
258 }
259 USB_KdPrint(DBGLVL_DEFAULT,
260 ("ReadWrite_Cancel ntStatus = 0x%x decimal %d\n",
261 ntStatus, ntStatus));
262 } else
263 spin_unlock_irq(&pdx->stagedLock);
264
265 dev_info(&pdx->interface - dev, "ReadWrite_Cancel done");
266 return ntStatus;
267#else
268 return U14ERR_NOERROR;
269#endif
270
271}
272
273
274
275
276
277static int InSelfTest(DEVICE_EXTENSION * pdx, unsigned int *pState)
278{
279 unsigned int state, error;
280 int iReturn = Get1401State(pdx, &state, &error);
281 if (iReturn == U14ERR_NOERROR)
282 iReturn = (state == (unsigned int)-1) ||
283 ((state & 0xff) == 0x80);
284 *pState = state;
285 return iReturn;
286}
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306bool Is1401(DEVICE_EXTENSION * pdx)
307{
308 int iReturn;
309 dev_dbg(&pdx->interface->dev, "%s", __func__);
310
311 ced_draw_down(pdx);
312 FlushInBuff(pdx);
313 FlushOutBuff(pdx);
314
315
316
317 iReturn = usb_lock_device_for_reset(pdx->udev, pdx->interface);
318
319
320
321 mutex_unlock(&pdx->io_mutex);
322 if (iReturn >= 0)
323 {
324 iReturn = usb_reset_device(pdx->udev);
325 usb_unlock_device(pdx->udev);
326 }
327
328 mutex_lock(&pdx->io_mutex);
329 pdx->dwDMAFlag = MODE_CHAR;
330 if (iReturn == 0)
331 {
332 unsigned int state;
333 iReturn = InSelfTest(pdx, &state);
334 if (iReturn > 0)
335 {
336 unsigned long ulTimeOut = jiffies + 30 * HZ;
337 while ((iReturn > 0) && time_before(jiffies, ulTimeOut)) {
338 schedule();
339 iReturn = InSelfTest(pdx, &state);
340 }
341 }
342
343 if (iReturn == 0)
344 iReturn = state == 0;
345 } else
346 iReturn = 0;
347 pdx->bForceReset = false;
348
349 return iReturn > 0;
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366bool QuickCheck(DEVICE_EXTENSION * pdx, bool bTestBuff, bool bCanReset)
367{
368 bool bRet = false;
369 bool bShortTest;
370
371 bShortTest = ((pdx->dwDMAFlag == MODE_CHAR) &&
372 (!pdx->bForceReset) &&
373 (pdx->sCurrentState >= U14ERR_STD));
374
375 dev_dbg(&pdx->interface->dev,
376 "%s DMAFlag:%d, state:%d, force:%d, testBuff:%d, short:%d",
377 __func__, pdx->dwDMAFlag, pdx->sCurrentState, pdx->bForceReset,
378 bTestBuff, bShortTest);
379
380 if ((bTestBuff) &&
381 (pdx->dwNumInput || pdx->dwNumOutput))
382 {
383 bShortTest = false;
384 dev_dbg(&pdx->interface->dev,
385 "%s will reset as buffers not empty", __func__);
386 }
387
388 if (bShortTest || !bCanReset)
389 {
390 unsigned int state, error;
391 dev_dbg(&pdx->interface->dev, "%s->Get1401State", __func__);
392 if (Get1401State(pdx, &state, &error) == U14ERR_NOERROR)
393 {
394 if ((state & 0xFF) == 0)
395 bRet = true;
396 }
397 }
398
399 if (!bRet && bCanReset)
400 {
401 dev_info(&pdx->interface->dev, "%s->Is1401 %d %d %d %d",
402 __func__, bShortTest, pdx->sCurrentState, bTestBuff,
403 pdx->bForceReset);
404 bRet = Is1401(pdx);
405 }
406
407 return bRet;
408}
409
410
411
412
413
414
415int Reset1401(DEVICE_EXTENSION * pdx)
416{
417 mutex_lock(&pdx->io_mutex);
418 dev_dbg(&pdx->interface->dev, "ABout to call QuickCheck");
419 QuickCheck(pdx, true, true);
420 mutex_unlock(&pdx->io_mutex);
421 return U14ERR_NOERROR;
422}
423
424
425
426
427
428
429int GetChar(DEVICE_EXTENSION * pdx)
430{
431 int iReturn = U14ERR_NOIN;
432 mutex_lock(&pdx->io_mutex);
433
434 dev_dbg(&pdx->interface->dev, "GetChar");
435
436 Allowi(pdx);
437 SendChars(pdx);
438
439 spin_lock_irq(&pdx->charInLock);
440 if (pdx->dwNumInput > 0)
441 {
442 iReturn = pdx->inputBuffer[pdx->dwInBuffGet++];
443 if (pdx->dwInBuffGet >= INBUF_SZ)
444 pdx->dwInBuffGet = 0;
445 pdx->dwNumInput--;
446 } else
447 iReturn = U14ERR_NOIN;
448 spin_unlock_irq(&pdx->charInLock);
449
450 Allowi(pdx);
451
452 mutex_unlock(&pdx->io_mutex);
453 return iReturn;
454}
455
456
457
458
459
460
461
462
463
464
465
466
467int GetString(DEVICE_EXTENSION * pdx, char __user * pUser, int n)
468{
469 int nAvailable;
470 int iReturn = U14ERR_NOIN;
471 if (n <= 0)
472 return -ENOMEM;
473
474 mutex_lock(&pdx->io_mutex);
475 Allowi(pdx);
476 SendChars(pdx);
477
478 spin_lock_irq(&pdx->charInLock);
479 nAvailable = pdx->dwNumInput;
480 if (nAvailable > n)
481 nAvailable = n;
482
483 if (nAvailable > 0)
484 {
485 char buffer[INBUF_SZ + 1];
486 int nGot = 0;
487 int nCopyToUser;
488 char cData;
489 do {
490 cData = pdx->inputBuffer[pdx->dwInBuffGet++];
491 if (cData == CR_CHAR)
492 cData = (char)0;
493
494 if (pdx->dwInBuffGet >= INBUF_SZ)
495 pdx->dwInBuffGet = 0;
496
497 buffer[nGot++] = cData;
498 }
499 while ((nGot < nAvailable) && cData);
500
501 nCopyToUser = nGot;
502 if (cData)
503 {
504 buffer[nGot] = (char)0;
505 if (nGot < n)
506 ++nCopyToUser;
507 }
508
509 pdx->dwNumInput -= nGot;
510 spin_unlock_irq(&pdx->charInLock);
511
512 dev_dbg(&pdx->interface->dev,
513 "GetString read %d characters >%s<", nGot, buffer);
514 if (copy_to_user(pUser, buffer, nCopyToUser))
515 iReturn = -EFAULT;
516 else
517 iReturn = nGot;
518 } else
519 spin_unlock_irq(&pdx->charInLock);
520
521 Allowi(pdx);
522 mutex_unlock(&pdx->io_mutex);
523
524 return iReturn;
525}
526
527
528
529
530int Stat1401(DEVICE_EXTENSION * pdx)
531{
532 int iReturn;
533 mutex_lock(&pdx->io_mutex);
534 Allowi(pdx);
535 SendChars(pdx);
536 iReturn = pdx->dwNumInput;
537 mutex_unlock(&pdx->io_mutex);
538 return iReturn;
539}
540
541
542
543
544
545
546
547
548int LineCount(DEVICE_EXTENSION * pdx)
549{
550 int iReturn = 0;
551
552 mutex_lock(&pdx->io_mutex);
553 Allowi(pdx);
554 SendChars(pdx);
555 spin_lock_irq(&pdx->charInLock);
556
557 if (pdx->dwNumInput > 0)
558 {
559 unsigned int dwIndex = pdx->dwInBuffGet;
560 unsigned int dwEnd = pdx->dwInBuffPut;
561 do {
562 if (pdx->inputBuffer[dwIndex++] == CR_CHAR)
563 ++iReturn;
564
565 if (dwIndex >= INBUF_SZ)
566 dwIndex = 0;
567 }
568 while (dwIndex != dwEnd);
569 }
570
571 spin_unlock_irq(&pdx->charInLock);
572 dev_dbg(&pdx->interface->dev, "LineCount returned %d", iReturn);
573 mutex_unlock(&pdx->io_mutex);
574 return iReturn;
575}
576
577
578
579
580
581
582int GetOutBufSpace(DEVICE_EXTENSION * pdx)
583{
584 int iReturn;
585 mutex_lock(&pdx->io_mutex);
586 SendChars(pdx);
587 iReturn = (int)(OUTBUF_SZ - pdx->dwNumOutput);
588 dev_dbg(&pdx->interface->dev, "OutBufSpace %d", iReturn);
589 mutex_unlock(&pdx->io_mutex);
590 return iReturn;
591}
592
593
594
595
596
597
598
599
600int ClearArea(DEVICE_EXTENSION * pdx, int nArea)
601{
602 int iReturn = U14ERR_NOERROR;
603
604 if ((nArea < 0) || (nArea >= MAX_TRANSAREAS)) {
605 iReturn = U14ERR_BADAREA;
606 dev_err(&pdx->interface->dev, "%s Attempt to clear area %d",
607 __func__, nArea);
608 } else {
609 TRANSAREA *pTA = &pdx->rTransDef[nArea];
610 if (!pTA->bUsed)
611 iReturn = U14ERR_NOTSET;
612 else {
613
614
615 struct page **pPages = 0;
616 int nPages = 0;
617 int np;
618
619 dev_dbg(&pdx->interface->dev, "%s area %d", __func__,
620 nArea);
621 spin_lock_irq(&pdx->stagedLock);
622 if ((pdx->StagedId == nArea)
623 && (pdx->dwDMAFlag > MODE_CHAR)) {
624 iReturn = U14ERR_UNLOCKFAIL;
625 dev_err(&pdx->interface->dev,
626 "%s call on area %d while active",
627 __func__, nArea);
628 } else {
629 pPages = pTA->pPages;
630 nPages = pTA->nPages;
631 if (pTA->dwEventSz)
632 wake_up_interruptible(&pTA->wqEvent);
633
634 if (pdx->bXFerWaiting
635 && (pdx->rDMAInfo.wIdent == nArea))
636 pdx->bXFerWaiting = false;
637
638
639
640 memset(pTA, 0,
641 sizeof(TRANSAREA) -
642 sizeof(wait_queue_head_t));
643 }
644 spin_unlock_irq(&pdx->stagedLock);
645
646 if (pPages)
647 {
648
649
650
651 dev_dbg(&pdx->interface->dev, "%s nPages=%d",
652 __func__, nPages);
653
654 for (np = 0; np < nPages; ++np) {
655 if (pPages[np]) {
656 SetPageDirty(pPages[np]);
657 page_cache_release(pPages[np]);
658 }
659 }
660
661 kfree(pPages);
662 dev_dbg(&pdx->interface->dev,
663 "%s kfree(pPages) done", __func__);
664 }
665 }
666 }
667
668 return iReturn;
669}
670
671
672
673
674
675
676
677static int SetArea(DEVICE_EXTENSION * pdx, int nArea, char __user * puBuf,
678 unsigned int dwLength, bool bCircular, bool bCircToHost)
679{
680
681
682
683 unsigned long ulStart = ((unsigned long)puBuf) & PAGE_MASK;
684 unsigned int ulOffset = ((unsigned long)puBuf) & (PAGE_SIZE - 1);
685 int len = (dwLength + ulOffset + PAGE_SIZE - 1) >> PAGE_SHIFT;
686
687 TRANSAREA *pTA = &pdx->rTransDef[nArea];
688 struct page **pPages = 0;
689 int nPages = 0;
690
691 int iReturn = ClearArea(pdx, nArea);
692 if ((iReturn != U14ERR_NOTSET) &&
693 (iReturn != U14ERR_NOERROR))
694 return iReturn;
695
696 if (!access_ok(VERIFY_WRITE, puBuf, dwLength))
697 return -EFAULT;
698
699
700 pPages = kmalloc(len * sizeof(struct page *), GFP_KERNEL);
701 if (!pPages) {
702 iReturn = U14ERR_NOMEMORY;
703 goto error;
704 }
705 dev_dbg(&pdx->interface->dev, "%s %p, length=%06x, circular %d",
706 __func__, puBuf, dwLength, bCircular);
707
708
709 down_read(¤t->mm->mmap_sem);
710 nPages =
711 get_user_pages(current, current->mm, ulStart, len, 1, 0, pPages, 0);
712 up_read(¤t->mm->mmap_sem);
713 dev_dbg(&pdx->interface->dev, "%s nPages = %d", __func__, nPages);
714
715 if (nPages > 0)
716 {
717
718
719
720 spin_lock_irq(&pdx->stagedLock);
721 pTA->lpvBuff = puBuf;
722 pTA->dwBaseOffset = ulOffset;
723 pTA->dwLength = dwLength;
724 pTA->pPages = pPages;
725 pTA->nPages = nPages;
726
727 pTA->bCircular = bCircular;
728 pTA->bCircToHost = bCircToHost;
729
730 pTA->aBlocks[0].dwOffset = 0;
731 pTA->aBlocks[0].dwSize = 0;
732 pTA->aBlocks[1].dwOffset = 0;
733 pTA->aBlocks[1].dwSize = 0;
734 pTA->bUsed = true;
735
736 spin_unlock_irq(&pdx->stagedLock);
737 iReturn = U14ERR_NOERROR;
738 } else {
739 iReturn = U14ERR_LOCKFAIL;
740 goto error;
741 }
742
743 return iReturn;
744
745error:
746 kfree(pPages);
747 return iReturn;
748}
749
750
751
752
753
754
755
756
757int SetTransfer(DEVICE_EXTENSION * pdx, TRANSFERDESC __user * pTD)
758{
759 int iReturn;
760 TRANSFERDESC td;
761
762 if (copy_from_user(&td, pTD, sizeof(td)))
763 return -EFAULT;
764
765 mutex_lock(&pdx->io_mutex);
766 dev_dbg(&pdx->interface->dev, "%s area:%d, size:%08x", __func__,
767 td.wAreaNum, td.dwLength);
768
769
770
771 iReturn =
772 SetArea(pdx, td.wAreaNum,
773 (char __user *)((unsigned long)td.lpvBuff), td.dwLength,
774 false, false);
775 mutex_unlock(&pdx->io_mutex);
776 return iReturn;
777}
778
779
780
781
782
783int UnsetTransfer(DEVICE_EXTENSION * pdx, int nArea)
784{
785 int iReturn;
786 mutex_lock(&pdx->io_mutex);
787 iReturn = ClearArea(pdx, nArea);
788 mutex_unlock(&pdx->io_mutex);
789 return iReturn;
790}
791
792
793
794
795
796
797
798
799
800int SetEvent(DEVICE_EXTENSION * pdx, TRANSFEREVENT __user * pTE)
801{
802 int iReturn = U14ERR_NOERROR;
803 TRANSFEREVENT te;
804
805
806 if (copy_from_user(&te, pTE, sizeof(te)))
807 return -EFAULT;
808
809 if (te.wAreaNum >= MAX_TRANSAREAS)
810 return U14ERR_BADAREA;
811 else {
812 TRANSAREA *pTA = &pdx->rTransDef[te.wAreaNum];
813 mutex_lock(&pdx->io_mutex);
814 spin_lock_irq(&pdx->stagedLock);
815 if (pTA->bUsed)
816 {
817 pTA->dwEventSt = te.dwStart;
818 pTA->dwEventSz = te.dwLength;
819 pTA->bEventToHost = te.wFlags & 1;
820 pTA->iWakeUp = 0;
821 } else
822 iReturn = U14ERR_NOTSET;
823 spin_unlock_irq(&pdx->stagedLock);
824 mutex_unlock(&pdx->io_mutex);
825 }
826 return iReturn ==
827 U14ERR_NOERROR ? (te.iSetEvent ? 1 : U14ERR_NOERROR) : iReturn;
828}
829
830
831
832
833
834
835
836int WaitEvent(DEVICE_EXTENSION * pdx, int nArea, int msTimeOut)
837{
838 int iReturn;
839 if ((unsigned)nArea >= MAX_TRANSAREAS)
840 return U14ERR_BADAREA;
841 else {
842 int iWait;
843 TRANSAREA *pTA = &pdx->rTransDef[nArea];
844 msTimeOut = (msTimeOut * HZ + 999) / 1000;
845
846
847
848
849
850 mutex_lock(&pdx->io_mutex);
851 if (!pTA->bUsed || !pTA->dwEventSz)
852 return U14ERR_NOTSET;
853 mutex_unlock(&pdx->io_mutex);
854
855 if (msTimeOut)
856 iWait =
857 wait_event_interruptible_timeout(pTA->wqEvent,
858 pTA->iWakeUp
859 || !pTA->bUsed,
860 msTimeOut);
861 else
862 iWait =
863 wait_event_interruptible(pTA->wqEvent, pTA->iWakeUp
864 || !pTA->bUsed);
865 if (iWait)
866 iReturn = -ERESTARTSYS;
867 else
868 iReturn = pTA->iWakeUp;
869
870 spin_lock_irq(&pdx->stagedLock);
871 pTA->iWakeUp = 0;
872 spin_unlock_irq(&pdx->stagedLock);
873 }
874 return iReturn;
875}
876
877
878
879
880
881
882
883int TestEvent(DEVICE_EXTENSION * pdx, int nArea)
884{
885 int iReturn;
886 if ((unsigned)nArea >= MAX_TRANSAREAS)
887 iReturn = U14ERR_BADAREA;
888 else {
889 TRANSAREA *pTA = &pdx->rTransDef[nArea];
890 mutex_lock(&pdx->io_mutex);
891 spin_lock_irq(&pdx->stagedLock);
892 iReturn = pTA->iWakeUp;
893 pTA->iWakeUp = 0;
894 spin_unlock_irq(&pdx->stagedLock);
895 mutex_unlock(&pdx->io_mutex);
896 }
897 return iReturn;
898}
899
900
901
902
903
904int GetTransfer(DEVICE_EXTENSION * pdx, TGET_TX_BLOCK __user * pTX)
905{
906 int iReturn = U14ERR_NOERROR;
907 unsigned int dwIdent;
908
909 mutex_lock(&pdx->io_mutex);
910 dwIdent = pdx->StagedId;
911 if (dwIdent >= MAX_TRANSAREAS)
912 iReturn = U14ERR_BADAREA;
913 else {
914
915 TGET_TX_BLOCK *tx;
916
917 tx = kzalloc(sizeof(*tx), GFP_KERNEL);
918 if (!tx) {
919 mutex_unlock(&pdx->io_mutex);
920 return -ENOMEM;
921 }
922 tx->size = pdx->rTransDef[dwIdent].dwLength;
923 tx->linear = (long long)((long)pdx->rTransDef[dwIdent].lpvBuff);
924 tx->avail = GET_TX_MAXENTRIES;
925 tx->used = 1;
926 tx->entries[0].physical =
927 (long long)(tx->linear + pdx->StagedOffset);
928 tx->entries[0].size = tx->size;
929
930 if (copy_to_user(pTX, tx, sizeof(*tx)))
931 iReturn = -EFAULT;
932 kfree(tx);
933 }
934 mutex_unlock(&pdx->io_mutex);
935 return iReturn;
936}
937
938
939
940
941
942
943int KillIO1401(DEVICE_EXTENSION * pdx)
944{
945 dev_dbg(&pdx->interface->dev, "%s", __func__);
946 mutex_lock(&pdx->io_mutex);
947 FlushOutBuff(pdx);
948 FlushInBuff(pdx);
949 mutex_unlock(&pdx->io_mutex);
950 return U14ERR_NOERROR;
951}
952
953
954
955
956
957
958int BlkTransState(DEVICE_EXTENSION * pdx)
959{
960 int iReturn = pdx->dwDMAFlag != MODE_CHAR;
961 dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
962 return iReturn;
963}
964
965
966
967
968
969
970int StateOf1401(DEVICE_EXTENSION * pdx)
971{
972 int iReturn;
973 mutex_lock(&pdx->io_mutex);
974
975 QuickCheck(pdx, false, false);
976 iReturn = pdx->sCurrentState;
977
978 mutex_unlock(&pdx->io_mutex);
979 dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
980
981 return iReturn;
982}
983
984
985
986
987
988
989
990int StartSelfTest(DEVICE_EXTENSION * pdx)
991{
992 int nGot;
993 mutex_lock(&pdx->io_mutex);
994 dev_dbg(&pdx->interface->dev, "%s", __func__);
995
996 ced_draw_down(pdx);
997 FlushInBuff(pdx);
998 FlushOutBuff(pdx);
999
1000 pdx->dwDMAFlag = MODE_CHAR;
1001
1002 nGot = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0), DB_SELFTEST, (H_TO_D | VENDOR | DEVREQ), 0, 0, 0, 0, HZ);
1003 pdx->ulSelfTestTime = jiffies + HZ * 30;
1004
1005 mutex_unlock(&pdx->io_mutex);
1006 if (nGot < 0)
1007 dev_err(&pdx->interface->dev, "%s err=%d", __func__, nGot);
1008 return nGot < 0 ? U14ERR_FAIL : U14ERR_NOERROR;
1009}
1010
1011
1012
1013
1014
1015
1016int CheckSelfTest(DEVICE_EXTENSION * pdx, TGET_SELFTEST __user * pGST)
1017{
1018 unsigned int state, error;
1019 int iReturn;
1020 TGET_SELFTEST gst;
1021 memset(&gst, 0, sizeof(gst));
1022
1023 mutex_lock(&pdx->io_mutex);
1024
1025 dev_dbg(&pdx->interface->dev, "%s", __func__);
1026 iReturn = Get1401State(pdx, &state, &error);
1027 if (iReturn == U14ERR_NOERROR)
1028 iReturn = Get1401State(pdx, &state, &error);
1029
1030 if (iReturn != U14ERR_NOERROR)
1031 {
1032 dev_err(&pdx->interface->dev,
1033 "%s Get1401State=%d, assuming still testing", __func__,
1034 iReturn);
1035 state = 0x80;
1036 error = 0;
1037 iReturn = U14ERR_NOERROR;
1038 }
1039
1040 if ((state == -1) && (error == -1))
1041 {
1042 dev_err(&pdx->interface->dev,
1043 "%s Get1401State failed, assuming still testing",
1044 __func__);
1045 state = 0x80;
1046 error = 0;
1047 }
1048
1049 if ((state & 0xFF) == 0x80)
1050 {
1051 if (state & 0x00FF0000)
1052 {
1053 gst.code = (state & 0x00FF0000) >> 16;
1054 gst.x = error & 0x0000FFFF;
1055 gst.y = (error & 0xFFFF0000) >> 16;
1056 dev_dbg(&pdx->interface->dev, "Self-test error code %d",
1057 gst.code);
1058 } else
1059 {
1060 unsigned long ulNow = jiffies;
1061 if (time_after(ulNow, pdx->ulSelfTestTime)) {
1062 gst.code = -2;
1063 dev_dbg(&pdx->interface->dev,
1064 "Self-test timed-out");
1065 } else
1066 dev_dbg(&pdx->interface->dev,
1067 "Self-test on-going");
1068 }
1069 } else {
1070 gst.code = -1;
1071 dev_dbg(&pdx->interface->dev, "Self-test done");
1072 }
1073
1074 if (gst.code < 0)
1075 {
1076 if ((pdx->nPipes == 4) && (pdx->s1401Type <= TYPEPOWER))
1077 Is1401(pdx);
1078 else
1079 QuickCheck(pdx, true, true);
1080 }
1081 mutex_unlock(&pdx->io_mutex);
1082
1083 if (copy_to_user(pGST, &gst, sizeof(gst)))
1084 return -EFAULT;
1085
1086 return iReturn;
1087}
1088
1089
1090
1091
1092
1093
1094int TypeOf1401(DEVICE_EXTENSION * pdx)
1095{
1096 int iReturn = TYPEUNKNOWN;
1097 mutex_lock(&pdx->io_mutex);
1098 dev_dbg(&pdx->interface->dev, "%s", __func__);
1099
1100 switch (pdx->s1401Type) {
1101 case TYPE1401:
1102 iReturn = U14ERR_STD;
1103 break;
1104 case TYPEPLUS:
1105 iReturn = U14ERR_PLUS;
1106 break;
1107 case TYPEU1401:
1108 iReturn = U14ERR_U1401;
1109 break;
1110 default:
1111 if ((pdx->s1401Type >= TYPEPOWER) && (pdx->s1401Type <= 25))
1112 iReturn = pdx->s1401Type + 4;
1113 else
1114 iReturn = TYPEUNKNOWN;
1115 }
1116 dev_dbg(&pdx->interface->dev, "%s %d", __func__, iReturn);
1117 mutex_unlock(&pdx->io_mutex);
1118
1119 return iReturn;
1120}
1121
1122
1123
1124
1125
1126
1127int TransferFlags(DEVICE_EXTENSION * pdx)
1128{
1129 int iReturn = U14TF_MULTIA | U14TF_DIAG |
1130 U14TF_NOTIFY | U14TF_CIRCTH;
1131 dev_dbg(&pdx->interface->dev, "%s", __func__);
1132 mutex_lock(&pdx->io_mutex);
1133 if (pdx->bIsUSB2)
1134 iReturn |= U14TF_USB2;
1135 mutex_unlock(&pdx->io_mutex);
1136
1137 return iReturn;
1138}
1139
1140
1141
1142
1143
1144
1145static int DbgCmd1401(DEVICE_EXTENSION * pdx, unsigned char cmd,
1146 unsigned int data)
1147{
1148 int iReturn;
1149 dev_dbg(&pdx->interface->dev, "%s entry", __func__);
1150 iReturn = usb_control_msg(pdx->udev, usb_sndctrlpipe(pdx->udev, 0), cmd, (H_TO_D | VENDOR | DEVREQ), (unsigned short)data, (unsigned short)(data >> 16), 0, 0, HZ);
1151 if (iReturn < 0)
1152 dev_err(&pdx->interface->dev, "%s fail code=%d", __func__,
1153 iReturn);
1154
1155 return iReturn;
1156}
1157
1158
1159
1160
1161
1162
1163int DbgPeek(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1164{
1165 int iReturn;
1166 TDBGBLOCK db;
1167
1168 if (copy_from_user(&db, pDB, sizeof(db)))
1169 return -EFAULT;
1170
1171 mutex_lock(&pdx->io_mutex);
1172 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1173
1174 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1175 if (iReturn == U14ERR_NOERROR)
1176 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1177 if (iReturn == U14ERR_NOERROR)
1178 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1179 if (iReturn == U14ERR_NOERROR)
1180 iReturn = DbgCmd1401(pdx, DB_PEEK, 0);
1181 mutex_unlock(&pdx->io_mutex);
1182
1183 return iReturn;
1184}
1185
1186
1187
1188
1189
1190
1191
1192int DbgPoke(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1193{
1194 int iReturn;
1195 TDBGBLOCK db;
1196
1197 if (copy_from_user(&db, pDB, sizeof(db)))
1198 return -EFAULT;
1199
1200 mutex_lock(&pdx->io_mutex);
1201 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1202
1203 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1204 if (iReturn == U14ERR_NOERROR)
1205 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1206 if (iReturn == U14ERR_NOERROR)
1207 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1208 if (iReturn == U14ERR_NOERROR)
1209 iReturn = DbgCmd1401(pdx, DB_POKE, db.iData);
1210 mutex_unlock(&pdx->io_mutex);
1211
1212 return iReturn;
1213}
1214
1215
1216
1217
1218
1219
1220
1221int DbgRampData(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1222{
1223 int iReturn;
1224 TDBGBLOCK db;
1225
1226 if (copy_from_user(&db, pDB, sizeof(db)))
1227 return -EFAULT;
1228
1229 mutex_lock(&pdx->io_mutex);
1230 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1231
1232 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1233 if (iReturn == U14ERR_NOERROR)
1234 iReturn = DbgCmd1401(pdx, DB_SETDEF, db.iDefault);
1235 if (iReturn == U14ERR_NOERROR)
1236 iReturn = DbgCmd1401(pdx, DB_SETMASK, db.iMask);
1237 if (iReturn == U14ERR_NOERROR)
1238 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1239 if (iReturn == U14ERR_NOERROR)
1240 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1241 if (iReturn == U14ERR_NOERROR)
1242 iReturn = DbgCmd1401(pdx, DB_RAMPD, 0);
1243 mutex_unlock(&pdx->io_mutex);
1244
1245 return iReturn;
1246}
1247
1248
1249
1250
1251
1252
1253int DbgRampAddr(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1254{
1255 int iReturn;
1256 TDBGBLOCK db;
1257
1258 if (copy_from_user(&db, pDB, sizeof(db)))
1259 return -EFAULT;
1260
1261 mutex_lock(&pdx->io_mutex);
1262 dev_dbg(&pdx->interface->dev, "%s", __func__);
1263
1264 iReturn = DbgCmd1401(pdx, DB_SETDEF, db.iDefault);
1265 if (iReturn == U14ERR_NOERROR)
1266 iReturn = DbgCmd1401(pdx, DB_SETMASK, db.iMask);
1267 if (iReturn == U14ERR_NOERROR)
1268 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1269 if (iReturn == U14ERR_NOERROR)
1270 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1271 if (iReturn == U14ERR_NOERROR)
1272 iReturn = DbgCmd1401(pdx, DB_RAMPA, 0);
1273 mutex_unlock(&pdx->io_mutex);
1274
1275 return iReturn;
1276}
1277
1278
1279
1280
1281
1282
1283int DbgGetData(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1284{
1285 int iReturn;
1286 TDBGBLOCK db;
1287 memset(&db, 0, sizeof(db));
1288
1289 mutex_lock(&pdx->io_mutex);
1290 dev_dbg(&pdx->interface->dev, "%s", __func__);
1291
1292
1293 iReturn = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
1294 DB_DATA, (D_TO_H | VENDOR | DEVREQ), 0, 0,
1295 &db.iData, sizeof(db.iData), HZ);
1296 if (iReturn == sizeof(db.iData)) {
1297 if (copy_to_user(pDB, &db, sizeof(db)))
1298 iReturn = -EFAULT;
1299 else
1300 iReturn = U14ERR_NOERROR;
1301 } else
1302 dev_err(&pdx->interface->dev, "%s failed, code %d", __func__,
1303 iReturn);
1304
1305 mutex_unlock(&pdx->io_mutex);
1306
1307 return iReturn;
1308}
1309
1310
1311
1312
1313
1314
1315
1316int DbgStopLoop(DEVICE_EXTENSION * pdx)
1317{
1318 int iReturn;
1319 unsigned int uState, uErr;
1320
1321 mutex_lock(&pdx->io_mutex);
1322 dev_dbg(&pdx->interface->dev, "%s", __func__);
1323 iReturn = Get1401State(pdx, &uState, &uErr);
1324 mutex_unlock(&pdx->io_mutex);
1325
1326 return iReturn;
1327}
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337int SetCircular(DEVICE_EXTENSION * pdx, TRANSFERDESC __user * pTD)
1338{
1339 int iReturn;
1340 bool bToHost;
1341 TRANSFERDESC td;
1342
1343 if (copy_from_user(&td, pTD, sizeof(td)))
1344 return -EFAULT;
1345
1346 mutex_lock(&pdx->io_mutex);
1347 dev_dbg(&pdx->interface->dev, "%s area:%d, size:%08x", __func__,
1348 td.wAreaNum, td.dwLength);
1349 bToHost = td.eSize != 0;
1350
1351
1352
1353
1354 iReturn =
1355 SetArea(pdx, td.wAreaNum,
1356 (char __user *)((unsigned long)td.lpvBuff), td.dwLength,
1357 true, bToHost);
1358 mutex_unlock(&pdx->io_mutex);
1359 return iReturn;
1360}
1361
1362
1363
1364
1365
1366
1367int GetCircBlock(DEVICE_EXTENSION * pdx, TCIRCBLOCK __user * pCB)
1368{
1369 int iReturn = U14ERR_NOERROR;
1370 unsigned int nArea;
1371 TCIRCBLOCK cb;
1372
1373 dev_dbg(&pdx->interface->dev, "%s", __func__);
1374
1375 if (copy_from_user(&cb, pCB, sizeof(cb)))
1376 return -EFAULT;
1377
1378 mutex_lock(&pdx->io_mutex);
1379
1380 nArea = cb.nArea;
1381 cb.dwOffset = 0;
1382 cb.dwSize = 0;
1383
1384 if (nArea < MAX_TRANSAREAS)
1385 {
1386 TRANSAREA *pArea = &pdx->rTransDef[nArea];
1387 spin_lock_irq(&pdx->stagedLock);
1388
1389 if ((pArea->bUsed) && (pArea->bCircular) &&
1390 (pArea->bCircToHost))
1391 {
1392 if (pArea->aBlocks[0].dwSize > 0)
1393 {
1394 cb.dwOffset = pArea->aBlocks[0].dwOffset;
1395 cb.dwSize = pArea->aBlocks[0].dwSize;
1396 dev_dbg(&pdx->interface->dev,
1397 "%s return block 0: %d bytes at %d",
1398 __func__, cb.dwSize, cb.dwOffset);
1399 }
1400 } else
1401 iReturn = U14ERR_NOTSET;
1402
1403 spin_unlock_irq(&pdx->stagedLock);
1404 } else
1405 iReturn = U14ERR_BADAREA;
1406
1407 if (copy_to_user(pCB, &cb, sizeof(cb)))
1408 iReturn = -EFAULT;
1409
1410 mutex_unlock(&pdx->io_mutex);
1411 return iReturn;
1412}
1413
1414
1415
1416
1417
1418
1419int FreeCircBlock(DEVICE_EXTENSION * pdx, TCIRCBLOCK __user * pCB)
1420{
1421 int iReturn = U14ERR_NOERROR;
1422 unsigned int nArea, uStart, uSize;
1423 TCIRCBLOCK cb;
1424
1425 dev_dbg(&pdx->interface->dev, "%s", __func__);
1426
1427 if (copy_from_user(&cb, pCB, sizeof(cb)))
1428 return -EFAULT;
1429
1430 mutex_lock(&pdx->io_mutex);
1431
1432 nArea = cb.nArea;
1433 uStart = cb.dwOffset;
1434 uSize = cb.dwSize;
1435 cb.dwOffset = 0;
1436 cb.dwSize = 0;
1437
1438 if (nArea < MAX_TRANSAREAS)
1439 {
1440 TRANSAREA *pArea = &pdx->rTransDef[nArea];
1441 spin_lock_irq(&pdx->stagedLock);
1442
1443 if ((pArea->bUsed) && (pArea->bCircular) &&
1444 (pArea->bCircToHost))
1445 {
1446 bool bWaiting = false;
1447
1448 if ((pArea->aBlocks[0].dwSize >= uSize) &&
1449 (pArea->aBlocks[0].dwOffset == uStart))
1450 {
1451 pArea->aBlocks[0].dwSize -= uSize;
1452 pArea->aBlocks[0].dwOffset += uSize;
1453 if (pArea->aBlocks[0].dwSize == 0)
1454 {
1455 if (pArea->aBlocks[1].dwSize)
1456 {
1457 pArea->aBlocks[0] = pArea->aBlocks[1];
1458 pArea->aBlocks[1].dwSize = 0;
1459 pArea->aBlocks[1].dwOffset = 0;
1460 } else
1461 pArea->aBlocks[0].dwOffset = 0;
1462 }
1463
1464 dev_dbg(&pdx->interface->dev,
1465 "%s free %d bytes at %d, return %d bytes at %d, wait=%d",
1466 __func__, uSize, uStart,
1467 pArea->aBlocks[0].dwSize,
1468 pArea->aBlocks[0].dwOffset,
1469 pdx->bXFerWaiting);
1470
1471
1472 if (pArea->aBlocks[0].dwSize > 0)
1473 {
1474 cb.dwOffset =
1475 pArea->aBlocks[0].dwOffset;
1476 cb.dwSize = pArea->aBlocks[0].dwSize;
1477 }
1478
1479 bWaiting = pdx->bXFerWaiting;
1480 if (bWaiting && pdx->bStagedUrbPending) {
1481 dev_err(&pdx->interface->dev,
1482 "%s ERROR: waiting xfer and staged Urb pending!",
1483 __func__);
1484 bWaiting = false;
1485 }
1486 } else {
1487 dev_err(&pdx->interface->dev,
1488 "%s ERROR: freeing %d bytes at %d, block 0 is %d bytes at %d",
1489 __func__, uSize, uStart,
1490 pArea->aBlocks[0].dwSize,
1491 pArea->aBlocks[0].dwOffset);
1492 iReturn = U14ERR_NOMEMORY;
1493 }
1494
1495
1496 if (bWaiting)
1497 {
1498 int RWMStat =
1499 ReadWriteMem(pdx, !pdx->rDMAInfo.bOutWard,
1500 pdx->rDMAInfo.wIdent,
1501 pdx->rDMAInfo.dwOffset,
1502 pdx->rDMAInfo.dwSize);
1503 if (RWMStat != U14ERR_NOERROR)
1504 dev_err(&pdx->interface->dev,
1505 "%s rw setup failed %d",
1506 __func__, RWMStat);
1507 }
1508 } else
1509 iReturn = U14ERR_NOTSET;
1510
1511 spin_unlock_irq(&pdx->stagedLock);
1512 } else
1513 iReturn = U14ERR_BADAREA;
1514
1515 if (copy_to_user(pCB, &cb, sizeof(cb)))
1516 iReturn = -EFAULT;
1517
1518 mutex_unlock(&pdx->io_mutex);
1519 return iReturn;
1520}
1521