2 * Remote VUB300 SDIO/SDmem Host Controller Driver
4 * Copyright (C) 2010 Elan Digital Systems Limited
6 * based on USB Skeleton driver - 2.2
8 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2
14 * VUB300: is a USB 2.0 client device with a single SDIO/SDmem/MMC slot
15 * Any SDIO/SDmem/MMC device plugged into the VUB300 will appear,
16 * by virtue of this driver, to have been plugged into a local
17 * SDIO host controller, similar to, say, a PCI Ricoh controller
18 * This is because this kernel device driver is both a USB 2.0
19 * client device driver AND an MMC host controller driver. Thus
20 * if there is an existing driver for the inserted SDIO/SDmem/MMC
21 * device then that driver will be used by the kernel to manage
22 * the device in exactly the same fashion as if it had been
23 * directly plugged into, say, a local pci bus Ricoh controller
25 * RANT: this driver was written using a display 128x48 - converting it
26 * to a line width of 80 makes it very difficult to support. In
27 * particular functions have been broken down into sub functions
28 * and the original meaningful names have been shortened into
30 * The problem is that executing a fragment of code subject to
31 * two conditions means an indentation of 24, thus leaving only
32 * 56 characters for a C statement. And that is quite ridiculous!
34 * Data types: data passed to/from the VUB300 is fixed to a number of
35 * bits and driver data fields reflect that limit by using
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/kref.h>
44 #include <linux/uaccess.h>
45 #include <linux/usb.h>
46 #include <linux/mutex.h>
47 #include <linux/mmc/host.h>
48 #include <linux/mmc/card.h>
49 #include <linux/mmc/sdio_func.h>
50 #include <linux/mmc/sdio_ids.h>
51 #include <linux/workqueue.h>
52 #include <linux/ctype.h>
53 #include <linux/firmware.h>
54 #include <linux/scatterlist.h>
56 struct host_controller_info
{
62 #define FIRMWARE_BLOCK_BOUNDARY 1024
63 struct sd_command_header
{
67 u8 command_type
; /* Bit7 - Rd/Wr */
69 u8 transfer_size
[4]; /* ReadSize + ReadSize */
75 u8 reserved
[44]; /* to pad out to 64 bytes */
78 struct sd_irqpoll_header
{
82 u8 command_type
; /* Bit7 - Rd/Wr */
83 u8 padding
[16]; /* don't ask why !! */
86 u8 reserved
[42]; /* to pad out to 64 bytes */
89 struct sd_common_header
{
95 struct sd_response_header
{
101 u8 command_response
[0];
104 struct sd_status_header
{
110 u16 host_header_size
;
111 u16 func_header_size
;
112 u16 ctrl_header_size
;
115 struct sd_error_header
{
122 struct sd_interrupt_header
{
128 struct offload_registers_access
{
133 #define INTERRUPT_REGISTER_ACCESSES 15
134 struct sd_offloaded_interrupt
{
138 struct offload_registers_access reg
[INTERRUPT_REGISTER_ACCESSES
];
141 struct sd_register_header
{
147 u8 command_response
[6];
150 #define PIGGYBACK_REGISTER_ACCESSES 14
151 struct sd_offloaded_piggyback
{
152 struct sd_register_header sdio
;
153 struct offload_registers_access reg
[PIGGYBACK_REGISTER_ACCESSES
];
157 struct sd_common_header common
;
158 struct sd_status_header status
;
159 struct sd_error_header error
;
160 struct sd_interrupt_header interrupt
;
161 struct sd_response_header response
;
162 struct sd_offloaded_interrupt irq
;
163 struct sd_offloaded_piggyback pig
;
167 struct sd_command_header head
;
168 struct sd_irqpoll_header poll
;
171 enum SD_RESPONSE_TYPE
{
172 SDRT_UNSPECIFIED
= 0,
185 #define RESPONSE_INTERRUPT 0x01
186 #define RESPONSE_ERROR 0x02
187 #define RESPONSE_STATUS 0x03
188 #define RESPONSE_IRQ_DISABLED 0x05
189 #define RESPONSE_IRQ_ENABLED 0x06
190 #define RESPONSE_PIGGYBACKED 0x07
191 #define RESPONSE_NO_INTERRUPT 0x08
192 #define RESPONSE_PIG_DISABLED 0x09
193 #define RESPONSE_PIG_ENABLED 0x0A
194 #define SD_ERROR_1BIT_TIMEOUT 0x01
195 #define SD_ERROR_4BIT_TIMEOUT 0x02
196 #define SD_ERROR_1BIT_CRC_WRONG 0x03
197 #define SD_ERROR_4BIT_CRC_WRONG 0x04
198 #define SD_ERROR_1BIT_CRC_ERROR 0x05
199 #define SD_ERROR_4BIT_CRC_ERROR 0x06
200 #define SD_ERROR_NO_CMD_ENDBIT 0x07
201 #define SD_ERROR_NO_1BIT_DATEND 0x08
202 #define SD_ERROR_NO_4BIT_DATEND 0x09
203 #define SD_ERROR_1BIT_UNEXPECTED_TIMEOUT 0x0A
204 #define SD_ERROR_4BIT_UNEXPECTED_TIMEOUT 0x0B
205 #define SD_ERROR_ILLEGAL_COMMAND 0x0C
206 #define SD_ERROR_NO_DEVICE 0x0D
207 #define SD_ERROR_TRANSFER_LENGTH 0x0E
208 #define SD_ERROR_1BIT_DATA_TIMEOUT 0x0F
209 #define SD_ERROR_4BIT_DATA_TIMEOUT 0x10
210 #define SD_ERROR_ILLEGAL_STATE 0x11
211 #define SD_ERROR_UNKNOWN_ERROR 0x12
212 #define SD_ERROR_RESERVED_ERROR 0x13
213 #define SD_ERROR_INVALID_FUNCTION 0x14
214 #define SD_ERROR_OUT_OF_RANGE 0x15
215 #define SD_ERROR_STAT_CMD 0x16
216 #define SD_ERROR_STAT_DATA 0x17
217 #define SD_ERROR_STAT_CMD_TIMEOUT 0x18
218 #define SD_ERROR_SDCRDY_STUCK 0x19
219 #define SD_ERROR_UNHANDLED 0x1A
220 #define SD_ERROR_OVERRUN 0x1B
221 #define SD_ERROR_PIO_TIMEOUT 0x1C
223 #define FUN(c) (0x000007 & (c->arg>>28))
224 #define REG(c) (0x01FFFF & (c->arg>>9))
226 static bool limit_speed_to_24_MHz
;
227 module_param(limit_speed_to_24_MHz
, bool, 0644);
228 MODULE_PARM_DESC(limit_speed_to_24_MHz
, "Limit Max SDIO Clock Speed to 24 MHz");
230 static bool pad_input_to_usb_pkt
;
231 module_param(pad_input_to_usb_pkt
, bool, 0644);
232 MODULE_PARM_DESC(pad_input_to_usb_pkt
,
233 "Pad USB data input transfers to whole USB Packet");
235 static bool disable_offload_processing
;
236 module_param(disable_offload_processing
, bool, 0644);
237 MODULE_PARM_DESC(disable_offload_processing
, "Disable Offload Processing");
239 static bool force_1_bit_data_xfers
;
240 module_param(force_1_bit_data_xfers
, bool, 0644);
241 MODULE_PARM_DESC(force_1_bit_data_xfers
,
242 "Force SDIO Data Transfers to 1-bit Mode");
244 static bool force_polling_for_irqs
;
245 module_param(force_polling_for_irqs
, bool, 0644);
246 MODULE_PARM_DESC(force_polling_for_irqs
, "Force Polling for SDIO interrupts");
248 static int firmware_irqpoll_timeout
= 1024;
249 module_param(firmware_irqpoll_timeout
, int, 0644);
250 MODULE_PARM_DESC(firmware_irqpoll_timeout
, "VUB300 firmware irqpoll timeout");
252 static int force_max_req_size
= 128;
253 module_param(force_max_req_size
, int, 0644);
254 MODULE_PARM_DESC(force_max_req_size
, "set max request size in kBytes");
256 #ifdef SMSC_DEVELOPMENT_BOARD
257 static int firmware_rom_wait_states
= 0x04;
259 static int firmware_rom_wait_states
= 0x1C;
262 module_param(firmware_rom_wait_states
, int, 0644);
263 MODULE_PARM_DESC(firmware_rom_wait_states
,
264 "ROM wait states byte=RRRIIEEE (Reserved Internal External)");
266 #define ELAN_VENDOR_ID 0x2201
267 #define VUB300_VENDOR_ID 0x0424
268 #define VUB300_PRODUCT_ID 0x012C
269 static struct usb_device_id vub300_table
[] = {
270 {USB_DEVICE(ELAN_VENDOR_ID
, VUB300_PRODUCT_ID
)},
271 {USB_DEVICE(VUB300_VENDOR_ID
, VUB300_PRODUCT_ID
)},
272 {} /* Terminating entry */
274 MODULE_DEVICE_TABLE(usb
, vub300_table
);
276 static struct workqueue_struct
*cmndworkqueue
;
277 static struct workqueue_struct
*pollworkqueue
;
278 static struct workqueue_struct
*deadworkqueue
;
280 static inline int interface_to_InterfaceNumber(struct usb_interface
*interface
)
284 if (!interface
->cur_altsetting
)
286 return interface
->cur_altsetting
->desc
.bInterfaceNumber
;
289 struct sdio_register
{
291 unsigned sdio_reg
:17;
296 unsigned sparebit
:26;
299 struct vub300_mmc_host
{
300 struct usb_device
*udev
;
301 struct usb_interface
*interface
;
303 struct mutex cmd_mutex
;
304 struct mutex irq_mutex
;
305 char vub_name
[3 + (9 * 8) + 4 + 1]; /* max of 7 sdio fn's */
306 u8 cmnd_out_ep
; /* EndPoint for commands */
307 u8 cmnd_res_ep
; /* EndPoint for responses */
308 u8 data_out_ep
; /* EndPoint for out data */
309 u8 data_inp_ep
; /* EndPoint for inp data */
313 bool large_usb_packets
;
314 bool app_spec
; /* ApplicationSpecific */
315 bool irq_enabled
; /* by the MMC CORE */
316 bool irq_disabled
; /* in the firmware */
317 unsigned bus_width
:4;
318 u8 total_offload_count
;
319 u8 dynamic_register_count
;
323 int usb_transport_fail
;
326 struct sdio_register sdio_register
[16];
327 struct offload_interrupt_function_register
{
329 #define MAXREGS (1<<MAXREGBITS)
330 #define MAXREGMASK (MAXREGS-1)
333 struct offload_registers_access reg
[MAXREGS
];
335 u16 fbs
[8]; /* Function Block Size */
336 struct mmc_command
*cmd
;
337 struct mmc_request
*req
;
338 struct mmc_data
*data
;
339 struct mmc_host
*mmc
;
341 struct urb
*command_out_urb
;
342 struct urb
*command_res_urb
;
343 struct completion command_complete
;
344 struct completion irqpoll_complete
;
345 union sd_command cmnd
;
346 union sd_response resp
;
347 struct timer_list sg_transfer_timer
;
348 struct usb_sg_request sg_request
;
349 struct timer_list inactivity_timer
;
350 struct work_struct deadwork
;
351 struct work_struct cmndwork
;
352 struct delayed_work pollwork
;
353 struct host_controller_info hc_info
;
354 struct sd_status_header system_port_status
;
355 u8 padded_buffer
[64];
358 #define kref_to_vub300_mmc_host(d) container_of(d, struct vub300_mmc_host, kref)
359 #define SET_TRANSFER_PSEUDOCODE 21
360 #define SET_INTERRUPT_PSEUDOCODE 20
361 #define SET_FAILURE_MODE 18
362 #define SET_ROM_WAIT_STATES 16
363 #define SET_IRQ_ENABLE 13
364 #define SET_CLOCK_SPEED 11
365 #define SET_FUNCTION_BLOCK_SIZE 9
366 #define SET_SD_DATA_MODE 6
367 #define SET_SD_POWER 4
368 #define ENTER_DFU_MODE 3
369 #define GET_HC_INF0 1
370 #define GET_SYSTEM_PORT_STATUS 0
372 static void vub300_delete(struct kref
*kref
)
373 { /* kref callback - softirq */
374 struct vub300_mmc_host
*vub300
= kref_to_vub300_mmc_host(kref
);
375 struct mmc_host
*mmc
= vub300
->mmc
;
376 usb_free_urb(vub300
->command_out_urb
);
377 vub300
->command_out_urb
= NULL
;
378 usb_free_urb(vub300
->command_res_urb
);
379 vub300
->command_res_urb
= NULL
;
380 usb_put_dev(vub300
->udev
);
383 * and hence also frees vub300
384 * which is contained at the end of struct mmc
388 static void vub300_queue_cmnd_work(struct vub300_mmc_host
*vub300
)
390 kref_get(&vub300
->kref
);
391 if (queue_work(cmndworkqueue
, &vub300
->cmndwork
)) {
393 * then the cmndworkqueue was not previously
394 * running and the above get ref is obvious
395 * required and will be put when the thread
396 * terminates by a specific call
400 * the cmndworkqueue was already running from
401 * a previous invocation and thus to keep the
402 * kref counts correct we must undo the get
404 kref_put(&vub300
->kref
, vub300_delete
);
408 static void vub300_queue_poll_work(struct vub300_mmc_host
*vub300
, int delay
)
410 kref_get(&vub300
->kref
);
411 if (queue_delayed_work(pollworkqueue
, &vub300
->pollwork
, delay
)) {
413 * then the pollworkqueue was not previously
414 * running and the above get ref is obvious
415 * required and will be put when the thread
416 * terminates by a specific call
420 * the pollworkqueue was already running from
421 * a previous invocation and thus to keep the
422 * kref counts correct we must undo the get
424 kref_put(&vub300
->kref
, vub300_delete
);
428 static void vub300_queue_dead_work(struct vub300_mmc_host
*vub300
)
430 kref_get(&vub300
->kref
);
431 if (queue_work(deadworkqueue
, &vub300
->deadwork
)) {
433 * then the deadworkqueue was not previously
434 * running and the above get ref is obvious
435 * required and will be put when the thread
436 * terminates by a specific call
440 * the deadworkqueue was already running from
441 * a previous invocation and thus to keep the
442 * kref counts correct we must undo the get
444 kref_put(&vub300
->kref
, vub300_delete
);
448 static void irqpoll_res_completed(struct urb
*urb
)
449 { /* urb completion handler - hardirq */
450 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)urb
->context
;
452 vub300
->usb_transport_fail
= urb
->status
;
453 complete(&vub300
->irqpoll_complete
);
456 static void irqpoll_out_completed(struct urb
*urb
)
457 { /* urb completion handler - hardirq */
458 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)urb
->context
;
460 vub300
->usb_transport_fail
= urb
->status
;
461 complete(&vub300
->irqpoll_complete
);
466 usb_rcvbulkpipe(vub300
->udev
, vub300
->cmnd_res_ep
);
467 usb_fill_bulk_urb(vub300
->command_res_urb
, vub300
->udev
, pipe
,
468 &vub300
->resp
, sizeof(vub300
->resp
),
469 irqpoll_res_completed
, vub300
);
470 vub300
->command_res_urb
->actual_length
= 0;
471 ret
= usb_submit_urb(vub300
->command_res_urb
, GFP_ATOMIC
);
473 vub300
->usb_transport_fail
= ret
;
474 complete(&vub300
->irqpoll_complete
);
480 static void send_irqpoll(struct vub300_mmc_host
*vub300
)
482 /* cmd_mutex is held by vub300_pollwork_thread */
484 int timeout
= 0xFFFF & (0x0001FFFF - firmware_irqpoll_timeout
);
485 vub300
->cmnd
.poll
.header_size
= 22;
486 vub300
->cmnd
.poll
.header_type
= 1;
487 vub300
->cmnd
.poll
.port_number
= 0;
488 vub300
->cmnd
.poll
.command_type
= 2;
489 vub300
->cmnd
.poll
.poll_timeout_lsb
= 0xFF & (unsigned)timeout
;
490 vub300
->cmnd
.poll
.poll_timeout_msb
= 0xFF & (unsigned)(timeout
>> 8);
491 usb_fill_bulk_urb(vub300
->command_out_urb
, vub300
->udev
,
492 usb_sndbulkpipe(vub300
->udev
, vub300
->cmnd_out_ep
)
493 , &vub300
->cmnd
, sizeof(vub300
->cmnd
)
494 , irqpoll_out_completed
, vub300
);
495 retval
= usb_submit_urb(vub300
->command_out_urb
, GFP_KERNEL
);
497 vub300
->usb_transport_fail
= retval
;
498 vub300_queue_poll_work(vub300
, 1);
499 complete(&vub300
->irqpoll_complete
);
506 static void new_system_port_status(struct vub300_mmc_host
*vub300
)
508 int old_card_present
= vub300
->card_present
;
509 int new_card_present
=
510 (0x0001 & vub300
->system_port_status
.port_flags
) ? 1 : 0;
512 (0x0010 & vub300
->system_port_status
.port_flags
) ? 1 : 0;
513 if (new_card_present
&& !old_card_present
) {
514 dev_info(&vub300
->udev
->dev
, "card just inserted\n");
515 vub300
->card_present
= 1;
516 vub300
->bus_width
= 0;
517 if (disable_offload_processing
)
518 strncpy(vub300
->vub_name
, "EMPTY Processing Disabled",
519 sizeof(vub300
->vub_name
));
521 vub300
->vub_name
[0] = 0;
522 mmc_detect_change(vub300
->mmc
, 1);
523 } else if (!new_card_present
&& old_card_present
) {
524 dev_info(&vub300
->udev
->dev
, "card just ejected\n");
525 vub300
->card_present
= 0;
526 mmc_detect_change(vub300
->mmc
, 0);
532 static void __add_offloaded_reg_to_fifo(struct vub300_mmc_host
*vub300
,
533 struct offload_registers_access
534 *register_access
, u8 func
)
536 u8 r
= vub300
->fn
[func
].offload_point
+ vub300
->fn
[func
].offload_count
;
537 memcpy(&vub300
->fn
[func
].reg
[MAXREGMASK
& r
], register_access
,
538 sizeof(struct offload_registers_access
));
539 vub300
->fn
[func
].offload_count
+= 1;
540 vub300
->total_offload_count
+= 1;
543 static void add_offloaded_reg(struct vub300_mmc_host
*vub300
,
544 struct offload_registers_access
*register_access
)
546 u32 Register
= ((0x03 & register_access
->command_byte
[0]) << 15)
547 | ((0xFF & register_access
->command_byte
[1]) << 7)
548 | ((0xFE & register_access
->command_byte
[2]) >> 1);
549 u8 func
= ((0x70 & register_access
->command_byte
[0]) >> 4);
550 u8 regs
= vub300
->dynamic_register_count
;
552 while (0 < regs
-- && 1 == vub300
->sdio_register
[i
].activate
) {
553 if (vub300
->sdio_register
[i
].func_num
== func
&&
554 vub300
->sdio_register
[i
].sdio_reg
== Register
) {
555 if (vub300
->sdio_register
[i
].prepared
== 0)
556 vub300
->sdio_register
[i
].prepared
= 1;
557 vub300
->sdio_register
[i
].response
=
558 register_access
->Respond_Byte
[2];
559 vub300
->sdio_register
[i
].regvalue
=
560 register_access
->Respond_Byte
[3];
567 __add_offloaded_reg_to_fifo(vub300
, register_access
, func
);
570 static void check_vub300_port_status(struct vub300_mmc_host
*vub300
)
573 * cmd_mutex is held by vub300_pollwork_thread,
574 * vub300_deadwork_thread or vub300_cmndwork_thread
578 usb_control_msg(vub300
->udev
, usb_rcvctrlpipe(vub300
->udev
, 0),
579 GET_SYSTEM_PORT_STATUS
,
580 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
581 0x0000, 0x0000, &vub300
->system_port_status
,
582 sizeof(vub300
->system_port_status
), HZ
);
583 if (sizeof(vub300
->system_port_status
) == retval
)
584 new_system_port_status(vub300
);
587 static void __vub300_irqpoll_response(struct vub300_mmc_host
*vub300
)
589 /* cmd_mutex is held by vub300_pollwork_thread */
590 if (vub300
->command_res_urb
->actual_length
== 0)
593 switch (vub300
->resp
.common
.header_type
) {
594 case RESPONSE_INTERRUPT
:
595 mutex_lock(&vub300
->irq_mutex
);
596 if (vub300
->irq_enabled
)
597 mmc_signal_sdio_irq(vub300
->mmc
);
599 vub300
->irqs_queued
+= 1;
600 vub300
->irq_disabled
= 1;
601 mutex_unlock(&vub300
->irq_mutex
);
604 if (vub300
->resp
.error
.error_code
== SD_ERROR_NO_DEVICE
)
605 check_vub300_port_status(vub300
);
607 case RESPONSE_STATUS
:
608 vub300
->system_port_status
= vub300
->resp
.status
;
609 new_system_port_status(vub300
);
610 if (!vub300
->card_present
)
611 vub300_queue_poll_work(vub300
, HZ
/ 5);
613 case RESPONSE_IRQ_DISABLED
:
615 int offloaded_data_length
= vub300
->resp
.common
.header_size
- 3;
616 int register_count
= offloaded_data_length
>> 3;
618 while (register_count
--) {
619 add_offloaded_reg(vub300
, &vub300
->resp
.irq
.reg
[ri
]);
622 mutex_lock(&vub300
->irq_mutex
);
623 if (vub300
->irq_enabled
)
624 mmc_signal_sdio_irq(vub300
->mmc
);
626 vub300
->irqs_queued
+= 1;
627 vub300
->irq_disabled
= 1;
628 mutex_unlock(&vub300
->irq_mutex
);
631 case RESPONSE_IRQ_ENABLED
:
633 int offloaded_data_length
= vub300
->resp
.common
.header_size
- 3;
634 int register_count
= offloaded_data_length
>> 3;
636 while (register_count
--) {
637 add_offloaded_reg(vub300
, &vub300
->resp
.irq
.reg
[ri
]);
640 mutex_lock(&vub300
->irq_mutex
);
641 if (vub300
->irq_enabled
)
642 mmc_signal_sdio_irq(vub300
->mmc
);
643 else if (vub300
->irqs_queued
)
644 vub300
->irqs_queued
+= 1;
646 vub300
->irqs_queued
+= 1;
647 vub300
->irq_disabled
= 0;
648 mutex_unlock(&vub300
->irq_mutex
);
651 case RESPONSE_NO_INTERRUPT
:
652 vub300_queue_poll_work(vub300
, 1);
659 static void __do_poll(struct vub300_mmc_host
*vub300
)
661 /* cmd_mutex is held by vub300_pollwork_thread */
662 unsigned long commretval
;
663 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
664 init_completion(&vub300
->irqpoll_complete
);
665 send_irqpoll(vub300
);
666 commretval
= wait_for_completion_timeout(&vub300
->irqpoll_complete
,
667 msecs_to_jiffies(500));
668 if (vub300
->usb_transport_fail
) {
669 /* no need to do anything */
670 } else if (commretval
== 0) {
671 vub300
->usb_timed_out
= 1;
672 usb_kill_urb(vub300
->command_out_urb
);
673 usb_kill_urb(vub300
->command_res_urb
);
674 } else { /* commretval > 0 */
675 __vub300_irqpoll_response(vub300
);
679 /* this thread runs only when the driver
680 * is trying to poll the device for an IRQ
682 static void vub300_pollwork_thread(struct work_struct
*work
)
684 struct vub300_mmc_host
*vub300
= container_of(work
,
685 struct vub300_mmc_host
, pollwork
.work
);
686 if (!vub300
->interface
) {
687 kref_put(&vub300
->kref
, vub300_delete
);
690 mutex_lock(&vub300
->cmd_mutex
);
692 vub300_queue_poll_work(vub300
, 1);
693 } else if (!vub300
->card_present
) {
694 /* no need to do anything */
695 } else { /* vub300->card_present */
696 mutex_lock(&vub300
->irq_mutex
);
697 if (!vub300
->irq_enabled
) {
698 mutex_unlock(&vub300
->irq_mutex
);
699 } else if (vub300
->irqs_queued
) {
700 vub300
->irqs_queued
-= 1;
701 mmc_signal_sdio_irq(vub300
->mmc
);
702 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
703 mutex_unlock(&vub300
->irq_mutex
);
704 } else { /* NOT vub300->irqs_queued */
705 mutex_unlock(&vub300
->irq_mutex
);
709 mutex_unlock(&vub300
->cmd_mutex
);
710 kref_put(&vub300
->kref
, vub300_delete
);
713 static void vub300_deadwork_thread(struct work_struct
*work
)
715 struct vub300_mmc_host
*vub300
=
716 container_of(work
, struct vub300_mmc_host
, deadwork
);
717 if (!vub300
->interface
) {
718 kref_put(&vub300
->kref
, vub300_delete
);
721 mutex_lock(&vub300
->cmd_mutex
);
724 * a command got in as the inactivity
725 * timer expired - so we just let the
726 * processing of the command show if
729 } else if (vub300
->card_present
) {
730 check_vub300_port_status(vub300
);
731 } else if (vub300
->mmc
&& vub300
->mmc
->card
&&
732 mmc_card_present(vub300
->mmc
->card
)) {
734 * the MMC core must not have responded
735 * to the previous indication - lets
736 * hope that it eventually does so we
737 * will just ignore this for now
740 check_vub300_port_status(vub300
);
742 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
743 mutex_unlock(&vub300
->cmd_mutex
);
744 kref_put(&vub300
->kref
, vub300_delete
);
747 static void vub300_inactivity_timer_expired(unsigned long data
)
749 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)data
;
750 if (!vub300
->interface
) {
751 kref_put(&vub300
->kref
, vub300_delete
);
752 } else if (vub300
->cmd
) {
753 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
755 vub300_queue_dead_work(vub300
);
756 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
760 static int vub300_response_error(u8 error_code
)
762 switch (error_code
) {
763 case SD_ERROR_PIO_TIMEOUT
:
764 case SD_ERROR_1BIT_TIMEOUT
:
765 case SD_ERROR_4BIT_TIMEOUT
:
767 case SD_ERROR_STAT_DATA
:
768 case SD_ERROR_OVERRUN
:
769 case SD_ERROR_STAT_CMD
:
770 case SD_ERROR_STAT_CMD_TIMEOUT
:
771 case SD_ERROR_SDCRDY_STUCK
:
772 case SD_ERROR_UNHANDLED
:
773 case SD_ERROR_1BIT_CRC_WRONG
:
774 case SD_ERROR_4BIT_CRC_WRONG
:
775 case SD_ERROR_1BIT_CRC_ERROR
:
776 case SD_ERROR_4BIT_CRC_ERROR
:
777 case SD_ERROR_NO_CMD_ENDBIT
:
778 case SD_ERROR_NO_1BIT_DATEND
:
779 case SD_ERROR_NO_4BIT_DATEND
:
780 case SD_ERROR_1BIT_DATA_TIMEOUT
:
781 case SD_ERROR_4BIT_DATA_TIMEOUT
:
782 case SD_ERROR_1BIT_UNEXPECTED_TIMEOUT
:
783 case SD_ERROR_4BIT_UNEXPECTED_TIMEOUT
:
787 case SD_ERROR_ILLEGAL_COMMAND
:
789 case SD_ERROR_NO_DEVICE
:
796 static void command_res_completed(struct urb
*urb
)
797 { /* urb completion handler - hardirq */
798 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)urb
->context
;
800 /* we have to let the initiator handle the error */
801 } else if (vub300
->command_res_urb
->actual_length
== 0) {
803 * we have seen this happen once or twice and
804 * we suspect a buggy USB host controller
806 } else if (!vub300
->data
) {
807 /* this means that the command (typically CMD52) succeeded */
808 } else if (vub300
->resp
.common
.header_type
!= 0x02) {
810 * this is an error response from the VUB300 chip
811 * and we let the initiator handle it
813 } else if (vub300
->urb
) {
815 vub300_response_error(vub300
->resp
.error
.error_code
);
816 usb_unlink_urb(vub300
->urb
);
819 vub300_response_error(vub300
->resp
.error
.error_code
);
820 usb_sg_cancel(&vub300
->sg_request
);
822 complete(&vub300
->command_complete
); /* got_response_in */
825 static void command_out_completed(struct urb
*urb
)
826 { /* urb completion handler - hardirq */
827 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)urb
->context
;
829 complete(&vub300
->command_complete
);
833 usb_rcvbulkpipe(vub300
->udev
, vub300
->cmnd_res_ep
);
834 usb_fill_bulk_urb(vub300
->command_res_urb
, vub300
->udev
, pipe
,
835 &vub300
->resp
, sizeof(vub300
->resp
),
836 command_res_completed
, vub300
);
837 vub300
->command_res_urb
->actual_length
= 0;
838 ret
= usb_submit_urb(vub300
->command_res_urb
, GFP_ATOMIC
);
841 * the urb completion handler will call
842 * our completion handler
846 * and thus we only call it directly
847 * when it will not be called
849 complete(&vub300
->command_complete
);
855 * the STUFF bits are masked out for the comparisons
857 static void snoop_block_size_and_bus_width(struct vub300_mmc_host
*vub300
,
860 if ((0xFBFFFE00 & cmd_arg
) == 0x80022200)
861 vub300
->fbs
[1] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[1]);
862 else if ((0xFBFFFE00 & cmd_arg
) == 0x80022000)
863 vub300
->fbs
[1] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[1]);
864 else if ((0xFBFFFE00 & cmd_arg
) == 0x80042200)
865 vub300
->fbs
[2] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[2]);
866 else if ((0xFBFFFE00 & cmd_arg
) == 0x80042000)
867 vub300
->fbs
[2] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[2]);
868 else if ((0xFBFFFE00 & cmd_arg
) == 0x80062200)
869 vub300
->fbs
[3] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[3]);
870 else if ((0xFBFFFE00 & cmd_arg
) == 0x80062000)
871 vub300
->fbs
[3] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[3]);
872 else if ((0xFBFFFE00 & cmd_arg
) == 0x80082200)
873 vub300
->fbs
[4] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[4]);
874 else if ((0xFBFFFE00 & cmd_arg
) == 0x80082000)
875 vub300
->fbs
[4] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[4]);
876 else if ((0xFBFFFE00 & cmd_arg
) == 0x800A2200)
877 vub300
->fbs
[5] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[5]);
878 else if ((0xFBFFFE00 & cmd_arg
) == 0x800A2000)
879 vub300
->fbs
[5] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[5]);
880 else if ((0xFBFFFE00 & cmd_arg
) == 0x800C2200)
881 vub300
->fbs
[6] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[6]);
882 else if ((0xFBFFFE00 & cmd_arg
) == 0x800C2000)
883 vub300
->fbs
[6] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[6]);
884 else if ((0xFBFFFE00 & cmd_arg
) == 0x800E2200)
885 vub300
->fbs
[7] = (cmd_arg
<< 8) | (0x00FF & vub300
->fbs
[7]);
886 else if ((0xFBFFFE00 & cmd_arg
) == 0x800E2000)
887 vub300
->fbs
[7] = (0xFF & cmd_arg
) | (0xFF00 & vub300
->fbs
[7]);
888 else if ((0xFBFFFE03 & cmd_arg
) == 0x80000E00)
889 vub300
->bus_width
= 1;
890 else if ((0xFBFFFE03 & cmd_arg
) == 0x80000E02)
891 vub300
->bus_width
= 4;
894 static void send_command(struct vub300_mmc_host
*vub300
)
896 /* cmd_mutex is held by vub300_cmndwork_thread */
897 struct mmc_command
*cmd
= vub300
->cmd
;
898 struct mmc_data
*data
= vub300
->data
;
902 if (vub300
->app_spec
) {
903 switch (cmd
->opcode
) {
905 response_type
= SDRT_1
;
906 vub300
->resp_len
= 6;
907 if (0x00000000 == (0x00000003 & cmd
->arg
))
908 vub300
->bus_width
= 1;
909 else if (0x00000002 == (0x00000003 & cmd
->arg
))
910 vub300
->bus_width
= 4;
912 dev_err(&vub300
->udev
->dev
,
913 "unexpected ACMD6 bus_width=%d\n",
914 0x00000003 & cmd
->arg
);
917 response_type
= SDRT_1
;
918 vub300
->resp_len
= 6;
921 response_type
= SDRT_1
;
922 vub300
->resp_len
= 6;
925 response_type
= SDRT_1
;
926 vub300
->resp_len
= 6;
929 response_type
= SDRT_3
;
930 vub300
->resp_len
= 6;
933 response_type
= SDRT_1
;
934 vub300
->resp_len
= 6;
937 response_type
= SDRT_1
;
938 vub300
->resp_len
= 6;
941 response_type
= SDRT_1
;
942 vub300
->resp_len
= 6;
945 vub300
->resp_len
= 0;
946 cmd
->error
= -EINVAL
;
947 complete(&vub300
->command_complete
);
950 vub300
->app_spec
= 0;
952 switch (cmd
->opcode
) {
954 response_type
= SDRT_NONE
;
955 vub300
->resp_len
= 0;
958 response_type
= SDRT_3
;
959 vub300
->resp_len
= 6;
962 response_type
= SDRT_2
;
963 vub300
->resp_len
= 17;
966 response_type
= SDRT_6
;
967 vub300
->resp_len
= 6;
970 response_type
= SDRT_NONE
;
971 vub300
->resp_len
= 0;
974 response_type
= SDRT_4
;
975 vub300
->resp_len
= 6;
978 response_type
= SDRT_1
;
979 vub300
->resp_len
= 6;
982 response_type
= SDRT_1B
;
983 vub300
->resp_len
= 6;
986 response_type
= SDRT_7
;
987 vub300
->resp_len
= 6;
990 response_type
= SDRT_2
;
991 vub300
->resp_len
= 17;
994 response_type
= SDRT_2
;
995 vub300
->resp_len
= 17;
998 response_type
= SDRT_1B
;
999 vub300
->resp_len
= 6;
1002 response_type
= SDRT_1
;
1003 vub300
->resp_len
= 6;
1006 response_type
= SDRT_NONE
;
1007 vub300
->resp_len
= 0;
1010 for (i
= 0; i
< ARRAY_SIZE(vub300
->fbs
); i
++)
1011 vub300
->fbs
[i
] = 0xFFFF & cmd
->arg
;
1012 response_type
= SDRT_1
;
1013 vub300
->resp_len
= 6;
1020 response_type
= SDRT_1
;
1021 vub300
->resp_len
= 6;
1025 response_type
= SDRT_1B
;
1026 vub300
->resp_len
= 6;
1031 response_type
= SDRT_1
;
1032 vub300
->resp_len
= 6;
1035 response_type
= SDRT_1B
;
1036 vub300
->resp_len
= 6;
1039 response_type
= SDRT_1
;
1040 vub300
->resp_len
= 6;
1043 response_type
= SDRT_5
;
1044 vub300
->resp_len
= 6;
1045 snoop_block_size_and_bus_width(vub300
, cmd
->arg
);
1048 response_type
= SDRT_5
;
1049 vub300
->resp_len
= 6;
1052 response_type
= SDRT_1
;
1053 vub300
->resp_len
= 6;
1054 vub300
->app_spec
= 1;
1057 response_type
= SDRT_1
;
1058 vub300
->resp_len
= 6;
1061 vub300
->resp_len
= 0;
1062 cmd
->error
= -EINVAL
;
1063 complete(&vub300
->command_complete
);
1068 * it is a shame that we can not use "sizeof(struct sd_command_header)"
1069 * this is because the packet _must_ be padded to 64 bytes
1071 vub300
->cmnd
.head
.header_size
= 20;
1072 vub300
->cmnd
.head
.header_type
= 0x00;
1073 vub300
->cmnd
.head
.port_number
= 0; /* "0" means port 1 */
1074 vub300
->cmnd
.head
.command_type
= 0x00; /* standard read command */
1075 vub300
->cmnd
.head
.response_type
= response_type
;
1076 vub300
->cmnd
.head
.command_index
= cmd
->opcode
;
1077 vub300
->cmnd
.head
.arguments
[0] = cmd
->arg
>> 24;
1078 vub300
->cmnd
.head
.arguments
[1] = cmd
->arg
>> 16;
1079 vub300
->cmnd
.head
.arguments
[2] = cmd
->arg
>> 8;
1080 vub300
->cmnd
.head
.arguments
[3] = cmd
->arg
>> 0;
1081 if (cmd
->opcode
== 52) {
1082 int fn
= 0x7 & (cmd
->arg
>> 28);
1083 vub300
->cmnd
.head
.block_count
[0] = 0;
1084 vub300
->cmnd
.head
.block_count
[1] = 0;
1085 vub300
->cmnd
.head
.block_size
[0] = (vub300
->fbs
[fn
] >> 8) & 0xFF;
1086 vub300
->cmnd
.head
.block_size
[1] = (vub300
->fbs
[fn
] >> 0) & 0xFF;
1087 vub300
->cmnd
.head
.command_type
= 0x00;
1088 vub300
->cmnd
.head
.transfer_size
[0] = 0;
1089 vub300
->cmnd
.head
.transfer_size
[1] = 0;
1090 vub300
->cmnd
.head
.transfer_size
[2] = 0;
1091 vub300
->cmnd
.head
.transfer_size
[3] = 0;
1093 vub300
->cmnd
.head
.block_count
[0] = 0;
1094 vub300
->cmnd
.head
.block_count
[1] = 0;
1095 vub300
->cmnd
.head
.block_size
[0] = (vub300
->fbs
[0] >> 8) & 0xFF;
1096 vub300
->cmnd
.head
.block_size
[1] = (vub300
->fbs
[0] >> 0) & 0xFF;
1097 vub300
->cmnd
.head
.command_type
= 0x00;
1098 vub300
->cmnd
.head
.transfer_size
[0] = 0;
1099 vub300
->cmnd
.head
.transfer_size
[1] = 0;
1100 vub300
->cmnd
.head
.transfer_size
[2] = 0;
1101 vub300
->cmnd
.head
.transfer_size
[3] = 0;
1102 } else if (cmd
->opcode
== 53) {
1103 int fn
= 0x7 & (cmd
->arg
>> 28);
1104 if (0x08 & vub300
->cmnd
.head
.arguments
[0]) { /* BLOCK MODE */
1105 vub300
->cmnd
.head
.block_count
[0] =
1106 (data
->blocks
>> 8) & 0xFF;
1107 vub300
->cmnd
.head
.block_count
[1] =
1108 (data
->blocks
>> 0) & 0xFF;
1109 vub300
->cmnd
.head
.block_size
[0] =
1110 (data
->blksz
>> 8) & 0xFF;
1111 vub300
->cmnd
.head
.block_size
[1] =
1112 (data
->blksz
>> 0) & 0xFF;
1113 } else { /* BYTE MODE */
1114 vub300
->cmnd
.head
.block_count
[0] = 0;
1115 vub300
->cmnd
.head
.block_count
[1] = 0;
1116 vub300
->cmnd
.head
.block_size
[0] =
1117 (vub300
->datasize
>> 8) & 0xFF;
1118 vub300
->cmnd
.head
.block_size
[1] =
1119 (vub300
->datasize
>> 0) & 0xFF;
1121 vub300
->cmnd
.head
.command_type
=
1122 (MMC_DATA_READ
& data
->flags
) ? 0x00 : 0x80;
1123 vub300
->cmnd
.head
.transfer_size
[0] =
1124 (vub300
->datasize
>> 24) & 0xFF;
1125 vub300
->cmnd
.head
.transfer_size
[1] =
1126 (vub300
->datasize
>> 16) & 0xFF;
1127 vub300
->cmnd
.head
.transfer_size
[2] =
1128 (vub300
->datasize
>> 8) & 0xFF;
1129 vub300
->cmnd
.head
.transfer_size
[3] =
1130 (vub300
->datasize
>> 0) & 0xFF;
1131 if (vub300
->datasize
< vub300
->fbs
[fn
]) {
1132 vub300
->cmnd
.head
.block_count
[0] = 0;
1133 vub300
->cmnd
.head
.block_count
[1] = 0;
1136 vub300
->cmnd
.head
.block_count
[0] = (data
->blocks
>> 8) & 0xFF;
1137 vub300
->cmnd
.head
.block_count
[1] = (data
->blocks
>> 0) & 0xFF;
1138 vub300
->cmnd
.head
.block_size
[0] = (data
->blksz
>> 8) & 0xFF;
1139 vub300
->cmnd
.head
.block_size
[1] = (data
->blksz
>> 0) & 0xFF;
1140 vub300
->cmnd
.head
.command_type
=
1141 (MMC_DATA_READ
& data
->flags
) ? 0x00 : 0x80;
1142 vub300
->cmnd
.head
.transfer_size
[0] =
1143 (vub300
->datasize
>> 24) & 0xFF;
1144 vub300
->cmnd
.head
.transfer_size
[1] =
1145 (vub300
->datasize
>> 16) & 0xFF;
1146 vub300
->cmnd
.head
.transfer_size
[2] =
1147 (vub300
->datasize
>> 8) & 0xFF;
1148 vub300
->cmnd
.head
.transfer_size
[3] =
1149 (vub300
->datasize
>> 0) & 0xFF;
1150 if (vub300
->datasize
< vub300
->fbs
[0]) {
1151 vub300
->cmnd
.head
.block_count
[0] = 0;
1152 vub300
->cmnd
.head
.block_count
[1] = 0;
1155 if (vub300
->cmnd
.head
.block_size
[0] || vub300
->cmnd
.head
.block_size
[1]) {
1156 u16 block_size
= vub300
->cmnd
.head
.block_size
[1] |
1157 (vub300
->cmnd
.head
.block_size
[0] << 8);
1158 u16 block_boundary
= FIRMWARE_BLOCK_BOUNDARY
-
1159 (FIRMWARE_BLOCK_BOUNDARY
% block_size
);
1160 vub300
->cmnd
.head
.block_boundary
[0] =
1161 (block_boundary
>> 8) & 0xFF;
1162 vub300
->cmnd
.head
.block_boundary
[1] =
1163 (block_boundary
>> 0) & 0xFF;
1165 vub300
->cmnd
.head
.block_boundary
[0] = 0;
1166 vub300
->cmnd
.head
.block_boundary
[1] = 0;
1168 usb_fill_bulk_urb(vub300
->command_out_urb
, vub300
->udev
,
1169 usb_sndbulkpipe(vub300
->udev
, vub300
->cmnd_out_ep
),
1170 &vub300
->cmnd
, sizeof(vub300
->cmnd
),
1171 command_out_completed
, vub300
);
1172 retval
= usb_submit_urb(vub300
->command_out_urb
, GFP_KERNEL
);
1174 cmd
->error
= retval
;
1175 complete(&vub300
->command_complete
);
1183 * timer callback runs in atomic mode
1184 * so it cannot call usb_kill_urb()
1186 static void vub300_sg_timed_out(unsigned long data
)
1188 struct vub300_mmc_host
*vub300
= (struct vub300_mmc_host
*)data
;
1189 vub300
->usb_timed_out
= 1;
1190 usb_sg_cancel(&vub300
->sg_request
);
1191 usb_unlink_urb(vub300
->command_out_urb
);
1192 usb_unlink_urb(vub300
->command_res_urb
);
1195 static u16
roundup_to_multiple_of_64(u16 number
)
1197 return 0xFFC0 & (0x3F + number
);
1201 * this is a separate function to solve the 80 column width restriction
1203 static void __download_offload_pseudocode(struct vub300_mmc_host
*vub300
,
1204 const struct firmware
*fw
)
1206 u8 register_count
= 0;
1208 u16 interrupt_size
= 0;
1209 const u8
*data
= fw
->data
;
1210 int size
= fw
->size
;
1212 dev_info(&vub300
->udev
->dev
, "using %s for SDIO offload processing\n",
1216 } while (size
-- && c
); /* skip comment */
1217 dev_info(&vub300
->udev
->dev
, "using offload firmware %s %s\n", fw
->data
,
1220 dev_err(&vub300
->udev
->dev
,
1221 "corrupt offload pseudocode in firmware %s\n",
1223 strncpy(vub300
->vub_name
, "corrupt offload pseudocode",
1224 sizeof(vub300
->vub_name
));
1227 interrupt_size
+= *data
++;
1229 interrupt_size
<<= 8;
1230 interrupt_size
+= *data
++;
1232 if (interrupt_size
< size
) {
1233 u16 xfer_length
= roundup_to_multiple_of_64(interrupt_size
);
1234 u8
*xfer_buffer
= kmalloc(xfer_length
, GFP_KERNEL
);
1237 memcpy(xfer_buffer
, data
, interrupt_size
);
1238 memset(xfer_buffer
+ interrupt_size
, 0,
1239 xfer_length
- interrupt_size
);
1240 size
-= interrupt_size
;
1241 data
+= interrupt_size
;
1243 usb_control_msg(vub300
->udev
,
1244 usb_sndctrlpipe(vub300
->udev
, 0),
1245 SET_INTERRUPT_PSEUDOCODE
,
1246 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1247 USB_RECIP_DEVICE
, 0x0000, 0x0000,
1248 xfer_buffer
, xfer_length
, HZ
);
1251 strncpy(vub300
->vub_name
,
1252 "SDIO pseudocode download failed",
1253 sizeof(vub300
->vub_name
));
1257 dev_err(&vub300
->udev
->dev
,
1258 "not enough memory for xfer buffer to send"
1259 " INTERRUPT_PSEUDOCODE for %s %s\n", fw
->data
,
1261 strncpy(vub300
->vub_name
,
1262 "SDIO interrupt pseudocode download failed",
1263 sizeof(vub300
->vub_name
));
1267 dev_err(&vub300
->udev
->dev
,
1268 "corrupt interrupt pseudocode in firmware %s %s\n",
1269 fw
->data
, vub300
->vub_name
);
1270 strncpy(vub300
->vub_name
, "corrupt interrupt pseudocode",
1271 sizeof(vub300
->vub_name
));
1280 u16 xfer_length
= roundup_to_multiple_of_64(ts
);
1281 u8
*xfer_buffer
= kmalloc(xfer_length
, GFP_KERNEL
);
1284 memcpy(xfer_buffer
, data
, ts
);
1285 memset(xfer_buffer
+ ts
, 0,
1290 usb_control_msg(vub300
->udev
,
1291 usb_sndctrlpipe(vub300
->udev
, 0),
1292 SET_TRANSFER_PSEUDOCODE
,
1293 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1294 USB_RECIP_DEVICE
, 0x0000, 0x0000,
1295 xfer_buffer
, xfer_length
, HZ
);
1298 strncpy(vub300
->vub_name
,
1299 "SDIO pseudocode download failed",
1300 sizeof(vub300
->vub_name
));
1304 dev_err(&vub300
->udev
->dev
,
1305 "not enough memory for xfer buffer to send"
1306 " TRANSFER_PSEUDOCODE for %s %s\n", fw
->data
,
1308 strncpy(vub300
->vub_name
,
1309 "SDIO transfer pseudocode download failed",
1310 sizeof(vub300
->vub_name
));
1314 dev_err(&vub300
->udev
->dev
,
1315 "corrupt transfer pseudocode in firmware %s %s\n",
1316 fw
->data
, vub300
->vub_name
);
1317 strncpy(vub300
->vub_name
, "corrupt transfer pseudocode",
1318 sizeof(vub300
->vub_name
));
1321 register_count
+= *data
++;
1323 if (register_count
* 4 == size
) {
1324 int I
= vub300
->dynamic_register_count
= register_count
;
1327 unsigned int func_num
= 0;
1328 vub300
->sdio_register
[i
].func_num
= *data
++;
1330 func_num
+= *data
++;
1333 func_num
+= *data
++;
1336 func_num
+= *data
++;
1338 vub300
->sdio_register
[i
].sdio_reg
= func_num
;
1339 vub300
->sdio_register
[i
].activate
= 1;
1340 vub300
->sdio_register
[i
].prepared
= 0;
1343 dev_info(&vub300
->udev
->dev
,
1344 "initialized %d dynamic pseudocode registers\n",
1345 vub300
->dynamic_register_count
);
1348 dev_err(&vub300
->udev
->dev
,
1349 "corrupt dynamic registers in firmware %s\n",
1351 strncpy(vub300
->vub_name
, "corrupt dynamic registers",
1352 sizeof(vub300
->vub_name
));
1358 * if the binary containing the EMPTY PseudoCode can not be found
1359 * vub300->vub_name is set anyway in order to prevent an automatic retry
1361 static void download_offload_pseudocode(struct vub300_mmc_host
*vub300
)
1363 struct mmc_card
*card
= vub300
->mmc
->card
;
1364 int sdio_funcs
= card
->sdio_funcs
;
1365 const struct firmware
*fw
= NULL
;
1366 int l
= snprintf(vub300
->vub_name
, sizeof(vub300
->vub_name
),
1367 "vub_%04X%04X", card
->cis
.vendor
, card
->cis
.device
);
1370 for (n
= 0; n
< sdio_funcs
; n
++) {
1371 struct sdio_func
*sf
= card
->sdio_func
[n
];
1372 l
+= snprintf(vub300
->vub_name
+ l
,
1373 sizeof(vub300
->vub_name
) - l
, "_%04X%04X",
1374 sf
->vendor
, sf
->device
);
1376 snprintf(vub300
->vub_name
+ l
, sizeof(vub300
->vub_name
) - l
, ".bin");
1377 dev_info(&vub300
->udev
->dev
, "requesting offload firmware %s\n",
1379 retval
= request_firmware(&fw
, vub300
->vub_name
, &card
->dev
);
1381 strncpy(vub300
->vub_name
, "vub_default.bin",
1382 sizeof(vub300
->vub_name
));
1383 retval
= request_firmware(&fw
, vub300
->vub_name
, &card
->dev
);
1385 strncpy(vub300
->vub_name
,
1386 "no SDIO offload firmware found",
1387 sizeof(vub300
->vub_name
));
1389 __download_offload_pseudocode(vub300
, fw
);
1390 release_firmware(fw
);
1393 __download_offload_pseudocode(vub300
, fw
);
1394 release_firmware(fw
);
1398 static void vub300_usb_bulk_msg_completion(struct urb
*urb
)
1399 { /* urb completion handler - hardirq */
1400 complete((struct completion
*)urb
->context
);
1403 static int vub300_usb_bulk_msg(struct vub300_mmc_host
*vub300
,
1404 unsigned int pipe
, void *data
, int len
,
1405 int *actual_length
, int timeout_msecs
)
1407 /* cmd_mutex is held by vub300_cmndwork_thread */
1408 struct usb_device
*usb_dev
= vub300
->udev
;
1409 struct completion done
;
1411 vub300
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1414 usb_fill_bulk_urb(vub300
->urb
, usb_dev
, pipe
, data
, len
,
1415 vub300_usb_bulk_msg_completion
, NULL
);
1416 init_completion(&done
);
1417 vub300
->urb
->context
= &done
;
1418 vub300
->urb
->actual_length
= 0;
1419 retval
= usb_submit_urb(vub300
->urb
, GFP_KERNEL
);
1420 if (unlikely(retval
))
1422 if (!wait_for_completion_timeout
1423 (&done
, msecs_to_jiffies(timeout_msecs
))) {
1424 retval
= -ETIMEDOUT
;
1425 usb_kill_urb(vub300
->urb
);
1427 retval
= vub300
->urb
->status
;
1430 *actual_length
= vub300
->urb
->actual_length
;
1431 usb_free_urb(vub300
->urb
);
1436 static int __command_read_data(struct vub300_mmc_host
*vub300
,
1437 struct mmc_command
*cmd
, struct mmc_data
*data
)
1439 /* cmd_mutex is held by vub300_cmndwork_thread */
1440 int linear_length
= vub300
->datasize
;
1441 int padded_length
= vub300
->large_usb_packets
?
1442 ((511 + linear_length
) >> 9) << 9 :
1443 ((63 + linear_length
) >> 6) << 6;
1444 if ((padded_length
== linear_length
) || !pad_input_to_usb_pkt
) {
1447 pipe
= usb_rcvbulkpipe(vub300
->udev
, vub300
->data_inp_ep
);
1448 result
= usb_sg_init(&vub300
->sg_request
, vub300
->udev
,
1450 data
->sg_len
, 0, GFP_KERNEL
);
1452 usb_unlink_urb(vub300
->command_out_urb
);
1453 usb_unlink_urb(vub300
->command_res_urb
);
1454 cmd
->error
= result
;
1455 data
->bytes_xfered
= 0;
1458 vub300
->sg_transfer_timer
.expires
=
1459 jiffies
+ msecs_to_jiffies(2000 +
1460 (linear_length
/ 16384));
1461 add_timer(&vub300
->sg_transfer_timer
);
1462 usb_sg_wait(&vub300
->sg_request
);
1463 del_timer(&vub300
->sg_transfer_timer
);
1464 if (vub300
->sg_request
.status
< 0) {
1465 cmd
->error
= vub300
->sg_request
.status
;
1466 data
->bytes_xfered
= 0;
1469 data
->bytes_xfered
= vub300
->datasize
;
1470 return linear_length
;
1474 u8
*buf
= kmalloc(padded_length
, GFP_KERNEL
);
1477 unsigned pipe
= usb_rcvbulkpipe(vub300
->udev
,
1478 vub300
->data_inp_ep
);
1479 int actual_length
= 0;
1480 result
= vub300_usb_bulk_msg(vub300
, pipe
, buf
,
1481 padded_length
, &actual_length
,
1482 2000 + (padded_length
/ 16384));
1484 cmd
->error
= result
;
1485 data
->bytes_xfered
= 0;
1488 } else if (actual_length
< linear_length
) {
1489 cmd
->error
= -EREMOTEIO
;
1490 data
->bytes_xfered
= 0;
1494 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
,
1497 data
->bytes_xfered
= vub300
->datasize
;
1498 return linear_length
;
1501 cmd
->error
= -ENOMEM
;
1502 data
->bytes_xfered
= 0;
1508 static int __command_write_data(struct vub300_mmc_host
*vub300
,
1509 struct mmc_command
*cmd
, struct mmc_data
*data
)
1511 /* cmd_mutex is held by vub300_cmndwork_thread */
1512 unsigned pipe
= usb_sndbulkpipe(vub300
->udev
, vub300
->data_out_ep
);
1513 int linear_length
= vub300
->datasize
;
1514 int modulo_64_length
= linear_length
& 0x003F;
1515 int modulo_512_length
= linear_length
& 0x01FF;
1516 if (linear_length
< 64) {
1519 sg_copy_to_buffer(data
->sg
, data
->sg_len
,
1520 vub300
->padded_buffer
,
1521 sizeof(vub300
->padded_buffer
));
1522 memset(vub300
->padded_buffer
+ linear_length
, 0,
1523 sizeof(vub300
->padded_buffer
) - linear_length
);
1524 result
= vub300_usb_bulk_msg(vub300
, pipe
, vub300
->padded_buffer
,
1525 sizeof(vub300
->padded_buffer
),
1526 &actual_length
, 2000 +
1527 (sizeof(vub300
->padded_buffer
) /
1530 cmd
->error
= result
;
1531 data
->bytes_xfered
= 0;
1533 data
->bytes_xfered
= vub300
->datasize
;
1535 } else if ((!vub300
->large_usb_packets
&& (0 < modulo_64_length
)) ||
1536 (vub300
->large_usb_packets
&& (64 > modulo_512_length
))
1537 ) { /* don't you just love these work-rounds */
1538 int padded_length
= ((63 + linear_length
) >> 6) << 6;
1539 u8
*buf
= kmalloc(padded_length
, GFP_KERNEL
);
1543 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
,
1545 memset(buf
+ linear_length
, 0,
1546 padded_length
- linear_length
);
1548 vub300_usb_bulk_msg(vub300
, pipe
, buf
,
1549 padded_length
, &actual_length
,
1550 2000 + padded_length
/ 16384);
1553 cmd
->error
= result
;
1554 data
->bytes_xfered
= 0;
1556 data
->bytes_xfered
= vub300
->datasize
;
1559 cmd
->error
= -ENOMEM
;
1560 data
->bytes_xfered
= 0;
1562 } else { /* no data padding required */
1564 unsigned char buf
[64 * 4];
1565 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, sizeof(buf
));
1566 result
= usb_sg_init(&vub300
->sg_request
, vub300
->udev
,
1568 data
->sg_len
, 0, GFP_KERNEL
);
1570 usb_unlink_urb(vub300
->command_out_urb
);
1571 usb_unlink_urb(vub300
->command_res_urb
);
1572 cmd
->error
= result
;
1573 data
->bytes_xfered
= 0;
1575 vub300
->sg_transfer_timer
.expires
=
1576 jiffies
+ msecs_to_jiffies(2000 +
1577 linear_length
/ 16384);
1578 add_timer(&vub300
->sg_transfer_timer
);
1579 usb_sg_wait(&vub300
->sg_request
);
1581 data
->bytes_xfered
= 0;
1583 del_timer(&vub300
->sg_transfer_timer
);
1584 if (vub300
->sg_request
.status
< 0) {
1585 cmd
->error
= vub300
->sg_request
.status
;
1586 data
->bytes_xfered
= 0;
1588 data
->bytes_xfered
= vub300
->datasize
;
1593 return linear_length
;
1596 static void __vub300_command_response(struct vub300_mmc_host
*vub300
,
1597 struct mmc_command
*cmd
,
1598 struct mmc_data
*data
, int data_length
)
1600 /* cmd_mutex is held by vub300_cmndwork_thread */
1602 int msec_timeout
= 1000 + data_length
/ 4;
1604 wait_for_completion_timeout(&vub300
->command_complete
,
1605 msecs_to_jiffies(msec_timeout
));
1606 if (respretval
== 0) { /* TIMED OUT */
1607 /* we don't know which of "out" and "res" if any failed */
1609 vub300
->usb_timed_out
= 1;
1610 usb_kill_urb(vub300
->command_out_urb
);
1611 usb_kill_urb(vub300
->command_res_urb
);
1612 cmd
->error
= -ETIMEDOUT
;
1613 result
= usb_lock_device_for_reset(vub300
->udev
,
1616 result
= usb_reset_device(vub300
->udev
);
1617 usb_unlock_device(vub300
->udev
);
1619 } else if (respretval
< 0) {
1620 /* we don't know which of "out" and "res" if any failed */
1621 usb_kill_urb(vub300
->command_out_urb
);
1622 usb_kill_urb(vub300
->command_res_urb
);
1623 cmd
->error
= respretval
;
1624 } else if (cmd
->error
) {
1626 * the error occurred sending the command
1627 * or receiving the response
1629 } else if (vub300
->command_out_urb
->status
) {
1630 vub300
->usb_transport_fail
= vub300
->command_out_urb
->status
;
1631 cmd
->error
= -EPROTO
== vub300
->command_out_urb
->status
?
1632 -ESHUTDOWN
: vub300
->command_out_urb
->status
;
1633 } else if (vub300
->command_res_urb
->status
) {
1634 vub300
->usb_transport_fail
= vub300
->command_res_urb
->status
;
1635 cmd
->error
= -EPROTO
== vub300
->command_res_urb
->status
?
1636 -ESHUTDOWN
: vub300
->command_res_urb
->status
;
1637 } else if (vub300
->resp
.common
.header_type
== 0x00) {
1639 * the command completed successfully
1640 * and there was no piggybacked data
1642 } else if (vub300
->resp
.common
.header_type
== RESPONSE_ERROR
) {
1644 vub300_response_error(vub300
->resp
.error
.error_code
);
1646 usb_sg_cancel(&vub300
->sg_request
);
1647 } else if (vub300
->resp
.common
.header_type
== RESPONSE_PIGGYBACKED
) {
1648 int offloaded_data_length
=
1649 vub300
->resp
.common
.header_size
-
1650 sizeof(struct sd_register_header
);
1651 int register_count
= offloaded_data_length
>> 3;
1653 while (register_count
--) {
1654 add_offloaded_reg(vub300
, &vub300
->resp
.pig
.reg
[ri
]);
1657 vub300
->resp
.common
.header_size
=
1658 sizeof(struct sd_register_header
);
1659 vub300
->resp
.common
.header_type
= 0x00;
1661 } else if (vub300
->resp
.common
.header_type
== RESPONSE_PIG_DISABLED
) {
1662 int offloaded_data_length
=
1663 vub300
->resp
.common
.header_size
-
1664 sizeof(struct sd_register_header
);
1665 int register_count
= offloaded_data_length
>> 3;
1667 while (register_count
--) {
1668 add_offloaded_reg(vub300
, &vub300
->resp
.pig
.reg
[ri
]);
1671 mutex_lock(&vub300
->irq_mutex
);
1672 if (vub300
->irqs_queued
) {
1673 vub300
->irqs_queued
+= 1;
1674 } else if (vub300
->irq_enabled
) {
1675 vub300
->irqs_queued
+= 1;
1676 vub300_queue_poll_work(vub300
, 0);
1678 vub300
->irqs_queued
+= 1;
1680 vub300
->irq_disabled
= 1;
1681 mutex_unlock(&vub300
->irq_mutex
);
1682 vub300
->resp
.common
.header_size
=
1683 sizeof(struct sd_register_header
);
1684 vub300
->resp
.common
.header_type
= 0x00;
1686 } else if (vub300
->resp
.common
.header_type
== RESPONSE_PIG_ENABLED
) {
1687 int offloaded_data_length
=
1688 vub300
->resp
.common
.header_size
-
1689 sizeof(struct sd_register_header
);
1690 int register_count
= offloaded_data_length
>> 3;
1692 while (register_count
--) {
1693 add_offloaded_reg(vub300
, &vub300
->resp
.pig
.reg
[ri
]);
1696 mutex_lock(&vub300
->irq_mutex
);
1697 if (vub300
->irqs_queued
) {
1698 vub300
->irqs_queued
+= 1;
1699 } else if (vub300
->irq_enabled
) {
1700 vub300
->irqs_queued
+= 1;
1701 vub300_queue_poll_work(vub300
, 0);
1703 vub300
->irqs_queued
+= 1;
1705 vub300
->irq_disabled
= 0;
1706 mutex_unlock(&vub300
->irq_mutex
);
1707 vub300
->resp
.common
.header_size
=
1708 sizeof(struct sd_register_header
);
1709 vub300
->resp
.common
.header_type
= 0x00;
1712 cmd
->error
= -EINVAL
;
1716 static void construct_request_response(struct vub300_mmc_host
*vub300
,
1717 struct mmc_command
*cmd
)
1719 int resp_len
= vub300
->resp_len
;
1720 int less_cmd
= (17 == resp_len
) ? resp_len
: resp_len
- 1;
1721 int bytes
= 3 & less_cmd
;
1722 int words
= less_cmd
>> 2;
1723 u8
*r
= vub300
->resp
.response
.command_response
;
1725 cmd
->resp
[words
] = (r
[1 + (words
<< 2)] << 24)
1726 | (r
[2 + (words
<< 2)] << 16)
1727 | (r
[3 + (words
<< 2)] << 8);
1728 } else if (bytes
== 2) {
1729 cmd
->resp
[words
] = (r
[1 + (words
<< 2)] << 24)
1730 | (r
[2 + (words
<< 2)] << 16);
1731 } else if (bytes
== 1) {
1732 cmd
->resp
[words
] = (r
[1 + (words
<< 2)] << 24);
1734 while (words
-- > 0) {
1735 cmd
->resp
[words
] = (r
[1 + (words
<< 2)] << 24)
1736 | (r
[2 + (words
<< 2)] << 16)
1737 | (r
[3 + (words
<< 2)] << 8)
1738 | (r
[4 + (words
<< 2)] << 0);
1740 if ((cmd
->opcode
== 53) && (0x000000FF & cmd
->resp
[0]))
1741 cmd
->resp
[0] &= 0xFFFFFF00;
1744 /* this thread runs only when there is an upper level command req outstanding */
1745 static void vub300_cmndwork_thread(struct work_struct
*work
)
1747 struct vub300_mmc_host
*vub300
=
1748 container_of(work
, struct vub300_mmc_host
, cmndwork
);
1749 if (!vub300
->interface
) {
1750 kref_put(&vub300
->kref
, vub300_delete
);
1753 struct mmc_request
*req
= vub300
->req
;
1754 struct mmc_command
*cmd
= vub300
->cmd
;
1755 struct mmc_data
*data
= vub300
->data
;
1757 mutex_lock(&vub300
->cmd_mutex
);
1758 init_completion(&vub300
->command_complete
);
1759 if (likely(vub300
->vub_name
[0]) || !vub300
->mmc
->card
||
1760 !mmc_card_present(vub300
->mmc
->card
)) {
1762 * the name of the EMPTY Pseudo firmware file
1763 * is used as a flag to indicate that the file
1764 * has been already downloaded to the VUB300 chip
1766 } else if (0 == vub300
->mmc
->card
->sdio_funcs
) {
1767 strncpy(vub300
->vub_name
, "SD memory device",
1768 sizeof(vub300
->vub_name
));
1770 download_offload_pseudocode(vub300
);
1772 send_command(vub300
);
1775 else if (MMC_DATA_READ
& data
->flags
)
1776 data_length
= __command_read_data(vub300
, cmd
, data
);
1778 data_length
= __command_write_data(vub300
, cmd
, data
);
1779 __vub300_command_response(vub300
, cmd
, data
, data_length
);
1782 vub300
->data
= NULL
;
1784 if (cmd
->error
== -ENOMEDIUM
)
1785 check_vub300_port_status(vub300
);
1786 mutex_unlock(&vub300
->cmd_mutex
);
1787 mmc_request_done(vub300
->mmc
, req
);
1788 kref_put(&vub300
->kref
, vub300_delete
);
1791 construct_request_response(vub300
, cmd
);
1792 vub300
->resp_len
= 0;
1793 mutex_unlock(&vub300
->cmd_mutex
);
1794 kref_put(&vub300
->kref
, vub300_delete
);
1795 mmc_request_done(vub300
->mmc
, req
);
1801 static int examine_cyclic_buffer(struct vub300_mmc_host
*vub300
,
1802 struct mmc_command
*cmd
, u8 Function
)
1804 /* cmd_mutex is held by vub300_mmc_request */
1805 u8 cmd0
= 0xFF & (cmd
->arg
>> 24);
1806 u8 cmd1
= 0xFF & (cmd
->arg
>> 16);
1807 u8 cmd2
= 0xFF & (cmd
->arg
>> 8);
1808 u8 cmd3
= 0xFF & (cmd
->arg
>> 0);
1809 int first
= MAXREGMASK
& vub300
->fn
[Function
].offload_point
;
1810 struct offload_registers_access
*rf
= &vub300
->fn
[Function
].reg
[first
];
1811 if (cmd0
== rf
->command_byte
[0] &&
1812 cmd1
== rf
->command_byte
[1] &&
1813 cmd2
== rf
->command_byte
[2] &&
1814 cmd3
== rf
->command_byte
[3]) {
1816 cmd
->resp
[1] = checksum
<< 24;
1817 cmd
->resp
[0] = (rf
->Respond_Byte
[0] << 24)
1818 | (rf
->Respond_Byte
[1] << 16)
1819 | (rf
->Respond_Byte
[2] << 8)
1820 | (rf
->Respond_Byte
[3] << 0);
1821 vub300
->fn
[Function
].offload_point
+= 1;
1822 vub300
->fn
[Function
].offload_count
-= 1;
1823 vub300
->total_offload_count
-= 1;
1826 int delta
= 1; /* because it does not match the first one */
1827 u8 register_count
= vub300
->fn
[Function
].offload_count
- 1;
1828 u32 register_point
= vub300
->fn
[Function
].offload_point
+ 1;
1829 while (0 < register_count
) {
1830 int point
= MAXREGMASK
& register_point
;
1831 struct offload_registers_access
*r
=
1832 &vub300
->fn
[Function
].reg
[point
];
1833 if (cmd0
== r
->command_byte
[0] &&
1834 cmd1
== r
->command_byte
[1] &&
1835 cmd2
== r
->command_byte
[2] &&
1836 cmd3
== r
->command_byte
[3]) {
1838 cmd
->resp
[1] = checksum
<< 24;
1839 cmd
->resp
[0] = (r
->Respond_Byte
[0] << 24)
1840 | (r
->Respond_Byte
[1] << 16)
1841 | (r
->Respond_Byte
[2] << 8)
1842 | (r
->Respond_Byte
[3] << 0);
1843 vub300
->fn
[Function
].offload_point
+= delta
;
1844 vub300
->fn
[Function
].offload_count
-= delta
;
1845 vub300
->total_offload_count
-= delta
;
1848 register_point
+= 1;
1849 register_count
-= 1;
1858 static int satisfy_request_from_offloaded_data(struct vub300_mmc_host
*vub300
,
1859 struct mmc_command
*cmd
)
1861 /* cmd_mutex is held by vub300_mmc_request */
1862 u8 regs
= vub300
->dynamic_register_count
;
1866 while (0 < regs
--) {
1867 if ((vub300
->sdio_register
[i
].func_num
== func
) &&
1868 (vub300
->sdio_register
[i
].sdio_reg
== reg
)) {
1869 if (!vub300
->sdio_register
[i
].prepared
) {
1871 } else if ((0x80000000 & cmd
->arg
) == 0x80000000) {
1873 * a write to a dynamic register
1874 * nullifies our offloaded value
1876 vub300
->sdio_register
[i
].prepared
= 0;
1882 u8 rsp2
= vub300
->sdio_register
[i
].response
;
1883 u8 rsp3
= vub300
->sdio_register
[i
].regvalue
;
1884 vub300
->sdio_register
[i
].prepared
= 0;
1885 cmd
->resp
[1] = checksum
<< 24;
1886 cmd
->resp
[0] = (rsp0
<< 24)
1897 if (vub300
->total_offload_count
== 0)
1899 else if (vub300
->fn
[func
].offload_count
== 0)
1902 return examine_cyclic_buffer(vub300
, cmd
, func
);
1905 static void vub300_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
1907 struct mmc_command
*cmd
= req
->cmd
;
1908 struct vub300_mmc_host
*vub300
= mmc_priv(mmc
);
1909 if (!vub300
->interface
) {
1910 cmd
->error
= -ESHUTDOWN
;
1911 mmc_request_done(mmc
, req
);
1914 struct mmc_data
*data
= req
->data
;
1915 if (!vub300
->card_powered
) {
1916 cmd
->error
= -ENOMEDIUM
;
1917 mmc_request_done(mmc
, req
);
1920 if (!vub300
->card_present
) {
1921 cmd
->error
= -ENOMEDIUM
;
1922 mmc_request_done(mmc
, req
);
1925 if (vub300
->usb_transport_fail
) {
1926 cmd
->error
= vub300
->usb_transport_fail
;
1927 mmc_request_done(mmc
, req
);
1930 if (!vub300
->interface
) {
1931 cmd
->error
= -ENODEV
;
1932 mmc_request_done(mmc
, req
);
1935 kref_get(&vub300
->kref
);
1936 mutex_lock(&vub300
->cmd_mutex
);
1937 mod_timer(&vub300
->inactivity_timer
, jiffies
+ HZ
);
1939 * for performance we have to return immediately
1940 * if the requested data has been offloaded
1942 if (cmd
->opcode
== 52 &&
1943 satisfy_request_from_offloaded_data(vub300
, cmd
)) {
1945 mutex_unlock(&vub300
->cmd_mutex
);
1946 kref_put(&vub300
->kref
, vub300_delete
);
1947 mmc_request_done(mmc
, req
);
1952 vub300
->data
= data
;
1954 vub300
->datasize
= data
->blksz
* data
->blocks
;
1956 vub300
->datasize
= 0;
1957 vub300_queue_cmnd_work(vub300
);
1958 mutex_unlock(&vub300
->cmd_mutex
);
1959 kref_put(&vub300
->kref
, vub300_delete
);
1961 * the kernel lock diagnostics complain
1962 * if the cmd_mutex * is "passed on"
1963 * to the cmndwork thread,
1964 * so we must release it now
1965 * and re-acquire it in the cmndwork thread
1971 static void __set_clock_speed(struct vub300_mmc_host
*vub300
, u8 buf
[8],
1972 struct mmc_ios
*ios
)
1974 int buf_array_size
= 8; /* ARRAY_SIZE(buf) does not work !!! */
1977 if (ios
->clock
>= 48000000)
1979 else if (ios
->clock
>= 24000000)
1981 else if (ios
->clock
>= 20000000)
1983 else if (ios
->clock
>= 15000000)
1985 else if (ios
->clock
>= 200000)
1992 for (i
= 0; i
< buf_array_size
; i
++) {
1998 usb_control_msg(vub300
->udev
, usb_sndctrlpipe(vub300
->udev
, 0),
2000 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2001 0x00, 0x00, buf
, buf_array_size
, HZ
);
2003 dev_err(&vub300
->udev
->dev
, "SET_CLOCK_SPEED"
2004 " %dkHz failed with retval=%d\n", kHzClock
, retval
);
2006 dev_dbg(&vub300
->udev
->dev
, "SET_CLOCK_SPEED"
2007 " %dkHz\n", kHzClock
);
2011 static void vub300_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
2013 struct vub300_mmc_host
*vub300
= mmc_priv(mmc
);
2014 if (!vub300
->interface
)
2016 kref_get(&vub300
->kref
);
2017 mutex_lock(&vub300
->cmd_mutex
);
2018 if ((ios
->power_mode
== MMC_POWER_OFF
) && vub300
->card_powered
) {
2019 vub300
->card_powered
= 0;
2020 usb_control_msg(vub300
->udev
, usb_sndctrlpipe(vub300
->udev
, 0),
2022 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2023 0x0000, 0x0000, NULL
, 0, HZ
);
2024 /* must wait for the VUB300 u-proc to boot up */
2026 } else if ((ios
->power_mode
== MMC_POWER_UP
) && !vub300
->card_powered
) {
2027 usb_control_msg(vub300
->udev
, usb_sndctrlpipe(vub300
->udev
, 0),
2029 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2030 0x0001, 0x0000, NULL
, 0, HZ
);
2032 vub300
->card_powered
= 1;
2033 } else if (ios
->power_mode
== MMC_POWER_ON
) {
2034 u8
*buf
= kmalloc(8, GFP_KERNEL
);
2036 __set_clock_speed(vub300
, buf
, ios
);
2040 /* this should mean no change of state */
2042 mutex_unlock(&vub300
->cmd_mutex
);
2043 kref_put(&vub300
->kref
, vub300_delete
);
2046 static int vub300_mmc_get_ro(struct mmc_host
*mmc
)
2048 struct vub300_mmc_host
*vub300
= mmc_priv(mmc
);
2049 return vub300
->read_only
;
2052 static void vub300_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
2054 struct vub300_mmc_host
*vub300
= mmc_priv(mmc
);
2055 if (!vub300
->interface
)
2057 kref_get(&vub300
->kref
);
2059 mutex_lock(&vub300
->irq_mutex
);
2060 if (vub300
->irqs_queued
) {
2061 vub300
->irqs_queued
-= 1;
2062 mmc_signal_sdio_irq(vub300
->mmc
);
2063 } else if (vub300
->irq_disabled
) {
2064 vub300
->irq_disabled
= 0;
2065 vub300
->irq_enabled
= 1;
2066 vub300_queue_poll_work(vub300
, 0);
2067 } else if (vub300
->irq_enabled
) {
2068 /* this should not happen, so we will just ignore it */
2070 vub300
->irq_enabled
= 1;
2071 vub300_queue_poll_work(vub300
, 0);
2073 mutex_unlock(&vub300
->irq_mutex
);
2075 vub300
->irq_enabled
= 0;
2077 kref_put(&vub300
->kref
, vub300_delete
);
2080 static void vub300_init_card(struct mmc_host
*mmc
, struct mmc_card
*card
)
2082 struct vub300_mmc_host
*vub300
= mmc_priv(mmc
);
2083 dev_info(&vub300
->udev
->dev
, "NO host QUIRKS for this card\n");
2086 static struct mmc_host_ops vub300_mmc_ops
= {
2087 .request
= vub300_mmc_request
,
2088 .set_ios
= vub300_mmc_set_ios
,
2089 .get_ro
= vub300_mmc_get_ro
,
2090 .enable_sdio_irq
= vub300_enable_sdio_irq
,
2091 .init_card
= vub300_init_card
,
2094 static int vub300_probe(struct usb_interface
*interface
,
2095 const struct usb_device_id
*id
)
2097 struct vub300_mmc_host
*vub300
;
2098 struct usb_host_interface
*iface_desc
;
2099 struct usb_device
*udev
= usb_get_dev(interface_to_usbdev(interface
));
2101 int retval
= -ENOMEM
;
2102 struct urb
*command_out_urb
;
2103 struct urb
*command_res_urb
;
2104 struct mmc_host
*mmc
;
2105 char manufacturer
[48];
2107 char serial_number
[32];
2108 usb_string(udev
, udev
->descriptor
.iManufacturer
, manufacturer
,
2109 sizeof(manufacturer
));
2110 usb_string(udev
, udev
->descriptor
.iProduct
, product
, sizeof(product
));
2111 usb_string(udev
, udev
->descriptor
.iSerialNumber
, serial_number
,
2112 sizeof(serial_number
));
2113 dev_info(&udev
->dev
, "probing VID:PID(%04X:%04X) %s %s %s\n",
2114 udev
->descriptor
.idVendor
, udev
->descriptor
.idProduct
,
2115 manufacturer
, product
, serial_number
);
2116 command_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2117 if (!command_out_urb
) {
2121 command_res_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2122 if (!command_res_urb
) {
2126 /* this also allocates memory for our VUB300 mmc host device */
2127 mmc
= mmc_alloc_host(sizeof(struct vub300_mmc_host
), &udev
->dev
);
2130 dev_err(&udev
->dev
, "not enough memory for the mmc_host\n");
2133 /* MMC core transfer sizes tunable parameters */
2135 if (!force_1_bit_data_xfers
)
2136 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
2137 if (!force_polling_for_irqs
)
2138 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
2139 mmc
->caps
&= ~MMC_CAP_NEEDS_POLL
;
2141 * MMC_CAP_NEEDS_POLL causes core.c:mmc_rescan() to poll
2142 * for devices which results in spurious CMD7's being
2143 * issued which stops some SDIO cards from working
2145 if (limit_speed_to_24_MHz
) {
2146 mmc
->caps
|= MMC_CAP_MMC_HIGHSPEED
;
2147 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
2148 mmc
->f_max
= 24000000;
2149 dev_info(&udev
->dev
, "limiting SDIO speed to 24_MHz\n");
2151 mmc
->caps
|= MMC_CAP_MMC_HIGHSPEED
;
2152 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
2153 mmc
->f_max
= 48000000;
2155 mmc
->f_min
= 200000;
2156 mmc
->max_blk_count
= 511;
2157 mmc
->max_blk_size
= 512;
2158 mmc
->max_segs
= 128;
2159 if (force_max_req_size
)
2160 mmc
->max_req_size
= force_max_req_size
* 1024;
2162 mmc
->max_req_size
= 64 * 1024;
2163 mmc
->max_seg_size
= mmc
->max_req_size
;
2165 mmc
->ocr_avail
|= MMC_VDD_165_195
;
2166 mmc
->ocr_avail
|= MMC_VDD_20_21
;
2167 mmc
->ocr_avail
|= MMC_VDD_21_22
;
2168 mmc
->ocr_avail
|= MMC_VDD_22_23
;
2169 mmc
->ocr_avail
|= MMC_VDD_23_24
;
2170 mmc
->ocr_avail
|= MMC_VDD_24_25
;
2171 mmc
->ocr_avail
|= MMC_VDD_25_26
;
2172 mmc
->ocr_avail
|= MMC_VDD_26_27
;
2173 mmc
->ocr_avail
|= MMC_VDD_27_28
;
2174 mmc
->ocr_avail
|= MMC_VDD_28_29
;
2175 mmc
->ocr_avail
|= MMC_VDD_29_30
;
2176 mmc
->ocr_avail
|= MMC_VDD_30_31
;
2177 mmc
->ocr_avail
|= MMC_VDD_31_32
;
2178 mmc
->ocr_avail
|= MMC_VDD_32_33
;
2179 mmc
->ocr_avail
|= MMC_VDD_33_34
;
2180 mmc
->ocr_avail
|= MMC_VDD_34_35
;
2181 mmc
->ocr_avail
|= MMC_VDD_35_36
;
2182 mmc
->ops
= &vub300_mmc_ops
;
2183 vub300
= mmc_priv(mmc
);
2185 vub300
->card_powered
= 0;
2186 vub300
->bus_width
= 0;
2187 vub300
->cmnd
.head
.block_size
[0] = 0x00;
2188 vub300
->cmnd
.head
.block_size
[1] = 0x00;
2189 vub300
->app_spec
= 0;
2190 mutex_init(&vub300
->cmd_mutex
);
2191 mutex_init(&vub300
->irq_mutex
);
2192 vub300
->command_out_urb
= command_out_urb
;
2193 vub300
->command_res_urb
= command_res_urb
;
2194 vub300
->usb_timed_out
= 0;
2195 vub300
->dynamic_register_count
= 0;
2197 for (i
= 0; i
< ARRAY_SIZE(vub300
->fn
); i
++) {
2198 vub300
->fn
[i
].offload_point
= 0;
2199 vub300
->fn
[i
].offload_count
= 0;
2202 vub300
->total_offload_count
= 0;
2203 vub300
->irq_enabled
= 0;
2204 vub300
->irq_disabled
= 0;
2205 vub300
->irqs_queued
= 0;
2207 for (i
= 0; i
< ARRAY_SIZE(vub300
->sdio_register
); i
++)
2208 vub300
->sdio_register
[i
++].activate
= 0;
2210 vub300
->udev
= udev
;
2211 vub300
->interface
= interface
;
2212 vub300
->cmnd_res_ep
= 0;
2213 vub300
->cmnd_out_ep
= 0;
2214 vub300
->data_inp_ep
= 0;
2215 vub300
->data_out_ep
= 0;
2217 for (i
= 0; i
< ARRAY_SIZE(vub300
->fbs
); i
++)
2218 vub300
->fbs
[i
] = 512;
2221 * set up the endpoint information
2223 * use the first pair of bulk-in and bulk-out
2224 * endpoints for Command/Response+Interrupt
2226 * use the second pair of bulk-in and bulk-out
2227 * endpoints for Data In/Out
2229 vub300
->large_usb_packets
= 0;
2230 iface_desc
= interface
->cur_altsetting
;
2231 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2232 struct usb_endpoint_descriptor
*endpoint
=
2233 &iface_desc
->endpoint
[i
].desc
;
2234 dev_info(&vub300
->udev
->dev
,
2235 "vub300 testing %s EndPoint(%d) %02X\n",
2236 usb_endpoint_is_bulk_in(endpoint
) ? "BULK IN" :
2237 usb_endpoint_is_bulk_out(endpoint
) ? "BULK OUT" :
2238 "UNKNOWN", i
, endpoint
->bEndpointAddress
);
2239 if (endpoint
->wMaxPacketSize
> 64)
2240 vub300
->large_usb_packets
= 1;
2241 if (usb_endpoint_is_bulk_in(endpoint
)) {
2242 if (!vub300
->cmnd_res_ep
) {
2243 vub300
->cmnd_res_ep
=
2244 endpoint
->bEndpointAddress
;
2245 } else if (!vub300
->data_inp_ep
) {
2246 vub300
->data_inp_ep
=
2247 endpoint
->bEndpointAddress
;
2249 dev_warn(&vub300
->udev
->dev
,
2251 " unexpected bulk_in endpoint");
2253 } else if (usb_endpoint_is_bulk_out(endpoint
)) {
2254 if (!vub300
->cmnd_out_ep
) {
2255 vub300
->cmnd_out_ep
=
2256 endpoint
->bEndpointAddress
;
2257 } else if (!vub300
->data_out_ep
) {
2258 vub300
->data_out_ep
=
2259 endpoint
->bEndpointAddress
;
2261 dev_warn(&vub300
->udev
->dev
,
2263 " unexpected bulk_out endpoint");
2266 dev_warn(&vub300
->udev
->dev
,
2267 "vub300 ignoring EndPoint(%d) %02X", i
,
2268 endpoint
->bEndpointAddress
);
2271 if (vub300
->cmnd_res_ep
&& vub300
->cmnd_out_ep
&&
2272 vub300
->data_inp_ep
&& vub300
->data_out_ep
) {
2273 dev_info(&vub300
->udev
->dev
,
2275 " using EndPoints %02X %02X %02X %02X\n",
2276 vub300
->large_usb_packets
? "LARGE" : "SMALL",
2277 vub300
->cmnd_out_ep
, vub300
->cmnd_res_ep
,
2278 vub300
->data_out_ep
, vub300
->data_inp_ep
);
2279 /* we have the expected EndPoints */
2281 dev_err(&vub300
->udev
->dev
,
2282 "Could not find two sets of bulk-in/out endpoint pairs\n");
2287 usb_control_msg(vub300
->udev
, usb_rcvctrlpipe(vub300
->udev
, 0),
2289 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2290 0x0000, 0x0000, &vub300
->hc_info
,
2291 sizeof(vub300
->hc_info
), HZ
);
2295 usb_control_msg(vub300
->udev
, usb_rcvctrlpipe(vub300
->udev
, 0),
2296 SET_ROM_WAIT_STATES
,
2297 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2298 firmware_rom_wait_states
, 0x0000, NULL
, 0, HZ
);
2301 dev_info(&vub300
->udev
->dev
,
2302 "operating_mode = %s %s %d MHz %s %d byte USB packets\n",
2303 (mmc
->caps
& MMC_CAP_SDIO_IRQ
) ? "IRQs" : "POLL",
2304 (mmc
->caps
& MMC_CAP_4_BIT_DATA
) ? "4-bit" : "1-bit",
2305 mmc
->f_max
/ 1000000,
2306 pad_input_to_usb_pkt
? "padding input data to" : "with",
2307 vub300
->large_usb_packets
? 512 : 64);
2309 usb_control_msg(vub300
->udev
, usb_rcvctrlpipe(vub300
->udev
, 0),
2310 GET_SYSTEM_PORT_STATUS
,
2311 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2312 0x0000, 0x0000, &vub300
->system_port_status
,
2313 sizeof(vub300
->system_port_status
), HZ
);
2316 } else if (sizeof(vub300
->system_port_status
) == retval
) {
2317 vub300
->card_present
=
2318 (0x0001 & vub300
->system_port_status
.port_flags
) ? 1 : 0;
2320 (0x0010 & vub300
->system_port_status
.port_flags
) ? 1 : 0;
2324 usb_set_intfdata(interface
, vub300
);
2325 INIT_DELAYED_WORK(&vub300
->pollwork
, vub300_pollwork_thread
);
2326 INIT_WORK(&vub300
->cmndwork
, vub300_cmndwork_thread
);
2327 INIT_WORK(&vub300
->deadwork
, vub300_deadwork_thread
);
2328 kref_init(&vub300
->kref
);
2329 init_timer(&vub300
->sg_transfer_timer
);
2330 vub300
->sg_transfer_timer
.data
= (unsigned long)vub300
;
2331 vub300
->sg_transfer_timer
.function
= vub300_sg_timed_out
;
2332 kref_get(&vub300
->kref
);
2333 init_timer(&vub300
->inactivity_timer
);
2334 vub300
->inactivity_timer
.data
= (unsigned long)vub300
;
2335 vub300
->inactivity_timer
.function
= vub300_inactivity_timer_expired
;
2336 vub300
->inactivity_timer
.expires
= jiffies
+ HZ
;
2337 add_timer(&vub300
->inactivity_timer
);
2338 if (vub300
->card_present
)
2339 dev_info(&vub300
->udev
->dev
,
2340 "USB vub300 remote SDIO host controller[%d]"
2341 "connected with SD/SDIO card inserted\n",
2342 interface_to_InterfaceNumber(interface
));
2344 dev_info(&vub300
->udev
->dev
,
2345 "USB vub300 remote SDIO host controller[%d]"
2346 "connected with no SD/SDIO card inserted\n",
2347 interface_to_InterfaceNumber(interface
));
2353 * and hence also frees vub300
2354 * which is contained at the end of struct mmc
2357 usb_free_urb(command_res_urb
);
2359 usb_free_urb(command_out_urb
);
2365 static void vub300_disconnect(struct usb_interface
*interface
)
2367 struct vub300_mmc_host
*vub300
= usb_get_intfdata(interface
);
2368 if (!vub300
|| !vub300
->mmc
) {
2371 struct mmc_host
*mmc
= vub300
->mmc
;
2375 int ifnum
= interface_to_InterfaceNumber(interface
);
2376 usb_set_intfdata(interface
, NULL
);
2377 /* prevent more I/O from starting */
2378 vub300
->interface
= NULL
;
2379 kref_put(&vub300
->kref
, vub300_delete
);
2380 mmc_remove_host(mmc
);
2381 pr_info("USB vub300 remote SDIO host controller[%d]"
2382 " now disconnected", ifnum
);
2389 static int vub300_suspend(struct usb_interface
*intf
, pm_message_t message
)
2394 static int vub300_resume(struct usb_interface
*intf
)
2399 #define vub300_suspend NULL
2400 #define vub300_resume NULL
2402 static int vub300_pre_reset(struct usb_interface
*intf
)
2404 struct vub300_mmc_host
*vub300
= usb_get_intfdata(intf
);
2405 mutex_lock(&vub300
->cmd_mutex
);
2409 static int vub300_post_reset(struct usb_interface
*intf
)
2411 struct vub300_mmc_host
*vub300
= usb_get_intfdata(intf
);
2412 /* we are sure no URBs are active - no locking needed */
2413 vub300
->errors
= -EPIPE
;
2414 mutex_unlock(&vub300
->cmd_mutex
);
2418 static struct usb_driver vub300_driver
= {
2420 .probe
= vub300_probe
,
2421 .disconnect
= vub300_disconnect
,
2422 .suspend
= vub300_suspend
,
2423 .resume
= vub300_resume
,
2424 .pre_reset
= vub300_pre_reset
,
2425 .post_reset
= vub300_post_reset
,
2426 .id_table
= vub300_table
,
2427 .supports_autosuspend
= 1,
2430 static int __init
vub300_init(void)
2434 pr_info("VUB300 Driver rom wait states = %02X irqpoll timeout = %04X",
2435 firmware_rom_wait_states
, 0x0FFFF & firmware_irqpoll_timeout
);
2436 cmndworkqueue
= create_singlethread_workqueue("kvub300c");
2437 if (!cmndworkqueue
) {
2438 pr_err("not enough memory for the REQUEST workqueue");
2442 pollworkqueue
= create_singlethread_workqueue("kvub300p");
2443 if (!pollworkqueue
) {
2444 pr_err("not enough memory for the IRQPOLL workqueue");
2448 deadworkqueue
= create_singlethread_workqueue("kvub300d");
2449 if (!deadworkqueue
) {
2450 pr_err("not enough memory for the EXPIRED workqueue");
2454 result
= usb_register(&vub300_driver
);
2456 pr_err("usb_register failed. Error number %d", result
);
2461 destroy_workqueue(deadworkqueue
);
2463 destroy_workqueue(pollworkqueue
);
2465 destroy_workqueue(cmndworkqueue
);
2470 static void __exit
vub300_exit(void)
2472 usb_deregister(&vub300_driver
);
2473 flush_workqueue(cmndworkqueue
);
2474 flush_workqueue(pollworkqueue
);
2475 flush_workqueue(deadworkqueue
);
2476 destroy_workqueue(cmndworkqueue
);
2477 destroy_workqueue(pollworkqueue
);
2478 destroy_workqueue(deadworkqueue
);
2481 module_init(vub300_init
);
2482 module_exit(vub300_exit
);
2484 MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
2485 MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
2486 MODULE_LICENSE("GPL");