Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / ced1401 / ced_ioc.c
1 /* ced_ioc.c
2 ioctl part of the 1401 usb device driver for linux.
3 Copyright (C) 2010 Cambridge Electronic Design Ltd
4 Author Greg P Smith (greg@ced.co.uk)
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 ** FlushOutBuff
37 **
38 ** Empties the Output buffer and sets int lines. Used from user level only
39 ****************************************************************************/
40 void FlushOutBuff(DEVICE_EXTENSION * pdx)
41 {
42 dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__,
43 pdx->sCurrentState);
44 if (pdx->sCurrentState == U14ERR_TIME) /* Do nothing if hardware in trouble */
45 return;
46 // CharSend_Cancel(pdx); /* Kill off any pending I/O */
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 ** FlushInBuff
57 **
58 ** Empties the input buffer and sets int lines
59 ****************************************************************************/
60 void FlushInBuff(DEVICE_EXTENSION * pdx)
61 {
62 dev_dbg(&pdx->interface->dev, "%s currentState=%d", __func__,
63 pdx->sCurrentState);
64 if (pdx->sCurrentState == U14ERR_TIME) /* Do nothing if hardware in trouble */
65 return;
66 // CharRead_Cancel(pDevObject); /* Kill off any pending I/O */
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 ** PutChars
76 **
77 ** Utility routine to copy chars into the output buffer and fire them off.
78 ** called from user mode, holds charOutLock.
79 ****************************************************************************/
80 static int PutChars(DEVICE_EXTENSION * pdx, const char *pCh,
81 unsigned int uCount)
82 {
83 int iReturn;
84 spin_lock_irq(&pdx->charOutLock); // get the output spin lock
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); // ...give a chance to transmit data
95 } else {
96 iReturn = U14ERR_NOOUT; // no room at the out (ha-ha)
97 spin_unlock_irq(&pdx->charOutLock);
98 }
99 return iReturn;
100 }
101
102 /*****************************************************************************
103 ** Add the data in pData (local pointer) of length n to the output buffer, and
104 ** trigger an output transfer if this is appropriate. User mode.
105 ** Holds the io_mutex
106 *****************************************************************************/
107 int SendString(DEVICE_EXTENSION * pdx, const char __user * pData,
108 unsigned int n)
109 {
110 int iReturn = U14ERR_NOERROR; // assume all will be well
111 char buffer[OUTBUF_SZ + 1]; // space in our address space for characters
112 if (n > OUTBUF_SZ) // check space in local buffer...
113 return U14ERR_NOOUT; // ...too many characters
114 if (copy_from_user(buffer, pData, n))
115 return -EFAULT;
116 buffer[n] = 0; // terminate for debug purposes
117
118 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
119 if (n > 0) // do nothing if nowt to do!
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, false); // make sure we have input int
127 mutex_unlock(&pdx->io_mutex);
128
129 return iReturn;
130 }
131
132 /****************************************************************************
133 ** SendChar
134 **
135 ** Sends a single character to the 1401. User mode, holds io_mutex.
136 ****************************************************************************/
137 int SendChar(DEVICE_EXTENSION * pdx, char c)
138 {
139 int iReturn;
140 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
141 iReturn = PutChars(pdx, &c, 1);
142 dev_dbg(&pdx->interface->dev, "SendChar >%c< (0x%02x)", c, c);
143 Allowi(pdx, false); // Make sure char reads are running
144 mutex_unlock(&pdx->io_mutex);
145 return iReturn;
146 }
147
148 /***************************************************************************
149 **
150 ** Get1401State
151 **
152 ** Retrieves state information from the 1401, adjusts the 1401 state held
153 ** in the device extension to indicate the current 1401 type.
154 **
155 ** *state is updated with information about the 1401 state as returned by the
156 ** 1401. The low byte is a code for what 1401 is doing:
157 **
158 ** 0 normal 1401 operation
159 ** 1 sending chars to host
160 ** 2 sending block data to host
161 ** 3 reading block data from host
162 ** 4 sending an escape sequence to the host
163 ** 0x80 1401 is executing self-test, in which case the upper word
164 ** is the last error code seen (or zero for no new error).
165 **
166 ** *error is updated with error information if a self-test error code
167 ** is returned in the upper word of state.
168 **
169 ** both state and error are set to -1 if there are comms problems, and
170 ** to zero if there is a simple failure.
171 **
172 ** return error code (U14ERR_NOERROR for OK)
173 */
174 int Get1401State(DEVICE_EXTENSION * pdx, __u32 * state, __u32 * error)
175 {
176 int nGot;
177 dev_dbg(&pdx->interface->dev, "Get1401State() entry");
178
179 *state = 0xFFFFFFFF; // Start off with invalid state
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; // Indicate that things are very wrong indeed
187 *state = 0; // Force status values to a known state
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]; // Return the state values to the calling code
196 *error = pdx->statBuf[1];
197
198 nDevice = pdx->udev->descriptor.bcdDevice >> 8; // 1401 type code value
199 switch (nDevice) // so we can clean up current state
200 {
201 case 0:
202 pdx->sCurrentState = U14ERR_U1401;
203 break;
204
205 default: // allow lots of device codes for future 1401s
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 ** ReadWrite_Cancel
219 **
220 ** Kills off staged read\write request from the USB if one is pending.
221 ****************************************************************************/
222 int 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 // We can fill this in when we know how we will implement the staged transfer stuff
231 spin_lock_irq(&pdx->stagedLock);
232
233 if (pdx->bStagedUrbPending) // anything to be cancelled? May need more...
234 {
235 dev_info(&pdx->interface - dev,
236 "ReadWrite_Cancel about to cancel Urb");
237
238 // KeClearEvent(&pdx->StagingDoneEvent); // Clear the staging done flag
239 USB_ASSERT(pdx->pStagedIrp != NULL);
240
241 // Release the spinlock first otherwise the completion routine may hang
242 // on the spinlock while this function hands waiting for the event.
243 spin_unlock_irq(&pdx->stagedLock);
244 bResult = IoCancelIrp(pdx->pStagedIrp); // Actually do the cancel
245 if (bResult) {
246 LARGE_INTEGER timeout;
247 timeout.QuadPart = -10000000; // Use a timeout of 1 second
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 ** InSelfTest - utility to check in self test. Return 1 for ST, 0 for not or
275 ** a -ve error code if we failed for some reason.
276 ***************************************************************************/
277 static int InSelfTest(DEVICE_EXTENSION * pdx, unsigned int *pState)
278 {
279 unsigned int state, error;
280 int iReturn = Get1401State(pdx, &state, &error); // see if in self-test
281 if (iReturn == U14ERR_NOERROR) // if all still OK
282 iReturn = (state == (unsigned int)-1) || // TX problem or...
283 ((state & 0xff) == 0x80); // ...self test
284 *pState = state; // return actual state
285 return iReturn;
286 }
287
288 /***************************************************************************
289 ** Is1401 - ALWAYS CALLED HOLDING THE io_mutex
290 **
291 ** Tests for the current state of the 1401. Sets sCurrentState:
292 **
293 ** U14ERR_NOIF 1401 i/f card not installed (not done here)
294 ** U14ERR_OFF 1401 apparently not switched on
295 ** U14ERR_NC 1401 appears to be not connected
296 ** U14ERR_ILL 1401 if it is there its not very well at all
297 ** U14ERR_TIME 1401 appears OK, but doesn't communicate - very bad
298 ** U14ERR_STD 1401 OK and ready for use
299 ** U14ERR_PLUS 1401+ OK and ready for use
300 ** U14ERR_U1401 Micro1401 OK and ready for use
301 ** U14ERR_POWER Power1401 OK and ready for use
302 ** U14ERR_U14012 Micro1401 mkII OK and ready for use
303 **
304 ** Returns TRUE if a 1401 detected and OK, else FALSE
305 ****************************************************************************/
306 bool Is1401(DEVICE_EXTENSION * pdx)
307 {
308 int iReturn;
309 dev_dbg(&pdx->interface->dev, "%s", __func__);
310
311 ced_draw_down(pdx); // wait for, then kill outstanding Urbs
312 FlushInBuff(pdx); // Clear out input buffer & pipe
313 FlushOutBuff(pdx); // Clear output buffer & pipe
314
315 // The next call returns 0 if OK, but has returned 1 in the past, meaning that
316 // usb_unlock_device() is needed... now it always is
317 iReturn = usb_lock_device_for_reset(pdx->udev, pdx->interface);
318
319 // release the io_mutex because if we don't, we will deadlock due to system
320 // calls back into the driver.
321 mutex_unlock(&pdx->io_mutex); // locked, so we will not get system calls
322 if (iReturn >= 0) // if we failed
323 {
324 iReturn = usb_reset_device(pdx->udev); // try to do the reset
325 usb_unlock_device(pdx->udev); // undo the lock
326 }
327
328 mutex_lock(&pdx->io_mutex); // hold stuff off while we wait
329 pdx->dwDMAFlag = MODE_CHAR; // Clear DMA mode flag regardless!
330 if (iReturn == 0) // if all is OK still
331 {
332 unsigned int state;
333 iReturn = InSelfTest(pdx, &state); // see if likely in self test
334 if (iReturn > 0) // do we need to wait for self-test?
335 {
336 unsigned long ulTimeOut = jiffies + 30 * HZ; // when to give up
337 while ((iReturn > 0) && time_before(jiffies, ulTimeOut)) {
338 schedule(); // let other stuff run
339 iReturn = InSelfTest(pdx, &state); // see if done yet
340 }
341 }
342
343 if (iReturn == 0) // if all is OK...
344 iReturn = state == 0; // then sucess is that the state is 0
345 } else
346 iReturn = 0; // we failed
347 pdx->bForceReset = false; // Clear forced reset flag now
348
349 return iReturn > 0;
350 }
351
352 /****************************************************************************
353 ** QuickCheck - ALWAYS CALLED HOLDING THE io_mutex
354 ** This is used to test for a 1401. It will try to do a quick check if all is
355 ** OK, that is the 1401 was OK the last time it was asked, and there is no DMA
356 ** in progress, and if the bTestBuff flag is set, the character buffers must be
357 ** empty too. If the quick check shows that the state is still the same, then
358 ** all is OK.
359 **
360 ** If any of the above conditions are not met, or if the state or type of the
361 ** 1401 has changed since the previous test, the full Is1401 test is done, but
362 ** only if bCanReset is also TRUE.
363 **
364 ** The return value is TRUE if a useable 1401 is found, FALSE if not
365 */
366 bool QuickCheck(DEVICE_EXTENSION * pdx, bool bTestBuff, bool bCanReset)
367 {
368 bool bRet = false; // assume it will fail and we will reset
369 bool bShortTest;
370
371 bShortTest = ((pdx->dwDMAFlag == MODE_CHAR) && // no DMA running
372 (!pdx->bForceReset) && // Not had a real reset forced
373 (pdx->sCurrentState >= U14ERR_STD)); // No 1401 errors stored
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) && // Buffer check requested, and...
381 (pdx->dwNumInput || pdx->dwNumOutput)) // ...characters were in the buffer?
382 {
383 bShortTest = false; // Then do the full test
384 dev_dbg(&pdx->interface->dev,
385 "%s will reset as buffers not empty", __func__);
386 }
387
388 if (bShortTest || !bCanReset) // Still OK to try the short test?
389 { // Always test if no reset - we want state update
390 unsigned int state, error;
391 dev_dbg(&pdx->interface->dev, "%s->Get1401State", __func__);
392 if (Get1401State(pdx, &state, &error) == U14ERR_NOERROR) // Check on the 1401 state
393 {
394 if ((state & 0xFF) == 0) // If call worked, check the status value
395 bRet = true; // If that was zero, all is OK, no reset needed
396 }
397 }
398
399 if (!bRet && bCanReset) // If all not OK, then
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); // do full test
405 }
406
407 return bRet;
408 }
409
410 /****************************************************************************
411 ** Reset1401
412 **
413 ** Resets the 1401 and empties the i/o buffers
414 *****************************************************************************/
415 int Reset1401(DEVICE_EXTENSION * pdx)
416 {
417 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
418 dev_dbg(&pdx->interface->dev, "ABout to call QuickCheck");
419 QuickCheck(pdx, true, true); // Check 1401, reset if not OK
420 mutex_unlock(&pdx->io_mutex);
421 return U14ERR_NOERROR;
422 }
423
424 /****************************************************************************
425 ** GetChar
426 **
427 ** Gets a single character from the 1401
428 ****************************************************************************/
429 int GetChar(DEVICE_EXTENSION * pdx)
430 {
431 int iReturn = U14ERR_NOIN; // assume we will get nothing
432 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
433
434 dev_dbg(&pdx->interface->dev, "GetChar");
435
436 Allowi(pdx, false); // Make sure char reads are running
437 SendChars(pdx); // and send any buffered chars
438
439 spin_lock_irq(&pdx->charInLock);
440 if (pdx->dwNumInput > 0) // worth looking
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; // no input data to read
448 spin_unlock_irq(&pdx->charInLock);
449
450 Allowi(pdx, false); // Make sure char reads are running
451
452 mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
453 return iReturn;
454 }
455
456 /****************************************************************************
457 ** GetString
458 **
459 ** Gets a string from the 1401. Returns chars up to the next CR or when
460 ** there are no more to read or nowhere to put them. CR is translated to
461 ** 0 and counted as a character. If the string does not end in a 0, we will
462 ** add one, if there is room, but it is not counted as a character.
463 **
464 ** returns the count of characters (including the terminator, or 0 if none
465 ** or a negative error code.
466 ****************************************************************************/
467 int GetString(DEVICE_EXTENSION * pdx, char __user * pUser, int n)
468 {
469 int nAvailable; // character in the buffer
470 int iReturn = U14ERR_NOIN;
471 if (n <= 0)
472 return -ENOMEM;
473
474 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
475 Allowi(pdx, false); // Make sure char reads are running
476 SendChars(pdx); // and send any buffered chars
477
478 spin_lock_irq(&pdx->charInLock);
479 nAvailable = pdx->dwNumInput; // characters available now
480 if (nAvailable > n) // read max of space in pUser...
481 nAvailable = n; // ...or input characters
482
483 if (nAvailable > 0) // worth looking?
484 {
485 char buffer[INBUF_SZ + 1]; // space for a linear copy of data
486 int nGot = 0;
487 int nCopyToUser; // number to copy to user
488 char cData;
489 do {
490 cData = pdx->inputBuffer[pdx->dwInBuffGet++];
491 if (cData == CR_CHAR) // replace CR with zero
492 cData = (char)0;
493
494 if (pdx->dwInBuffGet >= INBUF_SZ)
495 pdx->dwInBuffGet = 0; // wrap buffer pointer
496
497 buffer[nGot++] = cData; // save the output
498 }
499 while ((nGot < nAvailable) && cData);
500
501 nCopyToUser = nGot; // what to copy...
502 if (cData) // do we need null
503 {
504 buffer[nGot] = (char)0; // make it tidy
505 if (nGot < n) // if space in user buffer...
506 ++nCopyToUser; // ...copy the 0 as well.
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; // report characters read
518 } else
519 spin_unlock_irq(&pdx->charInLock);
520
521 Allowi(pdx, false); // Make sure char reads are running
522 mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
523
524 return iReturn;
525 }
526
527 /*******************************************************************************
528 ** Get count of characters in the inout buffer.
529 *******************************************************************************/
530 int Stat1401(DEVICE_EXTENSION * pdx)
531 {
532 int iReturn;
533 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
534 Allowi(pdx, false); // make sure we allow pending chars
535 SendChars(pdx); // in both directions
536 iReturn = pdx->dwNumInput; // no lock as single read
537 mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
538 return iReturn;
539 }
540
541 /****************************************************************************
542 ** LineCount
543 **
544 ** Returns the number of newline chars in the buffer. There is no need for
545 ** any fancy interlocks as we only read the interrupt routine data, and the
546 ** system is arranged so nothing can be destroyed.
547 ****************************************************************************/
548 int LineCount(DEVICE_EXTENSION * pdx)
549 {
550 int iReturn = 0; // will be count of line ends
551
552 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
553 Allowi(pdx, false); // Make sure char reads are running
554 SendChars(pdx); // and send any buffered chars
555 spin_lock_irq(&pdx->charInLock); // Get protection
556
557 if (pdx->dwNumInput > 0) // worth looking?
558 {
559 unsigned int dwIndex = pdx->dwInBuffGet; // start at first available
560 unsigned int dwEnd = pdx->dwInBuffPut; // Position for search end
561 do {
562 if (pdx->inputBuffer[dwIndex++] == CR_CHAR)
563 ++iReturn; // inc count if CR
564
565 if (dwIndex >= INBUF_SZ) // see if we fall off buff
566 dwIndex = 0;
567 }
568 while (dwIndex != dwEnd); // go to last avaliable
569 }
570
571 spin_unlock_irq(&pdx->charInLock);
572 dev_dbg(&pdx->interface->dev, "LineCount returned %d", iReturn);
573 mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
574 return iReturn;
575 }
576
577 /****************************************************************************
578 ** GetOutBufSpace
579 **
580 ** Gets the space in the output buffer. Called from user code.
581 *****************************************************************************/
582 int GetOutBufSpace(DEVICE_EXTENSION * pdx)
583 {
584 int iReturn;
585 mutex_lock(&pdx->io_mutex); // Protect disconnect from new i/o
586 SendChars(pdx); // send any buffered chars
587 iReturn = (int)(OUTBUF_SZ - pdx->dwNumOutput); // no lock needed for single read
588 dev_dbg(&pdx->interface->dev, "OutBufSpace %d", iReturn);
589 mutex_unlock(&pdx->io_mutex); // Protect disconnect from new i/o
590 return iReturn;
591 }
592
593 /****************************************************************************
594 **
595 ** ClearArea
596 **
597 ** Clears up a transfer area. This is always called in the context of a user
598 ** request, never from a call-back.
599 ****************************************************************************/
600 int 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]; // to save typing
610 if (!pTA->bUsed) // if not used...
611 iReturn = U14ERR_NOTSET; // ...nothing to be done
612 else {
613 // We must save the memory we return as we shouldn't mess with memory while
614 // holding a spin lock.
615 struct page **pPages = 0; // save page address list
616 int nPages = 0; // and number of pages
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; // cannot delete as in use
625 dev_err(&pdx->interface->dev,
626 "%s call on area %d while active",
627 __func__, nArea);
628 } else {
629 pPages = pTA->pPages; // save page address list
630 nPages = pTA->nPages; // and page count
631 if (pTA->dwEventSz) // if events flagging in use
632 wake_up_interruptible(&pTA->wqEvent); // release anything that was waiting
633
634 if (pdx->bXFerWaiting
635 && (pdx->rDMAInfo.wIdent == nArea))
636 pdx->bXFerWaiting = false; // Cannot have pending xfer if area cleared
637
638 // Clean out the TRANSAREA except for the wait queue, which is at the end
639 // This sets bUsed to false and dwEventSz to 0 to say area not used and no events.
640 memset(pTA, 0,
641 sizeof(TRANSAREA) -
642 sizeof(wait_queue_head_t));
643 }
644 spin_unlock_irq(&pdx->stagedLock);
645
646 if (pPages) // if we decided to release the memory
647 {
648 // Now we must undo the pinning down of the pages. We will assume the worst and mark
649 // all the pages as dirty. Don't be tempted to move this up above as you must not be
650 // holding a spin lock to do this stuff as it is not atomic.
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 ** SetArea
673 **
674 ** Sets up a transfer area - the functional part. Called by both
675 ** SetTransfer and SetCircular.
676 ****************************************************************************/
677 static int SetArea(DEVICE_EXTENSION * pdx, int nArea, char __user * puBuf,
678 unsigned int dwLength, bool bCircular, bool bCircToHost)
679 {
680 // Start by working out the page aligned start of the area and the size
681 // of the area in pages, allowing for the start not being aligned and the
682 // end needing to be rounded up to a page boundary.
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]; // to save typing
688 struct page **pPages = 0; // space for page tables
689 int nPages = 0; // and number of pages
690
691 int iReturn = ClearArea(pdx, nArea); // see if OK to use this area
692 if ((iReturn != U14ERR_NOTSET) && // if not area unused and...
693 (iReturn != U14ERR_NOERROR)) // ...not all OK, then...
694 return iReturn; // ...we cannot use this area
695
696 if (!access_ok(VERIFY_WRITE, puBuf, dwLength)) // if we cannot access the memory...
697 return -EFAULT; // ...then we are done
698
699 // Now allocate space to hold the page pointer and virtual address pointer tables
700 pPages =
701 (struct page **)kmalloc(len * sizeof(struct page *), GFP_KERNEL);
702 if (!pPages) {
703 iReturn = U14ERR_NOMEMORY;
704 goto error;
705 }
706 dev_dbg(&pdx->interface->dev, "%s %p, length=%06x, circular %d",
707 __func__, puBuf, dwLength, bCircular);
708
709 // To pin down user pages we must first acquire the mapping semaphore.
710 down_read(&current->mm->mmap_sem); // get memory map semaphore
711 nPages =
712 get_user_pages(current, current->mm, ulStart, len, 1, 0, pPages, 0);
713 up_read(&current->mm->mmap_sem); // release the semaphore
714 dev_dbg(&pdx->interface->dev, "%s nPages = %d", __func__, nPages);
715
716 if (nPages > 0) // if we succeeded
717 {
718 // If you are tempted to use page_address (form LDD3), forget it. You MUST use
719 // kmap() or kmap_atomic() to get a virtual address. page_address will give you
720 // (null) or at least it does in this context with an x86 machine.
721 spin_lock_irq(&pdx->stagedLock);
722 pTA->lpvBuff = puBuf; // keep start of region (user address)
723 pTA->dwBaseOffset = ulOffset; // save offset in first page to start of xfer
724 pTA->dwLength = dwLength; // Size if the region in bytes
725 pTA->pPages = pPages; // list of pages that are used by buffer
726 pTA->nPages = nPages; // number of pages
727
728 pTA->bCircular = bCircular;
729 pTA->bCircToHost = bCircToHost;
730
731 pTA->aBlocks[0].dwOffset = 0;
732 pTA->aBlocks[0].dwSize = 0;
733 pTA->aBlocks[1].dwOffset = 0;
734 pTA->aBlocks[1].dwSize = 0;
735 pTA->bUsed = true; // This is now a used block
736
737 spin_unlock_irq(&pdx->stagedLock);
738 iReturn = U14ERR_NOERROR; // say all was well
739 } else {
740 iReturn = U14ERR_LOCKFAIL;
741 goto error;
742 }
743
744 return iReturn;
745
746 error:
747 kfree(pPages);
748 return iReturn;
749 }
750
751 /****************************************************************************
752 ** SetTransfer
753 **
754 ** Sets up a transfer area record. If the area is already set, we attempt to
755 ** unset it. Unsetting will fail if the area is booked, and a transfer to that
756 ** area is in progress. Otherwise, we will release the area and re-assign it.
757 ****************************************************************************/
758 int SetTransfer(DEVICE_EXTENSION * pdx, TRANSFERDESC __user * pTD)
759 {
760 int iReturn;
761 TRANSFERDESC td;
762
763 if (copy_from_user(&td, pTD, sizeof(td)))
764 return -EFAULT;
765
766 mutex_lock(&pdx->io_mutex);
767 dev_dbg(&pdx->interface->dev, "%s area:%d, size:%08x", __func__,
768 td.wAreaNum, td.dwLength);
769 // The strange cast is done so that we don't get warnings in 32-bit linux about the size of the
770 // pointer. The pointer is always passed as a 64-bit object so that we don't have problems using
771 // a 32-bit program on a 64-bit system. unsigned long is 64-bits on a 64-bit system.
772 iReturn =
773 SetArea(pdx, td.wAreaNum,
774 (char __user *)((unsigned long)td.lpvBuff), td.dwLength,
775 false, false);
776 mutex_unlock(&pdx->io_mutex);
777 return iReturn;
778 }
779
780 /****************************************************************************
781 ** UnSetTransfer
782 ** Erases a transfer area record
783 ****************************************************************************/
784 int UnsetTransfer(DEVICE_EXTENSION * pdx, int nArea)
785 {
786 int iReturn;
787 mutex_lock(&pdx->io_mutex);
788 iReturn = ClearArea(pdx, nArea);
789 mutex_unlock(&pdx->io_mutex);
790 return iReturn;
791 }
792
793 /****************************************************************************
794 ** SetEvent
795 ** Creates an event that we can test for based on a transfer to/from an area.
796 ** The area must be setup for a transfer. We attempt to simulate the Windows
797 ** driver behavior for events (as we don't actually use them), which is to
798 ** pretend that whatever the user asked for was achieved, so we return 1 if
799 ** try to create one, and 0 if they ask to remove (assuming all else was OK).
800 ****************************************************************************/
801 int SetEvent(DEVICE_EXTENSION * pdx, TRANSFEREVENT __user * pTE)
802 {
803 int iReturn = U14ERR_NOERROR;
804 TRANSFEREVENT te;
805
806 // get a local copy of the data
807 if (copy_from_user(&te, pTE, sizeof(te)))
808 return -EFAULT;
809
810 if (te.wAreaNum >= MAX_TRANSAREAS) // the area must exist
811 return U14ERR_BADAREA;
812 else {
813 TRANSAREA *pTA = &pdx->rTransDef[te.wAreaNum];
814 mutex_lock(&pdx->io_mutex); // make sure we have no competitor
815 spin_lock_irq(&pdx->stagedLock);
816 if (pTA->bUsed) // area must be in use
817 {
818 pTA->dwEventSt = te.dwStart; // set area regions
819 pTA->dwEventSz = te.dwLength; // set size (0 cancels it)
820 pTA->bEventToHost = te.wFlags & 1; // set the direction
821 pTA->iWakeUp = 0; // zero the wake up count
822 } else
823 iReturn = U14ERR_NOTSET;
824 spin_unlock_irq(&pdx->stagedLock);
825 mutex_unlock(&pdx->io_mutex);
826 }
827 return iReturn ==
828 U14ERR_NOERROR ? (te.iSetEvent ? 1 : U14ERR_NOERROR) : iReturn;
829 }
830
831 /****************************************************************************
832 ** WaitEvent
833 ** Sleep the process with a timeout waiting for an event. Returns the number
834 ** of times that a block met the event condition since we last cleared it or
835 ** 0 if timed out, or -ve error (bad area or not set, or signal).
836 ****************************************************************************/
837 int WaitEvent(DEVICE_EXTENSION * pdx, int nArea, int msTimeOut)
838 {
839 int iReturn;
840 if ((unsigned)nArea >= MAX_TRANSAREAS)
841 return U14ERR_BADAREA;
842 else {
843 int iWait;
844 TRANSAREA *pTA = &pdx->rTransDef[nArea];
845 msTimeOut = (msTimeOut * HZ + 999) / 1000; // convert timeout to jiffies
846
847 // We cannot wait holding the mutex, but we check the flags while holding
848 // it. This may well be pointless as another thread could get in between
849 // releasing it and the wait call. However, this would have to clear the
850 // iWakeUp flag. However, the !pTA-bUsed may help us in this case.
851 mutex_lock(&pdx->io_mutex); // make sure we have no competitor
852 if (!pTA->bUsed || !pTA->dwEventSz) // check something to wait for...
853 return U14ERR_NOTSET; // ...else we do nothing
854 mutex_unlock(&pdx->io_mutex);
855
856 if (msTimeOut)
857 iWait =
858 wait_event_interruptible_timeout(pTA->wqEvent,
859 pTA->iWakeUp
860 || !pTA->bUsed,
861 msTimeOut);
862 else
863 iWait =
864 wait_event_interruptible(pTA->wqEvent, pTA->iWakeUp
865 || !pTA->bUsed);
866 if (iWait)
867 iReturn = -ERESTARTSYS; // oops - we have had a SIGNAL
868 else
869 iReturn = pTA->iWakeUp; // else the wakeup count
870
871 spin_lock_irq(&pdx->stagedLock);
872 pTA->iWakeUp = 0; // clear the flag
873 spin_unlock_irq(&pdx->stagedLock);
874 }
875 return iReturn;
876 }
877
878 /****************************************************************************
879 ** TestEvent
880 ** Test the event to see if a WaitEvent would return immediately. Returns the
881 ** number of times a block completed since the last call, or 0 if none or a
882 ** negative error.
883 ****************************************************************************/
884 int TestEvent(DEVICE_EXTENSION * pdx, int nArea)
885 {
886 int iReturn;
887 if ((unsigned)nArea >= MAX_TRANSAREAS)
888 iReturn = U14ERR_BADAREA;
889 else {
890 TRANSAREA *pTA = &pdx->rTransDef[nArea];
891 mutex_lock(&pdx->io_mutex); // make sure we have no competitor
892 spin_lock_irq(&pdx->stagedLock);
893 iReturn = pTA->iWakeUp; // get wakeup count since last call
894 pTA->iWakeUp = 0; // clear the count
895 spin_unlock_irq(&pdx->stagedLock);
896 mutex_unlock(&pdx->io_mutex);
897 }
898 return iReturn;
899 }
900
901 /****************************************************************************
902 ** GetTransferInfo
903 ** Puts the current state of the 1401 in a TGET_TX_BLOCK.
904 *****************************************************************************/
905 int GetTransfer(DEVICE_EXTENSION * pdx, TGET_TX_BLOCK __user * pTX)
906 {
907 int iReturn = U14ERR_NOERROR;
908 unsigned int dwIdent;
909
910 mutex_lock(&pdx->io_mutex);
911 dwIdent = pdx->StagedId; // area ident for last xfer
912 if (dwIdent >= MAX_TRANSAREAS)
913 iReturn = U14ERR_BADAREA;
914 else {
915 // Return the best information we have - we don't have physical addresses
916 TGET_TX_BLOCK tx;
917 memset(&tx, 0, sizeof(tx)); // clean out local work structure
918 tx.size = pdx->rTransDef[dwIdent].dwLength;
919 tx.linear = (long long)((long)pdx->rTransDef[dwIdent].lpvBuff);
920 tx.avail = GET_TX_MAXENTRIES; // how many blocks we could return
921 tx.used = 1; // number we actually return
922 tx.entries[0].physical =
923 (long long)(tx.linear + pdx->StagedOffset);
924 tx.entries[0].size = tx.size;
925
926 if (copy_to_user(pTX, &tx, sizeof(tx)))
927 iReturn = -EFAULT;
928 }
929 mutex_unlock(&pdx->io_mutex);
930 return iReturn;
931 }
932
933 /****************************************************************************
934 ** KillIO1401
935 **
936 ** Empties the host i/o buffers
937 ****************************************************************************/
938 int KillIO1401(DEVICE_EXTENSION * pdx)
939 {
940 dev_dbg(&pdx->interface->dev, "%s", __func__);
941 mutex_lock(&pdx->io_mutex);
942 FlushOutBuff(pdx);
943 FlushInBuff(pdx);
944 mutex_unlock(&pdx->io_mutex);
945 return U14ERR_NOERROR;
946 }
947
948 /****************************************************************************
949 ** BlkTransState
950 ** Returns a 0 or a 1 for whether DMA is happening. No point holding a mutex
951 ** for this as it only does one read.
952 *****************************************************************************/
953 int BlkTransState(DEVICE_EXTENSION * pdx)
954 {
955 int iReturn = pdx->dwDMAFlag != MODE_CHAR;
956 dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
957 return iReturn;
958 }
959
960 /****************************************************************************
961 ** StateOf1401
962 **
963 ** Puts the current state of the 1401 in the Irp return buffer.
964 *****************************************************************************/
965 int StateOf1401(DEVICE_EXTENSION * pdx)
966 {
967 int iReturn;
968 mutex_lock(&pdx->io_mutex);
969
970 QuickCheck(pdx, false, false); // get state up to date, no reset
971 iReturn = pdx->sCurrentState;
972
973 mutex_unlock(&pdx->io_mutex);
974 dev_dbg(&pdx->interface->dev, "%s = %d", __func__, iReturn);
975
976 return iReturn;
977 }
978
979 /****************************************************************************
980 ** StartSelfTest
981 **
982 ** Initiates a self-test cycle. The assumption is that we have no interrupts
983 ** active, so we should make sure that this is the case.
984 *****************************************************************************/
985 int StartSelfTest(DEVICE_EXTENSION * pdx)
986 {
987 int nGot;
988 mutex_lock(&pdx->io_mutex);
989 dev_dbg(&pdx->interface->dev, "%s", __func__);
990
991 ced_draw_down(pdx); // wait for, then kill outstanding Urbs
992 FlushInBuff(pdx); // Clear out input buffer & pipe
993 FlushOutBuff(pdx); // Clear output buffer & pipe
994 // ReadWrite_Cancel(pDeviceObject); /* so things stay tidy */
995 pdx->dwDMAFlag = MODE_CHAR; /* Clear DMA mode flags here */
996
997 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
998 pdx->ulSelfTestTime = jiffies + HZ * 30; // 30 seconds into the future
999
1000 mutex_unlock(&pdx->io_mutex);
1001 if (nGot < 0)
1002 dev_err(&pdx->interface->dev, "%s err=%d", __func__, nGot);
1003 return nGot < 0 ? U14ERR_FAIL : U14ERR_NOERROR;
1004 }
1005
1006 /****************************************************************************
1007 ** CheckSelfTest
1008 **
1009 ** Check progress of a self-test cycle
1010 ****************************************************************************/
1011 int CheckSelfTest(DEVICE_EXTENSION * pdx, TGET_SELFTEST __user * pGST)
1012 {
1013 unsigned int state, error;
1014 int iReturn;
1015 TGET_SELFTEST gst; // local work space
1016 memset(&gst, 0, sizeof(gst)); // clear out the space (sets code 0)
1017
1018 mutex_lock(&pdx->io_mutex);
1019
1020 dev_dbg(&pdx->interface->dev, "%s", __func__);
1021 iReturn = Get1401State(pdx, &state, &error);
1022 if (iReturn == U14ERR_NOERROR) // Only accept zero if it happens twice
1023 iReturn = Get1401State(pdx, &state, &error);
1024
1025 if (iReturn != U14ERR_NOERROR) // Self-test can cause comms errors
1026 { // so we assume still testing
1027 dev_err(&pdx->interface->dev,
1028 "%s Get1401State=%d, assuming still testing", __func__,
1029 iReturn);
1030 state = 0x80; // Force still-testing, no error
1031 error = 0;
1032 iReturn = U14ERR_NOERROR;
1033 }
1034
1035 if ((state == -1) && (error == -1)) // If Get1401State had problems
1036 {
1037 dev_err(&pdx->interface->dev,
1038 "%s Get1401State failed, assuming still testing",
1039 __func__);
1040 state = 0x80; // Force still-testing, no error
1041 error = 0;
1042 }
1043
1044 if ((state & 0xFF) == 0x80) // If we are still in self-test
1045 {
1046 if (state & 0x00FF0000) // Have we got an error?
1047 {
1048 gst.code = (state & 0x00FF0000) >> 16; // read the error code
1049 gst.x = error & 0x0000FFFF; // Error data X
1050 gst.y = (error & 0xFFFF0000) >> 16; // and data Y
1051 dev_dbg(&pdx->interface->dev, "Self-test error code %d",
1052 gst.code);
1053 } else // No error, check for timeout
1054 {
1055 unsigned long ulNow = jiffies; // get current time
1056 if (time_after(ulNow, pdx->ulSelfTestTime)) {
1057 gst.code = -2; // Flag the timeout
1058 dev_dbg(&pdx->interface->dev,
1059 "Self-test timed-out");
1060 } else
1061 dev_dbg(&pdx->interface->dev,
1062 "Self-test on-going");
1063 }
1064 } else {
1065 gst.code = -1; // Flag the test is done
1066 dev_dbg(&pdx->interface->dev, "Self-test done");
1067 }
1068
1069 if (gst.code < 0) // If we have a problem or finished
1070 { // If using the 2890 we should reset properly
1071 if ((pdx->nPipes == 4) && (pdx->s1401Type <= TYPEPOWER))
1072 Is1401(pdx); // Get 1401 reset and OK
1073 else
1074 QuickCheck(pdx, true, true); // Otherwise check without reset unless problems
1075 }
1076 mutex_unlock(&pdx->io_mutex);
1077
1078 if (copy_to_user(pGST, &gst, sizeof(gst)))
1079 return -EFAULT;
1080
1081 return iReturn;
1082 }
1083
1084 /****************************************************************************
1085 ** TypeOf1401
1086 **
1087 ** Returns code for standard, plus, micro1401, power1401 or none
1088 ****************************************************************************/
1089 int TypeOf1401(DEVICE_EXTENSION * pdx)
1090 {
1091 int iReturn = TYPEUNKNOWN;
1092 mutex_lock(&pdx->io_mutex);
1093 dev_dbg(&pdx->interface->dev, "%s", __func__);
1094
1095 switch (pdx->s1401Type) {
1096 case TYPE1401:
1097 iReturn = U14ERR_STD;
1098 break; // Handle these types directly
1099 case TYPEPLUS:
1100 iReturn = U14ERR_PLUS;
1101 break;
1102 case TYPEU1401:
1103 iReturn = U14ERR_U1401;
1104 break;
1105 default:
1106 if ((pdx->s1401Type >= TYPEPOWER) && (pdx->s1401Type <= 25))
1107 iReturn = pdx->s1401Type + 4; // We can calculate types
1108 else // for up-coming 1401 designs
1109 iReturn = TYPEUNKNOWN; // Don't know or not there
1110 }
1111 dev_dbg(&pdx->interface->dev, "%s %d", __func__, iReturn);
1112 mutex_unlock(&pdx->io_mutex);
1113
1114 return iReturn;
1115 }
1116
1117 /****************************************************************************
1118 ** TransferFlags
1119 **
1120 ** Returns flags on block transfer abilities
1121 ****************************************************************************/
1122 int TransferFlags(DEVICE_EXTENSION * pdx)
1123 {
1124 int iReturn = U14TF_MULTIA | U14TF_DIAG | // we always have multiple DMA area
1125 U14TF_NOTIFY | U14TF_CIRCTH; // diagnostics, notify and circular
1126 dev_dbg(&pdx->interface->dev, "%s", __func__);
1127 mutex_lock(&pdx->io_mutex);
1128 if (pdx->bIsUSB2) // Set flag for USB2 if appropriate
1129 iReturn |= U14TF_USB2;
1130 mutex_unlock(&pdx->io_mutex);
1131
1132 return iReturn;
1133 }
1134
1135 /***************************************************************************
1136 ** DbgCmd1401
1137 ** Issues a debug\diagnostic command to the 1401 along with a 32-bit datum
1138 ** This is a utility command used for dbg operations.
1139 */
1140 static int DbgCmd1401(DEVICE_EXTENSION * pdx, unsigned char cmd,
1141 unsigned int data)
1142 {
1143 int iReturn;
1144 dev_dbg(&pdx->interface->dev, "%s entry", __func__);
1145 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); // allow 1 second timeout
1146 if (iReturn < 0)
1147 dev_err(&pdx->interface->dev, "%s fail code=%d", __func__,
1148 iReturn);
1149
1150 return iReturn;
1151 }
1152
1153 /****************************************************************************
1154 ** DbgPeek
1155 **
1156 ** Execute the diagnostic peek operation. Uses address, width and repeats.
1157 ****************************************************************************/
1158 int DbgPeek(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1159 {
1160 int iReturn;
1161 TDBGBLOCK db;
1162
1163 if (copy_from_user(&db, pDB, sizeof(db)))
1164 return -EFAULT;
1165
1166 mutex_lock(&pdx->io_mutex);
1167 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1168
1169 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1170 if (iReturn == U14ERR_NOERROR)
1171 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1172 if (iReturn == U14ERR_NOERROR)
1173 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1174 if (iReturn == U14ERR_NOERROR)
1175 iReturn = DbgCmd1401(pdx, DB_PEEK, 0);
1176 mutex_unlock(&pdx->io_mutex);
1177
1178 return iReturn;
1179 }
1180
1181 /****************************************************************************
1182 ** DbgPoke
1183 **
1184 ** Execute the diagnostic poke operation. Parameters are in the CSBLOCK struct
1185 ** in order address, size, repeats and value to poke.
1186 ****************************************************************************/
1187 int DbgPoke(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1188 {
1189 int iReturn;
1190 TDBGBLOCK db;
1191
1192 if (copy_from_user(&db, pDB, sizeof(db)))
1193 return -EFAULT;
1194
1195 mutex_lock(&pdx->io_mutex);
1196 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1197
1198 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1199 if (iReturn == U14ERR_NOERROR)
1200 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1201 if (iReturn == U14ERR_NOERROR)
1202 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1203 if (iReturn == U14ERR_NOERROR)
1204 iReturn = DbgCmd1401(pdx, DB_POKE, db.iData);
1205 mutex_unlock(&pdx->io_mutex);
1206
1207 return iReturn;
1208 }
1209
1210 /****************************************************************************
1211 ** DbgRampData
1212 **
1213 ** Execute the diagnostic ramp data operation. Parameters are in the CSBLOCK struct
1214 ** in order address, default, enable mask, size and repeats.
1215 ****************************************************************************/
1216 int DbgRampData(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1217 {
1218 int iReturn;
1219 TDBGBLOCK db;
1220
1221 if (copy_from_user(&db, pDB, sizeof(db)))
1222 return -EFAULT;
1223
1224 mutex_lock(&pdx->io_mutex);
1225 dev_dbg(&pdx->interface->dev, "%s @ %08x", __func__, db.iAddr);
1226
1227 iReturn = DbgCmd1401(pdx, DB_SETADD, db.iAddr);
1228 if (iReturn == U14ERR_NOERROR)
1229 iReturn = DbgCmd1401(pdx, DB_SETDEF, db.iDefault);
1230 if (iReturn == U14ERR_NOERROR)
1231 iReturn = DbgCmd1401(pdx, DB_SETMASK, db.iMask);
1232 if (iReturn == U14ERR_NOERROR)
1233 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1234 if (iReturn == U14ERR_NOERROR)
1235 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1236 if (iReturn == U14ERR_NOERROR)
1237 iReturn = DbgCmd1401(pdx, DB_RAMPD, 0);
1238 mutex_unlock(&pdx->io_mutex);
1239
1240 return iReturn;
1241 }
1242
1243 /****************************************************************************
1244 ** DbgRampAddr
1245 **
1246 ** Execute the diagnostic ramp address operation
1247 ****************************************************************************/
1248 int DbgRampAddr(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1249 {
1250 int iReturn;
1251 TDBGBLOCK db;
1252
1253 if (copy_from_user(&db, pDB, sizeof(db)))
1254 return -EFAULT;
1255
1256 mutex_lock(&pdx->io_mutex);
1257 dev_dbg(&pdx->interface->dev, "%s", __func__);
1258
1259 iReturn = DbgCmd1401(pdx, DB_SETDEF, db.iDefault);
1260 if (iReturn == U14ERR_NOERROR)
1261 iReturn = DbgCmd1401(pdx, DB_SETMASK, db.iMask);
1262 if (iReturn == U14ERR_NOERROR)
1263 iReturn = DbgCmd1401(pdx, DB_WIDTH, db.iWidth);
1264 if (iReturn == U14ERR_NOERROR)
1265 iReturn = DbgCmd1401(pdx, DB_REPEATS, db.iRepeats);
1266 if (iReturn == U14ERR_NOERROR)
1267 iReturn = DbgCmd1401(pdx, DB_RAMPA, 0);
1268 mutex_unlock(&pdx->io_mutex);
1269
1270 return iReturn;
1271 }
1272
1273 /****************************************************************************
1274 ** DbgGetData
1275 **
1276 ** Retrieve the data resulting from the last debug Peek operation
1277 ****************************************************************************/
1278 int DbgGetData(DEVICE_EXTENSION * pdx, TDBGBLOCK __user * pDB)
1279 {
1280 int iReturn;
1281 TDBGBLOCK db;
1282 memset(&db, 0, sizeof(db)); // fill returned block with 0s
1283
1284 mutex_lock(&pdx->io_mutex);
1285 dev_dbg(&pdx->interface->dev, "%s", __func__);
1286
1287 // Read back the last peeked value from the 1401.
1288 iReturn = usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
1289 DB_DATA, (D_TO_H | VENDOR | DEVREQ), 0, 0,
1290 &db.iData, sizeof(db.iData), HZ);
1291 if (iReturn == sizeof(db.iData)) {
1292 if (copy_to_user(pDB, &db, sizeof(db)))
1293 iReturn = -EFAULT;
1294 else
1295 iReturn = U14ERR_NOERROR;
1296 } else
1297 dev_err(&pdx->interface->dev, "%s failed, code %d", __func__,
1298 iReturn);
1299
1300 mutex_unlock(&pdx->io_mutex);
1301
1302 return iReturn;
1303 }
1304
1305 /****************************************************************************
1306 ** DbgStopLoop
1307 **
1308 ** Stop any never-ending debug loop, we just call Get1401State for USB
1309 **
1310 ****************************************************************************/
1311 int DbgStopLoop(DEVICE_EXTENSION * pdx)
1312 {
1313 int iReturn;
1314 unsigned int uState, uErr;
1315
1316 mutex_lock(&pdx->io_mutex);
1317 dev_dbg(&pdx->interface->dev, "%s", __func__);
1318 iReturn = Get1401State(pdx, &uState, &uErr);
1319 mutex_unlock(&pdx->io_mutex);
1320
1321 return iReturn;
1322 }
1323
1324 /****************************************************************************
1325 ** SetCircular
1326 **
1327 ** Sets up a transfer area record for circular transfers. If the area is
1328 ** already set, we attempt to unset it. Unsetting will fail if the area is
1329 ** booked and a transfer to that area is in progress. Otherwise, we will
1330 ** release the area and re-assign it.
1331 ****************************************************************************/
1332 int SetCircular(DEVICE_EXTENSION * pdx, TRANSFERDESC __user * pTD)
1333 {
1334 int iReturn;
1335 bool bToHost;
1336 TRANSFERDESC td;
1337
1338 if (copy_from_user(&td, pTD, sizeof(td)))
1339 return -EFAULT;
1340
1341 mutex_lock(&pdx->io_mutex);
1342 dev_dbg(&pdx->interface->dev, "%s area:%d, size:%08x", __func__,
1343 td.wAreaNum, td.dwLength);
1344 bToHost = td.eSize != 0; // this is used as the tohost flag
1345
1346 // The strange cast is done so that we don't get warnings in 32-bit linux about the size of the
1347 // pointer. The pointer is always passed as a 64-bit object so that we don't have problems using
1348 // a 32-bit program on a 64-bit system. unsigned long is 64-bits on a 64-bit system.
1349 iReturn =
1350 SetArea(pdx, td.wAreaNum,
1351 (char __user *)((unsigned long)td.lpvBuff), td.dwLength,
1352 true, bToHost);
1353 mutex_unlock(&pdx->io_mutex);
1354 return iReturn;
1355 }
1356
1357 /****************************************************************************
1358 ** GetCircBlock
1359 **
1360 ** Return the next available block of circularly-transferred data.
1361 ****************************************************************************/
1362 int GetCircBlock(DEVICE_EXTENSION * pdx, TCIRCBLOCK __user * pCB)
1363 {
1364 int iReturn = U14ERR_NOERROR;
1365 unsigned int nArea;
1366 TCIRCBLOCK cb;
1367
1368 dev_dbg(&pdx->interface->dev, "%s", __func__);
1369
1370 if (copy_from_user(&cb, pCB, sizeof(cb)))
1371 return -EFAULT;
1372
1373 mutex_lock(&pdx->io_mutex);
1374
1375 nArea = cb.nArea; // Retrieve parameters first
1376 cb.dwOffset = 0; // set default result (nothing)
1377 cb.dwSize = 0;
1378
1379 if (nArea < MAX_TRANSAREAS) // The area number must be OK
1380 {
1381 TRANSAREA *pArea = &pdx->rTransDef[nArea]; // Pointer to relevant info
1382 spin_lock_irq(&pdx->stagedLock); // Lock others out
1383
1384 if ((pArea->bUsed) && (pArea->bCircular) && // Must be circular area
1385 (pArea->bCircToHost)) // For now at least must be to host
1386 {
1387 if (pArea->aBlocks[0].dwSize > 0) // Got anything?
1388 {
1389 cb.dwOffset = pArea->aBlocks[0].dwOffset;
1390 cb.dwSize = pArea->aBlocks[0].dwSize;
1391 dev_dbg(&pdx->interface->dev,
1392 "%s return block 0: %d bytes at %d",
1393 __func__, cb.dwSize, cb.dwOffset);
1394 }
1395 } else
1396 iReturn = U14ERR_NOTSET;
1397
1398 spin_unlock_irq(&pdx->stagedLock);
1399 } else
1400 iReturn = U14ERR_BADAREA;
1401
1402 if (copy_to_user(pCB, &cb, sizeof(cb)))
1403 iReturn = -EFAULT;
1404
1405 mutex_unlock(&pdx->io_mutex);
1406 return iReturn;
1407 }
1408
1409 /****************************************************************************
1410 ** FreeCircBlock
1411 **
1412 ** Frees a block of circularly-transferred data and returns the next one.
1413 ****************************************************************************/
1414 int FreeCircBlock(DEVICE_EXTENSION * pdx, TCIRCBLOCK __user * pCB)
1415 {
1416 int iReturn = U14ERR_NOERROR;
1417 unsigned int nArea, uStart, uSize;
1418 TCIRCBLOCK cb;
1419
1420 dev_dbg(&pdx->interface->dev, "%s", __func__);
1421
1422 if (copy_from_user(&cb, pCB, sizeof(cb)))
1423 return -EFAULT;
1424
1425 mutex_lock(&pdx->io_mutex);
1426
1427 nArea = cb.nArea; // Retrieve parameters first
1428 uStart = cb.dwOffset;
1429 uSize = cb.dwSize;
1430 cb.dwOffset = 0; // then set default result (nothing)
1431 cb.dwSize = 0;
1432
1433 if (nArea < MAX_TRANSAREAS) // The area number must be OK
1434 {
1435 TRANSAREA *pArea = &pdx->rTransDef[nArea]; // Pointer to relevant info
1436 spin_lock_irq(&pdx->stagedLock); // Lock others out
1437
1438 if ((pArea->bUsed) && (pArea->bCircular) && // Must be circular area
1439 (pArea->bCircToHost)) // For now at least must be to host
1440 {
1441 bool bWaiting = false;
1442
1443 if ((pArea->aBlocks[0].dwSize >= uSize) && // Got anything?
1444 (pArea->aBlocks[0].dwOffset == uStart)) // Must be legal data
1445 {
1446 pArea->aBlocks[0].dwSize -= uSize;
1447 pArea->aBlocks[0].dwOffset += uSize;
1448 if (pArea->aBlocks[0].dwSize == 0) // Have we emptied this block?
1449 {
1450 if (pArea->aBlocks[1].dwSize) // Is there a second block?
1451 {
1452 pArea->aBlocks[0] = pArea->aBlocks[1]; // Copy down block 2 data
1453 pArea->aBlocks[1].dwSize = 0; // and mark the second block as unused
1454 pArea->aBlocks[1].dwOffset = 0;
1455 } else
1456 pArea->aBlocks[0].dwOffset = 0;
1457 }
1458
1459 dev_dbg(&pdx->interface->dev,
1460 "%s free %d bytes at %d, return %d bytes at %d, wait=%d",
1461 __func__, uSize, uStart,
1462 pArea->aBlocks[0].dwSize,
1463 pArea->aBlocks[0].dwOffset,
1464 pdx->bXFerWaiting);
1465
1466 // Return the next available block of memory as well
1467 if (pArea->aBlocks[0].dwSize > 0) // Got anything?
1468 {
1469 cb.dwOffset =
1470 pArea->aBlocks[0].dwOffset;
1471 cb.dwSize = pArea->aBlocks[0].dwSize;
1472 }
1473
1474 bWaiting = pdx->bXFerWaiting;
1475 if (bWaiting && pdx->bStagedUrbPending) {
1476 dev_err(&pdx->interface->dev,
1477 "%s ERROR: waiting xfer and staged Urb pending!",
1478 __func__);
1479 bWaiting = false;
1480 }
1481 } else {
1482 dev_err(&pdx->interface->dev,
1483 "%s ERROR: freeing %d bytes at %d, block 0 is %d bytes at %d",
1484 __func__, uSize, uStart,
1485 pArea->aBlocks[0].dwSize,
1486 pArea->aBlocks[0].dwOffset);
1487 iReturn = U14ERR_NOMEMORY;
1488 }
1489
1490 // If we have one, kick off pending transfer
1491 if (bWaiting) // Got a block xfer waiting?
1492 {
1493 int RWMStat =
1494 ReadWriteMem(pdx, !pdx->rDMAInfo.bOutWard,
1495 pdx->rDMAInfo.wIdent,
1496 pdx->rDMAInfo.dwOffset,
1497 pdx->rDMAInfo.dwSize);
1498 if (RWMStat != U14ERR_NOERROR)
1499 dev_err(&pdx->interface->dev,
1500 "%s rw setup failed %d",
1501 __func__, RWMStat);
1502 }
1503 } else
1504 iReturn = U14ERR_NOTSET;
1505
1506 spin_unlock_irq(&pdx->stagedLock);
1507 } else
1508 iReturn = U14ERR_BADAREA;
1509
1510 if (copy_to_user(pCB, &cb, sizeof(cb)))
1511 return -EFAULT;
1512
1513 mutex_unlock(&pdx->io_mutex);
1514 return iReturn;
1515 }