1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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>
55
56struct host_controller_info {
57 u8 info_size;
58 u16 firmware_version;
59 u8 number_of_ports;
60} __packed;
61
62#define FIRMWARE_BLOCK_BOUNDARY 1024
63struct sd_command_header {
64 u8 header_size;
65 u8 header_type;
66 u8 port_number;
67 u8 command_type;
68 u8 command_index;
69 u8 transfer_size[4];
70 u8 response_type;
71 u8 arguments[4];
72 u8 block_count[2];
73 u8 block_size[2];
74 u8 block_boundary[2];
75 u8 reserved[44];
76} __packed;
77
78struct sd_irqpoll_header {
79 u8 header_size;
80 u8 header_type;
81 u8 port_number;
82 u8 command_type;
83 u8 padding[16];
84 u8 poll_timeout_msb;
85 u8 poll_timeout_lsb;
86 u8 reserved[42];
87} __packed;
88
89struct sd_common_header {
90 u8 header_size;
91 u8 header_type;
92 u8 port_number;
93} __packed;
94
95struct sd_response_header {
96 u8 header_size;
97 u8 header_type;
98 u8 port_number;
99 u8 command_type;
100 u8 command_index;
101 u8 command_response[0];
102} __packed;
103
104struct sd_status_header {
105 u8 header_size;
106 u8 header_type;
107 u8 port_number;
108 u16 port_flags;
109 u32 sdio_clock;
110 u16 host_header_size;
111 u16 func_header_size;
112 u16 ctrl_header_size;
113} __packed;
114
115struct sd_error_header {
116 u8 header_size;
117 u8 header_type;
118 u8 port_number;
119 u8 error_code;
120} __packed;
121
122struct sd_interrupt_header {
123 u8 header_size;
124 u8 header_type;
125 u8 port_number;
126} __packed;
127
128struct offload_registers_access {
129 u8 command_byte[4];
130 u8 Respond_Byte[4];
131} __packed;
132
133#define INTERRUPT_REGISTER_ACCESSES 15
134struct sd_offloaded_interrupt {
135 u8 header_size;
136 u8 header_type;
137 u8 port_number;
138 struct offload_registers_access reg[INTERRUPT_REGISTER_ACCESSES];
139} __packed;
140
141struct sd_register_header {
142 u8 header_size;
143 u8 header_type;
144 u8 port_number;
145 u8 command_type;
146 u8 command_index;
147 u8 command_response[6];
148} __packed;
149
150#define PIGGYBACK_REGISTER_ACCESSES 14
151struct sd_offloaded_piggyback {
152 struct sd_register_header sdio;
153 struct offload_registers_access reg[PIGGYBACK_REGISTER_ACCESSES];
154} __packed;
155
156union sd_response {
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;
164} __packed;
165
166union sd_command {
167 struct sd_command_header head;
168 struct sd_irqpoll_header poll;
169} __packed;
170
171enum SD_RESPONSE_TYPE {
172 SDRT_UNSPECIFIED = 0,
173 SDRT_NONE,
174 SDRT_1,
175 SDRT_1B,
176 SDRT_2,
177 SDRT_3,
178 SDRT_4,
179 SDRT_5,
180 SDRT_5B,
181 SDRT_6,
182 SDRT_7,
183};
184
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
222
223#define FUN(c) (0x000007 & (c->arg>>28))
224#define REG(c) (0x01FFFF & (c->arg>>9))
225
226static bool limit_speed_to_24_MHz;
227module_param(limit_speed_to_24_MHz, bool, 0644);
228MODULE_PARM_DESC(limit_speed_to_24_MHz, "Limit Max SDIO Clock Speed to 24 MHz");
229
230static bool pad_input_to_usb_pkt;
231module_param(pad_input_to_usb_pkt, bool, 0644);
232MODULE_PARM_DESC(pad_input_to_usb_pkt,
233 "Pad USB data input transfers to whole USB Packet");
234
235static bool disable_offload_processing;
236module_param(disable_offload_processing, bool, 0644);
237MODULE_PARM_DESC(disable_offload_processing, "Disable Offload Processing");
238
239static bool force_1_bit_data_xfers;
240module_param(force_1_bit_data_xfers, bool, 0644);
241MODULE_PARM_DESC(force_1_bit_data_xfers,
242 "Force SDIO Data Transfers to 1-bit Mode");
243
244static bool force_polling_for_irqs;
245module_param(force_polling_for_irqs, bool, 0644);
246MODULE_PARM_DESC(force_polling_for_irqs, "Force Polling for SDIO interrupts");
247
248static int firmware_irqpoll_timeout = 1024;
249module_param(firmware_irqpoll_timeout, int, 0644);
250MODULE_PARM_DESC(firmware_irqpoll_timeout, "VUB300 firmware irqpoll timeout");
251
252static int force_max_req_size = 128;
253module_param(force_max_req_size, int, 0644);
254MODULE_PARM_DESC(force_max_req_size, "set max request size in kBytes");
255
256#ifdef SMSC_DEVELOPMENT_BOARD
257static int firmware_rom_wait_states = 0x04;
258#else
259static int firmware_rom_wait_states = 0x1C;
260#endif
261
262module_param(firmware_rom_wait_states, int, 0644);
263MODULE_PARM_DESC(firmware_rom_wait_states,
264 "ROM wait states byte=RRRIIEEE (Reserved Internal External)");
265
266#define ELAN_VENDOR_ID 0x2201
267#define VUB300_VENDOR_ID 0x0424
268#define VUB300_PRODUCT_ID 0x012C
269static 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 {}
273};
274MODULE_DEVICE_TABLE(usb, vub300_table);
275
276static struct workqueue_struct *cmndworkqueue;
277static struct workqueue_struct *pollworkqueue;
278static struct workqueue_struct *deadworkqueue;
279
280static inline int interface_to_InterfaceNumber(struct usb_interface *interface)
281{
282 if (!interface)
283 return -1;
284 if (!interface->cur_altsetting)
285 return -1;
286 return interface->cur_altsetting->desc.bInterfaceNumber;
287}
288
289struct sdio_register {
290 unsigned func_num:3;
291 unsigned sdio_reg:17;
292 unsigned activate:1;
293 unsigned prepared:1;
294 unsigned regvalue:8;
295 unsigned response:8;
296 unsigned sparebit:26;
297};
298
299struct vub300_mmc_host {
300 struct usb_device *udev;
301 struct usb_interface *interface;
302 struct kref kref;
303 struct mutex cmd_mutex;
304 struct mutex irq_mutex;
305 char vub_name[3 + (9 * 8) + 4 + 1];
306 u8 cmnd_out_ep;
307 u8 cmnd_res_ep;
308 u8 data_out_ep;
309 u8 data_inp_ep;
310 bool card_powered;
311 bool card_present;
312 bool read_only;
313 bool large_usb_packets;
314 bool app_spec;
315 bool irq_enabled;
316 bool irq_disabled;
317 unsigned bus_width:4;
318 u8 total_offload_count;
319 u8 dynamic_register_count;
320 u8 resp_len;
321 u32 datasize;
322 int errors;
323 int usb_transport_fail;
324 int usb_timed_out;
325 int irqs_queued;
326 struct sdio_register sdio_register[16];
327 struct offload_interrupt_function_register {
328#define MAXREGBITS 4
329#define MAXREGS (1<<MAXREGBITS)
330#define MAXREGMASK (MAXREGS-1)
331 u8 offload_count;
332 u32 offload_point;
333 struct offload_registers_access reg[MAXREGS];
334 } fn[8];
335 u16 fbs[8];
336 struct mmc_command *cmd;
337 struct mmc_request *req;
338 struct mmc_data *data;
339 struct mmc_host *mmc;
340 struct urb *urb;
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];
356};
357
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
371
372static void vub300_delete(struct kref *kref)
373{
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);
381 mmc_free_host(mmc);
382
383
384
385
386}
387
388static void vub300_queue_cmnd_work(struct vub300_mmc_host *vub300)
389{
390 kref_get(&vub300->kref);
391 if (queue_work(cmndworkqueue, &vub300->cmndwork)) {
392
393
394
395
396
397
398 } else {
399
400
401
402
403
404 kref_put(&vub300->kref, vub300_delete);
405 }
406}
407
408static void vub300_queue_poll_work(struct vub300_mmc_host *vub300, int delay)
409{
410 kref_get(&vub300->kref);
411 if (queue_delayed_work(pollworkqueue, &vub300->pollwork, delay)) {
412
413
414
415
416
417
418 } else {
419
420
421
422
423
424 kref_put(&vub300->kref, vub300_delete);
425 }
426}
427
428static void vub300_queue_dead_work(struct vub300_mmc_host *vub300)
429{
430 kref_get(&vub300->kref);
431 if (queue_work(deadworkqueue, &vub300->deadwork)) {
432
433
434
435
436
437
438 } else {
439
440
441
442
443
444 kref_put(&vub300->kref, vub300_delete);
445 }
446}
447
448static void irqpoll_res_completed(struct urb *urb)
449{
450 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
451 if (urb->status)
452 vub300->usb_transport_fail = urb->status;
453 complete(&vub300->irqpoll_complete);
454}
455
456static void irqpoll_out_completed(struct urb *urb)
457{
458 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
459 if (urb->status) {
460 vub300->usb_transport_fail = urb->status;
461 complete(&vub300->irqpoll_complete);
462 return;
463 } else {
464 int ret;
465 unsigned int pipe =
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);
472 if (ret) {
473 vub300->usb_transport_fail = ret;
474 complete(&vub300->irqpoll_complete);
475 }
476 return;
477 }
478}
479
480static void send_irqpoll(struct vub300_mmc_host *vub300)
481{
482
483 int retval;
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);
496 if (0 > retval) {
497 vub300->usb_transport_fail = retval;
498 vub300_queue_poll_work(vub300, 1);
499 complete(&vub300->irqpoll_complete);
500 return;
501 } else {
502 return;
503 }
504}
505
506static void new_system_port_status(struct vub300_mmc_host *vub300)
507{
508 int old_card_present = vub300->card_present;
509 int new_card_present =
510 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
511 vub300->read_only =
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));
520 else
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);
527 } else {
528
529 }
530}
531
532static void __add_offloaded_reg_to_fifo(struct vub300_mmc_host *vub300,
533 struct offload_registers_access
534 *register_access, u8 func)
535{
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;
541}
542
543static void add_offloaded_reg(struct vub300_mmc_host *vub300,
544 struct offload_registers_access *register_access)
545{
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;
551 u8 i = 0;
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];
561 return;
562 } else {
563 i += 1;
564 continue;
565 }
566 }
567 __add_offloaded_reg_to_fifo(vub300, register_access, func);
568}
569
570static void check_vub300_port_status(struct vub300_mmc_host *vub300)
571{
572
573
574
575
576 int retval;
577 retval =
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);
585}
586
587static void __vub300_irqpoll_response(struct vub300_mmc_host *vub300)
588{
589
590 if (vub300->command_res_urb->actual_length == 0)
591 return;
592
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);
598 else
599 vub300->irqs_queued += 1;
600 vub300->irq_disabled = 1;
601 mutex_unlock(&vub300->irq_mutex);
602 break;
603 case RESPONSE_ERROR:
604 if (vub300->resp.error.error_code == SD_ERROR_NO_DEVICE)
605 check_vub300_port_status(vub300);
606 break;
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);
612 break;
613 case RESPONSE_IRQ_DISABLED:
614 {
615 int offloaded_data_length = vub300->resp.common.header_size - 3;
616 int register_count = offloaded_data_length >> 3;
617 int ri = 0;
618 while (register_count--) {
619 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
620 ri += 1;
621 }
622 mutex_lock(&vub300->irq_mutex);
623 if (vub300->irq_enabled)
624 mmc_signal_sdio_irq(vub300->mmc);
625 else
626 vub300->irqs_queued += 1;
627 vub300->irq_disabled = 1;
628 mutex_unlock(&vub300->irq_mutex);
629 break;
630 }
631 case RESPONSE_IRQ_ENABLED:
632 {
633 int offloaded_data_length = vub300->resp.common.header_size - 3;
634 int register_count = offloaded_data_length >> 3;
635 int ri = 0;
636 while (register_count--) {
637 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
638 ri += 1;
639 }
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;
645 else
646 vub300->irqs_queued += 1;
647 vub300->irq_disabled = 0;
648 mutex_unlock(&vub300->irq_mutex);
649 break;
650 }
651 case RESPONSE_NO_INTERRUPT:
652 vub300_queue_poll_work(vub300, 1);
653 break;
654 default:
655 break;
656 }
657}
658
659static void __do_poll(struct vub300_mmc_host *vub300)
660{
661
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
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 {
675 __vub300_irqpoll_response(vub300);
676 }
677}
678
679
680
681
682static void vub300_pollwork_thread(struct work_struct *work)
683{
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);
688 return;
689 }
690 mutex_lock(&vub300->cmd_mutex);
691 if (vub300->cmd) {
692 vub300_queue_poll_work(vub300, 1);
693 } else if (!vub300->card_present) {
694
695 } else {
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 {
705 mutex_unlock(&vub300->irq_mutex);
706 __do_poll(vub300);
707 }
708 }
709 mutex_unlock(&vub300->cmd_mutex);
710 kref_put(&vub300->kref, vub300_delete);
711}
712
713static void vub300_deadwork_thread(struct work_struct *work)
714{
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);
719 return;
720 }
721 mutex_lock(&vub300->cmd_mutex);
722 if (vub300->cmd) {
723
724
725
726
727
728
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)) {
733
734
735
736
737
738
739 } else {
740 check_vub300_port_status(vub300);
741 }
742 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
743 mutex_unlock(&vub300->cmd_mutex);
744 kref_put(&vub300->kref, vub300_delete);
745}
746
747static void vub300_inactivity_timer_expired(unsigned long data)
748{
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);
754 } else {
755 vub300_queue_dead_work(vub300);
756 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
757 }
758}
759
760static int vub300_response_error(u8 error_code)
761{
762 switch (error_code) {
763 case SD_ERROR_PIO_TIMEOUT:
764 case SD_ERROR_1BIT_TIMEOUT:
765 case SD_ERROR_4BIT_TIMEOUT:
766 return -ETIMEDOUT;
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:
784 return -EILSEQ;
785 case 33:
786 return -EILSEQ;
787 case SD_ERROR_ILLEGAL_COMMAND:
788 return -EINVAL;
789 case SD_ERROR_NO_DEVICE:
790 return -ENOMEDIUM;
791 default:
792 return -ENODEV;
793 }
794}
795
796static void command_res_completed(struct urb *urb)
797{
798 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
799 if (urb->status) {
800
801 } else if (vub300->command_res_urb->actual_length == 0) {
802
803
804
805
806 } else if (!vub300->data) {
807
808 } else if (vub300->resp.common.header_type != 0x02) {
809
810
811
812
813 } else if (vub300->urb) {
814 vub300->cmd->error =
815 vub300_response_error(vub300->resp.error.error_code);
816 usb_unlink_urb(vub300->urb);
817 } else {
818 vub300->cmd->error =
819 vub300_response_error(vub300->resp.error.error_code);
820 usb_sg_cancel(&vub300->sg_request);
821 }
822 complete(&vub300->command_complete);
823}
824
825static void command_out_completed(struct urb *urb)
826{
827 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
828 if (urb->status) {
829 complete(&vub300->command_complete);
830 } else {
831 int ret;
832 unsigned int pipe =
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);
839 if (ret == 0) {
840
841
842
843
844 } else {
845
846
847
848
849 complete(&vub300->command_complete);
850 }
851 }
852}
853
854
855
856
857static void snoop_block_size_and_bus_width(struct vub300_mmc_host *vub300,
858 u32 cmd_arg)
859{
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;
892}
893
894static void send_command(struct vub300_mmc_host *vub300)
895{
896
897 struct mmc_command *cmd = vub300->cmd;
898 struct mmc_data *data = vub300->data;
899 int retval;
900 int i;
901 u8 response_type;
902 if (vub300->app_spec) {
903 switch (cmd->opcode) {
904 case 6:
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;
911 else
912 dev_err(&vub300->udev->dev,
913 "unexpected ACMD6 bus_width=%d\n",
914 0x00000003 & cmd->arg);
915 break;
916 case 13:
917 response_type = SDRT_1;
918 vub300->resp_len = 6;
919 break;
920 case 22:
921 response_type = SDRT_1;
922 vub300->resp_len = 6;
923 break;
924 case 23:
925 response_type = SDRT_1;
926 vub300->resp_len = 6;
927 break;
928 case 41:
929 response_type = SDRT_3;
930 vub300->resp_len = 6;
931 break;
932 case 42:
933 response_type = SDRT_1;
934 vub300->resp_len = 6;
935 break;
936 case 51:
937 response_type = SDRT_1;
938 vub300->resp_len = 6;
939 break;
940 case 55:
941 response_type = SDRT_1;
942 vub300->resp_len = 6;
943 break;
944 default:
945 vub300->resp_len = 0;
946 cmd->error = -EINVAL;
947 complete(&vub300->command_complete);
948 return;
949 }
950 vub300->app_spec = 0;
951 } else {
952 switch (cmd->opcode) {
953 case 0:
954 response_type = SDRT_NONE;
955 vub300->resp_len = 0;
956 break;
957 case 1:
958 response_type = SDRT_3;
959 vub300->resp_len = 6;
960 break;
961 case 2:
962 response_type = SDRT_2;
963 vub300->resp_len = 17;
964 break;
965 case 3:
966 response_type = SDRT_6;
967 vub300->resp_len = 6;
968 break;
969 case 4:
970 response_type = SDRT_NONE;
971 vub300->resp_len = 0;
972 break;
973 case 5:
974 response_type = SDRT_4;
975 vub300->resp_len = 6;
976 break;
977 case 6:
978 response_type = SDRT_1;
979 vub300->resp_len = 6;
980 break;
981 case 7:
982 response_type = SDRT_1B;
983 vub300->resp_len = 6;
984 break;
985 case 8:
986 response_type = SDRT_7;
987 vub300->resp_len = 6;
988 break;
989 case 9:
990 response_type = SDRT_2;
991 vub300->resp_len = 17;
992 break;
993 case 10:
994 response_type = SDRT_2;
995 vub300->resp_len = 17;
996 break;
997 case 12:
998 response_type = SDRT_1B;
999 vub300->resp_len = 6;
1000 break;
1001 case 13:
1002 response_type = SDRT_1;
1003 vub300->resp_len = 6;
1004 break;
1005 case 15:
1006 response_type = SDRT_NONE;
1007 vub300->resp_len = 0;
1008 break;
1009 case 16:
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;
1014 break;
1015 case 17:
1016 case 18:
1017 case 24:
1018 case 25:
1019 case 27:
1020 response_type = SDRT_1;
1021 vub300->resp_len = 6;
1022 break;
1023 case 28:
1024 case 29:
1025 response_type = SDRT_1B;
1026 vub300->resp_len = 6;
1027 break;
1028 case 30:
1029 case 32:
1030 case 33:
1031 response_type = SDRT_1;
1032 vub300->resp_len = 6;
1033 break;
1034 case 38:
1035 response_type = SDRT_1B;
1036 vub300->resp_len = 6;
1037 break;
1038 case 42:
1039 response_type = SDRT_1;
1040 vub300->resp_len = 6;
1041 break;
1042 case 52:
1043 response_type = SDRT_5;
1044 vub300->resp_len = 6;
1045 snoop_block_size_and_bus_width(vub300, cmd->arg);
1046 break;
1047 case 53:
1048 response_type = SDRT_5;
1049 vub300->resp_len = 6;
1050 break;
1051 case 55:
1052 response_type = SDRT_1;
1053 vub300->resp_len = 6;
1054 vub300->app_spec = 1;
1055 break;
1056 case 56:
1057 response_type = SDRT_1;
1058 vub300->resp_len = 6;
1059 break;
1060 default:
1061 vub300->resp_len = 0;
1062 cmd->error = -EINVAL;
1063 complete(&vub300->command_complete);
1064 return;
1065 }
1066 }
1067
1068
1069
1070
1071 vub300->cmnd.head.header_size = 20;
1072 vub300->cmnd.head.header_type = 0x00;
1073 vub300->cmnd.head.port_number = 0;
1074 vub300->cmnd.head.command_type = 0x00;
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;
1092 } else if (!data) {
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]) {
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 {
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;
1120 }
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;
1134 }
1135 } else {
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;
1153 }
1154 }
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;
1164 } else {
1165 vub300->cmnd.head.block_boundary[0] = 0;
1166 vub300->cmnd.head.block_boundary[1] = 0;
1167 }
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);
1173 if (retval < 0) {
1174 cmd->error = retval;
1175 complete(&vub300->command_complete);
1176 return;
1177 } else {
1178 return;
1179 }
1180}
1181
1182
1183
1184
1185
1186static void vub300_sg_timed_out(unsigned long data)
1187{
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);
1193}
1194
1195static u16 roundup_to_multiple_of_64(u16 number)
1196{
1197 return 0xFFC0 & (0x3F + number);
1198}
1199
1200
1201
1202
1203static void __download_offload_pseudocode(struct vub300_mmc_host *vub300,
1204 const struct firmware *fw)
1205{
1206 u8 register_count = 0;
1207 u16 ts = 0;
1208 u16 interrupt_size = 0;
1209 const u8 *data = fw->data;
1210 int size = fw->size;
1211 u8 c;
1212 dev_info(&vub300->udev->dev, "using %s for SDIO offload processing\n",
1213 vub300->vub_name);
1214 do {
1215 c = *data++;
1216 } while (size-- && c);
1217 dev_info(&vub300->udev->dev, "using offload firmware %s %s\n", fw->data,
1218 vub300->vub_name);
1219 if (size < 4) {
1220 dev_err(&vub300->udev->dev,
1221 "corrupt offload pseudocode in firmware %s\n",
1222 vub300->vub_name);
1223 strncpy(vub300->vub_name, "corrupt offload pseudocode",
1224 sizeof(vub300->vub_name));
1225 return;
1226 }
1227 interrupt_size += *data++;
1228 size -= 1;
1229 interrupt_size <<= 8;
1230 interrupt_size += *data++;
1231 size -= 1;
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);
1235 if (xfer_buffer) {
1236 int retval;
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;
1242 retval =
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);
1249 kfree(xfer_buffer);
1250 if (retval < 0) {
1251 strncpy(vub300->vub_name,
1252 "SDIO pseudocode download failed",
1253 sizeof(vub300->vub_name));
1254 return;
1255 }
1256 } else {
1257 dev_err(&vub300->udev->dev,
1258 "not enough memory for xfer buffer to send"
1259 " INTERRUPT_PSEUDOCODE for %s %s\n", fw->data,
1260 vub300->vub_name);
1261 strncpy(vub300->vub_name,
1262 "SDIO interrupt pseudocode download failed",
1263 sizeof(vub300->vub_name));
1264 return;
1265 }
1266 } else {
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));
1272 return;
1273 }
1274 ts += *data++;
1275 size -= 1;
1276 ts <<= 8;
1277 ts += *data++;
1278 size -= 1;
1279 if (ts < size) {
1280 u16 xfer_length = roundup_to_multiple_of_64(ts);
1281 u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1282 if (xfer_buffer) {
1283 int retval;
1284 memcpy(xfer_buffer, data, ts);
1285 memset(xfer_buffer + ts, 0,
1286 xfer_length - ts);
1287 size -= ts;
1288 data += ts;
1289 retval =
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);
1296 kfree(xfer_buffer);
1297 if (retval < 0) {
1298 strncpy(vub300->vub_name,
1299 "SDIO pseudocode download failed",
1300 sizeof(vub300->vub_name));
1301 return;
1302 }
1303 } else {
1304 dev_err(&vub300->udev->dev,
1305 "not enough memory for xfer buffer to send"
1306 " TRANSFER_PSEUDOCODE for %s %s\n", fw->data,
1307 vub300->vub_name);
1308 strncpy(vub300->vub_name,
1309 "SDIO transfer pseudocode download failed",
1310 sizeof(vub300->vub_name));
1311 return;
1312 }
1313 } else {
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));
1319 return;
1320 }
1321 register_count += *data++;
1322 size -= 1;
1323 if (register_count * 4 == size) {
1324 int I = vub300->dynamic_register_count = register_count;
1325 int i = 0;
1326 while (I--) {
1327 unsigned int func_num = 0;
1328 vub300->sdio_register[i].func_num = *data++;
1329 size -= 1;
1330 func_num += *data++;
1331 size -= 1;
1332 func_num <<= 8;
1333 func_num += *data++;
1334 size -= 1;
1335 func_num <<= 8;
1336 func_num += *data++;
1337 size -= 1;
1338 vub300->sdio_register[i].sdio_reg = func_num;
1339 vub300->sdio_register[i].activate = 1;
1340 vub300->sdio_register[i].prepared = 0;
1341 i += 1;
1342 }
1343 dev_info(&vub300->udev->dev,
1344 "initialized %d dynamic pseudocode registers\n",
1345 vub300->dynamic_register_count);
1346 return;
1347 } else {
1348 dev_err(&vub300->udev->dev,
1349 "corrupt dynamic registers in firmware %s\n",
1350 vub300->vub_name);
1351 strncpy(vub300->vub_name, "corrupt dynamic registers",
1352 sizeof(vub300->vub_name));
1353 return;
1354 }
1355}
1356
1357
1358
1359
1360
1361static void download_offload_pseudocode(struct vub300_mmc_host *vub300)
1362{
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);
1368 int n = 0;
1369 int retval;
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);
1375 }
1376 snprintf(vub300->vub_name + l, sizeof(vub300->vub_name) - l, ".bin");
1377 dev_info(&vub300->udev->dev, "requesting offload firmware %s\n",
1378 vub300->vub_name);
1379 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1380 if (retval < 0) {
1381 strncpy(vub300->vub_name, "vub_default.bin",
1382 sizeof(vub300->vub_name));
1383 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1384 if (retval < 0) {
1385 strncpy(vub300->vub_name,
1386 "no SDIO offload firmware found",
1387 sizeof(vub300->vub_name));
1388 } else {
1389 __download_offload_pseudocode(vub300, fw);
1390 release_firmware(fw);
1391 }
1392 } else {
1393 __download_offload_pseudocode(vub300, fw);
1394 release_firmware(fw);
1395 }
1396}
1397
1398static void vub300_usb_bulk_msg_completion(struct urb *urb)
1399{
1400 complete((struct completion *)urb->context);
1401}
1402
1403static 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)
1406{
1407
1408 struct usb_device *usb_dev = vub300->udev;
1409 struct completion done;
1410 int retval;
1411 vub300->urb = usb_alloc_urb(0, GFP_KERNEL);
1412 if (!vub300->urb)
1413 return -ENOMEM;
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))
1421 goto out;
1422 if (!wait_for_completion_timeout
1423 (&done, msecs_to_jiffies(timeout_msecs))) {
1424 retval = -ETIMEDOUT;
1425 usb_kill_urb(vub300->urb);
1426 } else {
1427 retval = vub300->urb->status;
1428 }
1429out:
1430 *actual_length = vub300->urb->actual_length;
1431 usb_free_urb(vub300->urb);
1432 vub300->urb = NULL;
1433 return retval;
1434}
1435
1436static int __command_read_data(struct vub300_mmc_host *vub300,
1437 struct mmc_command *cmd, struct mmc_data *data)
1438{
1439
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) {
1445 int result;
1446 unsigned pipe;
1447 pipe = usb_rcvbulkpipe(vub300->udev, vub300->data_inp_ep);
1448 result = usb_sg_init(&vub300->sg_request, vub300->udev,
1449 pipe, 0, data->sg,
1450 data->sg_len, 0, GFP_KERNEL);
1451 if (result < 0) {
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;
1456 return 0;
1457 } else {
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;
1467 return 0;
1468 } else {
1469 data->bytes_xfered = vub300->datasize;
1470 return linear_length;
1471 }
1472 }
1473 } else {
1474 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1475 if (buf) {
1476 int result;
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));
1483 if (result < 0) {
1484 cmd->error = result;
1485 data->bytes_xfered = 0;
1486 kfree(buf);
1487 return 0;
1488 } else if (actual_length < linear_length) {
1489 cmd->error = -EREMOTEIO;
1490 data->bytes_xfered = 0;
1491 kfree(buf);
1492 return 0;
1493 } else {
1494 sg_copy_from_buffer(data->sg, data->sg_len, buf,
1495 linear_length);
1496 kfree(buf);
1497 data->bytes_xfered = vub300->datasize;
1498 return linear_length;
1499 }
1500 } else {
1501 cmd->error = -ENOMEM;
1502 data->bytes_xfered = 0;
1503 return 0;
1504 }
1505 }
1506}
1507
1508static int __command_write_data(struct vub300_mmc_host *vub300,
1509 struct mmc_command *cmd, struct mmc_data *data)
1510{
1511
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) {
1517 int result;
1518 int actual_length;
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) /
1528 16384));
1529 if (result < 0) {
1530 cmd->error = result;
1531 data->bytes_xfered = 0;
1532 } else {
1533 data->bytes_xfered = vub300->datasize;
1534 }
1535 } else if ((!vub300->large_usb_packets && (0 < modulo_64_length)) ||
1536 (vub300->large_usb_packets && (64 > modulo_512_length))
1537 ) {
1538 int padded_length = ((63 + linear_length) >> 6) << 6;
1539 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1540 if (buf) {
1541 int result;
1542 int actual_length;
1543 sg_copy_to_buffer(data->sg, data->sg_len, buf,
1544 padded_length);
1545 memset(buf + linear_length, 0,
1546 padded_length - linear_length);
1547 result =
1548 vub300_usb_bulk_msg(vub300, pipe, buf,
1549 padded_length, &actual_length,
1550 2000 + padded_length / 16384);
1551 kfree(buf);
1552 if (result < 0) {
1553 cmd->error = result;
1554 data->bytes_xfered = 0;
1555 } else {
1556 data->bytes_xfered = vub300->datasize;
1557 }
1558 } else {
1559 cmd->error = -ENOMEM;
1560 data->bytes_xfered = 0;
1561 }
1562 } else {
1563 int result;
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,
1567 pipe, 0, data->sg,
1568 data->sg_len, 0, GFP_KERNEL);
1569 if (result < 0) {
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;
1574 } else {
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);
1580 if (cmd->error) {
1581 data->bytes_xfered = 0;
1582 } else {
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;
1587 } else {
1588 data->bytes_xfered = vub300->datasize;
1589 }
1590 }
1591 }
1592 }
1593 return linear_length;
1594}
1595
1596static void __vub300_command_response(struct vub300_mmc_host *vub300,
1597 struct mmc_command *cmd,
1598 struct mmc_data *data, int data_length)
1599{
1600
1601 long respretval;
1602 int msec_timeout = 1000 + data_length / 4;
1603 respretval =
1604 wait_for_completion_timeout(&vub300->command_complete,
1605 msecs_to_jiffies(msec_timeout));
1606 if (respretval == 0) {
1607
1608 int result;
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,
1614 vub300->interface);
1615 if (result == 0) {
1616 result = usb_reset_device(vub300->udev);
1617 usb_unlock_device(vub300->udev);
1618 }
1619 } else if (respretval < 0) {
1620
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) {
1625
1626
1627
1628
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) {
1638
1639
1640
1641
1642 } else if (vub300->resp.common.header_type == RESPONSE_ERROR) {
1643 cmd->error =
1644 vub300_response_error(vub300->resp.error.error_code);
1645 if (vub300->data)
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;
1652 int ri = 0;
1653 while (register_count--) {
1654 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1655 ri += 1;
1656 }
1657 vub300->resp.common.header_size =
1658 sizeof(struct sd_register_header);
1659 vub300->resp.common.header_type = 0x00;
1660 cmd->error = 0;
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;
1666 int ri = 0;
1667 while (register_count--) {
1668 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1669 ri += 1;
1670 }
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);
1677 } else {
1678 vub300->irqs_queued += 1;
1679 }
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;
1685 cmd->error = 0;
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;
1691 int ri = 0;
1692 while (register_count--) {
1693 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1694 ri += 1;
1695 }
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);
1702 } else {
1703 vub300->irqs_queued += 1;
1704 }
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;
1710 cmd->error = 0;
1711 } else {
1712 cmd->error = -EINVAL;
1713 }
1714}
1715
1716static void construct_request_response(struct vub300_mmc_host *vub300,
1717 struct mmc_command *cmd)
1718{
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;
1724 if (bytes == 3) {
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);
1733 }
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);
1739 }
1740 if ((cmd->opcode == 53) && (0x000000FF & cmd->resp[0]))
1741 cmd->resp[0] &= 0xFFFFFF00;
1742}
1743
1744
1745static void vub300_cmndwork_thread(struct work_struct *work)
1746{
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);
1751 return;
1752 } else {
1753 struct mmc_request *req = vub300->req;
1754 struct mmc_command *cmd = vub300->cmd;
1755 struct mmc_data *data = vub300->data;
1756 int data_length;
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)) {
1761
1762
1763
1764
1765
1766 } else if (0 == vub300->mmc->card->sdio_funcs) {
1767 strncpy(vub300->vub_name, "SD memory device",
1768 sizeof(vub300->vub_name));
1769 } else {
1770 download_offload_pseudocode(vub300);
1771 }
1772 send_command(vub300);
1773 if (!data)
1774 data_length = 0;
1775 else if (MMC_DATA_READ & data->flags)
1776 data_length = __command_read_data(vub300, cmd, data);
1777 else
1778 data_length = __command_write_data(vub300, cmd, data);
1779 __vub300_command_response(vub300, cmd, data, data_length);
1780 vub300->req = NULL;
1781 vub300->cmd = NULL;
1782 vub300->data = NULL;
1783 if (cmd->error) {
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);
1789 return;
1790 } else {
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);
1796 return;
1797 }
1798 }
1799}
1800
1801static int examine_cyclic_buffer(struct vub300_mmc_host *vub300,
1802 struct mmc_command *cmd, u8 Function)
1803{
1804
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]) {
1815 u8 checksum = 0x00;
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;
1824 return 1;
1825 } else {
1826 int delta = 1;
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]) {
1837 u8 checksum = 0x00;
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;
1846 return 1;
1847 } else {
1848 register_point += 1;
1849 register_count -= 1;
1850 delta += 1;
1851 continue;
1852 }
1853 }
1854 return 0;
1855 }
1856}
1857
1858static int satisfy_request_from_offloaded_data(struct vub300_mmc_host *vub300,
1859 struct mmc_command *cmd)
1860{
1861
1862 u8 regs = vub300->dynamic_register_count;
1863 u8 i = 0;
1864 u8 func = FUN(cmd);
1865 u32 reg = REG(cmd);
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) {
1870 return 0;
1871 } else if ((0x80000000 & cmd->arg) == 0x80000000) {
1872
1873
1874
1875
1876 vub300->sdio_register[i].prepared = 0;
1877 return 0;
1878 } else {
1879 u8 checksum = 0x00;
1880 u8 rsp0 = 0x00;
1881 u8 rsp1 = 0x00;
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)
1887 | (rsp1 << 16)
1888 | (rsp2 << 8)
1889 | (rsp3 << 0);
1890 return 1;
1891 }
1892 } else {
1893 i += 1;
1894 continue;
1895 }
1896 }
1897 if (vub300->total_offload_count == 0)
1898 return 0;
1899 else if (vub300->fn[func].offload_count == 0)
1900 return 0;
1901 else
1902 return examine_cyclic_buffer(vub300, cmd, func);
1903}
1904
1905static void vub300_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1906{
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);
1912 return;
1913 } else {
1914 struct mmc_data *data = req->data;
1915 if (!vub300->card_powered) {
1916 cmd->error = -ENOMEDIUM;
1917 mmc_request_done(mmc, req);
1918 return;
1919 }
1920 if (!vub300->card_present) {
1921 cmd->error = -ENOMEDIUM;
1922 mmc_request_done(mmc, req);
1923 return;
1924 }
1925 if (vub300->usb_transport_fail) {
1926 cmd->error = vub300->usb_transport_fail;
1927 mmc_request_done(mmc, req);
1928 return;
1929 }
1930 if (!vub300->interface) {
1931 cmd->error = -ENODEV;
1932 mmc_request_done(mmc, req);
1933 return;
1934 }
1935 kref_get(&vub300->kref);
1936 mutex_lock(&vub300->cmd_mutex);
1937 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
1938
1939
1940
1941
1942 if (cmd->opcode == 52 &&
1943 satisfy_request_from_offloaded_data(vub300, cmd)) {
1944 cmd->error = 0;
1945 mutex_unlock(&vub300->cmd_mutex);
1946 kref_put(&vub300->kref, vub300_delete);
1947 mmc_request_done(mmc, req);
1948 return;
1949 } else {
1950 vub300->cmd = cmd;
1951 vub300->req = req;
1952 vub300->data = data;
1953 if (data)
1954 vub300->datasize = data->blksz * data->blocks;
1955 else
1956 vub300->datasize = 0;
1957 vub300_queue_cmnd_work(vub300);
1958 mutex_unlock(&vub300->cmd_mutex);
1959 kref_put(&vub300->kref, vub300_delete);
1960
1961
1962
1963
1964
1965
1966
1967 }
1968 }
1969}
1970
1971static void __set_clock_speed(struct vub300_mmc_host *vub300, u8 buf[8],
1972 struct mmc_ios *ios)
1973{
1974 int buf_array_size = 8;
1975 int retval;
1976 u32 kHzClock;
1977 if (ios->clock >= 48000000)
1978 kHzClock = 48000;
1979 else if (ios->clock >= 24000000)
1980 kHzClock = 24000;
1981 else if (ios->clock >= 20000000)
1982 kHzClock = 20000;
1983 else if (ios->clock >= 15000000)
1984 kHzClock = 15000;
1985 else if (ios->clock >= 200000)
1986 kHzClock = 200;
1987 else
1988 kHzClock = 0;
1989 {
1990 int i;
1991 u64 c = kHzClock;
1992 for (i = 0; i < buf_array_size; i++) {
1993 buf[i] = c;
1994 c >>= 8;
1995 }
1996 }
1997 retval =
1998 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
1999 SET_CLOCK_SPEED,
2000 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2001 0x00, 0x00, buf, buf_array_size, HZ);
2002 if (retval != 8) {
2003 dev_err(&vub300->udev->dev, "SET_CLOCK_SPEED"
2004 " %dkHz failed with retval=%d\n", kHzClock, retval);
2005 } else {
2006 dev_dbg(&vub300->udev->dev, "SET_CLOCK_SPEED"
2007 " %dkHz\n", kHzClock);
2008 }
2009}
2010
2011static void vub300_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2012{
2013 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2014 if (!vub300->interface)
2015 return;
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),
2021 SET_SD_POWER,
2022 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2023 0x0000, 0x0000, NULL, 0, HZ);
2024
2025 msleep(600);
2026 } else if ((ios->power_mode == MMC_POWER_UP) && !vub300->card_powered) {
2027 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2028 SET_SD_POWER,
2029 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2030 0x0001, 0x0000, NULL, 0, HZ);
2031 msleep(600);
2032 vub300->card_powered = 1;
2033 } else if (ios->power_mode == MMC_POWER_ON) {
2034 u8 *buf = kmalloc(8, GFP_KERNEL);
2035 if (buf) {
2036 __set_clock_speed(vub300, buf, ios);
2037 kfree(buf);
2038 }
2039 } else {
2040
2041 }
2042 mutex_unlock(&vub300->cmd_mutex);
2043 kref_put(&vub300->kref, vub300_delete);
2044}
2045
2046static int vub300_mmc_get_ro(struct mmc_host *mmc)
2047{
2048 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2049 return vub300->read_only;
2050}
2051
2052static void vub300_enable_sdio_irq(struct mmc_host *mmc, int enable)
2053{
2054 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2055 if (!vub300->interface)
2056 return;
2057 kref_get(&vub300->kref);
2058 if (enable) {
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
2069 } else {
2070 vub300->irq_enabled = 1;
2071 vub300_queue_poll_work(vub300, 0);
2072 }
2073 mutex_unlock(&vub300->irq_mutex);
2074 } else {
2075 vub300->irq_enabled = 0;
2076 }
2077 kref_put(&vub300->kref, vub300_delete);
2078}
2079
2080static void vub300_init_card(struct mmc_host *mmc, struct mmc_card *card)
2081{
2082 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2083 dev_info(&vub300->udev->dev, "NO host QUIRKS for this card\n");
2084}
2085
2086static 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,
2092};
2093
2094static int vub300_probe(struct usb_interface *interface,
2095 const struct usb_device_id *id)
2096{
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));
2100 int i;
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];
2106 char product[32];
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) {
2118 retval = -ENOMEM;
2119 goto error0;
2120 }
2121 command_res_urb = usb_alloc_urb(0, GFP_KERNEL);
2122 if (!command_res_urb) {
2123 retval = -ENOMEM;
2124 goto error1;
2125 }
2126
2127 mmc = mmc_alloc_host(sizeof(struct vub300_mmc_host), &udev->dev);
2128 if (!mmc) {
2129 retval = -ENOMEM;
2130 dev_err(&udev->dev, "not enough memory for the mmc_host\n");
2131 goto error4;
2132 }
2133
2134 mmc->caps = 0;
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;
2140
2141
2142
2143
2144
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");
2150 } else {
2151 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2152 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2153 mmc->f_max = 48000000;
2154 }
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;
2161 else
2162 mmc->max_req_size = 64 * 1024;
2163 mmc->max_seg_size = mmc->max_req_size;
2164 mmc->ocr_avail = 0;
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);
2184 vub300->mmc = 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;
2196
2197 for (i = 0; i < ARRAY_SIZE(vub300->fn); i++) {
2198 vub300->fn[i].offload_point = 0;
2199 vub300->fn[i].offload_count = 0;
2200 }
2201
2202 vub300->total_offload_count = 0;
2203 vub300->irq_enabled = 0;
2204 vub300->irq_disabled = 0;
2205 vub300->irqs_queued = 0;
2206
2207 for (i = 0; i < ARRAY_SIZE(vub300->sdio_register); i++)
2208 vub300->sdio_register[i++].activate = 0;
2209
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;
2216
2217 for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
2218 vub300->fbs[i] = 512;
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
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;
2248 } else {
2249 dev_warn(&vub300->udev->dev,
2250 "ignoring"
2251 " unexpected bulk_in endpoint");
2252 }
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;
2260 } else {
2261 dev_warn(&vub300->udev->dev,
2262 "ignoring"
2263 " unexpected bulk_out endpoint");
2264 }
2265 } else {
2266 dev_warn(&vub300->udev->dev,
2267 "vub300 ignoring EndPoint(%d) %02X", i,
2268 endpoint->bEndpointAddress);
2269 }
2270 }
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,
2274 "vub300 %s packets"
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
2280 } else {
2281 dev_err(&vub300->udev->dev,
2282 "Could not find two sets of bulk-in/out endpoint pairs\n");
2283 retval = -EINVAL;
2284 goto error5;
2285 }
2286 retval =
2287 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2288 GET_HC_INF0,
2289 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2290 0x0000, 0x0000, &vub300->hc_info,
2291 sizeof(vub300->hc_info), HZ);
2292 if (retval < 0)
2293 goto error5;
2294 retval =
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);
2299 if (retval < 0)
2300 goto error5;
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);
2308 retval =
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);
2314 if (retval < 0) {
2315 goto error4;
2316 } else if (sizeof(vub300->system_port_status) == retval) {
2317 vub300->card_present =
2318 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
2319 vub300->read_only =
2320 (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
2321 } else {
2322 goto error4;
2323 }
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));
2343 else
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));
2348 mmc_add_host(mmc);
2349 return 0;
2350error5:
2351 mmc_free_host(mmc);
2352
2353
2354
2355
2356error4:
2357 usb_free_urb(command_res_urb);
2358error1:
2359 usb_free_urb(command_out_urb);
2360error0:
2361 usb_put_dev(udev);
2362 return retval;
2363}
2364
2365static void vub300_disconnect(struct usb_interface *interface)
2366{
2367 struct vub300_mmc_host *vub300 = usb_get_intfdata(interface);
2368 if (!vub300 || !vub300->mmc) {
2369 return;
2370 } else {
2371 struct mmc_host *mmc = vub300->mmc;
2372 if (!vub300->mmc) {
2373 return;
2374 } else {
2375 int ifnum = interface_to_InterfaceNumber(interface);
2376 usb_set_intfdata(interface, NULL);
2377
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);
2383 return;
2384 }
2385 }
2386}
2387
2388#ifdef CONFIG_PM
2389static int vub300_suspend(struct usb_interface *intf, pm_message_t message)
2390{
2391 return 0;
2392}
2393
2394static int vub300_resume(struct usb_interface *intf)
2395{
2396 return 0;
2397}
2398#else
2399#define vub300_suspend NULL
2400#define vub300_resume NULL
2401#endif
2402static int vub300_pre_reset(struct usb_interface *intf)
2403{
2404 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2405 mutex_lock(&vub300->cmd_mutex);
2406 return 0;
2407}
2408
2409static int vub300_post_reset(struct usb_interface *intf)
2410{
2411 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2412
2413 vub300->errors = -EPIPE;
2414 mutex_unlock(&vub300->cmd_mutex);
2415 return 0;
2416}
2417
2418static struct usb_driver vub300_driver = {
2419 .name = "vub300",
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,
2428};
2429
2430static int __init vub300_init(void)
2431{
2432 int result;
2433
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");
2439 result = -ENOMEM;
2440 goto out1;
2441 }
2442 pollworkqueue = create_singlethread_workqueue("kvub300p");
2443 if (!pollworkqueue) {
2444 pr_err("not enough memory for the IRQPOLL workqueue");
2445 result = -ENOMEM;
2446 goto out2;
2447 }
2448 deadworkqueue = create_singlethread_workqueue("kvub300d");
2449 if (!deadworkqueue) {
2450 pr_err("not enough memory for the EXPIRED workqueue");
2451 result = -ENOMEM;
2452 goto out3;
2453 }
2454 result = usb_register(&vub300_driver);
2455 if (result) {
2456 pr_err("usb_register failed. Error number %d", result);
2457 goto out4;
2458 }
2459 return 0;
2460out4:
2461 destroy_workqueue(deadworkqueue);
2462out3:
2463 destroy_workqueue(pollworkqueue);
2464out2:
2465 destroy_workqueue(cmndworkqueue);
2466out1:
2467 return result;
2468}
2469
2470static void __exit vub300_exit(void)
2471{
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);
2479}
2480
2481module_init(vub300_init);
2482module_exit(vub300_exit);
2483
2484MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
2485MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
2486MODULE_LICENSE("GPL");
2487