28 #include <linux/sched.h>
29 #include <linux/slab.h>
31 #include <scsi/scsi.h>
53 unsigned int buflen,
void *sglist,
54 void **sgptr,
unsigned int *
offset,
78 while (cnt < buflen && sg) {
84 if (sglen > buflen - cnt) {
100 unsigned int plen =
min(sglen, (
unsigned int)
102 unsigned char *
ptr =
kmap(page);
105 memcpy(ptr + poff, buffer + cnt, plen);
107 memcpy(buffer + cnt, ptr + poff, plen);
123 static unsigned int rts51x_access_xfer_buf(
unsigned char *
buffer,
129 (
void **)sgptr, offset, dir);
136 unsigned int buflen,
struct scsi_cmnd *srb)
138 unsigned int offset = 0;
141 buflen =
min(buflen, scsi_bufflen(srb));
142 buflen = rts51x_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
144 if (buflen < scsi_bufflen(srb))
145 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
149 unsigned int buflen,
struct scsi_cmnd *srb)
151 unsigned int offset = 0;
154 buflen =
min(buflen, scsi_bufflen(srb));
155 buflen = rts51x_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
157 if (buflen < scsi_bufflen(srb))
158 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
164 static void urb_done_completion(
struct urb *
urb)
166 struct completion *urb_done_ptr = urb->context;
178 static int rts51x_msg_common(
struct rts51x_chip *
chip,
struct urb *urb,
191 init_completion(&urb_done);
194 urb->context = &urb_done;
195 urb->actual_length = 0;
196 urb->error_count = 0;
204 if (urb->transfer_buffer == rts51x->
iobuf) {
205 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
208 urb->setup_dma = rts51x->
cr_dma;
242 timeleft == 0 ?
"Timeout" :
"Signal");
249 status = urb->status;
255 static int rts51x_clear_halt(
struct rts51x_chip *chip,
unsigned int pipe);
260 static int interpret_urb_result(
struct rts51x_chip *chip,
unsigned int pipe,
262 unsigned int partial)
271 if (partial != length) {
281 if (usb_pipecontrol(pipe)) {
286 RTS51X_DEBUGP(
"clearing endpoint halt for pipe 0x%x\n", pipe);
287 if (rts51x_clear_halt(chip, pipe) < 0)
344 RTS51X_DEBUGP(
"%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
345 __func__, request, requesttype, value, index, size);
348 rts51x->
cr->bRequestType = requesttype;
356 (
unsigned char *)rts51x->
cr, data, size,
357 urb_done_completion,
NULL);
358 result = rts51x_msg_common(chip, rts51x->
current_urb, timeout);
360 return interpret_urb_result(chip, pipe, size, result,
364 static int rts51x_clear_halt(
struct rts51x_chip *chip,
unsigned int pipe)
367 int endp = usb_pipeendpoint(pipe);
369 if (usb_pipein(pipe))
383 static void rts51x_sg_clean(
struct usb_sg_request *
io)
386 while (io->entries--)
394 static int rts51x_sg_init(
struct usb_sg_request *io,
struct usb_device *
dev,
396 int nents,
size_t length,
gfp_t mem_flags)
398 return usb_sg_init(io, dev, pipe, period, sg, nents, length, mem_flags);
401 static int rts51x_sg_wait(
struct usb_sg_request *io,
int timeout)
408 spin_lock_irq(&io->lock);
410 while (i < entries && !io->status) {
413 io->urbs[
i]->dev = io->dev;
419 spin_unlock_irq(&io->lock);
425 io->urbs[
i]->dev =
NULL;
443 io->urbs[
i]->dev =
NULL;
445 dev_dbg(&io->dev->dev,
"%s, submit --> %d\n",
449 spin_lock_irq(&io->lock);
450 if (retval && (io->status == 0 || io->status == -
ECONNRESET))
453 io->count -= entries -
i;
456 spin_unlock_irq(&io->lock);
465 timeleft == 0 ?
"Timeout" :
"Signal");
483 static int rts51x_bulk_transfer_sglist(
struct rts51x_chip *chip,
487 unsigned int *act_len,
int timeout)
499 rts51x_sg_init(&chip->
usb->current_sg, chip->
usb->pusb_dev, pipe, 0,
521 result = rts51x_sg_wait(&chip->
usb->current_sg, timeout);
527 *act_len = chip->
usb->current_sg.bytes;
528 return interpret_urb_result(chip, pipe, length, result,
529 chip->
usb->current_sg.bytes);
532 static int rts51x_bulk_transfer_buf(
struct rts51x_chip *chip,
534 void *
buf,
unsigned int length,
535 unsigned int *act_len,
int timeout)
540 usb_fill_bulk_urb(chip->
usb->current_urb, chip->
usb->pusb_dev, pipe,
541 buf, length, urb_done_completion,
NULL);
542 result = rts51x_msg_common(chip, chip->
usb->current_urb, timeout);
546 *act_len = chip->
usb->current_urb->actual_length;
547 return interpret_urb_result(chip, pipe, length, result,
548 chip->
usb->current_urb->actual_length);
552 void *buf,
unsigned int len,
int use_sg,
553 unsigned int *act_len,
int timeout)
562 rts51x_bulk_transfer_sglist(chip, pipe,
564 use_sg, len, act_len, timeout);
567 rts51x_bulk_transfer_buf(chip, pipe, buf, len, act_len,
575 void *buf,
void **
ptr,
unsigned int *offset,
576 unsigned int len,
int use_sg,
577 unsigned int *act_len,
int timeout)
589 if (usb_pipeout(pipe)) {
594 rts51x_bulk_transfer_buf(chip, pipe, tmp_buf, len, act_len,
597 if (usb_pipein(pipe)) {
605 unsigned int step = 0;
609 rts51x_bulk_transfer_buf(chip, pipe, buf + step, len,
625 struct usb_host_endpoint *ep;
633 init_completion(&urb_done);
635 ep = chip->
usb->pusb_dev->ep_in[usb_pipeendpoint(pipe)];
640 usb_fill_int_urb(chip->
usb->intr_urb, chip->
usb->pusb_dev, pipe,
641 status, 2, urb_done_completion, &urb_done, 1);
643 result = rts51x_msg_common(chip, chip->
usb->intr_urb, 50);
645 return interpret_urb_result(chip, pipe, 2, result,
646 chip->
usb->intr_urb->actual_length);
650 0x70, 0, 0x02, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0 };
652 0x70, 0, 0x05, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0 };
662 if (check_fake_card_ready(chip,
674 int prevent = srb->
cmnd[4] & 0x1;