Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/staging/ced1401/usb1401.c

  1 /***********************************************************************************
  2  CED1401 usb driver. This basic loading is based on the usb-skeleton.c code that is:
  3  Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  4  Copyright (C) 2012 Alois Schloegl <alois.schloegl@ist.ac.at>
  5  There is not a great deal of the skeleton left.
  6 
  7  All the remainder dealing specifically with the CED1401 is based on drivers written
  8  by CED for other systems (mainly Windows) and is:
  9  Copyright (C) 2010 Cambridge Electronic Design Ltd
 10  Author Greg P Smith (greg@ced.co.uk)
 11 
 12  This program is free software; you can redistribute it and/or
 13  modify it under the terms of the GNU General Public License
 14  as published by the Free Software Foundation; either version 2
 15  of the License, or (at your option) any later version.
 16 
 17  This program is distributed in the hope that it will be useful,
 18  but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20  GNU General Public License for more details.
 21 
 22  You should have received a copy of the GNU General Public License
 23  along with this program; if not, write to the Free Software
 24  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 25 
 26 Endpoints
 27 *********
 28 There are 4 endpoints plus the control endpoint in the standard interface
 29 provided by most 1401s. The control endpoint is used for standard USB requests,
 30 plus various CED-specific transactions such as start self test, debug and get
 31 the 1401 status. The other endpoints are:
 32 
 33  1 Characters to the 1401
 34  2 Characters from the 1401
 35  3 Block data to the 1401
 36  4 Block data to the host.
 37 
 38 inside the driver these are indexed as an array from 0 to 3, transactions
 39 over the control endpoint are carried out using a separate mechanism. The
 40 use of the endpoints is mostly straightforward, with the driver issuing
 41 IO request packets (IRPs) as required to transfer data to and from the 1401.
 42 The handling of endpoint 2 is different because it is used for characters
 43 from the 1401, which can appear spontaneously and without any other driver
 44 activity - for example to repeatedly request DMA transfers in Spike2. The
 45 desired effect is achieved by using an interrupt endpoint which can be
 46 polled to see if it has data available, and writing the driver so that it
 47 always maintains a pending read IRP from that endpoint which will read the
 48 character data and terminate as soon as the 1401 makes data available. This
 49 works very well, some care is taken with when you kick off this character
 50 read IRP to avoid it being active when it is not wanted but generally it
 51 is running all the time.
 52 
 53 In the 2270, there are only three endpoints plus the control endpoint. In
 54 addition to the transactions mentioned above, the control endpoint is used
 55 to transfer character data to the 1401. The other endpoints are used as:
 56 
 57  1 Characters from the 1401
 58  2 Block data to the 1401
 59  3 Block data to the host.
 60 
 61 The type of interface available is specified by the interface subclass field
 62 in the interface descriptor provided by the 1401. See the USB_INT_ constants
 63 for the values that this field can hold.
 64 
 65 ****************************************************************************
 66 Linux implementation
 67 
 68 Although Linux Device Drivers (3rd Edition) was a major source of information,
 69 it is very out of date. A lot of information was gleaned from the latest
 70 usb_skeleton.c code (you need to download the kernel sources to get this).
 71 
 72 To match the Windows version, everything is done using ioctl calls. All the
 73 device state is held in the DEVICE_EXTENSION (named to match Windows use).
 74 Block transfers are done by using get_user_pages() to pin down a list of
 75 pages that we hold a pointer to in the device driver. We also allocate a
 76 coherent transfer buffer of size STAGED_SZ (this must be a multiple of the
 77 bulk endpoint size so that the 1401 does not realise that we break large
 78 transfers down into smaller pieces). We use kmap_atomic() to get a kernel
 79 va for each page, as it is required, for copying; see CopyUserSpace().
 80 
 81 All character and data transfers are done using asynchronous IO. All Urbs are
 82 tracked by anchoring them. Status and debug ioctls are implemented with the
 83 synchronous non-Urb based transfers.
 84 */
 85 
 86 #include <linux/kernel.h>
 87 #include <linux/errno.h>
 88 #include <linux/usb.h>
 89 #include <linux/mutex.h>
 90 #include <linux/mm.h>
 91 #include <linux/highmem.h>
 92 #include <linux/slab.h>
 93 #include <linux/module.h>
 94 #include <linux/kref.h>
 95 #include <linux/uaccess.h>
 96 
 97 #include "usb1401.h"
 98 
 99 /* Define these values to match your devices */
100 #define USB_CED_VENDOR_ID       0x0525
101 #define USB_CED_PRODUCT_ID      0xa0f0
102 
103 /* table of devices that work with this driver */
104 static const struct usb_device_id ced_table[] = {
105         {USB_DEVICE(USB_CED_VENDOR_ID, USB_CED_PRODUCT_ID)},
106         {}                      /* Terminating entry */
107 };
108 
109 MODULE_DEVICE_TABLE(usb, ced_table);
110 
111 /* Get a minor range for your devices from the usb maintainer */
112 #define USB_CED_MINOR_BASE      192
113 
114 /* our private defines. if this grows any larger, use your own .h file */
115 #define MAX_TRANSFER            (PAGE_SIZE - 512)
116 /* MAX_TRANSFER is chosen so that the VM is not stressed by
117    allocations > PAGE_SIZE and the number of packets in a page
118    is an integer 512 is the largest possible packet on EHCI */
119 #define WRITES_IN_FLIGHT        8
120 /* arbitrarily chosen */
121 
122 static struct usb_driver ced_driver;
123 
124 static void ced_delete(struct kref *kref)
125 {
126         DEVICE_EXTENSION *pdx = to_DEVICE_EXTENSION(kref);
127 
128         /*  Free up the output buffer, then free the output urb. Note that the interface member */
129         /*  of pdx will probably be NULL, so cannot be used to get to dev. */
130         usb_free_coherent(pdx->udev, OUTBUF_SZ, pdx->pCoherCharOut,
131                           pdx->pUrbCharOut->transfer_dma);
132         usb_free_urb(pdx->pUrbCharOut);
133 
134         /*  Do the same for chan input */
135         usb_free_coherent(pdx->udev, INBUF_SZ, pdx->pCoherCharIn,
136                           pdx->pUrbCharIn->transfer_dma);
137         usb_free_urb(pdx->pUrbCharIn);
138 
139         /*  Do the same for the block transfers */
140         usb_free_coherent(pdx->udev, STAGED_SZ, pdx->pCoherStagedIO,
141                           pdx->pStagedUrb->transfer_dma);
142         usb_free_urb(pdx->pStagedUrb);
143 
144         usb_put_dev(pdx->udev);
145         kfree(pdx);
146 }
147 
148 /*  This is the driver end of the open() call from user space. */
149 static int ced_open(struct inode *inode, struct file *file)
150 {
151         DEVICE_EXTENSION *pdx;
152         int retval = 0;
153         int subminor = iminor(inode);
154         struct usb_interface *interface =
155             usb_find_interface(&ced_driver, subminor);
156         if (!interface) {
157                 pr_err("%s - error, can't find device for minor %d", __func__,
158                        subminor);
159                 retval = -ENODEV;
160                 goto exit;
161         }
162 
163         pdx = usb_get_intfdata(interface);
164         if (!pdx) {
165                 retval = -ENODEV;
166                 goto exit;
167         }
168 
169         dev_dbg(&interface->dev, "%s: got pdx\n", __func__);
170 
171         /* increment our usage count for the device */
172         kref_get(&pdx->kref);
173 
174         /* lock the device to allow correctly handling errors
175          * in resumption */
176         mutex_lock(&pdx->io_mutex);
177 
178         if (!pdx->open_count++) {
179                 retval = usb_autopm_get_interface(interface);
180                 if (retval) {
181                         pdx->open_count--;
182                         mutex_unlock(&pdx->io_mutex);
183                         kref_put(&pdx->kref, ced_delete);
184                         goto exit;
185                 }
186         } else {                /* uncomment this block if you want exclusive open */
187                 dev_err(&interface->dev, "%s: fail: already open\n", __func__);
188                 retval = -EBUSY;
189                 pdx->open_count--;
190                 mutex_unlock(&pdx->io_mutex);
191                 kref_put(&pdx->kref, ced_delete);
192                 goto exit;
193         }
194         /* prevent the device from being autosuspended */
195 
196         /* save our object in the file's private structure */
197         file->private_data = pdx;
198         mutex_unlock(&pdx->io_mutex);
199 
200 exit:
201         return retval;
202 }
203 
204 static int ced_release(struct inode *inode, struct file *file)
205 {
206         DEVICE_EXTENSION *pdx = file->private_data;
207         if (pdx == NULL)
208                 return -ENODEV;
209 
210         dev_dbg(&pdx->interface->dev, "%s: called\n", __func__);
211         mutex_lock(&pdx->io_mutex);
212         if (!--pdx->open_count && pdx->interface)       /*  Allow autosuspend */
213                 usb_autopm_put_interface(pdx->interface);
214         mutex_unlock(&pdx->io_mutex);
215 
216         kref_put(&pdx->kref, ced_delete);       /*  decrement the count on our device */
217         return 0;
218 }
219 
220 static int ced_flush(struct file *file, fl_owner_t id)
221 {
222         int res;
223         DEVICE_EXTENSION *pdx = file->private_data;
224         if (pdx == NULL)
225                 return -ENODEV;
226 
227         dev_dbg(&pdx->interface->dev, "%s: char in pend=%d\n",
228                 __func__, pdx->bReadCharsPending);
229 
230         /* wait for io to stop */
231         mutex_lock(&pdx->io_mutex);
232         dev_dbg(&pdx->interface->dev, "%s: got io_mutex\n", __func__);
233         ced_draw_down(pdx);
234 
235         /* read out errors, leave subsequent opens a clean slate */
236         spin_lock_irq(&pdx->err_lock);
237         res = pdx->errors ? (pdx->errors == -EPIPE ? -EPIPE : -EIO) : 0;
238         pdx->errors = 0;
239         spin_unlock_irq(&pdx->err_lock);
240 
241         mutex_unlock(&pdx->io_mutex);
242         dev_dbg(&pdx->interface->dev, "%s: exit reached\n", __func__);
243 
244         return res;
245 }
246 
247 /***************************************************************************
248 ** CanAcceptIoRequests
249 ** If the device is removed, interface is set NULL. We also clear our pointer
250 ** from the interface, so we should make sure that pdx is not NULL. This will
251 ** not help with a device extension held by a file.
252 ** return true if can accept new io requests, else false
253 */
254 static bool CanAcceptIoRequests(DEVICE_EXTENSION *pdx)
255 {
256         return pdx && pdx->interface;   /*  Can we accept IO requests */
257 }
258 
259 /****************************************************************************
260 ** Callback routine to complete writes. This may need to fire off another
261 ** urb to complete the transfer.
262 ****************************************************************************/
263 static void ced_writechar_callback(struct urb *pUrb)
264 {
265         DEVICE_EXTENSION *pdx = pUrb->context;
266         int nGot = pUrb->actual_length; /*  what we transferred */
267 
268         if (pUrb->status) {     /*  sync/async unlink faults aren't errors */
269                 if (!
270                     (pUrb->status == -ENOENT || pUrb->status == -ECONNRESET
271                      || pUrb->status == -ESHUTDOWN)) {
272                         dev_err(&pdx->interface->dev,
273                                 "%s: nonzero write bulk status received: %d\n",
274                                 __func__, pUrb->status);
275                 }
276 
277                 spin_lock(&pdx->err_lock);
278                 pdx->errors = pUrb->status;
279                 spin_unlock(&pdx->err_lock);
280                 nGot = 0;       /*   and tidy up again if so */
281 
282                 spin_lock(&pdx->charOutLock);   /*  already at irq level */
283                 pdx->dwOutBuffGet = 0;  /*  Reset the output buffer */
284                 pdx->dwOutBuffPut = 0;
285                 pdx->dwNumOutput = 0;   /*  Clear the char count */
286                 pdx->bPipeError[0] = 1; /*  Flag an error for later */
287                 pdx->bSendCharsPending = false; /*  Allow other threads again */
288                 spin_unlock(&pdx->charOutLock); /*  already at irq level */
289                 dev_dbg(&pdx->interface->dev,
290                         "%s: char out done, 0 chars sent\n", __func__);
291         } else {
292                 dev_dbg(&pdx->interface->dev,
293                         "%s: char out done, %d chars sent\n", __func__, nGot);
294                 spin_lock(&pdx->charOutLock);   /*  already at irq level */
295                 pdx->dwNumOutput -= nGot;       /*  Now adjust the char send buffer */
296                 pdx->dwOutBuffGet += nGot;      /*  to match what we did */
297                 if (pdx->dwOutBuffGet >= OUTBUF_SZ)     /*  Can't do this any earlier as data could be overwritten */
298                         pdx->dwOutBuffGet = 0;
299 
300                 if (pdx->dwNumOutput > 0) {     /*  if more to be done... */
301                         int nPipe = 0;  /*  The pipe number to use */
302                         int iReturn;
303                         char *pDat = &pdx->outputBuffer[pdx->dwOutBuffGet];
304                         unsigned int dwCount = pdx->dwNumOutput;        /*  maximum to send */
305                         if ((pdx->dwOutBuffGet + dwCount) > OUTBUF_SZ)  /*  does it cross buffer end? */
306                                 dwCount = OUTBUF_SZ - pdx->dwOutBuffGet;
307                         spin_unlock(&pdx->charOutLock); /*  we are done with stuff that changes */
308                         memcpy(pdx->pCoherCharOut, pDat, dwCount);      /*  copy output data to the buffer */
309                         usb_fill_bulk_urb(pdx->pUrbCharOut, pdx->udev,
310                                           usb_sndbulkpipe(pdx->udev,
311                                                           pdx->epAddr[0]),
312                                           pdx->pCoherCharOut, dwCount,
313                                           ced_writechar_callback, pdx);
314                         pdx->pUrbCharOut->transfer_flags |=
315                             URB_NO_TRANSFER_DMA_MAP;
316                         usb_anchor_urb(pdx->pUrbCharOut, &pdx->submitted);      /*  in case we need to kill it */
317                         iReturn = usb_submit_urb(pdx->pUrbCharOut, GFP_ATOMIC);
318                         dev_dbg(&pdx->interface->dev, "%s: n=%d>%s<\n",
319                                 __func__, dwCount, pDat);
320                         spin_lock(&pdx->charOutLock);   /*  grab lock for errors */
321                         if (iReturn) {
322                                 pdx->bPipeError[nPipe] = 1;     /*  Flag an error to be handled later */
323                                 pdx->bSendCharsPending = false; /*  Allow other threads again */
324                                 usb_unanchor_urb(pdx->pUrbCharOut);
325                                 dev_err(&pdx->interface->dev,
326                                         "%s: usb_submit_urb() returned %d\n",
327                                         __func__, iReturn);
328                         }
329                 } else
330                         pdx->bSendCharsPending = false; /*  Allow other threads again */
331                 spin_unlock(&pdx->charOutLock); /*  already at irq level */
332         }
333 }
334 
335 /****************************************************************************
336 ** SendChars
337 ** Transmit the characters in the output buffer to the 1401. This may need
338 ** breaking down into multiple transfers.
339 ****************************************************************************/
340 int SendChars(DEVICE_EXTENSION *pdx)
341 {
342         int iReturn = U14ERR_NOERROR;
343 
344         spin_lock_irq(&pdx->charOutLock);       /*  Protect ourselves */
345 
346         if ((!pdx->bSendCharsPending) &&        /*  Not currently sending */
347             (pdx->dwNumOutput > 0) &&   /*   has characters to output */
348             (CanAcceptIoRequests(pdx))) { /*   and current activity is OK */
349                 unsigned int dwCount = pdx->dwNumOutput;        /*  Get a copy of the character count */
350                 pdx->bSendCharsPending = true;  /*  Set flag to lock out other threads */
351 
352                 dev_dbg(&pdx->interface->dev,
353                         "Send %d chars to 1401, EP0 flag %d\n",
354                         dwCount, pdx->nPipes == 3);
355                 /*  If we have only 3 end points we must send the characters to the 1401 using EP0. */
356                 if (pdx->nPipes == 3) {
357                         /*  For EP0 character transmissions to the 1401, we have to hang about until they */
358                         /*  are gone, as otherwise without more character IO activity they will never go. */
359                         unsigned int count = dwCount;   /*  Local char counter */
360                         unsigned int index = 0; /*  The index into the char buffer */
361 
362                         spin_unlock_irq(&pdx->charOutLock);     /*  Free spinlock as we call USBD */
363 
364                         while ((count > 0) && (iReturn == U14ERR_NOERROR)) {
365                                 /*  We have to break the transfer up into 64-byte chunks because of a 2270 problem */
366                                 int n = count > 64 ? 64 : count;        /*  Chars for this xfer, max of 64 */
367                                 int nSent = usb_control_msg(pdx->udev,
368                                                             usb_sndctrlpipe(pdx->udev, 0),      /*  use end point 0 */
369                                                             DB_CHARS,   /*  bRequest */
370                                                             (H_TO_D | VENDOR | DEVREQ), /*  to the device, vendor request to the device */
371                                                             0, 0,       /*  value and index are both 0 */
372                                                             &pdx->outputBuffer[index],  /*  where to send from */
373                                                             n,  /*  how much to send */
374                                                             1000);      /*  timeout in jiffies */
375                                 if (nSent <= 0) {
376                                         iReturn = nSent ? nSent : -ETIMEDOUT;   /*  if 0 chars says we timed out */
377                                         dev_err(&pdx->interface->dev,
378                                                 "Send %d chars by EP0 failed: %d\n",
379                                                 n, iReturn);
380                                 } else {
381                                         dev_dbg(&pdx->interface->dev,
382                                                 "Sent %d chars by EP0\n", n);
383                                         count -= nSent;
384                                         index += nSent;
385                                 }
386                         }
387 
388                         spin_lock_irq(&pdx->charOutLock);       /*  Protect pdx changes, released by general code */
389                         pdx->dwOutBuffGet = 0;  /*  so reset the output buffer */
390                         pdx->dwOutBuffPut = 0;
391                         pdx->dwNumOutput = 0;   /*  and clear the buffer count */
392                         pdx->bSendCharsPending = false; /*  Allow other threads again */
393                 } else {        /*  Here for sending chars normally - we hold the spin lock */
394                         int nPipe = 0;  /*  The pipe number to use */
395                         char *pDat = &pdx->outputBuffer[pdx->dwOutBuffGet];
396 
397                         if ((pdx->dwOutBuffGet + dwCount) > OUTBUF_SZ)  /*  does it cross buffer end? */
398                                 dwCount = OUTBUF_SZ - pdx->dwOutBuffGet;
399                         spin_unlock_irq(&pdx->charOutLock);     /*  we are done with stuff that changes */
400                         memcpy(pdx->pCoherCharOut, pDat, dwCount);      /*  copy output data to the buffer */
401                         usb_fill_bulk_urb(pdx->pUrbCharOut, pdx->udev,
402                                           usb_sndbulkpipe(pdx->udev,
403                                                           pdx->epAddr[0]),
404                                           pdx->pCoherCharOut, dwCount,
405                                           ced_writechar_callback, pdx);
406                         pdx->pUrbCharOut->transfer_flags |=
407                             URB_NO_TRANSFER_DMA_MAP;
408                         usb_anchor_urb(pdx->pUrbCharOut, &pdx->submitted);
409                         iReturn = usb_submit_urb(pdx->pUrbCharOut, GFP_KERNEL);
410                         spin_lock_irq(&pdx->charOutLock);       /*  grab lock for errors */
411                         if (iReturn) {
412                                 pdx->bPipeError[nPipe] = 1;     /*  Flag an error to be handled later */
413                                 pdx->bSendCharsPending = false; /*  Allow other threads again */
414                                 usb_unanchor_urb(pdx->pUrbCharOut);     /*  remove from list of active urbs */
415                         }
416                 }
417         } else if (pdx->bSendCharsPending && (pdx->dwNumOutput > 0))
418                 dev_dbg(&pdx->interface->dev,
419                         "%s: bSendCharsPending:true\n", __func__);
420 
421         dev_dbg(&pdx->interface->dev, "%s: exit code: %d\n", __func__, iReturn);
422         spin_unlock_irq(&pdx->charOutLock);     /*  Now let go of the spinlock */
423         return iReturn;
424 }
425 
426 /***************************************************************************
427 ** CopyUserSpace
428 ** This moves memory between pinned down user space and the pCoherStagedIO
429 ** memory buffer we use for transfers. Copy n bytes in the directions that
430 ** is defined by pdx->StagedRead. The user space is determined by the area
431 ** in pdx->StagedId and the offset in pdx->StagedDone. The user
432 ** area may well not start on a page boundary, so allow for that.
433 **
434 ** We have a table of physical pages that describe the area, so we can use
435 ** this to get a virtual address that the kernel can use.
436 **
437 ** pdx  Is our device extension which holds all we know about the transfer.
438 ** n    The number of bytes to move one way or the other.
439 ***************************************************************************/
440 static void CopyUserSpace(DEVICE_EXTENSION *pdx, int n)
441 {
442         unsigned int nArea = pdx->StagedId;
443         if (nArea < MAX_TRANSAREAS) {
444                 TRANSAREA *pArea = &pdx->rTransDef[nArea];      /*  area to be used */
445                 unsigned int dwOffset =
446                     pdx->StagedDone + pdx->StagedOffset + pArea->dwBaseOffset;
447                 char *pCoherBuf = pdx->pCoherStagedIO;  /*  coherent buffer */
448                 if (!pArea->bUsed) {
449                         dev_err(&pdx->interface->dev, "%s: area %d unused\n",
450                                 __func__, nArea);
451                         return;
452                 }
453 
454                 while (n) {
455                         int nPage = dwOffset >> PAGE_SHIFT;     /*  page number in table */
456                         if (nPage < pArea->nPages) {
457                                 char *pvAddress =
458                                     (char *)kmap_atomic(pArea->pPages[nPage]);
459                                 if (pvAddress) {
460                                         unsigned int uiPageOff = dwOffset & (PAGE_SIZE - 1);    /*  offset into the page */
461                                         size_t uiXfer = PAGE_SIZE - uiPageOff;  /*  max to transfer on this page */
462                                         if (uiXfer > n) /*  limit byte count if too much */
463                                                 uiXfer = n;     /*  for the page */
464                                         if (pdx->StagedRead)
465                                                 memcpy(pvAddress + uiPageOff,
466                                                        pCoherBuf, uiXfer);
467                                         else
468                                                 memcpy(pCoherBuf,
469                                                        pvAddress + uiPageOff,
470                                                        uiXfer);
471                                         kunmap_atomic(pvAddress);
472                                         dwOffset += uiXfer;
473                                         pCoherBuf += uiXfer;
474                                         n -= uiXfer;
475                                 } else {
476                                         dev_err(&pdx->interface->dev,
477                                                 "%s: did not map page %d\n",
478                                                 __func__, nPage);
479                                         return;
480                                 }
481 
482                         } else {
483                                 dev_err(&pdx->interface->dev,
484                                         "%s: exceeded pages %d\n",
485                                         __func__, nPage);
486                                 return;
487                         }
488                 }
489         } else
490                 dev_err(&pdx->interface->dev, "%s: bad area %d\n",
491                         __func__, nArea);
492 }
493 
494 /*  Forward declarations for stuff used circularly */
495 static int StageChunk(DEVICE_EXTENSION *pdx);
496 /***************************************************************************
497 ** ReadWrite_Complete
498 **
499 **  Completion routine for our staged read/write Irps
500 */
501 static void staged_callback(struct urb *pUrb)
502 {
503         DEVICE_EXTENSION *pdx = pUrb->context;
504         unsigned int nGot = pUrb->actual_length;        /*  what we transferred */
505         bool bCancel = false;
506         bool bRestartCharInput; /*  used at the end */
507 
508         spin_lock(&pdx->stagedLock);    /*  stop ReadWriteMem() action while this routine is running */
509         pdx->bStagedUrbPending = false; /*  clear the flag for staged IRP pending */
510 
511         if (pUrb->status) {     /*  sync/async unlink faults aren't errors */
512                 if (!
513                     (pUrb->status == -ENOENT || pUrb->status == -ECONNRESET
514                      || pUrb->status == -ESHUTDOWN)) {
515                         dev_err(&pdx->interface->dev,
516                                 "%s: nonzero write bulk status received: %d\n",
517                                 __func__, pUrb->status);
518                 } else
519                         dev_info(&pdx->interface->dev,
520                                  "%s: staged xfer cancelled\n", __func__);
521 
522                 spin_lock(&pdx->err_lock);
523                 pdx->errors = pUrb->status;
524                 spin_unlock(&pdx->err_lock);
525                 nGot = 0;       /*   and tidy up again if so */
526                 bCancel = true;
527         } else {
528                 dev_dbg(&pdx->interface->dev, "%s: %d chars xferred\n",
529                         __func__, nGot);
530                 if (pdx->StagedRead)    /*  if reading, save to user space */
531                         CopyUserSpace(pdx, nGot);       /*  copy from buffer to user */
532                 if (nGot == 0)
533                         dev_dbg(&pdx->interface->dev, "%s: ZLP\n", __func__);
534         }
535 
536         /*  Update the transfer length based on the TransferBufferLength value in the URB */
537         pdx->StagedDone += nGot;
538 
539         dev_dbg(&pdx->interface->dev, "%s: done %d bytes of %d\n",
540                 __func__, pdx->StagedDone, pdx->StagedLength);
541 
542         if ((pdx->StagedDone == pdx->StagedLength) ||   /*  If no more to do */
543             (bCancel)) {                /*  or this IRP was cancelled */
544                 TRANSAREA *pArea = &pdx->rTransDef[pdx->StagedId];      /*  Transfer area info */
545                 dev_dbg(&pdx->interface->dev,
546                         "%s: transfer done, bytes %d, cancel %d\n",
547                         __func__, pdx->StagedDone, bCancel);
548 
549                 /*  Here is where we sort out what to do with this transfer if using a circular buffer. We have */
550                 /*   a completed transfer that can be assumed to fit into the transfer area. We should be able to */
551                 /*   add this to the end of a growing block or to use it to start a new block unless the code */
552                 /*   that calculates the offset to use (in ReadWriteMem) is totally duff. */
553                 if ((pArea->bCircular) && (pArea->bCircToHost) && (!bCancel) && /*  Time to sort out circular buffer info? */
554                     (pdx->StagedRead)) {        /*  Only for tohost transfers for now */
555                         if (pArea->aBlocks[1].dwSize > 0) {     /*  If block 1 is in use we must append to it */
556                                 if (pdx->StagedOffset ==
557                                     (pArea->aBlocks[1].dwOffset +
558                                      pArea->aBlocks[1].dwSize)) {
559                                         pArea->aBlocks[1].dwSize +=
560                                             pdx->StagedLength;
561                                         dev_dbg(&pdx->interface->dev,
562                                                 "RWM_Complete, circ block 1 now %d bytes at %d\n",
563                                                 pArea->aBlocks[1].dwSize,
564                                                 pArea->aBlocks[1].dwOffset);
565                                 } else {
566                                         /*  Here things have gone very, very, wrong, but I cannot see how this can actually be achieved */
567                                         pArea->aBlocks[1].dwOffset =
568                                             pdx->StagedOffset;
569                                         pArea->aBlocks[1].dwSize =
570                                             pdx->StagedLength;
571                                         dev_err(&pdx->interface->dev,
572                                                 "%s: ERROR, circ block 1 re-started %d bytes at %d\n",
573                                                 __func__,
574                                                 pArea->aBlocks[1].dwSize,
575                                                 pArea->aBlocks[1].dwOffset);
576                                 }
577                         } else {        /*  If block 1 is not used, we try to add to block 0 */
578                                 if (pArea->aBlocks[0].dwSize > 0) {     /*  Got stored block 0 information? */
579                                         /*  Must append onto the existing block 0 */
580                                         if (pdx->StagedOffset ==
581                                             (pArea->aBlocks[0].dwOffset +
582                                              pArea->aBlocks[0].dwSize)) {
583                                                 pArea->aBlocks[0].dwSize += pdx->StagedLength;  /*  Just add this transfer in */
584                                                 dev_dbg(&pdx->interface->dev,
585                                                         "RWM_Complete, circ block 0 now %d bytes at %d\n",
586                                                         pArea->aBlocks[0].
587                                                         dwSize,
588                                                         pArea->aBlocks[0].
589                                                         dwOffset);
590                                         } else {        /*  If it doesn't append, put into new block 1 */
591                                                 pArea->aBlocks[1].dwOffset =
592                                                     pdx->StagedOffset;
593                                                 pArea->aBlocks[1].dwSize =
594                                                     pdx->StagedLength;
595                                                 dev_dbg(&pdx->interface->dev,
596                                                         "RWM_Complete, circ block 1 started %d bytes at %d\n",
597                                                         pArea->aBlocks[1].
598                                                         dwSize,
599                                                         pArea->aBlocks[1].
600                                                         dwOffset);
601                                         }
602                                 } else  { /*  No info stored yet, just save in block 0 */
603                                         pArea->aBlocks[0].dwOffset =
604                                             pdx->StagedOffset;
605                                         pArea->aBlocks[0].dwSize =
606                                             pdx->StagedLength;
607                                         dev_dbg(&pdx->interface->dev,
608                                                 "RWM_Complete, circ block 0 started %d bytes at %d\n",
609                                                 pArea->aBlocks[0].dwSize,
610                                                 pArea->aBlocks[0].dwOffset);
611                                 }
612                         }
613                 }
614 
615                 if (!bCancel) { /*  Don't generate an event if cancelled */
616                         dev_dbg(&pdx->interface->dev,
617                                 "RWM_Complete,  bCircular %d, bToHost %d, eStart %d, eSize %d\n",
618                                 pArea->bCircular, pArea->bEventToHost,
619                                 pArea->dwEventSt, pArea->dwEventSz);
620                         if ((pArea->dwEventSz) &&       /*  Set a user-mode event... */
621                             (pdx->StagedRead == pArea->bEventToHost)) { /*  ...on transfers in this direction? */
622                                 int iWakeUp = 0;        /*  assume */
623                                 /*  If we have completed the right sort of DMA transfer then set the event to notify */
624                                 /*    the user code to wake up anyone that is waiting. */
625                                 if ((pArea->bCircular) &&       /*  Circular areas use a simpler test */
626                                     (pArea->bCircToHost)) {     /*  only in supported direction */
627                                         /*  Is total data waiting up to size limit? */
628                                         unsigned int dwTotal =
629                                             pArea->aBlocks[0].dwSize +
630                                             pArea->aBlocks[1].dwSize;
631                                         iWakeUp = (dwTotal >= pArea->dwEventSz);
632                                 } else {
633                                         unsigned int transEnd =
634                                             pdx->StagedOffset +
635                                             pdx->StagedLength;
636                                         unsigned int eventEnd =
637                                             pArea->dwEventSt + pArea->dwEventSz;
638                                         iWakeUp = (pdx->StagedOffset < eventEnd)
639                                             && (transEnd > pArea->dwEventSt);
640                                 }
641 
642                                 if (iWakeUp) {
643                                         dev_dbg(&pdx->interface->dev,
644                                                 "About to set event to notify app\n");
645                                         wake_up_interruptible(&pArea->wqEvent); /*  wake up waiting processes */
646                                         ++pArea->iWakeUp;       /*  increment wakeup count */
647                                 }
648                         }
649                 }
650 
651                 pdx->dwDMAFlag = MODE_CHAR;     /*  Switch back to char mode before ReadWriteMem call */
652 
653                 if (!bCancel) { /*  Don't look for waiting transfer if cancelled */
654                         /*  If we have a transfer waiting, kick it off */
655                         if (pdx->bXFerWaiting) {        /*  Got a block xfer waiting? */
656                                 int iReturn;
657                                 dev_info(&pdx->interface->dev,
658                                          "*** RWM_Complete *** pending transfer will now be set up!!!\n");
659                                 iReturn =
660                                     ReadWriteMem(pdx, !pdx->rDMAInfo.bOutWard,
661                                                  pdx->rDMAInfo.wIdent,
662                                                  pdx->rDMAInfo.dwOffset,
663                                                  pdx->rDMAInfo.dwSize);
664 
665                                 if (iReturn)
666                                         dev_err(&pdx->interface->dev,
667                                                 "RWM_Complete rw setup failed %d\n",
668                                                 iReturn);
669                         }
670                 }
671 
672         } else                  /*  Here for more to do */
673                 StageChunk(pdx);        /*  fire off the next bit */
674 
675         /*  While we hold the stagedLock, see if we should reallow character input ints */
676         /*  Don't allow if cancelled, or if a new block has started or if there is a waiting block. */
677         /*  This feels wrong as we should ask which spin lock protects dwDMAFlag. */
678         bRestartCharInput = !bCancel && (pdx->dwDMAFlag == MODE_CHAR)
679             && !pdx->bXFerWaiting;
680 
681         spin_unlock(&pdx->stagedLock);  /*  Finally release the lock again */
682 
683         /*  This is not correct as dwDMAFlag is protected by the staged lock, but it is treated */
684         /*  in Allowi as if it were protected by the char lock. In any case, most systems will */
685         /*  not be upset by char input during DMA... sigh. Needs sorting out. */
686         if (bRestartCharInput)  /*  may be out of date, but... */
687                 Allowi(pdx);    /*  ...Allowi tests a lock too. */
688         dev_dbg(&pdx->interface->dev, "%s: done\n", __func__);
689 }
690 
691 /****************************************************************************
692 ** StageChunk
693 **
694 ** Generates the next chunk of data making up a staged transfer.
695 **
696 ** The calling code must have acquired the staging spinlock before calling
697 **  this function, and is responsible for releasing it. We are at callback level.
698 ****************************************************************************/
699 static int StageChunk(DEVICE_EXTENSION *pdx)
700 {
701         int iReturn = U14ERR_NOERROR;
702         unsigned int ChunkSize;
703         int nPipe = pdx->StagedRead ? 3 : 2;    /*  The pipe number to use for reads or writes */
704         if (pdx->nPipes == 3)
705                 nPipe--;        /*  Adjust for the 3-pipe case */
706         if (nPipe < 0)          /*  and trap case that should never happen */
707                 return U14ERR_FAIL;
708 
709         if (!CanAcceptIoRequests(pdx)) {        /*  got sudden remove? */
710                 dev_info(&pdx->interface->dev, "%s: sudden remove, giving up\n",
711                          __func__);
712                 return U14ERR_FAIL;     /*  could do with a better error */
713         }
714 
715         ChunkSize = (pdx->StagedLength - pdx->StagedDone);      /*  transfer length remaining */
716         if (ChunkSize > STAGED_SZ)      /*  make sure to keep legal */
717                 ChunkSize = STAGED_SZ;  /*   limit to max allowed */
718 
719         if (!pdx->StagedRead)   /*  if writing... */
720                 CopyUserSpace(pdx, ChunkSize);  /*  ...copy data into the buffer */
721 
722         usb_fill_bulk_urb(pdx->pStagedUrb, pdx->udev,
723                           pdx->StagedRead ? usb_rcvbulkpipe(pdx->udev,
724                                                             pdx->
725                                                             epAddr[nPipe]) :
726                           usb_sndbulkpipe(pdx->udev, pdx->epAddr[nPipe]),
727                           pdx->pCoherStagedIO, ChunkSize, staged_callback, pdx);
728         pdx->pStagedUrb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
729         usb_anchor_urb(pdx->pStagedUrb, &pdx->submitted);       /*  in case we need to kill it */
730         iReturn = usb_submit_urb(pdx->pStagedUrb, GFP_ATOMIC);
731         if (iReturn) {
732                 usb_unanchor_urb(pdx->pStagedUrb);      /*  kill it */
733                 pdx->bPipeError[nPipe] = 1;     /*  Flag an error to be handled later */
734                 dev_err(&pdx->interface->dev, "%s: submit urb failed, code %d\n",
735                         __func__, iReturn);
736         } else
737                 pdx->bStagedUrbPending = true;  /*  Set the flag for staged URB pending */
738         dev_dbg(&pdx->interface->dev, "%s: done so far:%d, this size:%d\n",
739                 __func__, pdx->StagedDone, ChunkSize);
740 
741         return iReturn;
742 }
743 
744 /***************************************************************************
745 ** ReadWriteMem
746 **
747 ** This routine is used generally for block read and write operations.
748 ** Breaks up a read or write in to specified sized chunks, as specified by pipe
749 ** information on maximum transfer size.
750 **
751 ** Any code that calls this must be holding the stagedLock
752 **
753 ** Arguments:
754 **    DeviceObject - pointer to our FDO (Functional Device Object)
755 **    Read - TRUE for read, FALSE for write. This is from POV of the driver
756 **    wIdent - the transfer area number - defines memory area and more.
757 **    dwOffs - the start offset within the transfer area of the start of this
758 **             transfer.
759 **    dwLen - the number of bytes to transfer.
760 */
761 int ReadWriteMem(DEVICE_EXTENSION *pdx, bool Read, unsigned short wIdent,
762                  unsigned int dwOffs, unsigned int dwLen)
763 {
764         TRANSAREA *pArea = &pdx->rTransDef[wIdent];     /*  Transfer area info */
765 
766         if (!CanAcceptIoRequests(pdx)) {        /*  Are we in a state to accept new requests? */
767                 dev_err(&pdx->interface->dev, "%s: can't accept requests\n",
768                         __func__);
769                 return U14ERR_FAIL;
770         }
771 
772         dev_dbg(&pdx->interface->dev,
773                 "%s: xfer %d bytes to %s, offset %d, area %d\n",
774                 __func__, dwLen, Read ? "host" : "1401", dwOffs, wIdent);
775 
776         /*  Amazingly, we can get an escape sequence back before the current staged Urb is done, so we */
777         /*   have to check for this situation and, if so, wait until all is OK. */
778         if (pdx->bStagedUrbPending) {
779                 pdx->bXFerWaiting = true;       /*  Flag we are waiting */
780                 dev_info(&pdx->interface->dev,
781                          "%s: xfer is waiting, as previous staged pending\n",
782                          __func__);
783                 return U14ERR_NOERROR;
784         }
785 
786         if (dwLen == 0) {               /*  allow 0-len read or write; just return success */
787                 dev_dbg(&pdx->interface->dev,
788                         "%s: OK; zero-len read/write request\n", __func__);
789                 return U14ERR_NOERROR;
790         }
791 
792         if ((pArea->bCircular) &&       /*  Circular transfer? */
793             (pArea->bCircToHost) && (Read)) {   /*  In a supported direction */
794                                 /*  If so, we sort out offset ourself */
795                 bool bWait = false;     /*  Flag for transfer having to wait */
796 
797                 dev_dbg(&pdx->interface->dev,
798                         "Circular buffers are %d at %d and %d at %d\n",
799                         pArea->aBlocks[0].dwSize, pArea->aBlocks[0].dwOffset,
800                         pArea->aBlocks[1].dwSize, pArea->aBlocks[1].dwOffset);
801                 if (pArea->aBlocks[1].dwSize > 0) {     /*  Using the second block already? */
802                         dwOffs = pArea->aBlocks[1].dwOffset + pArea->aBlocks[1].dwSize; /*  take offset from that */
803                         bWait = (dwOffs + dwLen) > pArea->aBlocks[0].dwOffset;  /*  Wait if will overwrite block 0? */
804                         bWait |= (dwOffs + dwLen) > pArea->dwLength;    /*  or if it overflows the buffer */
805                 } else {                /*  Area 1 not in use, try to use area 0 */
806                         if (pArea->aBlocks[0].dwSize == 0)      /*  Reset block 0 if not in use */
807                                 pArea->aBlocks[0].dwOffset = 0;
808                         dwOffs =
809                             pArea->aBlocks[0].dwOffset +
810                             pArea->aBlocks[0].dwSize;
811                         if ((dwOffs + dwLen) > pArea->dwLength) {       /*  Off the end of the buffer? */
812                                 pArea->aBlocks[1].dwOffset = 0; /*  Set up to use second block */
813                                 dwOffs = 0;
814                                 bWait = (dwOffs + dwLen) > pArea->aBlocks[0].dwOffset;  /*  Wait if will overwrite block 0? */
815                                 bWait |= (dwOffs + dwLen) > pArea->dwLength;    /*  or if it overflows the buffer */
816                         }
817                 }
818 
819                 if (bWait) {    /*  This transfer will have to wait? */
820                         pdx->bXFerWaiting = true;       /*  Flag we are waiting */
821                         dev_dbg(&pdx->interface->dev,
822                                 "%s: xfer waiting for circular buffer space\n",
823                                 __func__);
824                         return U14ERR_NOERROR;
825                 }
826 
827                 dev_dbg(&pdx->interface->dev,
828                         "%s: circular xfer, %d bytes starting at %d\n",
829                         __func__, dwLen, dwOffs);
830         }
831         /*  Save the parameters for the read\write transfer */
832         pdx->StagedRead = Read; /*  Save the parameters for this read */
833         pdx->StagedId = wIdent; /*  ID allows us to get transfer area info */
834         pdx->StagedOffset = dwOffs;     /*  The area within the transfer area */
835         pdx->StagedLength = dwLen;
836         pdx->StagedDone = 0;    /*  Initialise the byte count */
837         pdx->dwDMAFlag = MODE_LINEAR;   /*  Set DMA mode flag at this point */
838         pdx->bXFerWaiting = false;      /*  Clearly not a transfer waiting now */
839 
840 /*     KeClearEvent(&pdx->StagingDoneEvent);           // Clear the transfer done event */
841         StageChunk(pdx);        /*  fire off the first chunk */
842 
843         return U14ERR_NOERROR;
844 }
845 
846 /****************************************************************************
847 **
848 ** ReadChar
849 **
850 ** Reads a character a buffer. If there is no more
851 **  data we return FALSE. Used as part of decoding a DMA request.
852 **
853 ****************************************************************************/
854 static bool ReadChar(unsigned char *pChar, char *pBuf, unsigned int *pdDone,
855                      unsigned int dGot)
856 {
857         bool bRead = false;
858         unsigned int dDone = *pdDone;
859 
860         if (dDone < dGot) {     /*  If there is more data */
861                 *pChar = (unsigned char)pBuf[dDone];    /*  Extract the next char */
862                 dDone++;        /*  Increment the done count */
863                 *pdDone = dDone;
864                 bRead = true;   /*  and flag success */
865         }
866 
867         return bRead;
868 }
869 
870 #ifdef NOTUSED
871 /****************************************************************************
872 **
873 ** ReadWord
874 **
875 ** Reads a word from the 1401, just uses ReadChar twice; passes on any error
876 **
877 *****************************************************************************/
878 static bool ReadWord(unsigned short *pWord, char *pBuf, unsigned int *pdDone,
879                      unsigned int dGot)
880 {
881         if (ReadChar((unsigned char *)pWord, pBuf, pdDone, dGot))
882                 return ReadChar(((unsigned char *)pWord) + 1, pBuf, pdDone,
883                                 dGot);
884         else
885                 return false;
886 }
887 #endif
888 
889 /****************************************************************************
890 ** ReadHuff
891 **
892 ** Reads a coded number in and returns it, Code is:
893 ** If data is in range 0..127 we receive 1 byte. If data in range 128-16383
894 ** we receive two bytes, top bit of first indicates another on its way. If
895 ** data in range 16384-4194303 we get three bytes, top two bits of first set
896 ** to indicate three byte total.
897 **
898 *****************************************************************************/
899 static bool ReadHuff(volatile unsigned int *pDWord, char *pBuf,
900                      unsigned int *pdDone, unsigned int dGot)
901 {
902         unsigned char ucData;   /* for each read to ReadChar */
903         bool bReturn = true;    /* assume we will succeed */
904         unsigned int dwData = 0;        /* Accumulator for the data */
905 
906         if (ReadChar(&ucData, pBuf, pdDone, dGot)) {
907                 dwData = ucData;        /* copy the data */
908                 if ((dwData & 0x00000080) != 0) {       /* Bit set for more data ? */
909                         dwData &= 0x0000007F;   /* Clear the relevant bit */
910                         if (ReadChar(&ucData, pBuf, pdDone, dGot)) {
911                                 dwData = (dwData << 8) | ucData;
912                                 if ((dwData & 0x00004000) != 0) {       /* three byte sequence ? */
913                                         dwData &= 0x00003FFF;   /* Clear the relevant bit */
914                                         if (ReadChar
915                                             (&ucData, pBuf, pdDone, dGot))
916                                                 dwData = (dwData << 8) | ucData;
917                                         else
918                                                 bReturn = false;
919                                 }
920                         } else
921                                 bReturn = false;        /* couldn't read data */
922                 }
923         } else
924                 bReturn = false;
925 
926         *pDWord = dwData;       /* return the data */
927         return bReturn;
928 }
929 
930 /***************************************************************************
931 **
932 ** ReadDMAInfo
933 **
934 ** Tries to read info about the dma request from the 1401 and decode it into
935 ** the dma descriptor block. We have at this point had the escape character
936 ** from the 1401 and now we must read in the rest of the information about
937 ** the transfer request. Returns FALSE if 1401 fails to respond or obselete
938 ** code from 1401 or bad parameters.
939 **
940 ** The pBuf char pointer does not include the initial escape character, so
941 **  we start handling the data at offset zero.
942 **
943 *****************************************************************************/
944 static bool ReadDMAInfo(volatile DMADESC *pDmaDesc, DEVICE_EXTENSION *pdx,
945                         char *pBuf, unsigned int dwCount)
946 {
947         bool bResult = false;   /*  assume we won't succeed */
948         unsigned char ucData;
949         unsigned int dDone = 0; /*  We haven't parsed anything so far */
950 
951         dev_dbg(&pdx->interface->dev, "%s\n", __func__);
952 
953         if (ReadChar(&ucData, pBuf, &dDone, dwCount)) {
954                 unsigned char ucTransCode = (ucData & 0x0F);    /*  get code for transfer type */
955                 unsigned short wIdent = ((ucData >> 4) & 0x07); /*  and area identifier */
956 
957                 /*  fill in the structure we were given */
958                 pDmaDesc->wTransType = ucTransCode;     /*  type of transfer */
959                 pDmaDesc->wIdent = wIdent;      /*  area to use */
960                 pDmaDesc->dwSize = 0;   /*  initialise other bits */
961                 pDmaDesc->dwOffset = 0;
962 
963                 dev_dbg(&pdx->interface->dev, "%s: type: %d ident: %d\n",
964                         __func__, pDmaDesc->wTransType, pDmaDesc->wIdent);
965 
966                 pDmaDesc->bOutWard = (ucTransCode != TM_EXTTOHOST);     /*  set transfer direction */
967 
968                 switch (ucTransCode) {
969                 case TM_EXTTOHOST:      /*  Extended linear transfer modes (the only ones!) */
970                 case TM_EXTTO1401:
971                         {
972                                 bResult =
973                                     ReadHuff(&(pDmaDesc->dwOffset), pBuf,
974                                              &dDone, dwCount)
975                                     && ReadHuff(&(pDmaDesc->dwSize), pBuf,
976                                                 &dDone, dwCount);
977                                 if (bResult) {
978                                         dev_dbg(&pdx->interface->dev,
979                                                 "%s: xfer offset & size %d %d\n",
980                                                 __func__, pDmaDesc->dwOffset,
981                                                 pDmaDesc->dwSize);
982 
983                                         if ((wIdent >= MAX_TRANSAREAS) ||       /*  Illegal area number, or... */
984                                             (!pdx->rTransDef[wIdent].bUsed) ||  /*  area not set up, or... */
985                                             (pDmaDesc->dwOffset > pdx->rTransDef[wIdent].dwLength) ||   /*  range/size */
986                                             ((pDmaDesc->dwOffset +
987                                               pDmaDesc->dwSize) >
988                                              (pdx->rTransDef[wIdent].
989                                               dwLength))) {
990                                                 bResult = false;        /*  bad parameter(s) */
991                                                 dev_dbg(&pdx->interface->dev,
992                                                         "%s: bad param - id %d, bUsed %d, offset %d, size %d, area length %d\n",
993                                                         __func__, wIdent,
994                                                         pdx->rTransDef[wIdent].
995                                                         bUsed,
996                                                         pDmaDesc->dwOffset,
997                                                         pDmaDesc->dwSize,
998                                                         pdx->rTransDef[wIdent].
999                                                         dwLength);
1000                                         }
1001                                 }
1002                                 break;
1003                         }
1004                 default:
1005                         break;
1006                 }
1007         } else
1008                 bResult = false;
1009 
1010         if (!bResult)           /*  now check parameters for validity */
1011                 dev_err(&pdx->interface->dev, "%s: error reading Esc sequence\n",
1012                         __func__);
1013 
1014         return bResult;
1015 }
1016 
1017 /****************************************************************************
1018 **
1019 ** Handle1401Esc
1020 **
1021 ** Deals with an escape sequence coming from the 1401. This can either be
1022 **  a DMA transfer request of various types or a response to an escape sequence
1023 **  sent to the 1401. This is called from a callback.
1024 **
1025 ** Parameters are
1026 **
1027 ** dwCount - the number of characters in the device extension char in buffer,
1028 **           this is known to be at least 2 or we will not be called.
1029 **
1030 ****************************************************************************/
1031 static int Handle1401Esc(DEVICE_EXTENSION *pdx, char *pCh,
1032                          unsigned int dwCount)
1033 {
1034         int iReturn = U14ERR_FAIL;
1035 
1036         /*  I have no idea what this next test is about. '?' is 0x3f, which is area 3, code */
1037         /*  15. At the moment, this is not used, so it does no harm, but unless someone can */
1038         /*  tell me what this is for, it should be removed from this and the Windows driver. */
1039         if (pCh[0] == '?') {    /*  Is this an information response */
1040                                 /*  Parse and save the information */
1041         } else {
1042                 spin_lock(&pdx->stagedLock);    /*  Lock others out */
1043 
1044                 if (ReadDMAInfo(&pdx->rDMAInfo, pdx, pCh, dwCount)) {   /*  Get DMA parameters */
1045                         unsigned short wTransType = pdx->rDMAInfo.wTransType;   /*  check transfer type */
1046 
1047                         dev_dbg(&pdx->interface->dev,
1048                                 "%s: xfer to %s, offset %d, length %d\n",
1049                                 __func__,
1050                                 pdx->rDMAInfo.bOutWard ? "1401" : "host",
1051                                 pdx->rDMAInfo.dwOffset, pdx->rDMAInfo.dwSize);
1052 
1053                         if (pdx->bXFerWaiting) { /*  Check here for badly out of kilter... */
1054                                 /*  This can never happen, really */
1055                                 dev_err(&pdx->interface->dev,
1056                                         "ERROR: DMA setup while transfer still waiting\n");
1057                         } else {
1058                                 if ((wTransType == TM_EXTTOHOST)
1059                                     || (wTransType == TM_EXTTO1401)) {
1060                                         iReturn =
1061                                             ReadWriteMem(pdx,
1062                                                          !pdx->rDMAInfo.
1063                                                          bOutWard,
1064                                                          pdx->rDMAInfo.wIdent,
1065                                                          pdx->rDMAInfo.dwOffset,
1066                                                          pdx->rDMAInfo.dwSize);
1067                                         if (iReturn != U14ERR_NOERROR)
1068                                                 dev_err(&pdx->interface->dev,
1069                                                         "%s: ReadWriteMem() failed %d\n",
1070                                                         __func__, iReturn);
1071                                 } else  /*  This covers non-linear transfer setup */
1072                                         dev_err(&pdx->interface->dev,
1073                                                 "%s: Unknown block xfer type %d\n",
1074                                                 __func__, wTransType);
1075                         }
1076                 } else          /*  Failed to read parameters */
1077                         dev_err(&pdx->interface->dev, "%s: ReadDMAInfo() fail\n",
1078                                 __func__);
1079 
1080                 spin_unlock(&pdx->stagedLock);  /*  OK here */
1081         }
1082 
1083         dev_dbg(&pdx->interface->dev, "%s: returns %d\n", __func__, iReturn);
1084 
1085         return iReturn;
1086 }
1087 
1088 /****************************************************************************
1089 ** Callback for the character read complete or error
1090 ****************************************************************************/
1091 static void ced_readchar_callback(struct urb *pUrb)
1092 {
1093         DEVICE_EXTENSION *pdx = pUrb->context;
1094         int nGot = pUrb->actual_length; /*  what we transferred */
1095 
1096         if (pUrb->status) {     /*  Do we have a problem to handle? */
1097                 int nPipe = pdx->nPipes == 4 ? 1 : 0;   /*  The pipe number to use for error */
1098                 /*  sync/async unlink faults aren't errors... just saying device removed or stopped */
1099                 if (!
1100                     (pUrb->status == -ENOENT || pUrb->status == -ECONNRESET
1101                      || pUrb->status == -ESHUTDOWN)) {
1102                         dev_err(&pdx->interface->dev,
1103                                 "%s: nonzero write bulk status received: %d\n",
1104                                 __func__, pUrb->status);
1105                 } else
1106                         dev_dbg(&pdx->interface->dev,
1107                                 "%s: 0 chars pUrb->status=%d (shutdown?)\n",
1108                                 __func__, pUrb->status);
1109 
1110                 spin_lock(&pdx->err_lock);
1111                 pdx->errors = pUrb->status;
1112                 spin_unlock(&pdx->err_lock);
1113                 nGot = 0;       /*   and tidy up again if so */
1114 
1115                 spin_lock(&pdx->charInLock);    /*  already at irq level */
1116                 pdx->bPipeError[nPipe] = 1;     /*  Flag an error for later */
1117         } else {
1118                 if ((nGot > 1) && ((pdx->pCoherCharIn[0] & 0x7f) == 0x1b)) {    /*  Esc sequence? */
1119                         Handle1401Esc(pdx, &pdx->pCoherCharIn[1], nGot - 1);    /*  handle it */
1120                         spin_lock(&pdx->charInLock);    /*  already at irq level */
1121                 } else {
1122                         spin_lock(&pdx->charInLock);    /*  already at irq level */
1123                         if (nGot > 0) {
1124                                 unsigned int i;
1125                                 if (nGot < INBUF_SZ) {
1126                                         pdx->pCoherCharIn[nGot] = 0;    /*  tidy the string */
1127                                         dev_dbg(&pdx->interface->dev,
1128                                                 "%s: got %d chars >%s<\n",
1129                                                 __func__, nGot,
1130                                                 pdx->pCoherCharIn);
1131                                 }
1132                                 /*  We know that whatever we read must fit in the input buffer */
1133                                 for (i = 0; i < nGot; i++) {
1134                                         pdx->inputBuffer[pdx->dwInBuffPut++] =
1135                                             pdx->pCoherCharIn[i] & 0x7F;
1136                                         if (pdx->dwInBuffPut >= INBUF_SZ)
1137                                                 pdx->dwInBuffPut = 0;
1138                                 }
1139 
1140                                 if ((pdx->dwNumInput + nGot) <= INBUF_SZ)
1141                                         pdx->dwNumInput += nGot;        /*  Adjust the buffer count accordingly */
1142                         } else
1143                                 dev_dbg(&pdx->interface->dev, "%s: read ZLP\n",
1144                                         __func__);
1145                 }
1146         }
1147 
1148         pdx->bReadCharsPending = false; /*  No longer have a pending read */
1149         spin_unlock(&pdx->charInLock);  /*  already at irq level */
1150 
1151         Allowi(pdx);    /*  see if we can do the next one */
1152 }
1153 
1154 /****************************************************************************
1155 ** Allowi
1156 **
1157 ** This is used to make sure that there is always a pending input transfer so
1158 ** we can pick up any inward transfers. This can be called in multiple contexts
1159 ** so we use the irqsave version of the spinlock.
1160 ****************************************************************************/
1161 int Allowi(DEVICE_EXTENSION *pdx)
1162 {
1163         int iReturn = U14ERR_NOERROR;
1164         unsigned long flags;
1165         spin_lock_irqsave(&pdx->charInLock, flags);     /*  can be called in multiple contexts */
1166 
1167         /*  We don't want char input running while DMA is in progress as we know that this */
1168         /*   can cause sequencing problems for the 2270. So don't. It will also allow the */
1169         /*   ERR response to get back to the host code too early on some PCs, even if there */
1170         /*   is no actual driver failure, so we don't allow this at all. */
1171         if (!pdx->bInDrawDown &&        /*  stop input if */
1172             !pdx->bReadCharsPending &&  /*  If no read request outstanding */
1173             (pdx->dwNumInput < (INBUF_SZ / 2)) &&       /*   and there is some space */
1174             (pdx->dwDMAFlag == MODE_CHAR) &&    /*   not doing any DMA */
1175             (!pdx->bXFerWaiting) &&     /*   no xfer waiting to start */
1176             (CanAcceptIoRequests(pdx))) { /*   and activity is generally OK */
1177                                 /*   then off we go */
1178                 unsigned int nMax = INBUF_SZ - pdx->dwNumInput; /*  max we could read */
1179                 int nPipe = pdx->nPipes == 4 ? 1 : 0;   /*  The pipe number to use */
1180 
1181                 dev_dbg(&pdx->interface->dev, "%s: %d chars in input buffer\n",
1182                         __func__, pdx->dwNumInput);
1183 
1184                 usb_fill_int_urb(pdx->pUrbCharIn, pdx->udev,
1185                                  usb_rcvintpipe(pdx->udev, pdx->epAddr[nPipe]),
1186                                  pdx->pCoherCharIn, nMax, ced_readchar_callback,
1187                                  pdx, pdx->bInterval);
1188                 pdx->pUrbCharIn->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;     /*  short xfers are OK by default */
1189                 usb_anchor_urb(pdx->pUrbCharIn, &pdx->submitted);       /*  in case we need to kill it */
1190                 iReturn = usb_submit_urb(pdx->pUrbCharIn, GFP_ATOMIC);
1191                 if (iReturn) {
1192                         usb_unanchor_urb(pdx->pUrbCharIn);      /*  remove from list of active Urbs */
1193                         pdx->bPipeError[nPipe] = 1;     /*  Flag an error to be handled later */
1194                         dev_err(&pdx->interface->dev,
1195                                 "%s: submit urb failed: %d\n",
1196                                 __func__, iReturn);
1197                 } else
1198                         pdx->bReadCharsPending = true;  /*  Flag that we are active here */
1199         }
1200 
1201         spin_unlock_irqrestore(&pdx->charInLock, flags);
1202 
1203         return iReturn;
1204 
1205 }
1206 
1207 /*****************************************************************************
1208 ** The ioctl entry point to the driver that is used by us to talk to it.
1209 ** inode    The device node (no longer in 3.0.0 kernels)
1210 ** file     The file that is open, which holds our pdx pointer
1211 ** ulArg    The argument passed in. Note that long is 64-bits in 64-bit system, i.e. it is big
1212 **          enough for a 64-bit pointer.
1213 *****************************************************************************/
1214 static long ced_ioctl(struct file *file, unsigned int cmd, unsigned long ulArg)
1215 {
1216         int err = 0;
1217         DEVICE_EXTENSION *pdx = file->private_data;
1218         if (!CanAcceptIoRequests(pdx))  /*  check we still exist */
1219                 return -ENODEV;
1220 
1221         /*  Check that access is allowed, where is is needed. Anything that would have an indeterminate */
1222         /*  size will be checked by the specific command. */
1223         if (_IOC_DIR(cmd) & _IOC_READ)  /*  read from point of view of user... */
1224                 err = !access_ok(VERIFY_WRITE, (void __user *)ulArg, _IOC_SIZE(cmd));   /*  is kernel write */
1225         else if (_IOC_DIR(cmd) & _IOC_WRITE)    /*  and write from point of view of user... */
1226                 err = !access_ok(VERIFY_READ, (void __user *)ulArg, _IOC_SIZE(cmd));    /*  is kernel read */
1227         if (err)
1228                 return -EFAULT;
1229 
1230         switch (_IOC_NR(cmd)) {
1231         case _IOC_NR(IOCTL_CED_SENDSTRING(0)):
1232                 return SendString(pdx, (const char __user *)ulArg,
1233                                   _IOC_SIZE(cmd));
1234 
1235         case _IOC_NR(IOCTL_CED_RESET1401):
1236                 return Reset1401(pdx);
1237 
1238         case _IOC_NR(IOCTL_CED_GETCHAR):
1239                 return GetChar(pdx);
1240 
1241         case _IOC_NR(IOCTL_CED_SENDCHAR):
1242                 return SendChar(pdx, (char)ulArg);
1243 
1244         case _IOC_NR(IOCTL_CED_STAT1401):
1245                 return Stat1401(pdx);
1246 
1247         case _IOC_NR(IOCTL_CED_LINECOUNT):
1248                 return LineCount(pdx);
1249 
1250         case _IOC_NR(IOCTL_CED_GETSTRING(0)):
1251                 return GetString(pdx, (char __user *)ulArg, _IOC_SIZE(cmd));
1252 
1253         case _IOC_NR(IOCTL_CED_SETTRANSFER):
1254                 return SetTransfer(pdx, (struct transfer_area_desc __user *) ulArg);
1255 
1256         case _IOC_NR(IOCTL_CED_UNSETTRANSFER):
1257                 return UnsetTransfer(pdx, (int)ulArg);
1258 
1259         case _IOC_NR(IOCTL_CED_SETEVENT):
1260                 return SetEvent(pdx, (struct transfer_event __user *) ulArg);
1261 
1262         case _IOC_NR(IOCTL_CED_GETOUTBUFSPACE):
1263                 return GetOutBufSpace(pdx);
1264 
1265         case _IOC_NR(IOCTL_CED_GETBASEADDRESS):
1266                 return -1;
1267 
1268         case _IOC_NR(IOCTL_CED_GETDRIVERREVISION):
1269                 return (2 << 24) | (DRIVERMAJREV << 16) | DRIVERMINREV; /*  USB | MAJOR | MINOR */
1270 
1271         case _IOC_NR(IOCTL_CED_GETTRANSFER):
1272                 return GetTransfer(pdx, (TGET_TX_BLOCK __user *) ulArg);
1273 
1274         case _IOC_NR(IOCTL_CED_KILLIO1401):
1275                 return KillIO1401(pdx);
1276 
1277         case _IOC_NR(IOCTL_CED_STATEOF1401):
1278                 return StateOf1401(pdx);
1279 
1280         case _IOC_NR(IOCTL_CED_GRAB1401):
1281         case _IOC_NR(IOCTL_CED_FREE1401):
1282                 return U14ERR_NOERROR;
1283 
1284         case _IOC_NR(IOCTL_CED_STARTSELFTEST):
1285                 return StartSelfTest(pdx);
1286 
1287         case _IOC_NR(IOCTL_CED_CHECKSELFTEST):
1288                 return CheckSelfTest(pdx, (TGET_SELFTEST __user *) ulArg);
1289 
1290         case _IOC_NR(IOCTL_CED_TYPEOF1401):
1291                 return TypeOf1401(pdx);
1292 
1293         case _IOC_NR(IOCTL_CED_TRANSFERFLAGS):
1294                 return TransferFlags(pdx);
1295 
1296         case _IOC_NR(IOCTL_CED_DBGPEEK):
1297                 return DbgPeek(pdx, (TDBGBLOCK __user *) ulArg);
1298 
1299         case _IOC_NR(IOCTL_CED_DBGPOKE):
1300                 return DbgPoke(pdx, (TDBGBLOCK __user *) ulArg);
1301 
1302         case _IOC_NR(IOCTL_CED_DBGRAMPDATA):
1303                 return DbgRampData(pdx, (TDBGBLOCK __user *) ulArg);
1304 
1305         case _IOC_NR(IOCTL_CED_DBGRAMPADDR):
1306                 return DbgRampAddr(pdx, (TDBGBLOCK __user *) ulArg);
1307 
1308         case _IOC_NR(IOCTL_CED_DBGGETDATA):
1309                 return DbgGetData(pdx, (TDBGBLOCK __user *) ulArg);
1310 
1311         case _IOC_NR(IOCTL_CED_DBGSTOPLOOP):
1312                 return DbgStopLoop(pdx);
1313 
1314         case _IOC_NR(IOCTL_CED_FULLRESET):
1315                 pdx->bForceReset = true;        /*  Set a flag for a full reset */
1316                 break;
1317 
1318         case _IOC_NR(IOCTL_CED_SETCIRCULAR):
1319                 return SetCircular(pdx, (struct transfer_area_desc __user *) ulArg);
1320 
1321         case _IOC_NR(IOCTL_CED_GETCIRCBLOCK):
1322                 return GetCircBlock(pdx, (TCIRCBLOCK __user *) ulArg);
1323 
1324         case _IOC_NR(IOCTL_CED_FREECIRCBLOCK):
1325                 return FreeCircBlock(pdx, (TCIRCBLOCK __user *) ulArg);
1326 
1327         case _IOC_NR(IOCTL_CED_WAITEVENT):
1328                 return WaitEvent(pdx, (int)(ulArg & 0xff), (int)(ulArg >> 8));
1329 
1330         case _IOC_NR(IOCTL_CED_TESTEVENT):
1331                 return TestEvent(pdx, (int)ulArg);
1332 
1333         default:
1334                 return U14ERR_NO_SUCH_FN;
1335         }
1336         return U14ERR_NOERROR;
1337 }
1338 
1339 static const struct file_operations ced_fops = {
1340         .owner = THIS_MODULE,
1341         .open = ced_open,
1342         .release = ced_release,
1343         .flush = ced_flush,
1344         .llseek = noop_llseek,
1345         .unlocked_ioctl = ced_ioctl,
1346 };
1347 
1348 /*
1349  * usb class driver info in order to get a minor number from the usb core,
1350  * and to have the device registered with the driver core
1351  */
1352 static struct usb_class_driver ced_class = {
1353         .name = "cedusb%d",
1354         .fops = &ced_fops,
1355         .minor_base = USB_CED_MINOR_BASE,
1356 };
1357 
1358 /*  Check that the device that matches a 1401 vendor and product ID is OK to use and */
1359 /*  initialise our DEVICE_EXTENSION. */
1360 static int ced_probe(struct usb_interface *interface,
1361                      const struct usb_device_id *id)
1362 {
1363         DEVICE_EXTENSION *pdx;
1364         struct usb_host_interface *iface_desc;
1365         struct usb_endpoint_descriptor *endpoint;
1366         int i, bcdDevice;
1367         int retval = -ENOMEM;
1368 
1369         /*  allocate memory for our device extension and initialize it */
1370         pdx = kzalloc(sizeof(*pdx), GFP_KERNEL);
1371         if (!pdx)
1372                 goto error;
1373 
1374         for (i = 0; i < MAX_TRANSAREAS; ++i) {  /*  Initialise the wait queues */
1375                 init_waitqueue_head(&pdx->rTransDef[i].wqEvent);
1376         }
1377 
1378         /*  Put initialises for our stuff here. Note that all of *pdx is zero, so */
1379         /*  no need to explicitly zero it. */
1380         spin_lock_init(&pdx->charOutLock);
1381         spin_lock_init(&pdx->charInLock);
1382         spin_lock_init(&pdx->stagedLock);
1383 
1384         /*  Initialises from the skeleton stuff */
1385         kref_init(&pdx->kref);
1386         mutex_init(&pdx->io_mutex);
1387         spin_lock_init(&pdx->err_lock);
1388         init_usb_anchor(&pdx->submitted);
1389 
1390         pdx->udev = usb_get_dev(interface_to_usbdev(interface));
1391         pdx->interface = interface;
1392 
1393         /*  Attempt to identify the device */
1394         bcdDevice = pdx->udev->descriptor.bcdDevice;
1395         i = (bcdDevice >> 8);
1396         if (i == 0)
1397                 pdx->s1401Type = TYPEU1401;
1398         else if ((i >= 1) && (i <= 23))
1399                 pdx->s1401Type = i + 2;
1400         else {
1401                 dev_err(&interface->dev, "%s: Unknown device. bcdDevice = %d\n",
1402                         __func__, bcdDevice);
1403                 goto error;
1404         }
1405         /*  set up the endpoint information. We only care about the number of EP as */
1406         /*  we know that we are dealing with a 1401 device. */
1407         iface_desc = interface->cur_altsetting;
1408         pdx->nPipes = iface_desc->desc.bNumEndpoints;
1409         dev_info(&interface->dev, "1401Type=%d with %d End Points\n",
1410                  pdx->s1401Type, pdx->nPipes);
1411         if ((pdx->nPipes < 3) || (pdx->nPipes > 4))
1412                 goto error;
1413 
1414         /*  Allocate the URBs we hold for performing transfers */
1415         pdx->pUrbCharOut = usb_alloc_urb(0, GFP_KERNEL);        /*  character output URB */
1416         pdx->pUrbCharIn = usb_alloc_urb(0, GFP_KERNEL); /*  character input URB */
1417         pdx->pStagedUrb = usb_alloc_urb(0, GFP_KERNEL); /*  block transfer URB */
1418         if (!pdx->pUrbCharOut || !pdx->pUrbCharIn || !pdx->pStagedUrb) {
1419                 dev_err(&interface->dev, "%s: URB alloc failed\n", __func__);
1420                 goto error;
1421         }
1422 
1423         pdx->pCoherStagedIO =
1424             usb_alloc_coherent(pdx->udev, STAGED_SZ, GFP_KERNEL,
1425                                &pdx->pStagedUrb->transfer_dma);
1426         pdx->pCoherCharOut =
1427             usb_alloc_coherent(pdx->udev, OUTBUF_SZ, GFP_KERNEL,
1428                                &pdx->pUrbCharOut->transfer_dma);
1429         pdx->pCoherCharIn =
1430             usb_alloc_coherent(pdx->udev, INBUF_SZ, GFP_KERNEL,
1431                                &pdx->pUrbCharIn->transfer_dma);
1432         if (!pdx->pCoherCharOut || !pdx->pCoherCharIn || !pdx->pCoherStagedIO) {
1433                 dev_err(&interface->dev, "%s: Coherent buffer alloc failed\n",
1434                         __func__);
1435                 goto error;
1436         }
1437 
1438         for (i = 0; i < pdx->nPipes; ++i) {
1439                 endpoint = &iface_desc->endpoint[i].desc;
1440                 pdx->epAddr[i] = endpoint->bEndpointAddress;
1441                 dev_info(&interface->dev, "Pipe %d, ep address %02x\n",
1442                          i, pdx->epAddr[i]);
1443                 if (((pdx->nPipes == 3) && (i == 0)) || /*  if char input end point */
1444                     ((pdx->nPipes == 4) && (i == 1))) {
1445                         pdx->bInterval = endpoint->bInterval;   /*  save the endpoint interrupt interval */
1446                         dev_info(&interface->dev, "Pipe %d, bInterval = %d\n",
1447                                  i, pdx->bInterval);
1448                 }
1449                 /*  Detect USB2 by checking last ep size (64 if USB1) */
1450                 if (i == pdx->nPipes - 1) {     /*  if this is the last ep (bulk) */
1451                         pdx->bIsUSB2 =
1452                             le16_to_cpu(endpoint->wMaxPacketSize) > 64;
1453                         dev_info(&pdx->interface->dev, "USB%d\n",
1454                                  pdx->bIsUSB2 + 1);
1455                 }
1456         }
1457 
1458         /* save our data pointer in this interface device */
1459         usb_set_intfdata(interface, pdx);
1460 
1461         /* we can register the device now, as it is ready */
1462         retval = usb_register_dev(interface, &ced_class);
1463         if (retval) {
1464                 /* something prevented us from registering this driver */
1465                 dev_err(&interface->dev,
1466                         "Not able to get a minor for this device\n");
1467                 usb_set_intfdata(interface, NULL);
1468                 goto error;
1469         }
1470 
1471         /* let the user know what node this device is now attached to */
1472         dev_info(&interface->dev,
1473                  "USB CEDUSB device now attached to cedusb #%d\n",
1474                  interface->minor);
1475         return 0;
1476 
1477 error:
1478         if (pdx)
1479                 kref_put(&pdx->kref, ced_delete);       /*  frees allocated memory */
1480         return retval;
1481 }
1482 
1483 static void ced_disconnect(struct usb_interface *interface)
1484 {
1485         DEVICE_EXTENSION *pdx = usb_get_intfdata(interface);
1486         int minor = interface->minor;
1487         int i;
1488 
1489         usb_set_intfdata(interface, NULL);      /*  remove the pdx from the interface */
1490         usb_deregister_dev(interface, &ced_class);      /*  give back our minor device number */
1491 
1492         mutex_lock(&pdx->io_mutex);     /*  stop more I/O starting while... */
1493         ced_draw_down(pdx);     /*  ...wait for then kill any io */
1494         for (i = 0; i < MAX_TRANSAREAS; ++i) {
1495                 int iErr = ClearArea(pdx, i);   /*  ...release any used memory */
1496                 if (iErr == U14ERR_UNLOCKFAIL)
1497                         dev_err(&pdx->interface->dev, "%s: Area %d was in used\n",
1498                                 __func__, i);
1499         }
1500         pdx->interface = NULL;  /*  ...we kill off link to interface */
1501         mutex_unlock(&pdx->io_mutex);
1502 
1503         usb_kill_anchored_urbs(&pdx->submitted);
1504 
1505         kref_put(&pdx->kref, ced_delete);       /*  decrement our usage count */
1506 
1507         dev_info(&interface->dev, "USB cedusb #%d now disconnected\n", minor);
1508 }
1509 
1510 /*  Wait for all the urbs we know of to be done with, then kill off any that */
1511 /*  are left. NBNB we will need to have a mechanism to stop circular xfers */
1512 /*  from trying to fire off more urbs. We will wait up to 3 seconds for Urbs */
1513 /*  to be done. */
1514 void ced_draw_down(DEVICE_EXTENSION *pdx)
1515 {
1516         int time;
1517         dev_dbg(&pdx->interface->dev, "%s: called\n", __func__);
1518 
1519         pdx->bInDrawDown = true;
1520         time = usb_wait_anchor_empty_timeout(&pdx->submitted, 3000);
1521         if (!time) {            /*  if we timed out we kill the urbs */
1522                 usb_kill_anchored_urbs(&pdx->submitted);
1523                 dev_err(&pdx->interface->dev, "%s: timed out\n", __func__);
1524         }
1525         pdx->bInDrawDown = false;
1526 }
1527 
1528 static int ced_suspend(struct usb_interface *intf, pm_message_t message)
1529 {
1530         DEVICE_EXTENSION *pdx = usb_get_intfdata(intf);
1531         if (!pdx)
1532                 return 0;
1533         ced_draw_down(pdx);
1534 
1535         dev_dbg(&pdx->interface->dev, "%s: called\n", __func__);
1536         return 0;
1537 }
1538 
1539 static int ced_resume(struct usb_interface *intf)
1540 {
1541         DEVICE_EXTENSION *pdx = usb_get_intfdata(intf);
1542         if (!pdx)
1543                 return 0;
1544         dev_dbg(&pdx->interface->dev, "%s: called\n", __func__);
1545         return 0;
1546 }
1547 
1548 static int ced_pre_reset(struct usb_interface *intf)
1549 {
1550         DEVICE_EXTENSION *pdx = usb_get_intfdata(intf);
1551         dev_dbg(&pdx->interface->dev, "%s\n", __func__);
1552         mutex_lock(&pdx->io_mutex);
1553         ced_draw_down(pdx);
1554         return 0;
1555 }
1556 
1557 static int ced_post_reset(struct usb_interface *intf)
1558 {
1559         DEVICE_EXTENSION *pdx = usb_get_intfdata(intf);
1560         dev_dbg(&pdx->interface->dev, "%s\n", __func__);
1561 
1562         /* we are sure no URBs are active - no locking needed */
1563         pdx->errors = -EPIPE;
1564         mutex_unlock(&pdx->io_mutex);
1565 
1566         return 0;
1567 }
1568 
1569 static struct usb_driver ced_driver = {
1570         .name = "cedusb",
1571         .probe = ced_probe,
1572         .disconnect = ced_disconnect,
1573         .suspend = ced_suspend,
1574         .resume = ced_resume,
1575         .pre_reset = ced_pre_reset,
1576         .post_reset = ced_post_reset,
1577         .id_table = ced_table,
1578         .supports_autosuspend = 1,
1579 };
1580 
1581 module_usb_driver(ced_driver);
1582 MODULE_LICENSE("GPL");
1583 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us