1 /* Driver for Realtek RTS51xx USB card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22 * Edwin Rong (edwin_rong@realsil.com.cn)
23 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
26 #include <linux/blkdev.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_device.h>
37 #include "rts51x_chip.h"
38 #include "rts51x_card.h"
39 #include "rts51x_scsi.h"
40 #include "rts51x_transport.h"
43 /***********************************************************************
44 * Scatter-gather transfer buffer access routines
45 ***********************************************************************/
47 /* Copy a buffer of length buflen to/from the srb's transfer buffer.
48 * Update the **sgptr and *offset variables so that the next copy will
49 * pick up from where this one left off.
52 unsigned int rts51x_access_sglist(unsigned char *buffer,
53 unsigned int buflen, void *sglist,
54 void **sgptr, unsigned int *offset,
55 enum xfer_buf_dir dir)
58 struct scatterlist *sg = (struct scatterlist *)*sgptr;
60 /* We have to go through the list one entry
61 * at a time. Each s-g entry contains some number of pages, and
62 * each page has to be kmap()'ed separately. If the page is already
63 * in kernel-addressable memory then kmap() will return its address.
64 * If the page is not directly accessible -- such as a user buffer
65 * located in high memory -- then kmap() will map it to a temporary
66 * position in the kernel's virtual address space.
70 sg = (struct scatterlist *)sglist;
72 /* This loop handles a single s-g list entry, which may
73 * include multiple pages. Find the initial page structure
74 * and the starting offset within the page, and update
75 * the *offset and **sgptr values for the next loop.
78 while (cnt < buflen && sg) {
79 struct page *page = sg_page(sg) +
80 ((sg->offset + *offset) >> PAGE_SHIFT);
81 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE - 1);
82 unsigned int sglen = sg->length - *offset;
84 if (sglen > buflen - cnt) {
86 /* Transfer ends within this s-g entry */
91 /* Transfer continues to next s-g entry */
96 /* Transfer the data for all the pages in this
97 * s-g entry. For each page: call kmap(), do the
98 * transfer, and call kunmap() immediately after. */
100 unsigned int plen = min(sglen, (unsigned int)
102 unsigned char *ptr = kmap(page);
104 if (dir == TO_XFER_BUF)
105 memcpy(ptr + poff, buffer + cnt, plen);
107 memcpy(buffer + cnt, ptr + poff, plen);
110 /* Start at the beginning of the next page */
119 /* Return the amount actually transferred */
123 static unsigned int rts51x_access_xfer_buf(unsigned char *buffer,
124 unsigned int buflen, struct scsi_cmnd *srb,
125 struct scatterlist **sgptr,
126 unsigned int *offset, enum xfer_buf_dir dir)
128 return rts51x_access_sglist(buffer, buflen, (void *)scsi_sglist(srb),
129 (void **)sgptr, offset, dir);
132 /* Store the contents of buffer into srb's transfer buffer and set the
135 void rts51x_set_xfer_buf(unsigned char *buffer,
136 unsigned int buflen, struct scsi_cmnd *srb)
138 unsigned int offset = 0;
139 struct scatterlist *sg = NULL;
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);
148 void rts51x_get_xfer_buf(unsigned char *buffer,
149 unsigned int buflen, struct scsi_cmnd *srb)
151 unsigned int offset = 0;
152 struct scatterlist *sg = NULL;
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);
161 /* This is the completion handler which will wake us up when an URB
164 static void urb_done_completion(struct urb *urb)
166 struct completion *urb_done_ptr = urb->context;
169 complete(urb_done_ptr);
172 /* This is the common part of the URB message submission code
174 * All URBs from the driver involved in handling a queued scsi
175 * command _must_ pass through this function (or something like it) for the
176 * abort mechanisms to work properly.
178 static int rts51x_msg_common(struct rts51x_chip *chip, struct urb *urb,
181 struct rts51x_usb *rts51x = chip->usb;
182 struct completion urb_done;
186 /* don't submit URBs during abort processing */
187 if (test_bit(FLIDX_ABORTING, &rts51x->dflags))
188 TRACE_RET(chip, -EIO);
190 /* set up data structures for the wakeup system */
191 init_completion(&urb_done);
193 /* fill the common fields in the URB */
194 urb->context = &urb_done;
195 urb->actual_length = 0;
196 urb->error_count = 0;
199 /* we assume that if transfer_buffer isn't us->iobuf then it
200 * hasn't been mapped for DMA. Yes, this is clunky, but it's
201 * easier than always having the caller tell us whether the
202 * transfer buffer has already been mapped. */
203 urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
204 if (urb->transfer_buffer == rts51x->iobuf) {
205 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
206 urb->transfer_dma = rts51x->iobuf_dma;
208 urb->setup_dma = rts51x->cr_dma;
211 status = usb_submit_urb(urb, GFP_NOIO);
213 /* something went wrong */
214 TRACE_RET(chip, status);
217 /* since the URB has been submitted successfully, it's now okay
219 set_bit(FLIDX_URB_ACTIVE, &rts51x->dflags);
221 /* did an abort occur during the submission? */
222 if (test_bit(FLIDX_ABORTING, &rts51x->dflags)) {
224 /* cancel the URB, if it hasn't been cancelled already */
225 if (test_and_clear_bit(FLIDX_URB_ACTIVE, &rts51x->dflags)) {
226 RTS51X_DEBUGP("-- cancelling URB\n");
231 /* wait for the completion of the URB */
233 wait_for_completion_interruptible_timeout(&urb_done,
236 MAX_SCHEDULE_TIMEOUT);
238 clear_bit(FLIDX_URB_ACTIVE, &rts51x->dflags);
241 RTS51X_DEBUGP("%s -- cancelling URB\n",
242 timeleft == 0 ? "Timeout" : "Signal");
249 status = urb->status;
255 static int rts51x_clear_halt(struct rts51x_chip *chip, unsigned int pipe);
258 * Interpret the results of a URB transfer
260 static int interpret_urb_result(struct rts51x_chip *chip, unsigned int pipe,
261 unsigned int length, int result,
262 unsigned int partial)
264 int retval = STATUS_SUCCESS;
266 /* RTS51X_DEBUGP("Status code %d; transferred %u/%u\n",
267 result, partial, length); */
269 /* no error code; did we send all the data? */
271 if (partial != length) {
272 RTS51X_DEBUGP("-- short transfer\n");
273 TRACE_RET(chip, STATUS_TRANS_SHORT);
275 /* RTS51X_DEBUGP("-- transfer complete\n"); */
276 return STATUS_SUCCESS;
279 /* for control endpoints, (used by CB[I]) a stall indicates
280 * a failed command */
281 if (usb_pipecontrol(pipe)) {
282 RTS51X_DEBUGP("-- stall on control pipe\n");
283 TRACE_RET(chip, STATUS_STALLED);
285 /* for other sorts of endpoint, clear the stall */
286 RTS51X_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
287 if (rts51x_clear_halt(chip, pipe) < 0)
288 TRACE_RET(chip, STATUS_ERROR);
289 retval = STATUS_STALLED;
290 TRACE_GOTO(chip, Exit);
292 /* babble - the device tried to send more than
293 * we wanted to read */
295 RTS51X_DEBUGP("-- babble\n");
296 retval = STATUS_TRANS_LONG;
297 TRACE_GOTO(chip, Exit);
299 /* the transfer was cancelled by abort,
300 * disconnect, or timeout */
302 RTS51X_DEBUGP("-- transfer cancelled\n");
303 retval = STATUS_ERROR;
304 TRACE_GOTO(chip, Exit);
306 /* short scatter-gather read transfer */
308 RTS51X_DEBUGP("-- short read transfer\n");
309 retval = STATUS_TRANS_SHORT;
310 TRACE_GOTO(chip, Exit);
312 /* abort or disconnect in progress */
314 RTS51X_DEBUGP("-- abort or disconnect in progress\n");
315 retval = STATUS_ERROR;
316 TRACE_GOTO(chip, Exit);
319 RTS51X_DEBUGP("-- time out\n");
320 retval = STATUS_TIMEDOUT;
321 TRACE_GOTO(chip, Exit);
323 /* the catch-all error case */
325 RTS51X_DEBUGP("-- unknown error\n");
326 retval = STATUS_ERROR;
327 TRACE_GOTO(chip, Exit);
331 if ((retval != STATUS_SUCCESS) && !usb_pipecontrol(pipe))
332 rts51x_clear_hw_error(chip);
337 int rts51x_ctrl_transfer(struct rts51x_chip *chip, unsigned int pipe,
338 u8 request, u8 requesttype, u16 value, u16 index,
339 void *data, u16 size, int timeout)
341 struct rts51x_usb *rts51x = chip->usb;
344 RTS51X_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
345 __func__, request, requesttype, value, index, size);
347 /* fill in the devrequest structure */
348 rts51x->cr->bRequestType = requesttype;
349 rts51x->cr->bRequest = request;
350 rts51x->cr->wValue = cpu_to_le16(value);
351 rts51x->cr->wIndex = cpu_to_le16(index);
352 rts51x->cr->wLength = cpu_to_le16(size);
354 /* fill and submit the URB */
355 usb_fill_control_urb(rts51x->current_urb, rts51x->pusb_dev, pipe,
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,
361 rts51x->current_urb->actual_length);
364 static int rts51x_clear_halt(struct rts51x_chip *chip, unsigned int pipe)
367 int endp = usb_pipeendpoint(pipe);
369 if (usb_pipein(pipe))
372 result = rts51x_ctrl_transfer(chip, SND_CTRL_PIPE(chip),
373 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
374 USB_ENDPOINT_HALT, endp, NULL, 0, 3000);
375 if (result != STATUS_SUCCESS)
376 TRACE_RET(chip, STATUS_FAIL);
378 usb_reset_endpoint(chip->usb->pusb_dev, endp);
380 return STATUS_SUCCESS;
383 static void rts51x_sg_clean(struct usb_sg_request *io)
386 while (io->entries--)
387 usb_free_urb(io->urbs[io->entries]);
394 static int rts51x_sg_init(struct usb_sg_request *io, struct usb_device *dev,
395 unsigned pipe, unsigned period, struct scatterlist *sg,
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)
405 int entries = io->entries;
407 /* queue the urbs. */
408 spin_lock_irq(&io->lock);
410 while (i < entries && !io->status) {
413 io->urbs[i]->dev = io->dev;
414 retval = usb_submit_urb(io->urbs[i], GFP_ATOMIC);
416 /* after we submit, let completions or cancelations fire;
417 * we handshake using io->status.
419 spin_unlock_irq(&io->lock);
421 /* maybe the retry will recover */
422 case -ENXIO: /* hc didn't queue this one */
425 io->urbs[i]->dev = NULL;
430 /* no error? continue immediately.
432 * NOTE: to work better with UHCI (4K I/O buffer may
433 * need 3K of TDs) it may be good to limit how many
434 * URBs are queued at once; N milliseconds?
441 /* fail any uncompleted urbs */
443 io->urbs[i]->dev = NULL;
444 io->urbs[i]->status = retval;
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;
455 complete(&io->complete);
456 spin_unlock_irq(&io->lock);
459 wait_for_completion_interruptible_timeout(&io->complete,
462 MAX_SCHEDULE_TIMEOUT);
464 RTS51X_DEBUGP("%s -- cancelling SG request\n",
465 timeleft == 0 ? "Timeout" : "Signal");
468 io->status = -ETIMEDOUT;
478 * Transfer a scatter-gather list via bulk transfer
480 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
481 * above, but it uses the usbcore scatter-gather library.
483 static int rts51x_bulk_transfer_sglist(struct rts51x_chip *chip,
485 struct scatterlist *sg, int num_sg,
487 unsigned int *act_len, int timeout)
491 /* don't submit s-g requests during abort processing */
492 if (test_bit(FLIDX_ABORTING, &chip->usb->dflags))
493 TRACE_RET(chip, STATUS_ERROR);
495 /* initialize the scatter-gather request block */
496 RTS51X_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__,
499 rts51x_sg_init(&chip->usb->current_sg, chip->usb->pusb_dev, pipe, 0,
500 sg, num_sg, length, GFP_NOIO);
502 RTS51X_DEBUGP("rts51x_sg_init returned %d\n", result);
503 TRACE_RET(chip, STATUS_ERROR);
506 /* since the block has been initialized successfully, it's now
507 * okay to cancel it */
508 set_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags);
510 /* did an abort occur during the submission? */
511 if (test_bit(FLIDX_ABORTING, &chip->usb->dflags)) {
513 /* cancel the request, if it hasn't been cancelled already */
514 if (test_and_clear_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags)) {
515 RTS51X_DEBUGP("-- cancelling sg request\n");
516 usb_sg_cancel(&chip->usb->current_sg);
520 /* wait for the completion of the transfer */
521 result = rts51x_sg_wait(&chip->usb->current_sg, timeout);
523 clear_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags);
525 /* result = us->current_sg.status; */
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)
539 /* fill and submit the URB */
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);
544 /* store the actual length of the data transferred */
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);
551 int rts51x_transfer_data(struct rts51x_chip *chip, unsigned int pipe,
552 void *buf, unsigned int len, int use_sg,
553 unsigned int *act_len, int timeout)
562 rts51x_bulk_transfer_sglist(chip, pipe,
563 (struct scatterlist *)buf,
564 use_sg, len, act_len, timeout);
567 rts51x_bulk_transfer_buf(chip, pipe, buf, len, act_len,
574 int rts51x_transfer_data_partial(struct rts51x_chip *chip, unsigned int pipe,
575 void *buf, void **ptr, unsigned int *offset,
576 unsigned int len, int use_sg,
577 unsigned int *act_len, int timeout)
585 void *tmp_buf = kmalloc(len, GFP_KERNEL);
587 TRACE_RET(chip, STATUS_NOMEM);
589 if (usb_pipeout(pipe)) {
590 rts51x_access_sglist(tmp_buf, len, buf, ptr, offset,
594 rts51x_bulk_transfer_buf(chip, pipe, tmp_buf, len, act_len,
596 if (result == STATUS_SUCCESS) {
597 if (usb_pipein(pipe)) {
598 rts51x_access_sglist(tmp_buf, len, buf, ptr,
599 offset, TO_XFER_BUF);
605 unsigned int step = 0;
609 rts51x_bulk_transfer_buf(chip, pipe, buf + step, len,
622 int rts51x_get_epc_status(struct rts51x_chip *chip, u16 *status)
624 unsigned int pipe = RCV_INTR_PIPE(chip);
625 struct usb_host_endpoint *ep;
626 struct completion urb_done;
630 TRACE_RET(chip, STATUS_ERROR);
632 /* set up data structures for the wakeup system */
633 init_completion(&urb_done);
635 ep = chip->usb->pusb_dev->ep_in[usb_pipeendpoint(pipe)];
637 /* fill and submit the URB */
638 /* We set interval to 1 here, so the polling interval is controlled
639 * by our polling thread */
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, 100);
645 return interpret_urb_result(chip, pipe, 2, result,
646 chip->usb->intr_urb->actual_length);
649 u8 media_not_present[] = {
650 0x70, 0, 0x02, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0 };
651 u8 invalid_cmd_field[] = {
652 0x70, 0, 0x05, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0 };
654 void rts51x_invoke_transport(struct scsi_cmnd *srb, struct rts51x_chip *chip)
659 if (chip->option.ss_en) {
660 if (srb->cmnd[0] == TEST_UNIT_READY) {
661 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
662 if (check_fake_card_ready(chip,
664 srb->result = SAM_STAT_GOOD;
666 srb->result = SAM_STAT_CHECK_CONDITION;
667 memcpy(srb->sense_buffer,
668 media_not_present, SENSE_SIZE);
672 } else if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
673 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
674 int prevent = srb->cmnd[4] & 0x1;
677 srb->result = SAM_STAT_CHECK_CONDITION;
678 memcpy(srb->sense_buffer,
679 invalid_cmd_field, SENSE_SIZE);
681 srb->result = SAM_STAT_GOOD;
686 if (RTS51X_CHK_STAT(chip, STAT_SS)
687 || RTS51X_CHK_STAT(chip, STAT_SS_PRE)) {
689 RTS51X_DEBUGP("Try to wake up device\n");
690 chip->resume_from_scsi = 1;
692 rts51x_try_to_exit_ss(chip);
694 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
697 rts51x_init_chip(chip);
698 rts51x_init_cards(chip);
705 result = rts51x_scsi_handler(srb, chip);
707 /* if there is a transport error, reset and don't auto-sense */
708 if (result == TRANSPORT_ERROR) {
709 RTS51X_DEBUGP("-- transport indicates error, resetting\n");
710 srb->result = DID_ERROR << 16;
714 srb->result = SAM_STAT_GOOD;
717 * If we have a failure, we're going to do a REQUEST_SENSE
718 * automatically. Note that we differentiate between a command
719 * "failure" and an "error" in the transport mechanism.
721 if (result == TRANSPORT_FAILED) {
722 /* set the result so the higher layers expect this data */
723 srb->result = SAM_STAT_CHECK_CONDITION;
724 memcpy(srb->sense_buffer,
725 (unsigned char *)&(chip->sense_buffer[SCSI_LUN(srb)]),
726 sizeof(struct sense_data_t));
731 /* Error and abort processing: try to resynchronize with the device
732 * by issuing a port reset. If that fails, try a class-specific