-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcedusb.tar
3536 lines (3066 loc) · 160 KB
/
cedusb.tar
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
ced_ioc.c 0000664 0001750 0001750 00000162412 11722662012 011250 0 ustar greg greg /* ced_ioc.c
ioctl part of the 1401 usb device driver for linux.
Copyright (C) 2010 Cambridge Electronic Design Ltd
Author Greg P Smith ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#include <linux/page-flags.h>
#include <linux/pagemap.h>
#include <linux/jiffies.h>
#include "usb1401.h"
/****************************************************************************
** FlushOutBuff
**
** Empties the Output buffer and sets int lines. Used from user level only
****************************************************************************/
void FlushOutBuff(DEVICE_EXTENSION *pdx)
{
dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__, pdx->sCurrentState);
if (pdx->sCurrentState == U14ERR_TIME) /* Do nothing if hardware in trouble */
return;
// CharSend_Cancel(pdx); /* Kill off any pending I/O */
spin_lock_irq(&pdx->charOutLock);
pdx->dwNumOutput = 0;
pdx->dwOutBuffGet = 0;
pdx->dwOutBuffPut = 0;
spin_unlock_irq(&pdx->charOutLock);
}
/****************************************************************************
**
** FlushInBuff
**
** Empties the input buffer and sets int lines
****************************************************************************/
void FlushInBuff(DEVICE_EXTENSION *pdx)
{
dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__, pdx->sCurrentState);
if (pdx->sCurrentState == U14ERR_TIME) /* Do nothing if hardware in trouble */
return;
// CharRead_Cancel(pDevObject); /* Kill off any pending I/O */
spin_lock_irq(&pdx->charInLock);
pdx->dwNumInput = 0;
pdx->dwInBuffGet = 0;
pdx->dwInBuffPut = 0;
spin_unlock_irq(&pdx->charInLock);
}
/****************************************************************************
** PutChars
**
** Utility routine to copy chars into the output buffer and fire them off.
** called from user mode, holds charOutLock.
****************************************************************************/
static int PutChars(DEVICE_EXTENSION* pdx, const char* pCh, unsigned int uCount)
{
int iReturn;
spin_lock_irq(&pdx->charOutLock); // get the output spin lock
if ((OUTBUF_SZ - pdx->dwNumOutput) >= uCount)
{
unsigned int u;
for (u=0; u<uCount; u++)
{
pdx->outputBuffer[pdx->dwOutBuffPut++] = pCh[u];
if (pdx->dwOutBuffPut >= OUTBUF_SZ)
pdx->dwOutBuffPut = 0;
}
pdx->dwNumOutput += uCount;
spin_unlock_irq(&pdx->charOutLock);
iReturn = SendChars(pdx); // ...give a chance to transmit data
}
else
{
iReturn = U14ERR_NOOUT; // no room at the out (ha-ha)
spin_unlock_irq(&pdx->charOutLock);
}
return iReturn;
}
/*****************************************************************************
** Add the data in pData (local pointer) of length n to the output buffer, and
** trigger an output transfer if this is appropriate. User mode.
** Holds the io_mutex
*****************************************************************************/
int SendString(DEVICE_EXTENSION* pdx, const char __user* pData, unsigned int n)
{
int iReturn = U14ERR_NOERROR; // assume all will be well
char buffer[OUTBUF_SZ+1]; // space in our address space for characters
if (n > OUTBUF_SZ) // check space in local buffer...
return U14ERR_NOOUT; // ...too many characters
if (copy_from_user(buffer, pData, n))
return -ENOMEM; // could not copy
buffer[n] = 0; // terminate for debug purposes
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
if (n > 0) // do nothing if nowt to do!
{
dev_dbg(&pdx->interface->dev, "%s n=%d>%s<", __func__, n, buffer);
iReturn = PutChars(pdx, buffer, n);
}
Allowi(pdx, false); // make sure we have input int
mutex_unlock(&pdx->io_mutex);
return iReturn;
}
/****************************************************************************
** SendChar
**
** Sends a single character to the 1401. User mode, holds io_mutex.
****************************************************************************/
int SendChar(DEVICE_EXTENSION *pdx, char c)
{
int iReturn;
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
iReturn = PutChars(pdx, &c, 1);
dev_dbg(&pdx->interface->dev,"SendChar >%c< (0x%02x)", c, c);
Allowi(pdx, false); // Make sure char reads are running
mutex_unlock(&pdx->io_mutex);
return iReturn;
}
/***************************************************************************
**
** Get1401State
**
** Retrieves state information from the 1401, adjusts the 1401 state held
** in the device extension to indicate the current 1401 type.
**
** *state is updated with information about the 1401 state as returned by the
** 1401. The low byte is a code for what 1401 is doing:
**
** 0 normal 1401 operation
** 1 sending chars to host
** 2 sending block data to host
** 3 reading block data from host
** 4 sending an escape sequence to the host
** 0x80 1401 is executing self-test, in which case the upper word
** is the last error code seen (or zero for no new error).
**
** *error is updated with error information if a self-test error code
** is returned in the upper word of state.
**
** both state and error are set to -1 if there are comms problems, and
** to zero if there is a simple failure.
**
** return error code (U14ERR_NOERROR for OK)
*/
int Get1401State(DEVICE_EXTENSION* pdx, __u32* state, __u32* error)
{
int nGot;
dev_dbg(&pdx->interface->dev, "Get1401State() entry");
*state = 0xFFFFFFFF; // Start off with invalid state
nGot = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
GET_STATUS, (D_TO_H|VENDOR|DEVREQ), 0,0,
pdx->statBuf, sizeof(pdx->statBuf), HZ);
if (nGot != sizeof(pdx->statBuf))
{
dev_err(&pdx->interface->dev, "Get1401State() FAILED, return code %d", nGot);
pdx->sCurrentState = U14ERR_TIME; // Indicate that things are very wrong indeed
*state = 0; // Force status values to a known state
*error = 0;
}
else
{
int nDevice;
dev_dbg(&pdx->interface->dev, "Get1401State() Success, state: 0x%x, 0x%x",
pdx->statBuf[0], pdx->statBuf[1]);
*state = pdx->statBuf[0]; // Return the state values to the calling code
*error = pdx->statBuf[1];
nDevice = pdx->udev->descriptor.bcdDevice >> 8; // 1401 type code value
switch (nDevice) // so we can clean up current state
{
case 0:
pdx->sCurrentState = U14ERR_U1401;
break;
default: // allow lots of device codes for future 1401s
if ((nDevice >= 1) && (nDevice <= 23))
pdx->sCurrentState = (short)(nDevice + 6);
else
pdx->sCurrentState = U14ERR_ILL;
break;
}
}
return pdx->sCurrentState >= 0 ? U14ERR_NOERROR : pdx->sCurrentState;
}
/****************************************************************************
** ReadWrite_Cancel
**
** Kills off staged read\write request from the USB if one is pending.
****************************************************************************/
int ReadWrite_Cancel(DEVICE_EXTENSION *pdx)
{
dev_dbg(&pdx->interface->dev, "ReadWrite_Cancel entry %d", pdx->bStagedUrbPending);
#ifdef NOT_WRITTEN_YET
int ntStatus = STATUS_SUCCESS;
bool bResult = false;
unsigned int i;
// We can fill this in when we know how we will implement the staged transfer stuff
spin_lock_irq(&pdx->stagedLock);
if (pdx->bStagedUrbPending) // anything to be cancelled? May need more...
{
dev_info(&pdx->interface-dev, "ReadWrite_Cancel about to cancel Urb");
// KeClearEvent(&pdx->StagingDoneEvent); // Clear the staging done flag
USB_ASSERT(pdx->pStagedIrp != NULL);
// Release the spinlock first otherwise the completion routine may hang
// on the spinlock while this function hands waiting for the event.
spin_unlock_irq(&pdx->stagedLock);
bResult = IoCancelIrp(pdx->pStagedIrp); // Actually do the cancel
if (bResult)
{
LARGE_INTEGER timeout;
timeout.QuadPart = -10000000; // Use a timeout of 1 second
dev_info(&pdx->interface-dev, "ReadWrite_Cancel about to wait till done");
ntStatus = KeWaitForSingleObject(&pdx->StagingDoneEvent, Executive,
KernelMode, FALSE, &timeout);
}
else
{
dev_info(&pdx->interface-dev, "ReadWrite_Cancel, cancellation failed");
ntStatus = U14ERR_FAIL;
}
USB_KdPrint(DBGLVL_DEFAULT, ("ReadWrite_Cancel ntStatus = 0x%x decimal %d\n", ntStatus, ntStatus));
}
else
spin_unlock_irq(&pdx->stagedLock);
dev_info(&pdx->interface-dev, "ReadWrite_Cancel done");
return ntStatus;
#else
return U14ERR_NOERROR;
#endif
}
/***************************************************************************
** InSelfTest - utility to check in self test. Return 1 for ST, 0 for not or
** a -ve error code if we failed for some reason.
***************************************************************************/
static int InSelfTest(DEVICE_EXTENSION* pdx, unsigned int* pState)
{
unsigned int state, error;
int iReturn = Get1401State(pdx, &state, &error); // see if in self-test
if (iReturn == U14ERR_NOERROR) // if all still OK
iReturn = (state == (unsigned int)-1) || // TX problem or...
((state & 0xff) == 0x80); // ...self test
*pState = state; // return actual state
return iReturn;
}
/***************************************************************************
** Is1401 - ALWAYS CALLED HOLDING THE io_mutex
**
** Tests for the current state of the 1401. Sets sCurrentState:
**
** U14ERR_NOIF 1401 i/f card not installed (not done here)
** U14ERR_OFF 1401 apparently not switched on
** U14ERR_NC 1401 appears to be not connected
** U14ERR_ILL 1401 if it is there its not very well at all
** U14ERR_TIME 1401 appears OK, but doesn't communicate - very bad
** U14ERR_STD 1401 OK and ready for use
** U14ERR_PLUS 1401+ OK and ready for use
** U14ERR_U1401 Micro1401 OK and ready for use
** U14ERR_POWER Power1401 OK and ready for use
** U14ERR_U14012 Micro1401 mkII OK and ready for use
**
** Returns TRUE if a 1401 detected and OK, else FALSE
****************************************************************************/
bool Is1401(DEVICE_EXTENSION* pdx)
{
int iReturn;
dev_dbg(&pdx->interface->dev, "%s", __func__);
ced_draw_down(pdx); // wait for, then kill outstanding Urbs
FlushInBuff(pdx); // Clear out input buffer & pipe
FlushOutBuff(pdx); // Clear output buffer & pipe
// The next call returns 0 if OK, but has returned 1 in the past, meaning that
// usb_unlock_device() is needed... now it always is
iReturn = usb_lock_device_for_reset(pdx->udev, pdx->interface);
// release the io_mutex because if we don't, we will deadlock due to system
// calls back into the driver.
mutex_unlock(&pdx->io_mutex); // locked, so we will not get system calls
if (iReturn >= 0) // if we failed
{
iReturn = usb_reset_device(pdx->udev); // try to do the reset
usb_unlock_device(pdx->udev); // undo the lock
}
mutex_lock(&pdx->io_mutex); // hold stuff off while we wait
pdx->dwDMAFlag = MODE_CHAR; // Clear DMA mode flag regardless!
if (iReturn == 0) // if all is OK still
{
unsigned int state;
iReturn = InSelfTest(pdx, &state); // see if likely in self test
if (iReturn > 0) // do we need to wait for self-test?
{
unsigned long ulTimeOut = jiffies + 30*HZ; // when to give up
while((iReturn > 0) && time_before(jiffies, ulTimeOut))
{
schedule(); // let other stuff run
iReturn = InSelfTest(pdx, &state); // see if done yet
}
}
if (iReturn == 0) // if all is OK...
iReturn = state == 0; // then sucess is that the state is 0
}
else
iReturn = 0; // we failed
pdx->bForceReset = false; // Clear forced reset flag now
return iReturn > 0;
}
/****************************************************************************
** QuickCheck - ALWAYS CALLED HOLDING THE io_mutex
** This is used to test for a 1401. It will try to do a quick check if all is
** OK, that is the 1401 was OK the last time it was asked, and there is no DMA
** in progress, and if the bTestBuff flag is set, the character buffers must be
** empty too. If the quick check shows that the state is still the same, then
** all is OK.
**
** If any of the above conditions are not met, or if the state or type of the
** 1401 has changed since the previous test, the full Is1401 test is done, but
** only if bCanReset is also TRUE.
**
** The return value is TRUE if a useable 1401 is found, FALSE if not
*/
bool QuickCheck(DEVICE_EXTENSION* pdx, bool bTestBuff, bool bCanReset)
{
bool bRet = false; // assume it will fail and we will reset
bool bShortTest;
bShortTest = ((pdx->dwDMAFlag == MODE_CHAR) && // no DMA running
(!pdx->bForceReset) && // Not had a real reset forced
(pdx->sCurrentState >= U14ERR_STD)); // No 1401 errors stored
dev_dbg(&pdx->interface->dev, "%s DMAFlag:%d, state:%d, force:%d, testBuff:%d, short:%d",
__func__, pdx->dwDMAFlag, pdx->sCurrentState, pdx->bForceReset, bTestBuff, bShortTest);
if ((bTestBuff) && // Buffer check requested, and...
(pdx->dwNumInput || pdx->dwNumOutput)) // ...characters were in the buffer?
{
bShortTest = false; // Then do the full test
dev_dbg(&pdx->interface->dev, "%s will reset as buffers not empty", __func__);
}
if (bShortTest || !bCanReset) // Still OK to try the short test?
{ // Always test if no reset - we want state update
unsigned int state, error;
dev_dbg(&pdx->interface->dev, "%s->Get1401State", __func__);
if (Get1401State(pdx, &state, &error) == U14ERR_NOERROR) // Check on the 1401 state
{
if ((state & 0xFF) == 0) // If call worked, check the status value
bRet = true; // If that was zero, all is OK, no reset needed
}
}
if (!bRet && bCanReset) // If all not OK, then
{
dev_info(&pdx->interface->dev, "%s->Is1401 %d %d %d %d",
__func__, bShortTest, pdx->sCurrentState, bTestBuff, pdx->bForceReset);
bRet = Is1401(pdx); // do full test
}
return bRet;
}
/****************************************************************************
** Reset1401
**
** Resets the 1401 and empties the i/o buffers
*****************************************************************************/
int Reset1401(DEVICE_EXTENSION *pdx)
{
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
dev_dbg(&pdx->interface->dev,"ABout to call QuickCheck");
QuickCheck(pdx, true, true); // Check 1401, reset if not OK
mutex_unlock(&pdx->io_mutex);
return U14ERR_NOERROR;
}
/****************************************************************************
** GetChar
**
** Gets a single character from the 1401
****************************************************************************/
int GetChar(DEVICE_EXTENSION *pdx)
{
int iReturn = U14ERR_NOIN; // assume we will get nothing
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
dev_dbg(&pdx->interface->dev, "GetChar");
Allowi(pdx, false); // Make sure char reads are running
SendChars(pdx); // and send any buffered chars
spin_lock_irq(&pdx->charInLock);
if (pdx->dwNumInput > 0) // worth looking
{
iReturn = pdx->inputBuffer[pdx->dwInBuffGet++];
if (pdx->dwInBuffGet >= INBUF_SZ)
pdx->dwInBuffGet = 0;
pdx->dwNumInput--;
}
else
iReturn = U14ERR_NOIN; // no input data to read
spin_unlock_irq(&pdx->charInLock);
Allowi(pdx, false); // Make sure char reads are running
mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
return iReturn;
}
/****************************************************************************
** GetString
**
** Gets a string from the 1401. Returns chars up to the next CR or when
** there are no more to read or nowhere to put them. CR is translated to
** 0 and counted as a character. If the string does not end in a 0, we will
** add one, if there is room, but it is not counted as a character.
**
** returns the count of characters (including the terminator, or 0 if none
** or a negative error code.
****************************************************************************/
int GetString(DEVICE_EXTENSION *pdx, char __user* pUser, int n)
{
int nAvailable; // character in the buffer
int iReturn = U14ERR_NOIN;
if (n <= 0)
return -ENOMEM;
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
Allowi(pdx, false); // Make sure char reads are running
SendChars(pdx); // and send any buffered chars
spin_lock_irq(&pdx->charInLock);
nAvailable = pdx->dwNumInput; // characters available now
if (nAvailable > n) // read max of space in pUser...
nAvailable = n; // ...or input characters
if (nAvailable > 0) // worth looking?
{
char buffer[INBUF_SZ+1]; // space for a linear copy of data
int nGot = 0;
int nCopyToUser; // number to copy to user
char cData;
do
{
cData = pdx->inputBuffer[pdx->dwInBuffGet++];
if (cData == CR_CHAR) // replace CR with zero
cData = (char)0;
if (pdx->dwInBuffGet >= INBUF_SZ)
pdx->dwInBuffGet = 0; // wrap buffer pointer
buffer[nGot++] = cData; // save the output
}
while((nGot < nAvailable) && cData);
nCopyToUser = nGot; // what to copy...
if (cData) // do we need null
{
buffer[nGot] = (char)0; // make it tidy
if (nGot < n) // if space in user buffer...
++nCopyToUser; // ...copy the 0 as well.
}
pdx->dwNumInput -= nGot;
spin_unlock_irq(&pdx->charInLock);
dev_dbg(&pdx->interface->dev,"GetString read %d characters >%s<", nGot, buffer);
copy_to_user(pUser, buffer, nCopyToUser);
iReturn = nGot; // report characters read
}
else
spin_unlock_irq(&pdx->charInLock);
Allowi(pdx, false); // Make sure char reads are running
mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
return iReturn;
}
/*******************************************************************************
** Get count of characters in the inout buffer.
*******************************************************************************/
int Stat1401(DEVICE_EXTENSION *pdx)
{
int iReturn;
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
Allowi(pdx, false); // make sure we allow pending chars
SendChars(pdx); // in both directions
iReturn = pdx->dwNumInput; // no lock as single read
mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
return iReturn;
}
/****************************************************************************
** LineCount
**
** Returns the number of newline chars in the buffer. There is no need for
** any fancy interlocks as we only read the interrupt routine data, and the
** system is arranged so nothing can be destroyed.
****************************************************************************/
int LineCount(DEVICE_EXTENSION *pdx)
{
int iReturn = 0; // will be count of line ends
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
Allowi(pdx, false); // Make sure char reads are running
SendChars(pdx); // and send any buffered chars
spin_lock_irq(&pdx->charInLock); // Get protection
if (pdx->dwNumInput > 0) // worth looking?
{
unsigned int dwIndex = pdx->dwInBuffGet;// start at first available
unsigned int dwEnd = pdx->dwInBuffPut; // Position for search end
do
{
if (pdx->inputBuffer[dwIndex++] == CR_CHAR)
++iReturn; // inc count if CR
if (dwIndex >= INBUF_SZ) // see if we fall off buff
dwIndex = 0;
}
while (dwIndex != dwEnd); // go to last avaliable
}
spin_unlock_irq(&pdx->charInLock);
dev_dbg(&pdx->interface->dev,"LineCount returned %d", iReturn);
mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
return iReturn;
}
/****************************************************************************
** GetOutBufSpace
**
** Gets the space in the output buffer. Called from user code.
*****************************************************************************/
int GetOutBufSpace(DEVICE_EXTENSION *pdx)
{
int iReturn;
mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
SendChars(pdx); // send any buffered chars
iReturn = (int)(OUTBUF_SZ - pdx->dwNumOutput); // no lock needed for single read
dev_dbg(&pdx->interface->dev,"OutBufSpace %d", iReturn);
mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
return iReturn;
}
/****************************************************************************
**
** ClearArea
**
** Clears up a transfer area. This is always called in the context of a user
** request, never from a call-back.
****************************************************************************/
int ClearArea(DEVICE_EXTENSION *pdx, int nArea)
{
int iReturn = U14ERR_NOERROR;
if ((nArea < 0) || (nArea >= MAX_TRANSAREAS))
{
iReturn = U14ERR_BADAREA;
dev_err(&pdx->interface->dev, "%s Attempt to clear area %d", __func__, nArea);
}
else
{
TRANSAREA *pTA = &pdx->rTransDef[nArea]; // to save typing
if (!pTA->bUsed) // if not used...
iReturn = U14ERR_NOTSET; // ...nothing to be done
else
{
// We must save the memory we return as we shouldn't mess with memory while
// holding a spin lock.
struct page **pPages = 0; // save page address list
int nPages = 0; // and number of pages
int np;
dev_dbg(&pdx->interface->dev, "%s area %d", __func__, nArea);
spin_lock_irq(&pdx->stagedLock);
if ((pdx->StagedId == nArea) && (pdx->dwDMAFlag > MODE_CHAR))
{
iReturn = U14ERR_UNLOCKFAIL; // cannot delete as in use
dev_err(&pdx->interface->dev, "%s call on area %d while active", __func__, nArea);
}
else
{
pPages = pTA->pPages; // save page address list
nPages = pTA->nPages; // and page count
if (pTA->dwEventSz) // if events flagging in use
wake_up_interruptible(&pTA->wqEvent); // release anything that was waiting
if (pdx->bXFerWaiting && (pdx->rDMAInfo.wIdent == nArea))
pdx->bXFerWaiting = false; // Cannot have pending xfer if area cleared
// Clean out the TRANSAREA except for the wait queue, which is at the end
// This sets bUsed to false and dwEventSz to 0 to say area not used and no events.
memset(pTA, 0, sizeof(TRANSAREA)-sizeof(wait_queue_head_t));
}
spin_unlock_irq(&pdx->stagedLock);
if (pPages) // if we decided to release the memory
{
// Now we must undo the pinning down of the pages. We will assume the worst and mark
// all the pages as dirty. Don't be tempted to move this up above as you must not be
// holding a spin lock to do this stuff as it is not atomic.
dev_dbg(&pdx->interface->dev, "%s nPages=%d", __func__, nPages);
for (np = 0; np < nPages; ++np)
{
if (pPages[np])
{
SetPageDirty(pPages[np]);
page_cache_release(pPages[np]);
}
}
kfree(pPages);
dev_dbg(&pdx->interface->dev, "%s kfree(pPages) done", __func__);
}
}
}
return iReturn;
}
/****************************************************************************
** SetArea
**
** Sets up a transfer area - the functional part. Called by both
** SetTransfer and SetCircular.
****************************************************************************/
static int SetArea(DEVICE_EXTENSION *pdx, int nArea, char __user* puBuf,
unsigned int dwLength, bool bCircular, bool bCircToHost)
{
// Start by working out the page aligned start of the area and the size
// of the area in pages, allowing for the start not being aligned and the
// end needing to be rounded up to a page boundary.
unsigned long ulStart = ((unsigned long)puBuf) & PAGE_MASK;
unsigned int ulOffset = ((unsigned long)puBuf) & (PAGE_SIZE-1);
int len = (dwLength + ulOffset+PAGE_SIZE - 1) >> PAGE_SHIFT;
TRANSAREA *pTA = &pdx->rTransDef[nArea]; // to save typing
struct page **pPages = 0; // space for page tables
int nPages = 0; // and number of pages
int iReturn = ClearArea(pdx, nArea); // see if OK to use this area
if ((iReturn != U14ERR_NOTSET) && // if not area unused and...
(iReturn != U14ERR_NOERROR)) // ...not all OK, then...
return iReturn; // ...we cannot use this area
if (!access_ok(VERIFY_WRITE, puBuf, dwLength)) // if we cannot access the memory...
return -EFAULT; // ...then we are done
// Now allocate space to hold the page pointer and virtual address pointer tables
pPages = (struct page **)kmalloc(len*sizeof(struct page *), GFP_KERNEL);
if (!pPages)
{
iReturn = U14ERR_NOMEMORY;
goto error;
}
dev_dbg(&pdx->interface->dev, "%s %p, length=%06x, circular %d", __func__, puBuf, dwLength, bCircular);
// To pin down user pages we must first acquire the mapping semaphore.
down_read(¤t->mm->mmap_sem); // get memory map semaphore
nPages = get_user_pages(current, current->mm, ulStart, len, 1, 0, pPages, 0);
up_read(¤t->mm->mmap_sem); // release the semaphore
dev_dbg(&pdx->interface->dev, "%s nPages = %d", __func__, nPages);
if (nPages > 0) // if we succeeded
{
// If you are tempted to use page_address (form LDD3), forget it. You MUST use
// kmap() or kmap_atomic() to get a virtual address. page_address will give you
// (null) or at least it does in this context with an x86 machine.
spin_lock_irq(&pdx->stagedLock);
pTA->lpvBuff = puBuf; // keep start of region (user address)
pTA->dwBaseOffset = ulOffset; // save offset in first page to start of xfer
pTA->dwLength = dwLength; // Size if the region in bytes
pTA->pPages = pPages; // list of pages that are used by buffer
pTA->nPages = nPages; // number of pages
pTA->bCircular = bCircular;
pTA->bCircToHost = bCircToHost;
pTA->aBlocks[0].dwOffset = 0;
pTA->aBlocks[0].dwSize = 0;
pTA->aBlocks[1].dwOffset = 0;
pTA->aBlocks[1].dwSize = 0;
pTA->bUsed = true; // This is now a used block
spin_unlock_irq(&pdx->stagedLock);
iReturn = U14ERR_NOERROR; // say all was well
}
else
{
iReturn = U14ERR_LOCKFAIL;
goto error;
}
return iReturn;
error:
kfree(pPages);
return iReturn;
}
/****************************************************************************
** SetTransfer
**
** Sets up a transfer area record. If the area is already set, we attempt to
** unset it. Unsetting will fail if the area is booked, and a transfer to that
** area is in progress. Otherwise, we will release the area and re-assign it.
****************************************************************************/
int SetTransfer(DEVICE_EXTENSION *pdx, TRANSFERDESC __user *pTD)
{
int iReturn;
TRANSFERDESC td;
copy_from_user(&td, pTD, sizeof(td));
mutex_lock(&pdx->io_mutex);
dev_dbg(&pdx->interface->dev,"%s area:%d, size:%08x", __func__, td.wAreaNum, td.dwLength);
// The strange cast is done so that we don't get warnings in 32-bit linux about the size of the
// pointer. The pointer is always passed as a 64-bit object so that we don't have problems using
// a 32-bit program on a 64-bit system. unsigned long is 64-bits on a 64-bit system.
iReturn = SetArea(pdx, td.wAreaNum, (char __user *)((unsigned long)td.lpvBuff), td.dwLength, false, false);
mutex_unlock(&pdx->io_mutex);
return iReturn;
}
/****************************************************************************
** UnSetTransfer
** Erases a transfer area record
****************************************************************************/
int UnsetTransfer(DEVICE_EXTENSION *pdx, int nArea)
{
int iReturn;
mutex_lock(&pdx->io_mutex);
iReturn = ClearArea(pdx, nArea);
mutex_unlock(&pdx->io_mutex);
return iReturn;
}
/****************************************************************************
** SetEvent
** Creates an event that we can test for based on a transfer to/from an area.
** The area must be setup for a transfer. We attempt to simulate the Windows
** driver behavior for events (as we don't actually use them), which is to
** pretend that whatever the user asked for was achieved, so we return 1 if
** try to create one, and 0 if they ask to remove (assuming all else was OK).
****************************************************************************/
int SetEvent(DEVICE_EXTENSION *pdx, TRANSFEREVENT __user*pTE)
{
int iReturn = U14ERR_NOERROR;
TRANSFEREVENT te;
copy_from_user(&te, pTE, sizeof(te)); // get a local copy of the data
if (te.wAreaNum >= MAX_TRANSAREAS) // the area must exist
return U14ERR_BADAREA;
else
{
TRANSAREA *pTA = &pdx->rTransDef[te.wAreaNum];
mutex_lock(&pdx->io_mutex); // make sure we have no competitor
spin_lock_irq(&pdx->stagedLock);
if (pTA->bUsed) // area must be in use
{
pTA->dwEventSt = te.dwStart; // set area regions
pTA->dwEventSz = te.dwLength; // set size (0 cancels it)
pTA->bEventToHost = te.wFlags & 1; // set the direction
pTA->iWakeUp = 0; // zero the wake up count
}
else
iReturn = U14ERR_NOTSET;
spin_unlock_irq(&pdx->stagedLock);
mutex_unlock(&pdx->io_mutex);
}
return iReturn == U14ERR_NOERROR ? (te.iSetEvent ? 1 : U14ERR_NOERROR) : iReturn;
}
/****************************************************************************
** WaitEvent
** Sleep the process with a timeout waiting for an event. Returns the number
** of times that a block met the event condition since we last cleared it or
** 0 if timed out, or -ve error (bad area or not set, or signal).
****************************************************************************/
int WaitEvent(DEVICE_EXTENSION *pdx, int nArea, int msTimeOut)
{
int iReturn;
if ((unsigned)nArea > MAX_TRANSAREAS)
return U14ERR_BADAREA;
else
{
int iWait;
TRANSAREA *pTA = &pdx->rTransDef[nArea];
msTimeOut = (msTimeOut * HZ + 999)/1000; // convert timeout to jiffies
// We cannot wait holding the mutex, but we check the flags while holding
// it. This may well be pointless as another thread could get in between
// releasing it and the wait call. However, this would have to clear the
// iWakeUp flag. However, the !pTA-bUsed may help us in this case.
mutex_lock(&pdx->io_mutex); // make sure we have no competitor
if (!pTA->bUsed || !pTA->dwEventSz) // check something to wait for...
return U14ERR_NOTSET; // ...else we do nothing
mutex_unlock(&pdx->io_mutex);
if (msTimeOut)
iWait = wait_event_interruptible_timeout(pTA->wqEvent, pTA->iWakeUp || !pTA->bUsed, msTimeOut);
else
iWait = wait_event_interruptible(pTA->wqEvent, pTA->iWakeUp || !pTA->bUsed);
if (iWait)
iReturn = -ERESTARTSYS; // oops - we have had a SIGNAL
else
iReturn = pTA->iWakeUp; // else the wakeup count
spin_lock_irq(&pdx->stagedLock);
pTA->iWakeUp = 0; // clear the flag
spin_unlock_irq(&pdx->stagedLock);
}
return iReturn;
}
/****************************************************************************
** TestEvent
** Test the event to see if a WaitEvent would return immediately. Returns the
** number of times a block completed since the last call, or 0 if none or a
** negative error.
****************************************************************************/
int TestEvent(DEVICE_EXTENSION *pdx, int nArea)
{
int iReturn;
if ((unsigned)nArea > MAX_TRANSAREAS)
iReturn = U14ERR_BADAREA;
else
{
TRANSAREA *pTA = &pdx->rTransDef[nArea];
mutex_lock(&pdx->io_mutex); // make sure we have no competitor
spin_lock_irq(&pdx->stagedLock);
iReturn = pTA->iWakeUp; // get wakeup count since last call
pTA->iWakeUp = 0; // clear the count
spin_unlock_irq(&pdx->stagedLock);
mutex_unlock(&pdx->io_mutex);
}
return iReturn;
}
/****************************************************************************
** GetTransferInfo
** Puts the current state of the 1401 in a TGET_TX_BLOCK.
*****************************************************************************/
int GetTransfer(DEVICE_EXTENSION *pdx, TGET_TX_BLOCK __user *pTX)
{
int iReturn = U14ERR_NOERROR;
unsigned int dwIdent;
mutex_lock(&pdx->io_mutex);
dwIdent = pdx->StagedId; // area ident for last xfer
if (dwIdent >= MAX_TRANSAREAS)
iReturn = U14ERR_BADAREA;
else
{
// Return the best information we have - we don't have physical addresses
TGET_TX_BLOCK tx;
memset(&tx, 0, sizeof(tx)); // clean out local work structure
tx.size = pdx->rTransDef[dwIdent].dwLength;
tx.linear = (long long)((long)pdx->rTransDef[dwIdent].lpvBuff);
tx.avail = GET_TX_MAXENTRIES; // how many blocks we could return
tx.used = 1; // number we actually return
tx.entries[0].physical = (long long)(tx.linear+pdx->StagedOffset);
tx.entries[0].size = tx.size;
copy_to_user(pTX, &tx, sizeof(tx));
}
mutex_unlock(&pdx->io_mutex);
return iReturn;
}
/****************************************************************************
** KillIO1401
**
** Empties the host i/o buffers
****************************************************************************/
int KillIO1401(DEVICE_EXTENSION *pdx)
{
dev_dbg(&pdx->interface->dev, "%s", __func__);
mutex_lock(&pdx->io_mutex);
FlushOutBuff(pdx);
FlushInBuff(pdx);
mutex_unlock(&pdx->io_mutex);
return U14ERR_NOERROR;
}
/****************************************************************************
** BlkTransState
** Returns a 0 or a 1 for whether DMA is happening. No point holding a mutex
** for this as it only does one read.
*****************************************************************************/
int BlkTransState(DEVICE_EXTENSION *pdx)
{
int iReturn = pdx->dwDMAFlag != MODE_CHAR;
dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
return iReturn;
}
/****************************************************************************
** StateOf1401
**
** Puts the current state of the 1401 in the Irp return buffer.
*****************************************************************************/
int StateOf1401(DEVICE_EXTENSION *pdx)
{
int iReturn;
mutex_lock(&pdx->io_mutex);
QuickCheck(pdx, false, false); // get state up to date, no reset
iReturn = pdx->sCurrentState;
mutex_unlock(&pdx->io_mutex);
dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
return iReturn;
}
/****************************************************************************
** StartSelfTest
**
** Initiates a self-test cycle. The assumption is that we have no interrupts
** active, so we should make sure that this is the case.
*****************************************************************************/
int StartSelfTest(DEVICE_EXTENSION *pdx)
{
int nGot;
mutex_lock(&pdx->io_mutex);
dev_dbg(&pdx->interface->dev, "%s", __func__);
ced_draw_down(pdx); // wait for, then kill outstanding Urbs
FlushInBuff(pdx); // Clear out input buffer & pipe
FlushOutBuff(pdx); // Clear output buffer & pipe
// ReadWrite_Cancel(pDeviceObject); /* so things stay tidy */
pdx->dwDMAFlag = MODE_CHAR; /* Clear DMA mode flags here */
nGot = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
DB_SELFTEST, (H_TO_D|VENDOR|DEVREQ), 0, 0,
0, 0, HZ); // allow 1 second timeout
pdx->ulSelfTestTime = jiffies + HZ*30; // 30 seconds into the future
mutex_unlock(&pdx->io_mutex);
if (nGot < 0)
dev_err(&pdx->interface->dev, "%s err=%d", __func__, nGot);
return nGot < 0 ? U14ERR_FAIL : U14ERR_NOERROR;
}
/****************************************************************************
** CheckSelfTest
**
** Check progress of a self-test cycle
****************************************************************************/
int CheckSelfTest(DEVICE_EXTENSION *pdx, TGET_SELFTEST __user *pGST)
{
unsigned int state, error;
int iReturn;
TGET_SELFTEST gst; // local work space
memset(&gst, 0, sizeof(gst)); // clear out the space (sets code 0)
mutex_lock(&pdx->io_mutex);