1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Remote VUB300 SDIO/SDmem Host Controller Driver
4 *
5 * Copyright (C) 2010 Elan Digital Systems Limited
6 *
7 * based on USB Skeleton driver - 2.2
8 *
9 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
10 *
11 * VUB300: is a USB 2.0 client device with a single SDIO/SDmem/MMC slot
12 * Any SDIO/SDmem/MMC device plugged into the VUB300 will appear,
13 * by virtue of this driver, to have been plugged into a local
14 * SDIO host controller, similar to, say, a PCI Ricoh controller
15 * This is because this kernel device driver is both a USB 2.0
16 * client device driver AND an MMC host controller driver. Thus
17 * if there is an existing driver for the inserted SDIO/SDmem/MMC
18 * device then that driver will be used by the kernel to manage
19 * the device in exactly the same fashion as if it had been
20 * directly plugged into, say, a local pci bus Ricoh controller
21 *
22 * RANT: this driver was written using a display 128x48 - converting it
23 * to a line width of 80 makes it very difficult to support. In
24 * particular functions have been broken down into sub functions
25 * and the original meaningful names have been shortened into
26 * cryptic ones.
27 * The problem is that executing a fragment of code subject to
28 * two conditions means an indentation of 24, thus leaving only
29 * 56 characters for a C statement. And that is quite ridiculous!
30 *
31 * Data types: data passed to/from the VUB300 is fixed to a number of
32 * bits and driver data fields reflect that limit by using
33 * u8, u16, u32
34 */
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/kref.h>
41 #include <linux/uaccess.h>
42 #include <linux/usb.h>
43 #include <linux/mutex.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/card.h>
46 #include <linux/mmc/sdio_func.h>
47 #include <linux/mmc/sdio_ids.h>
48 #include <linux/workqueue.h>
49 #include <linux/ctype.h>
50 #include <linux/firmware.h>
51 #include <linux/scatterlist.h>
52
53 struct host_controller_info {
54 u8 info_size;
55 u16 firmware_version;
56 u8 number_of_ports;
57 } __packed;
58
59 #define FIRMWARE_BLOCK_BOUNDARY 1024
60 struct sd_command_header {
61 u8 header_size;
62 u8 header_type;
63 u8 port_number;
64 u8 command_type; /* Bit7 - Rd/Wr */
65 u8 command_index;
66 u8 transfer_size[4]; /* ReadSize + ReadSize */
67 u8 response_type;
68 u8 arguments[4];
69 u8 block_count[2];
70 u8 block_size[2];
71 u8 block_boundary[2];
72 u8 reserved[44]; /* to pad out to 64 bytes */
73 } __packed;
74
75 struct sd_irqpoll_header {
76 u8 header_size;
77 u8 header_type;
78 u8 port_number;
79 u8 command_type; /* Bit7 - Rd/Wr */
80 u8 padding[16]; /* don't ask why !! */
81 u8 poll_timeout_msb;
82 u8 poll_timeout_lsb;
83 u8 reserved[42]; /* to pad out to 64 bytes */
84 } __packed;
85
86 struct sd_common_header {
87 u8 header_size;
88 u8 header_type;
89 u8 port_number;
90 } __packed;
91
92 struct sd_response_header {
93 u8 header_size;
94 u8 header_type;
95 u8 port_number;
96 u8 command_type;
97 u8 command_index;
98 u8 command_response[];
99 } __packed;
100
101 struct sd_status_header {
102 u8 header_size;
103 u8 header_type;
104 u8 port_number;
105 u16 port_flags;
106 u32 sdio_clock;
107 u16 host_header_size;
108 u16 func_header_size;
109 u16 ctrl_header_size;
110 } __packed;
111
112 struct sd_error_header {
113 u8 header_size;
114 u8 header_type;
115 u8 port_number;
116 u8 error_code;
117 } __packed;
118
119 struct sd_interrupt_header {
120 u8 header_size;
121 u8 header_type;
122 u8 port_number;
123 } __packed;
124
125 struct offload_registers_access {
126 u8 command_byte[4];
127 u8 Respond_Byte[4];
128 } __packed;
129
130 #define INTERRUPT_REGISTER_ACCESSES 15
131 struct sd_offloaded_interrupt {
132 u8 header_size;
133 u8 header_type;
134 u8 port_number;
135 struct offload_registers_access reg[INTERRUPT_REGISTER_ACCESSES];
136 } __packed;
137
138 struct sd_register_header {
139 u8 header_size;
140 u8 header_type;
141 u8 port_number;
142 u8 command_type;
143 u8 command_index;
144 u8 command_response[6];
145 } __packed;
146
147 #define PIGGYBACK_REGISTER_ACCESSES 14
148 struct sd_offloaded_piggyback {
149 struct sd_register_header sdio;
150 struct offload_registers_access reg[PIGGYBACK_REGISTER_ACCESSES];
151 } __packed;
152
153 union sd_response {
154 struct sd_common_header common;
155 struct sd_status_header status;
156 struct sd_error_header error;
157 struct sd_interrupt_header interrupt;
158 struct sd_response_header response;
159 struct sd_offloaded_interrupt irq;
160 struct sd_offloaded_piggyback pig;
161 } __packed;
162
163 union sd_command {
164 struct sd_command_header head;
165 struct sd_irqpoll_header poll;
166 } __packed;
167
168 enum SD_RESPONSE_TYPE {
169 SDRT_UNSPECIFIED = 0,
170 SDRT_NONE,
171 SDRT_1,
172 SDRT_1B,
173 SDRT_2,
174 SDRT_3,
175 SDRT_4,
176 SDRT_5,
177 SDRT_5B,
178 SDRT_6,
179 SDRT_7,
180 };
181
182 #define RESPONSE_INTERRUPT 0x01
183 #define RESPONSE_ERROR 0x02
184 #define RESPONSE_STATUS 0x03
185 #define RESPONSE_IRQ_DISABLED 0x05
186 #define RESPONSE_IRQ_ENABLED 0x06
187 #define RESPONSE_PIGGYBACKED 0x07
188 #define RESPONSE_NO_INTERRUPT 0x08
189 #define RESPONSE_PIG_DISABLED 0x09
190 #define RESPONSE_PIG_ENABLED 0x0A
191 #define SD_ERROR_1BIT_TIMEOUT 0x01
192 #define SD_ERROR_4BIT_TIMEOUT 0x02
193 #define SD_ERROR_1BIT_CRC_WRONG 0x03
194 #define SD_ERROR_4BIT_CRC_WRONG 0x04
195 #define SD_ERROR_1BIT_CRC_ERROR 0x05
196 #define SD_ERROR_4BIT_CRC_ERROR 0x06
197 #define SD_ERROR_NO_CMD_ENDBIT 0x07
198 #define SD_ERROR_NO_1BIT_DATEND 0x08
199 #define SD_ERROR_NO_4BIT_DATEND 0x09
200 #define SD_ERROR_1BIT_UNEXPECTED_TIMEOUT 0x0A
201 #define SD_ERROR_4BIT_UNEXPECTED_TIMEOUT 0x0B
202 #define SD_ERROR_ILLEGAL_COMMAND 0x0C
203 #define SD_ERROR_NO_DEVICE 0x0D
204 #define SD_ERROR_TRANSFER_LENGTH 0x0E
205 #define SD_ERROR_1BIT_DATA_TIMEOUT 0x0F
206 #define SD_ERROR_4BIT_DATA_TIMEOUT 0x10
207 #define SD_ERROR_ILLEGAL_STATE 0x11
208 #define SD_ERROR_UNKNOWN_ERROR 0x12
209 #define SD_ERROR_RESERVED_ERROR 0x13
210 #define SD_ERROR_INVALID_FUNCTION 0x14
211 #define SD_ERROR_OUT_OF_RANGE 0x15
212 #define SD_ERROR_STAT_CMD 0x16
213 #define SD_ERROR_STAT_DATA 0x17
214 #define SD_ERROR_STAT_CMD_TIMEOUT 0x18
215 #define SD_ERROR_SDCRDY_STUCK 0x19
216 #define SD_ERROR_UNHANDLED 0x1A
217 #define SD_ERROR_OVERRUN 0x1B
218 #define SD_ERROR_PIO_TIMEOUT 0x1C
219
220 #define FUN(c) (0x000007 & (c->arg>>28))
221 #define REG(c) (0x01FFFF & (c->arg>>9))
222
223 static bool limit_speed_to_24_MHz;
224 module_param(limit_speed_to_24_MHz, bool, 0644);
225 MODULE_PARM_DESC(limit_speed_to_24_MHz, "Limit Max SDIO Clock Speed to 24 MHz");
226
227 static bool pad_input_to_usb_pkt;
228 module_param(pad_input_to_usb_pkt, bool, 0644);
229 MODULE_PARM_DESC(pad_input_to_usb_pkt,
230 "Pad USB data input transfers to whole USB Packet");
231
232 static bool disable_offload_processing;
233 module_param(disable_offload_processing, bool, 0644);
234 MODULE_PARM_DESC(disable_offload_processing, "Disable Offload Processing");
235
236 static bool force_1_bit_data_xfers;
237 module_param(force_1_bit_data_xfers, bool, 0644);
238 MODULE_PARM_DESC(force_1_bit_data_xfers,
239 "Force SDIO Data Transfers to 1-bit Mode");
240
241 static bool force_polling_for_irqs;
242 module_param(force_polling_for_irqs, bool, 0644);
243 MODULE_PARM_DESC(force_polling_for_irqs, "Force Polling for SDIO interrupts");
244
245 static int firmware_irqpoll_timeout = 1024;
246 module_param(firmware_irqpoll_timeout, int, 0644);
247 MODULE_PARM_DESC(firmware_irqpoll_timeout, "VUB300 firmware irqpoll timeout");
248
249 static int force_max_req_size = 128;
250 module_param(force_max_req_size, int, 0644);
251 MODULE_PARM_DESC(force_max_req_size, "set max request size in kBytes");
252
253 #ifdef SMSC_DEVELOPMENT_BOARD
254 static int firmware_rom_wait_states = 0x04;
255 #else
256 static int firmware_rom_wait_states = 0x1C;
257 #endif
258
259 module_param(firmware_rom_wait_states, int, 0644);
260 MODULE_PARM_DESC(firmware_rom_wait_states,
261 "ROM wait states byte=RRRIIEEE (Reserved Internal External)");
262
263 #define ELAN_VENDOR_ID 0x2201
264 #define VUB300_VENDOR_ID 0x0424
265 #define VUB300_PRODUCT_ID 0x012C
266 static const struct usb_device_id vub300_table[] = {
267 {USB_DEVICE(ELAN_VENDOR_ID, VUB300_PRODUCT_ID)},
268 {USB_DEVICE(VUB300_VENDOR_ID, VUB300_PRODUCT_ID)},
269 {} /* Terminating entry */
270 };
271 MODULE_DEVICE_TABLE(usb, vub300_table);
272
273 static struct workqueue_struct *cmndworkqueue;
274 static struct workqueue_struct *pollworkqueue;
275 static struct workqueue_struct *deadworkqueue;
276
interface_to_InterfaceNumber(struct usb_interface * interface)277 static inline int interface_to_InterfaceNumber(struct usb_interface *interface)
278 {
279 if (!interface)
280 return -1;
281 if (!interface->cur_altsetting)
282 return -1;
283 return interface->cur_altsetting->desc.bInterfaceNumber;
284 }
285
286 struct sdio_register {
287 unsigned func_num:3;
288 unsigned sdio_reg:17;
289 unsigned activate:1;
290 unsigned prepared:1;
291 unsigned regvalue:8;
292 unsigned response:8;
293 unsigned sparebit:26;
294 };
295
296 struct vub300_mmc_host {
297 struct usb_device *udev;
298 struct usb_interface *interface;
299 struct kref kref;
300 struct mutex cmd_mutex;
301 struct mutex irq_mutex;
302 char vub_name[3 + (9 * 8) + 4 + 1]; /* max of 7 sdio fn's */
303 u8 cmnd_out_ep; /* EndPoint for commands */
304 u8 cmnd_res_ep; /* EndPoint for responses */
305 u8 data_out_ep; /* EndPoint for out data */
306 u8 data_inp_ep; /* EndPoint for inp data */
307 bool card_powered;
308 bool card_present;
309 bool read_only;
310 bool large_usb_packets;
311 bool app_spec; /* ApplicationSpecific */
312 bool irq_enabled; /* by the MMC CORE */
313 bool irq_disabled; /* in the firmware */
314 unsigned bus_width:4;
315 u8 total_offload_count;
316 u8 dynamic_register_count;
317 u8 resp_len;
318 u32 datasize;
319 int errors;
320 int usb_transport_fail;
321 int usb_timed_out;
322 int irqs_queued;
323 struct sdio_register sdio_register[16];
324 struct offload_interrupt_function_register {
325 #define MAXREGBITS 4
326 #define MAXREGS (1<<MAXREGBITS)
327 #define MAXREGMASK (MAXREGS-1)
328 u8 offload_count;
329 u32 offload_point;
330 struct offload_registers_access reg[MAXREGS];
331 } fn[8];
332 u16 fbs[8]; /* Function Block Size */
333 struct mmc_command *cmd;
334 struct mmc_request *req;
335 struct mmc_data *data;
336 struct mmc_host *mmc;
337 struct urb *urb;
338 struct urb *command_out_urb;
339 struct urb *command_res_urb;
340 struct completion command_complete;
341 struct completion irqpoll_complete;
342 union sd_command cmnd;
343 union sd_response resp;
344 struct timer_list sg_transfer_timer;
345 struct usb_sg_request sg_request;
346 struct timer_list inactivity_timer;
347 struct work_struct deadwork;
348 struct work_struct cmndwork;
349 struct delayed_work pollwork;
350 struct host_controller_info hc_info;
351 struct sd_status_header system_port_status;
352 u8 padded_buffer[64];
353 };
354
355 #define kref_to_vub300_mmc_host(d) container_of(d, struct vub300_mmc_host, kref)
356 #define SET_TRANSFER_PSEUDOCODE 21
357 #define SET_INTERRUPT_PSEUDOCODE 20
358 #define SET_FAILURE_MODE 18
359 #define SET_ROM_WAIT_STATES 16
360 #define SET_IRQ_ENABLE 13
361 #define SET_CLOCK_SPEED 11
362 #define SET_FUNCTION_BLOCK_SIZE 9
363 #define SET_SD_DATA_MODE 6
364 #define SET_SD_POWER 4
365 #define ENTER_DFU_MODE 3
366 #define GET_HC_INF0 1
367 #define GET_SYSTEM_PORT_STATUS 0
368
vub300_delete(struct kref * kref)369 static void vub300_delete(struct kref *kref)
370 { /* kref callback - softirq */
371 struct vub300_mmc_host *vub300 = kref_to_vub300_mmc_host(kref);
372 usb_free_urb(vub300->command_out_urb);
373 vub300->command_out_urb = NULL;
374 usb_free_urb(vub300->command_res_urb);
375 vub300->command_res_urb = NULL;
376 usb_put_dev(vub300->udev);
377 /*
378 * and hence also frees vub300
379 * which is contained at the end of struct mmc
380 */
381 }
382
vub300_queue_cmnd_work(struct vub300_mmc_host * vub300)383 static void vub300_queue_cmnd_work(struct vub300_mmc_host *vub300)
384 {
385 kref_get(&vub300->kref);
386 if (queue_work(cmndworkqueue, &vub300->cmndwork)) {
387 /*
388 * then the cmndworkqueue was not previously
389 * running and the above get ref is obvious
390 * required and will be put when the thread
391 * terminates by a specific call
392 */
393 } else {
394 /*
395 * the cmndworkqueue was already running from
396 * a previous invocation and thus to keep the
397 * kref counts correct we must undo the get
398 */
399 kref_put(&vub300->kref, vub300_delete);
400 }
401 }
402
vub300_queue_poll_work(struct vub300_mmc_host * vub300,int delay)403 static void vub300_queue_poll_work(struct vub300_mmc_host *vub300, int delay)
404 {
405 kref_get(&vub300->kref);
406 if (queue_delayed_work(pollworkqueue, &vub300->pollwork, delay)) {
407 /*
408 * then the pollworkqueue was not previously
409 * running and the above get ref is obvious
410 * required and will be put when the thread
411 * terminates by a specific call
412 */
413 } else {
414 /*
415 * the pollworkqueue was already running from
416 * a previous invocation and thus to keep the
417 * kref counts correct we must undo the get
418 */
419 kref_put(&vub300->kref, vub300_delete);
420 }
421 }
422
vub300_queue_dead_work(struct vub300_mmc_host * vub300)423 static void vub300_queue_dead_work(struct vub300_mmc_host *vub300)
424 {
425 kref_get(&vub300->kref);
426 if (queue_work(deadworkqueue, &vub300->deadwork)) {
427 /*
428 * then the deadworkqueue was not previously
429 * running and the above get ref is obvious
430 * required and will be put when the thread
431 * terminates by a specific call
432 */
433 } else {
434 /*
435 * the deadworkqueue was already running from
436 * a previous invocation and thus to keep the
437 * kref counts correct we must undo the get
438 */
439 kref_put(&vub300->kref, vub300_delete);
440 }
441 }
442
irqpoll_res_completed(struct urb * urb)443 static void irqpoll_res_completed(struct urb *urb)
444 { /* urb completion handler - hardirq */
445 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
446 if (urb->status)
447 vub300->usb_transport_fail = urb->status;
448 complete(&vub300->irqpoll_complete);
449 }
450
irqpoll_out_completed(struct urb * urb)451 static void irqpoll_out_completed(struct urb *urb)
452 { /* urb completion handler - hardirq */
453 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
454 if (urb->status) {
455 vub300->usb_transport_fail = urb->status;
456 complete(&vub300->irqpoll_complete);
457 return;
458 } else {
459 int ret;
460 unsigned int pipe =
461 usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
462 usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
463 &vub300->resp, sizeof(vub300->resp),
464 irqpoll_res_completed, vub300);
465 vub300->command_res_urb->actual_length = 0;
466 ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
467 if (ret) {
468 vub300->usb_transport_fail = ret;
469 complete(&vub300->irqpoll_complete);
470 }
471 return;
472 }
473 }
474
send_irqpoll(struct vub300_mmc_host * vub300)475 static void send_irqpoll(struct vub300_mmc_host *vub300)
476 {
477 /* cmd_mutex is held by vub300_pollwork_thread */
478 int retval;
479 int timeout = 0xFFFF & (0x0001FFFF - firmware_irqpoll_timeout);
480 vub300->cmnd.poll.header_size = 22;
481 vub300->cmnd.poll.header_type = 1;
482 vub300->cmnd.poll.port_number = 0;
483 vub300->cmnd.poll.command_type = 2;
484 vub300->cmnd.poll.poll_timeout_lsb = 0xFF & (unsigned)timeout;
485 vub300->cmnd.poll.poll_timeout_msb = 0xFF & (unsigned)(timeout >> 8);
486 usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
487 usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep)
488 , &vub300->cmnd, sizeof(vub300->cmnd)
489 , irqpoll_out_completed, vub300);
490 retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
491 if (0 > retval) {
492 vub300->usb_transport_fail = retval;
493 vub300_queue_poll_work(vub300, 1);
494 complete(&vub300->irqpoll_complete);
495 return;
496 } else {
497 return;
498 }
499 }
500
new_system_port_status(struct vub300_mmc_host * vub300)501 static void new_system_port_status(struct vub300_mmc_host *vub300)
502 {
503 int old_card_present = vub300->card_present;
504 int new_card_present =
505 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
506 vub300->read_only =
507 (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
508 if (new_card_present && !old_card_present) {
509 dev_info(&vub300->udev->dev, "card just inserted\n");
510 vub300->card_present = 1;
511 vub300->bus_width = 0;
512 if (disable_offload_processing)
513 strscpy(vub300->vub_name, "EMPTY Processing Disabled",
514 sizeof(vub300->vub_name));
515 else
516 vub300->vub_name[0] = 0;
517 mmc_detect_change(vub300->mmc, 1);
518 } else if (!new_card_present && old_card_present) {
519 dev_info(&vub300->udev->dev, "card just ejected\n");
520 vub300->card_present = 0;
521 mmc_detect_change(vub300->mmc, 0);
522 } else {
523 /* no change */
524 }
525 }
526
__add_offloaded_reg_to_fifo(struct vub300_mmc_host * vub300,struct offload_registers_access * register_access,u8 func)527 static void __add_offloaded_reg_to_fifo(struct vub300_mmc_host *vub300,
528 struct offload_registers_access
529 *register_access, u8 func)
530 {
531 u8 r = vub300->fn[func].offload_point + vub300->fn[func].offload_count;
532 memcpy(&vub300->fn[func].reg[MAXREGMASK & r], register_access,
533 sizeof(struct offload_registers_access));
534 vub300->fn[func].offload_count += 1;
535 vub300->total_offload_count += 1;
536 }
537
add_offloaded_reg(struct vub300_mmc_host * vub300,struct offload_registers_access * register_access)538 static void add_offloaded_reg(struct vub300_mmc_host *vub300,
539 struct offload_registers_access *register_access)
540 {
541 u32 Register = ((0x03 & register_access->command_byte[0]) << 15)
542 | ((0xFF & register_access->command_byte[1]) << 7)
543 | ((0xFE & register_access->command_byte[2]) >> 1);
544 u8 func = ((0x70 & register_access->command_byte[0]) >> 4);
545 u8 regs = vub300->dynamic_register_count;
546 u8 i = 0;
547 while (0 < regs-- && 1 == vub300->sdio_register[i].activate) {
548 if (vub300->sdio_register[i].func_num == func &&
549 vub300->sdio_register[i].sdio_reg == Register) {
550 if (vub300->sdio_register[i].prepared == 0)
551 vub300->sdio_register[i].prepared = 1;
552 vub300->sdio_register[i].response =
553 register_access->Respond_Byte[2];
554 vub300->sdio_register[i].regvalue =
555 register_access->Respond_Byte[3];
556 return;
557 } else {
558 i += 1;
559 continue;
560 }
561 }
562 __add_offloaded_reg_to_fifo(vub300, register_access, func);
563 }
564
check_vub300_port_status(struct vub300_mmc_host * vub300)565 static void check_vub300_port_status(struct vub300_mmc_host *vub300)
566 {
567 /*
568 * cmd_mutex is held by vub300_pollwork_thread,
569 * vub300_deadwork_thread or vub300_cmndwork_thread
570 */
571 int retval;
572 retval =
573 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
574 GET_SYSTEM_PORT_STATUS,
575 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
576 0x0000, 0x0000, &vub300->system_port_status,
577 sizeof(vub300->system_port_status), 1000);
578 if (sizeof(vub300->system_port_status) == retval)
579 new_system_port_status(vub300);
580 }
581
__vub300_irqpoll_response(struct vub300_mmc_host * vub300)582 static void __vub300_irqpoll_response(struct vub300_mmc_host *vub300)
583 {
584 /* cmd_mutex is held by vub300_pollwork_thread */
585 if (vub300->command_res_urb->actual_length == 0)
586 return;
587
588 switch (vub300->resp.common.header_type) {
589 case RESPONSE_INTERRUPT:
590 mutex_lock(&vub300->irq_mutex);
591 if (vub300->irq_enabled)
592 mmc_signal_sdio_irq(vub300->mmc);
593 else
594 vub300->irqs_queued += 1;
595 vub300->irq_disabled = 1;
596 mutex_unlock(&vub300->irq_mutex);
597 break;
598 case RESPONSE_ERROR:
599 if (vub300->resp.error.error_code == SD_ERROR_NO_DEVICE)
600 check_vub300_port_status(vub300);
601 break;
602 case RESPONSE_STATUS:
603 vub300->system_port_status = vub300->resp.status;
604 new_system_port_status(vub300);
605 if (!vub300->card_present)
606 vub300_queue_poll_work(vub300, HZ / 5);
607 break;
608 case RESPONSE_IRQ_DISABLED:
609 {
610 int offloaded_data_length = vub300->resp.common.header_size - 3;
611 int register_count = offloaded_data_length >> 3;
612 int ri = 0;
613 while (register_count--) {
614 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
615 ri += 1;
616 }
617 mutex_lock(&vub300->irq_mutex);
618 if (vub300->irq_enabled)
619 mmc_signal_sdio_irq(vub300->mmc);
620 else
621 vub300->irqs_queued += 1;
622 vub300->irq_disabled = 1;
623 mutex_unlock(&vub300->irq_mutex);
624 break;
625 }
626 case RESPONSE_IRQ_ENABLED:
627 {
628 int offloaded_data_length = vub300->resp.common.header_size - 3;
629 int register_count = offloaded_data_length >> 3;
630 int ri = 0;
631 while (register_count--) {
632 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
633 ri += 1;
634 }
635 mutex_lock(&vub300->irq_mutex);
636 if (vub300->irq_enabled)
637 mmc_signal_sdio_irq(vub300->mmc);
638 else
639 vub300->irqs_queued += 1;
640 vub300->irq_disabled = 0;
641 mutex_unlock(&vub300->irq_mutex);
642 break;
643 }
644 case RESPONSE_NO_INTERRUPT:
645 vub300_queue_poll_work(vub300, 1);
646 break;
647 default:
648 break;
649 }
650 }
651
__do_poll(struct vub300_mmc_host * vub300)652 static void __do_poll(struct vub300_mmc_host *vub300)
653 {
654 /* cmd_mutex is held by vub300_pollwork_thread */
655 unsigned long commretval;
656 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
657 init_completion(&vub300->irqpoll_complete);
658 send_irqpoll(vub300);
659 commretval = wait_for_completion_timeout(&vub300->irqpoll_complete,
660 msecs_to_jiffies(500));
661 if (vub300->usb_transport_fail) {
662 /* no need to do anything */
663 } else if (commretval == 0) {
664 vub300->usb_timed_out = 1;
665 usb_kill_urb(vub300->command_out_urb);
666 usb_kill_urb(vub300->command_res_urb);
667 } else { /* commretval > 0 */
668 __vub300_irqpoll_response(vub300);
669 }
670 }
671
672 /* this thread runs only when the driver
673 * is trying to poll the device for an IRQ
674 */
vub300_pollwork_thread(struct work_struct * work)675 static void vub300_pollwork_thread(struct work_struct *work)
676 { /* NOT irq */
677 struct vub300_mmc_host *vub300 = container_of(work,
678 struct vub300_mmc_host, pollwork.work);
679 if (!vub300->interface) {
680 kref_put(&vub300->kref, vub300_delete);
681 return;
682 }
683 mutex_lock(&vub300->cmd_mutex);
684 if (vub300->cmd) {
685 vub300_queue_poll_work(vub300, 1);
686 } else if (!vub300->card_present) {
687 /* no need to do anything */
688 } else { /* vub300->card_present */
689 mutex_lock(&vub300->irq_mutex);
690 if (!vub300->irq_enabled) {
691 mutex_unlock(&vub300->irq_mutex);
692 } else if (vub300->irqs_queued) {
693 vub300->irqs_queued -= 1;
694 mmc_signal_sdio_irq(vub300->mmc);
695 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
696 mutex_unlock(&vub300->irq_mutex);
697 } else { /* NOT vub300->irqs_queued */
698 mutex_unlock(&vub300->irq_mutex);
699 __do_poll(vub300);
700 }
701 }
702 mutex_unlock(&vub300->cmd_mutex);
703 kref_put(&vub300->kref, vub300_delete);
704 }
705
vub300_deadwork_thread(struct work_struct * work)706 static void vub300_deadwork_thread(struct work_struct *work)
707 { /* NOT irq */
708 struct vub300_mmc_host *vub300 =
709 container_of(work, struct vub300_mmc_host, deadwork);
710 if (!vub300->interface) {
711 kref_put(&vub300->kref, vub300_delete);
712 return;
713 }
714 mutex_lock(&vub300->cmd_mutex);
715 if (vub300->cmd) {
716 /*
717 * a command got in as the inactivity
718 * timer expired - so we just let the
719 * processing of the command show if
720 * the device is dead
721 */
722 } else if (vub300->card_present) {
723 check_vub300_port_status(vub300);
724 } else if (vub300->mmc && vub300->mmc->card) {
725 /*
726 * the MMC core must not have responded
727 * to the previous indication - lets
728 * hope that it eventually does so we
729 * will just ignore this for now
730 */
731 } else {
732 check_vub300_port_status(vub300);
733 }
734 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
735 mutex_unlock(&vub300->cmd_mutex);
736 kref_put(&vub300->kref, vub300_delete);
737 }
738
vub300_inactivity_timer_expired(struct timer_list * t)739 static void vub300_inactivity_timer_expired(struct timer_list *t)
740 { /* softirq */
741 struct vub300_mmc_host *vub300 = timer_container_of(vub300, t,
742 inactivity_timer);
743 if (!vub300->interface) {
744 kref_put(&vub300->kref, vub300_delete);
745 } else if (vub300->cmd) {
746 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
747 } else {
748 vub300_queue_dead_work(vub300);
749 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
750 }
751 }
752
vub300_response_error(u8 error_code)753 static int vub300_response_error(u8 error_code)
754 {
755 switch (error_code) {
756 case SD_ERROR_PIO_TIMEOUT:
757 case SD_ERROR_1BIT_TIMEOUT:
758 case SD_ERROR_4BIT_TIMEOUT:
759 return -ETIMEDOUT;
760 case SD_ERROR_STAT_DATA:
761 case SD_ERROR_OVERRUN:
762 case SD_ERROR_STAT_CMD:
763 case SD_ERROR_STAT_CMD_TIMEOUT:
764 case SD_ERROR_SDCRDY_STUCK:
765 case SD_ERROR_UNHANDLED:
766 case SD_ERROR_1BIT_CRC_WRONG:
767 case SD_ERROR_4BIT_CRC_WRONG:
768 case SD_ERROR_1BIT_CRC_ERROR:
769 case SD_ERROR_4BIT_CRC_ERROR:
770 case SD_ERROR_NO_CMD_ENDBIT:
771 case SD_ERROR_NO_1BIT_DATEND:
772 case SD_ERROR_NO_4BIT_DATEND:
773 case SD_ERROR_1BIT_DATA_TIMEOUT:
774 case SD_ERROR_4BIT_DATA_TIMEOUT:
775 case SD_ERROR_1BIT_UNEXPECTED_TIMEOUT:
776 case SD_ERROR_4BIT_UNEXPECTED_TIMEOUT:
777 return -EILSEQ;
778 case 33:
779 return -EILSEQ;
780 case SD_ERROR_ILLEGAL_COMMAND:
781 return -EINVAL;
782 case SD_ERROR_NO_DEVICE:
783 return -ENOMEDIUM;
784 default:
785 return -ENODEV;
786 }
787 }
788
command_res_completed(struct urb * urb)789 static void command_res_completed(struct urb *urb)
790 { /* urb completion handler - hardirq */
791 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
792 if (urb->status) {
793 /* we have to let the initiator handle the error */
794 } else if (vub300->command_res_urb->actual_length == 0) {
795 /*
796 * we have seen this happen once or twice and
797 * we suspect a buggy USB host controller
798 */
799 } else if (!vub300->data) {
800 /* this means that the command (typically CMD52) succeeded */
801 } else if (vub300->resp.common.header_type != 0x02) {
802 /*
803 * this is an error response from the VUB300 chip
804 * and we let the initiator handle it
805 */
806 } else if (vub300->urb) {
807 vub300->cmd->error =
808 vub300_response_error(vub300->resp.error.error_code);
809 usb_unlink_urb(vub300->urb);
810 } else {
811 vub300->cmd->error =
812 vub300_response_error(vub300->resp.error.error_code);
813 usb_sg_cancel(&vub300->sg_request);
814 }
815 complete(&vub300->command_complete); /* got_response_in */
816 }
817
command_out_completed(struct urb * urb)818 static void command_out_completed(struct urb *urb)
819 { /* urb completion handler - hardirq */
820 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
821 if (urb->status) {
822 complete(&vub300->command_complete);
823 } else {
824 int ret;
825 unsigned int pipe =
826 usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
827 usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
828 &vub300->resp, sizeof(vub300->resp),
829 command_res_completed, vub300);
830 vub300->command_res_urb->actual_length = 0;
831 ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
832 if (ret == 0) {
833 /*
834 * the urb completion handler will call
835 * our completion handler
836 */
837 } else {
838 /*
839 * and thus we only call it directly
840 * when it will not be called
841 */
842 complete(&vub300->command_complete);
843 }
844 }
845 }
846
847 /*
848 * the STUFF bits are masked out for the comparisons
849 */
snoop_block_size_and_bus_width(struct vub300_mmc_host * vub300,u32 cmd_arg)850 static void snoop_block_size_and_bus_width(struct vub300_mmc_host *vub300,
851 u32 cmd_arg)
852 {
853 if ((0xFBFFFE00 & cmd_arg) == 0x80022200)
854 vub300->fbs[1] = (cmd_arg << 8) | (0x00FF & vub300->fbs[1]);
855 else if ((0xFBFFFE00 & cmd_arg) == 0x80022000)
856 vub300->fbs[1] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[1]);
857 else if ((0xFBFFFE00 & cmd_arg) == 0x80042200)
858 vub300->fbs[2] = (cmd_arg << 8) | (0x00FF & vub300->fbs[2]);
859 else if ((0xFBFFFE00 & cmd_arg) == 0x80042000)
860 vub300->fbs[2] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[2]);
861 else if ((0xFBFFFE00 & cmd_arg) == 0x80062200)
862 vub300->fbs[3] = (cmd_arg << 8) | (0x00FF & vub300->fbs[3]);
863 else if ((0xFBFFFE00 & cmd_arg) == 0x80062000)
864 vub300->fbs[3] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[3]);
865 else if ((0xFBFFFE00 & cmd_arg) == 0x80082200)
866 vub300->fbs[4] = (cmd_arg << 8) | (0x00FF & vub300->fbs[4]);
867 else if ((0xFBFFFE00 & cmd_arg) == 0x80082000)
868 vub300->fbs[4] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[4]);
869 else if ((0xFBFFFE00 & cmd_arg) == 0x800A2200)
870 vub300->fbs[5] = (cmd_arg << 8) | (0x00FF & vub300->fbs[5]);
871 else if ((0xFBFFFE00 & cmd_arg) == 0x800A2000)
872 vub300->fbs[5] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[5]);
873 else if ((0xFBFFFE00 & cmd_arg) == 0x800C2200)
874 vub300->fbs[6] = (cmd_arg << 8) | (0x00FF & vub300->fbs[6]);
875 else if ((0xFBFFFE00 & cmd_arg) == 0x800C2000)
876 vub300->fbs[6] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[6]);
877 else if ((0xFBFFFE00 & cmd_arg) == 0x800E2200)
878 vub300->fbs[7] = (cmd_arg << 8) | (0x00FF & vub300->fbs[7]);
879 else if ((0xFBFFFE00 & cmd_arg) == 0x800E2000)
880 vub300->fbs[7] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[7]);
881 else if ((0xFBFFFE03 & cmd_arg) == 0x80000E00)
882 vub300->bus_width = 1;
883 else if ((0xFBFFFE03 & cmd_arg) == 0x80000E02)
884 vub300->bus_width = 4;
885 }
886
send_command(struct vub300_mmc_host * vub300)887 static void send_command(struct vub300_mmc_host *vub300)
888 {
889 /* cmd_mutex is held by vub300_cmndwork_thread */
890 struct mmc_command *cmd = vub300->cmd;
891 struct mmc_data *data = vub300->data;
892 int retval;
893 int i;
894 u8 response_type;
895 if (vub300->app_spec) {
896 switch (cmd->opcode) {
897 case 6:
898 response_type = SDRT_1;
899 vub300->resp_len = 6;
900 if (0x00000000 == (0x00000003 & cmd->arg))
901 vub300->bus_width = 1;
902 else if (0x00000002 == (0x00000003 & cmd->arg))
903 vub300->bus_width = 4;
904 else
905 dev_err(&vub300->udev->dev,
906 "unexpected ACMD6 bus_width=%d\n",
907 0x00000003 & cmd->arg);
908 break;
909 case 13:
910 response_type = SDRT_1;
911 vub300->resp_len = 6;
912 break;
913 case 22:
914 response_type = SDRT_1;
915 vub300->resp_len = 6;
916 break;
917 case 23:
918 response_type = SDRT_1;
919 vub300->resp_len = 6;
920 break;
921 case 41:
922 response_type = SDRT_3;
923 vub300->resp_len = 6;
924 break;
925 case 42:
926 response_type = SDRT_1;
927 vub300->resp_len = 6;
928 break;
929 case 51:
930 response_type = SDRT_1;
931 vub300->resp_len = 6;
932 break;
933 case 55:
934 response_type = SDRT_1;
935 vub300->resp_len = 6;
936 break;
937 default:
938 vub300->resp_len = 0;
939 cmd->error = -EINVAL;
940 complete(&vub300->command_complete);
941 return;
942 }
943 vub300->app_spec = 0;
944 } else {
945 switch (cmd->opcode) {
946 case 0:
947 response_type = SDRT_NONE;
948 vub300->resp_len = 0;
949 break;
950 case 1:
951 response_type = SDRT_3;
952 vub300->resp_len = 6;
953 break;
954 case 2:
955 response_type = SDRT_2;
956 vub300->resp_len = 17;
957 break;
958 case 3:
959 response_type = SDRT_6;
960 vub300->resp_len = 6;
961 break;
962 case 4:
963 response_type = SDRT_NONE;
964 vub300->resp_len = 0;
965 break;
966 case 5:
967 response_type = SDRT_4;
968 vub300->resp_len = 6;
969 break;
970 case 6:
971 response_type = SDRT_1;
972 vub300->resp_len = 6;
973 break;
974 case 7:
975 response_type = SDRT_1B;
976 vub300->resp_len = 6;
977 break;
978 case 8:
979 response_type = SDRT_7;
980 vub300->resp_len = 6;
981 break;
982 case 9:
983 response_type = SDRT_2;
984 vub300->resp_len = 17;
985 break;
986 case 10:
987 response_type = SDRT_2;
988 vub300->resp_len = 17;
989 break;
990 case 12:
991 response_type = SDRT_1B;
992 vub300->resp_len = 6;
993 break;
994 case 13:
995 response_type = SDRT_1;
996 vub300->resp_len = 6;
997 break;
998 case 15:
999 response_type = SDRT_NONE;
1000 vub300->resp_len = 0;
1001 break;
1002 case 16:
1003 for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
1004 vub300->fbs[i] = 0xFFFF & cmd->arg;
1005 response_type = SDRT_1;
1006 vub300->resp_len = 6;
1007 break;
1008 case 17:
1009 case 18:
1010 case 24:
1011 case 25:
1012 case 27:
1013 response_type = SDRT_1;
1014 vub300->resp_len = 6;
1015 break;
1016 case 28:
1017 case 29:
1018 response_type = SDRT_1B;
1019 vub300->resp_len = 6;
1020 break;
1021 case 30:
1022 case 32:
1023 case 33:
1024 response_type = SDRT_1;
1025 vub300->resp_len = 6;
1026 break;
1027 case 38:
1028 response_type = SDRT_1B;
1029 vub300->resp_len = 6;
1030 break;
1031 case 42:
1032 response_type = SDRT_1;
1033 vub300->resp_len = 6;
1034 break;
1035 case 52:
1036 response_type = SDRT_5;
1037 vub300->resp_len = 6;
1038 snoop_block_size_and_bus_width(vub300, cmd->arg);
1039 break;
1040 case 53:
1041 response_type = SDRT_5;
1042 vub300->resp_len = 6;
1043 break;
1044 case 55:
1045 response_type = SDRT_1;
1046 vub300->resp_len = 6;
1047 vub300->app_spec = 1;
1048 break;
1049 case 56:
1050 response_type = SDRT_1;
1051 vub300->resp_len = 6;
1052 break;
1053 default:
1054 vub300->resp_len = 0;
1055 cmd->error = -EINVAL;
1056 complete(&vub300->command_complete);
1057 return;
1058 }
1059 }
1060 /*
1061 * it is a shame that we can not use "sizeof(struct sd_command_header)"
1062 * this is because the packet _must_ be padded to 64 bytes
1063 */
1064 vub300->cmnd.head.header_size = 20;
1065 vub300->cmnd.head.header_type = 0x00;
1066 vub300->cmnd.head.port_number = 0; /* "0" means port 1 */
1067 vub300->cmnd.head.command_type = 0x00; /* standard read command */
1068 vub300->cmnd.head.response_type = response_type;
1069 vub300->cmnd.head.command_index = cmd->opcode;
1070 vub300->cmnd.head.arguments[0] = cmd->arg >> 24;
1071 vub300->cmnd.head.arguments[1] = cmd->arg >> 16;
1072 vub300->cmnd.head.arguments[2] = cmd->arg >> 8;
1073 vub300->cmnd.head.arguments[3] = cmd->arg >> 0;
1074 if (cmd->opcode == 52) {
1075 int fn = 0x7 & (cmd->arg >> 28);
1076 vub300->cmnd.head.block_count[0] = 0;
1077 vub300->cmnd.head.block_count[1] = 0;
1078 vub300->cmnd.head.block_size[0] = (vub300->fbs[fn] >> 8) & 0xFF;
1079 vub300->cmnd.head.block_size[1] = (vub300->fbs[fn] >> 0) & 0xFF;
1080 vub300->cmnd.head.command_type = 0x00;
1081 vub300->cmnd.head.transfer_size[0] = 0;
1082 vub300->cmnd.head.transfer_size[1] = 0;
1083 vub300->cmnd.head.transfer_size[2] = 0;
1084 vub300->cmnd.head.transfer_size[3] = 0;
1085 } else if (!data) {
1086 vub300->cmnd.head.block_count[0] = 0;
1087 vub300->cmnd.head.block_count[1] = 0;
1088 vub300->cmnd.head.block_size[0] = (vub300->fbs[0] >> 8) & 0xFF;
1089 vub300->cmnd.head.block_size[1] = (vub300->fbs[0] >> 0) & 0xFF;
1090 vub300->cmnd.head.command_type = 0x00;
1091 vub300->cmnd.head.transfer_size[0] = 0;
1092 vub300->cmnd.head.transfer_size[1] = 0;
1093 vub300->cmnd.head.transfer_size[2] = 0;
1094 vub300->cmnd.head.transfer_size[3] = 0;
1095 } else if (cmd->opcode == 53) {
1096 int fn = 0x7 & (cmd->arg >> 28);
1097 if (0x08 & vub300->cmnd.head.arguments[0]) { /* BLOCK MODE */
1098 vub300->cmnd.head.block_count[0] =
1099 (data->blocks >> 8) & 0xFF;
1100 vub300->cmnd.head.block_count[1] =
1101 (data->blocks >> 0) & 0xFF;
1102 vub300->cmnd.head.block_size[0] =
1103 (data->blksz >> 8) & 0xFF;
1104 vub300->cmnd.head.block_size[1] =
1105 (data->blksz >> 0) & 0xFF;
1106 } else { /* BYTE MODE */
1107 vub300->cmnd.head.block_count[0] = 0;
1108 vub300->cmnd.head.block_count[1] = 0;
1109 vub300->cmnd.head.block_size[0] =
1110 (vub300->datasize >> 8) & 0xFF;
1111 vub300->cmnd.head.block_size[1] =
1112 (vub300->datasize >> 0) & 0xFF;
1113 }
1114 vub300->cmnd.head.command_type =
1115 (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1116 vub300->cmnd.head.transfer_size[0] =
1117 (vub300->datasize >> 24) & 0xFF;
1118 vub300->cmnd.head.transfer_size[1] =
1119 (vub300->datasize >> 16) & 0xFF;
1120 vub300->cmnd.head.transfer_size[2] =
1121 (vub300->datasize >> 8) & 0xFF;
1122 vub300->cmnd.head.transfer_size[3] =
1123 (vub300->datasize >> 0) & 0xFF;
1124 if (vub300->datasize < vub300->fbs[fn]) {
1125 vub300->cmnd.head.block_count[0] = 0;
1126 vub300->cmnd.head.block_count[1] = 0;
1127 }
1128 } else {
1129 vub300->cmnd.head.block_count[0] = (data->blocks >> 8) & 0xFF;
1130 vub300->cmnd.head.block_count[1] = (data->blocks >> 0) & 0xFF;
1131 vub300->cmnd.head.block_size[0] = (data->blksz >> 8) & 0xFF;
1132 vub300->cmnd.head.block_size[1] = (data->blksz >> 0) & 0xFF;
1133 vub300->cmnd.head.command_type =
1134 (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1135 vub300->cmnd.head.transfer_size[0] =
1136 (vub300->datasize >> 24) & 0xFF;
1137 vub300->cmnd.head.transfer_size[1] =
1138 (vub300->datasize >> 16) & 0xFF;
1139 vub300->cmnd.head.transfer_size[2] =
1140 (vub300->datasize >> 8) & 0xFF;
1141 vub300->cmnd.head.transfer_size[3] =
1142 (vub300->datasize >> 0) & 0xFF;
1143 if (vub300->datasize < vub300->fbs[0]) {
1144 vub300->cmnd.head.block_count[0] = 0;
1145 vub300->cmnd.head.block_count[1] = 0;
1146 }
1147 }
1148 if (vub300->cmnd.head.block_size[0] || vub300->cmnd.head.block_size[1]) {
1149 u16 block_size = vub300->cmnd.head.block_size[1] |
1150 (vub300->cmnd.head.block_size[0] << 8);
1151 u16 block_boundary = FIRMWARE_BLOCK_BOUNDARY -
1152 (FIRMWARE_BLOCK_BOUNDARY % block_size);
1153 vub300->cmnd.head.block_boundary[0] =
1154 (block_boundary >> 8) & 0xFF;
1155 vub300->cmnd.head.block_boundary[1] =
1156 (block_boundary >> 0) & 0xFF;
1157 } else {
1158 vub300->cmnd.head.block_boundary[0] = 0;
1159 vub300->cmnd.head.block_boundary[1] = 0;
1160 }
1161 usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
1162 usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep),
1163 &vub300->cmnd, sizeof(vub300->cmnd),
1164 command_out_completed, vub300);
1165 retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
1166 if (retval < 0) {
1167 cmd->error = retval;
1168 complete(&vub300->command_complete);
1169 return;
1170 } else {
1171 return;
1172 }
1173 }
1174
1175 /*
1176 * timer callback runs in atomic mode
1177 * so it cannot call usb_kill_urb()
1178 */
vub300_sg_timed_out(struct timer_list * t)1179 static void vub300_sg_timed_out(struct timer_list *t)
1180 {
1181 struct vub300_mmc_host *vub300 = timer_container_of(vub300, t,
1182 sg_transfer_timer);
1183 vub300->usb_timed_out = 1;
1184 usb_sg_cancel(&vub300->sg_request);
1185 usb_unlink_urb(vub300->command_out_urb);
1186 usb_unlink_urb(vub300->command_res_urb);
1187 }
1188
roundup_to_multiple_of_64(u16 number)1189 static u16 roundup_to_multiple_of_64(u16 number)
1190 {
1191 return 0xFFC0 & (0x3F + number);
1192 }
1193
1194 /*
1195 * this is a separate function to solve the 80 column width restriction
1196 */
__download_offload_pseudocode(struct vub300_mmc_host * vub300,const struct firmware * fw)1197 static void __download_offload_pseudocode(struct vub300_mmc_host *vub300,
1198 const struct firmware *fw)
1199 {
1200 u8 register_count = 0;
1201 u16 ts = 0;
1202 u16 interrupt_size = 0;
1203 const u8 *data = fw->data;
1204 int size = fw->size;
1205 u8 c;
1206 dev_info(&vub300->udev->dev, "using %s for SDIO offload processing\n",
1207 vub300->vub_name);
1208 do {
1209 c = *data++;
1210 } while (size-- && c); /* skip comment */
1211 dev_info(&vub300->udev->dev, "using offload firmware %s %s\n", fw->data,
1212 vub300->vub_name);
1213 if (size < 4) {
1214 dev_err(&vub300->udev->dev,
1215 "corrupt offload pseudocode in firmware %s\n",
1216 vub300->vub_name);
1217 strscpy(vub300->vub_name, "corrupt offload pseudocode",
1218 sizeof(vub300->vub_name));
1219 return;
1220 }
1221 interrupt_size += *data++;
1222 size -= 1;
1223 interrupt_size <<= 8;
1224 interrupt_size += *data++;
1225 size -= 1;
1226 if (interrupt_size < size) {
1227 u16 xfer_length = roundup_to_multiple_of_64(interrupt_size);
1228 u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1229 if (xfer_buffer) {
1230 int retval;
1231 memcpy(xfer_buffer, data, interrupt_size);
1232 memset(xfer_buffer + interrupt_size, 0,
1233 xfer_length - interrupt_size);
1234 size -= interrupt_size;
1235 data += interrupt_size;
1236 retval =
1237 usb_control_msg(vub300->udev,
1238 usb_sndctrlpipe(vub300->udev, 0),
1239 SET_INTERRUPT_PSEUDOCODE,
1240 USB_DIR_OUT | USB_TYPE_VENDOR |
1241 USB_RECIP_DEVICE, 0x0000, 0x0000,
1242 xfer_buffer, xfer_length, 1000);
1243 kfree(xfer_buffer);
1244 if (retval < 0)
1245 goto copy_error_message;
1246 } else {
1247 dev_err(&vub300->udev->dev,
1248 "not enough memory for xfer buffer to send"
1249 " INTERRUPT_PSEUDOCODE for %s %s\n", fw->data,
1250 vub300->vub_name);
1251 strscpy(vub300->vub_name,
1252 "SDIO interrupt pseudocode download failed",
1253 sizeof(vub300->vub_name));
1254 return;
1255 }
1256 } else {
1257 dev_err(&vub300->udev->dev,
1258 "corrupt interrupt pseudocode in firmware %s %s\n",
1259 fw->data, vub300->vub_name);
1260 strscpy(vub300->vub_name, "corrupt interrupt pseudocode",
1261 sizeof(vub300->vub_name));
1262 return;
1263 }
1264 ts += *data++;
1265 size -= 1;
1266 ts <<= 8;
1267 ts += *data++;
1268 size -= 1;
1269 if (ts < size) {
1270 u16 xfer_length = roundup_to_multiple_of_64(ts);
1271 u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1272 if (xfer_buffer) {
1273 int retval;
1274 memcpy(xfer_buffer, data, ts);
1275 memset(xfer_buffer + ts, 0,
1276 xfer_length - ts);
1277 size -= ts;
1278 data += ts;
1279 retval =
1280 usb_control_msg(vub300->udev,
1281 usb_sndctrlpipe(vub300->udev, 0),
1282 SET_TRANSFER_PSEUDOCODE,
1283 USB_DIR_OUT | USB_TYPE_VENDOR |
1284 USB_RECIP_DEVICE, 0x0000, 0x0000,
1285 xfer_buffer, xfer_length, 1000);
1286 kfree(xfer_buffer);
1287 if (retval < 0)
1288 goto copy_error_message;
1289 } else {
1290 dev_err(&vub300->udev->dev,
1291 "not enough memory for xfer buffer to send"
1292 " TRANSFER_PSEUDOCODE for %s %s\n", fw->data,
1293 vub300->vub_name);
1294 strscpy(vub300->vub_name,
1295 "SDIO transfer pseudocode download failed",
1296 sizeof(vub300->vub_name));
1297 return;
1298 }
1299 } else {
1300 dev_err(&vub300->udev->dev,
1301 "corrupt transfer pseudocode in firmware %s %s\n",
1302 fw->data, vub300->vub_name);
1303 strscpy(vub300->vub_name, "corrupt transfer pseudocode",
1304 sizeof(vub300->vub_name));
1305 return;
1306 }
1307 register_count += *data++;
1308 size -= 1;
1309 if (register_count * 4 == size) {
1310 int I = vub300->dynamic_register_count = register_count;
1311 int i = 0;
1312 while (I--) {
1313 unsigned int func_num = 0;
1314 vub300->sdio_register[i].func_num = *data++;
1315 size -= 1;
1316 func_num += *data++;
1317 size -= 1;
1318 func_num <<= 8;
1319 func_num += *data++;
1320 size -= 1;
1321 func_num <<= 8;
1322 func_num += *data++;
1323 size -= 1;
1324 vub300->sdio_register[i].sdio_reg = func_num;
1325 vub300->sdio_register[i].activate = 1;
1326 vub300->sdio_register[i].prepared = 0;
1327 i += 1;
1328 }
1329 dev_info(&vub300->udev->dev,
1330 "initialized %d dynamic pseudocode registers\n",
1331 vub300->dynamic_register_count);
1332 return;
1333 } else {
1334 dev_err(&vub300->udev->dev,
1335 "corrupt dynamic registers in firmware %s\n",
1336 vub300->vub_name);
1337 strscpy(vub300->vub_name, "corrupt dynamic registers",
1338 sizeof(vub300->vub_name));
1339 return;
1340 }
1341
1342 copy_error_message:
1343 strscpy(vub300->vub_name, "SDIO pseudocode download failed",
1344 sizeof(vub300->vub_name));
1345 }
1346
1347 /*
1348 * if the binary containing the EMPTY PseudoCode can not be found
1349 * vub300->vub_name is set anyway in order to prevent an automatic retry
1350 */
download_offload_pseudocode(struct vub300_mmc_host * vub300)1351 static void download_offload_pseudocode(struct vub300_mmc_host *vub300)
1352 {
1353 struct mmc_card *card = vub300->mmc->card;
1354 int sdio_funcs = card->sdio_funcs;
1355 const struct firmware *fw = NULL;
1356 int l = snprintf(vub300->vub_name, sizeof(vub300->vub_name),
1357 "vub_%04X%04X", card->cis.vendor, card->cis.device);
1358 int n = 0;
1359 int retval;
1360 for (n = 0; n < sdio_funcs; n++) {
1361 struct sdio_func *sf = card->sdio_func[n];
1362 l += scnprintf(vub300->vub_name + l,
1363 sizeof(vub300->vub_name) - l, "_%04X%04X",
1364 sf->vendor, sf->device);
1365 }
1366 snprintf(vub300->vub_name + l, sizeof(vub300->vub_name) - l, ".bin");
1367 dev_info(&vub300->udev->dev, "requesting offload firmware %s\n",
1368 vub300->vub_name);
1369 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1370 if (retval < 0) {
1371 strscpy(vub300->vub_name, "vub_default.bin",
1372 sizeof(vub300->vub_name));
1373 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1374 if (retval < 0) {
1375 strscpy(vub300->vub_name,
1376 "no SDIO offload firmware found",
1377 sizeof(vub300->vub_name));
1378 } else {
1379 __download_offload_pseudocode(vub300, fw);
1380 release_firmware(fw);
1381 }
1382 } else {
1383 __download_offload_pseudocode(vub300, fw);
1384 release_firmware(fw);
1385 }
1386 }
1387
vub300_usb_bulk_msg_completion(struct urb * urb)1388 static void vub300_usb_bulk_msg_completion(struct urb *urb)
1389 { /* urb completion handler - hardirq */
1390 complete((struct completion *)urb->context);
1391 }
1392
vub300_usb_bulk_msg(struct vub300_mmc_host * vub300,unsigned int pipe,void * data,int len,int * actual_length,int timeout_msecs)1393 static int vub300_usb_bulk_msg(struct vub300_mmc_host *vub300,
1394 unsigned int pipe, void *data, int len,
1395 int *actual_length, int timeout_msecs)
1396 {
1397 /* cmd_mutex is held by vub300_cmndwork_thread */
1398 struct usb_device *usb_dev = vub300->udev;
1399 struct completion done;
1400 int retval;
1401 vub300->urb = usb_alloc_urb(0, GFP_KERNEL);
1402 if (!vub300->urb)
1403 return -ENOMEM;
1404 usb_fill_bulk_urb(vub300->urb, usb_dev, pipe, data, len,
1405 vub300_usb_bulk_msg_completion, NULL);
1406 init_completion(&done);
1407 vub300->urb->context = &done;
1408 vub300->urb->actual_length = 0;
1409 retval = usb_submit_urb(vub300->urb, GFP_KERNEL);
1410 if (unlikely(retval))
1411 goto out;
1412 if (!wait_for_completion_timeout
1413 (&done, msecs_to_jiffies(timeout_msecs))) {
1414 retval = -ETIMEDOUT;
1415 usb_kill_urb(vub300->urb);
1416 } else {
1417 retval = vub300->urb->status;
1418 }
1419 out:
1420 *actual_length = vub300->urb->actual_length;
1421 usb_free_urb(vub300->urb);
1422 vub300->urb = NULL;
1423 return retval;
1424 }
1425
__command_read_data(struct vub300_mmc_host * vub300,struct mmc_command * cmd,struct mmc_data * data)1426 static int __command_read_data(struct vub300_mmc_host *vub300,
1427 struct mmc_command *cmd, struct mmc_data *data)
1428 {
1429 /* cmd_mutex is held by vub300_cmndwork_thread */
1430 int linear_length = vub300->datasize;
1431 int padded_length = vub300->large_usb_packets ?
1432 ((511 + linear_length) >> 9) << 9 :
1433 ((63 + linear_length) >> 6) << 6;
1434 if ((padded_length == linear_length) || !pad_input_to_usb_pkt) {
1435 int result;
1436 unsigned pipe;
1437 pipe = usb_rcvbulkpipe(vub300->udev, vub300->data_inp_ep);
1438 result = usb_sg_init(&vub300->sg_request, vub300->udev,
1439 pipe, 0, data->sg,
1440 data->sg_len, 0, GFP_KERNEL);
1441 if (result < 0) {
1442 usb_unlink_urb(vub300->command_out_urb);
1443 usb_unlink_urb(vub300->command_res_urb);
1444 cmd->error = result;
1445 data->bytes_xfered = 0;
1446 return 0;
1447 } else {
1448 vub300->sg_transfer_timer.expires =
1449 jiffies + msecs_to_jiffies(2000 +
1450 (linear_length / 16384));
1451 add_timer(&vub300->sg_transfer_timer);
1452 usb_sg_wait(&vub300->sg_request);
1453 timer_delete(&vub300->sg_transfer_timer);
1454 if (vub300->sg_request.status < 0) {
1455 cmd->error = vub300->sg_request.status;
1456 data->bytes_xfered = 0;
1457 return 0;
1458 } else {
1459 data->bytes_xfered = vub300->datasize;
1460 return linear_length;
1461 }
1462 }
1463 } else {
1464 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1465 if (buf) {
1466 int result;
1467 unsigned pipe = usb_rcvbulkpipe(vub300->udev,
1468 vub300->data_inp_ep);
1469 int actual_length = 0;
1470 result = vub300_usb_bulk_msg(vub300, pipe, buf,
1471 padded_length, &actual_length,
1472 2000 + (padded_length / 16384));
1473 if (result < 0) {
1474 cmd->error = result;
1475 data->bytes_xfered = 0;
1476 kfree(buf);
1477 return 0;
1478 } else if (actual_length < linear_length) {
1479 cmd->error = -EREMOTEIO;
1480 data->bytes_xfered = 0;
1481 kfree(buf);
1482 return 0;
1483 } else {
1484 sg_copy_from_buffer(data->sg, data->sg_len, buf,
1485 linear_length);
1486 kfree(buf);
1487 data->bytes_xfered = vub300->datasize;
1488 return linear_length;
1489 }
1490 } else {
1491 cmd->error = -ENOMEM;
1492 data->bytes_xfered = 0;
1493 return 0;
1494 }
1495 }
1496 }
1497
__command_write_data(struct vub300_mmc_host * vub300,struct mmc_command * cmd,struct mmc_data * data)1498 static int __command_write_data(struct vub300_mmc_host *vub300,
1499 struct mmc_command *cmd, struct mmc_data *data)
1500 {
1501 /* cmd_mutex is held by vub300_cmndwork_thread */
1502 unsigned pipe = usb_sndbulkpipe(vub300->udev, vub300->data_out_ep);
1503 int linear_length = vub300->datasize;
1504 int modulo_64_length = linear_length & 0x003F;
1505 int modulo_512_length = linear_length & 0x01FF;
1506 if (linear_length < 64) {
1507 int result;
1508 int actual_length;
1509 sg_copy_to_buffer(data->sg, data->sg_len,
1510 vub300->padded_buffer,
1511 sizeof(vub300->padded_buffer));
1512 memset(vub300->padded_buffer + linear_length, 0,
1513 sizeof(vub300->padded_buffer) - linear_length);
1514 result = vub300_usb_bulk_msg(vub300, pipe, vub300->padded_buffer,
1515 sizeof(vub300->padded_buffer),
1516 &actual_length, 2000 +
1517 (sizeof(vub300->padded_buffer) /
1518 16384));
1519 if (result < 0) {
1520 cmd->error = result;
1521 data->bytes_xfered = 0;
1522 } else {
1523 data->bytes_xfered = vub300->datasize;
1524 }
1525 } else if ((!vub300->large_usb_packets && (0 < modulo_64_length)) ||
1526 (vub300->large_usb_packets && (64 > modulo_512_length))
1527 ) { /* don't you just love these work-rounds */
1528 int padded_length = ((63 + linear_length) >> 6) << 6;
1529 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1530 if (buf) {
1531 int result;
1532 int actual_length;
1533 sg_copy_to_buffer(data->sg, data->sg_len, buf,
1534 padded_length);
1535 memset(buf + linear_length, 0,
1536 padded_length - linear_length);
1537 result =
1538 vub300_usb_bulk_msg(vub300, pipe, buf,
1539 padded_length, &actual_length,
1540 2000 + padded_length / 16384);
1541 kfree(buf);
1542 if (result < 0) {
1543 cmd->error = result;
1544 data->bytes_xfered = 0;
1545 } else {
1546 data->bytes_xfered = vub300->datasize;
1547 }
1548 } else {
1549 cmd->error = -ENOMEM;
1550 data->bytes_xfered = 0;
1551 }
1552 } else { /* no data padding required */
1553 int result;
1554 unsigned char buf[64 * 4];
1555 sg_copy_to_buffer(data->sg, data->sg_len, buf, sizeof(buf));
1556 result = usb_sg_init(&vub300->sg_request, vub300->udev,
1557 pipe, 0, data->sg,
1558 data->sg_len, 0, GFP_KERNEL);
1559 if (result < 0) {
1560 usb_unlink_urb(vub300->command_out_urb);
1561 usb_unlink_urb(vub300->command_res_urb);
1562 cmd->error = result;
1563 data->bytes_xfered = 0;
1564 } else {
1565 vub300->sg_transfer_timer.expires =
1566 jiffies + msecs_to_jiffies(2000 +
1567 linear_length / 16384);
1568 add_timer(&vub300->sg_transfer_timer);
1569 usb_sg_wait(&vub300->sg_request);
1570 if (cmd->error) {
1571 data->bytes_xfered = 0;
1572 } else {
1573 timer_delete(&vub300->sg_transfer_timer);
1574 if (vub300->sg_request.status < 0) {
1575 cmd->error = vub300->sg_request.status;
1576 data->bytes_xfered = 0;
1577 } else {
1578 data->bytes_xfered = vub300->datasize;
1579 }
1580 }
1581 }
1582 }
1583 return linear_length;
1584 }
1585
__vub300_command_response(struct vub300_mmc_host * vub300,struct mmc_command * cmd,struct mmc_data * data,int data_length)1586 static void __vub300_command_response(struct vub300_mmc_host *vub300,
1587 struct mmc_command *cmd,
1588 struct mmc_data *data, int data_length)
1589 {
1590 /* cmd_mutex is held by vub300_cmndwork_thread */
1591 long respretval;
1592 int msec_timeout = 1000 + data_length / 4;
1593 respretval =
1594 wait_for_completion_timeout(&vub300->command_complete,
1595 msecs_to_jiffies(msec_timeout));
1596 if (respretval == 0) { /* TIMED OUT */
1597 /* we don't know which of "out" and "res" if any failed */
1598 int result;
1599 vub300->usb_timed_out = 1;
1600 usb_kill_urb(vub300->command_out_urb);
1601 usb_kill_urb(vub300->command_res_urb);
1602 cmd->error = -ETIMEDOUT;
1603 result = usb_lock_device_for_reset(vub300->udev,
1604 vub300->interface);
1605 if (result == 0) {
1606 result = usb_reset_device(vub300->udev);
1607 usb_unlock_device(vub300->udev);
1608 }
1609 } else if (respretval < 0) {
1610 /* we don't know which of "out" and "res" if any failed */
1611 usb_kill_urb(vub300->command_out_urb);
1612 usb_kill_urb(vub300->command_res_urb);
1613 cmd->error = respretval;
1614 } else if (cmd->error) {
1615 /*
1616 * the error occurred sending the command
1617 * or receiving the response
1618 */
1619 } else if (vub300->command_out_urb->status) {
1620 vub300->usb_transport_fail = vub300->command_out_urb->status;
1621 cmd->error = -EPROTO == vub300->command_out_urb->status ?
1622 -ESHUTDOWN : vub300->command_out_urb->status;
1623 } else if (vub300->command_res_urb->status) {
1624 vub300->usb_transport_fail = vub300->command_res_urb->status;
1625 cmd->error = -EPROTO == vub300->command_res_urb->status ?
1626 -ESHUTDOWN : vub300->command_res_urb->status;
1627 } else if (vub300->resp.common.header_type == 0x00) {
1628 /*
1629 * the command completed successfully
1630 * and there was no piggybacked data
1631 */
1632 } else if (vub300->resp.common.header_type == RESPONSE_ERROR) {
1633 cmd->error =
1634 vub300_response_error(vub300->resp.error.error_code);
1635 if (vub300->data)
1636 usb_sg_cancel(&vub300->sg_request);
1637 } else if (vub300->resp.common.header_type == RESPONSE_PIGGYBACKED) {
1638 int offloaded_data_length =
1639 vub300->resp.common.header_size -
1640 sizeof(struct sd_register_header);
1641 int register_count = offloaded_data_length >> 3;
1642 int ri = 0;
1643 while (register_count--) {
1644 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1645 ri += 1;
1646 }
1647 vub300->resp.common.header_size =
1648 sizeof(struct sd_register_header);
1649 vub300->resp.common.header_type = 0x00;
1650 cmd->error = 0;
1651 } else if (vub300->resp.common.header_type == RESPONSE_PIG_DISABLED) {
1652 int offloaded_data_length =
1653 vub300->resp.common.header_size -
1654 sizeof(struct sd_register_header);
1655 int register_count = offloaded_data_length >> 3;
1656 int ri = 0;
1657 while (register_count--) {
1658 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1659 ri += 1;
1660 }
1661 mutex_lock(&vub300->irq_mutex);
1662 if (vub300->irqs_queued) {
1663 vub300->irqs_queued += 1;
1664 } else if (vub300->irq_enabled) {
1665 vub300->irqs_queued += 1;
1666 vub300_queue_poll_work(vub300, 0);
1667 } else {
1668 vub300->irqs_queued += 1;
1669 }
1670 vub300->irq_disabled = 1;
1671 mutex_unlock(&vub300->irq_mutex);
1672 vub300->resp.common.header_size =
1673 sizeof(struct sd_register_header);
1674 vub300->resp.common.header_type = 0x00;
1675 cmd->error = 0;
1676 } else if (vub300->resp.common.header_type == RESPONSE_PIG_ENABLED) {
1677 int offloaded_data_length =
1678 vub300->resp.common.header_size -
1679 sizeof(struct sd_register_header);
1680 int register_count = offloaded_data_length >> 3;
1681 int ri = 0;
1682 while (register_count--) {
1683 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1684 ri += 1;
1685 }
1686 mutex_lock(&vub300->irq_mutex);
1687 if (vub300->irqs_queued) {
1688 vub300->irqs_queued += 1;
1689 } else if (vub300->irq_enabled) {
1690 vub300->irqs_queued += 1;
1691 vub300_queue_poll_work(vub300, 0);
1692 } else {
1693 vub300->irqs_queued += 1;
1694 }
1695 vub300->irq_disabled = 0;
1696 mutex_unlock(&vub300->irq_mutex);
1697 vub300->resp.common.header_size =
1698 sizeof(struct sd_register_header);
1699 vub300->resp.common.header_type = 0x00;
1700 cmd->error = 0;
1701 } else {
1702 cmd->error = -EINVAL;
1703 }
1704 }
1705
construct_request_response(struct vub300_mmc_host * vub300,struct mmc_command * cmd)1706 static void construct_request_response(struct vub300_mmc_host *vub300,
1707 struct mmc_command *cmd)
1708 {
1709 int resp_len = vub300->resp_len;
1710 int less_cmd = (17 == resp_len) ? resp_len : resp_len - 1;
1711 int bytes = 3 & less_cmd;
1712 int words = less_cmd >> 2;
1713 u8 *r = vub300->resp.response.command_response;
1714
1715 if (!resp_len)
1716 return;
1717 if (bytes == 3) {
1718 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1719 | (r[2 + (words << 2)] << 16)
1720 | (r[3 + (words << 2)] << 8);
1721 } else if (bytes == 2) {
1722 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1723 | (r[2 + (words << 2)] << 16);
1724 } else if (bytes == 1) {
1725 cmd->resp[words] = (r[1 + (words << 2)] << 24);
1726 }
1727 while (words-- > 0) {
1728 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1729 | (r[2 + (words << 2)] << 16)
1730 | (r[3 + (words << 2)] << 8)
1731 | (r[4 + (words << 2)] << 0);
1732 }
1733 if ((cmd->opcode == 53) && (0x000000FF & cmd->resp[0]))
1734 cmd->resp[0] &= 0xFFFFFF00;
1735 }
1736
1737 /* this thread runs only when there is an upper level command req outstanding */
vub300_cmndwork_thread(struct work_struct * work)1738 static void vub300_cmndwork_thread(struct work_struct *work)
1739 {
1740 struct vub300_mmc_host *vub300 =
1741 container_of(work, struct vub300_mmc_host, cmndwork);
1742 if (!vub300->interface) {
1743 kref_put(&vub300->kref, vub300_delete);
1744 return;
1745 } else {
1746 struct mmc_request *req = vub300->req;
1747 struct mmc_command *cmd = vub300->cmd;
1748 struct mmc_data *data = vub300->data;
1749 int data_length;
1750 mutex_lock(&vub300->cmd_mutex);
1751 init_completion(&vub300->command_complete);
1752 if (likely(vub300->vub_name[0]) || !vub300->mmc->card) {
1753 /*
1754 * the name of the EMPTY Pseudo firmware file
1755 * is used as a flag to indicate that the file
1756 * has been already downloaded to the VUB300 chip
1757 */
1758 } else if (0 == vub300->mmc->card->sdio_funcs) {
1759 strscpy(vub300->vub_name, "SD memory device",
1760 sizeof(vub300->vub_name));
1761 } else {
1762 download_offload_pseudocode(vub300);
1763 }
1764 send_command(vub300);
1765 if (!data)
1766 data_length = 0;
1767 else if (MMC_DATA_READ & data->flags)
1768 data_length = __command_read_data(vub300, cmd, data);
1769 else
1770 data_length = __command_write_data(vub300, cmd, data);
1771 __vub300_command_response(vub300, cmd, data, data_length);
1772 vub300->req = NULL;
1773 vub300->cmd = NULL;
1774 vub300->data = NULL;
1775 if (cmd->error) {
1776 if (cmd->error == -ENOMEDIUM)
1777 check_vub300_port_status(vub300);
1778 mutex_unlock(&vub300->cmd_mutex);
1779 mmc_request_done(vub300->mmc, req);
1780 kref_put(&vub300->kref, vub300_delete);
1781 return;
1782 } else {
1783 construct_request_response(vub300, cmd);
1784 vub300->resp_len = 0;
1785 mutex_unlock(&vub300->cmd_mutex);
1786 kref_put(&vub300->kref, vub300_delete);
1787 mmc_request_done(vub300->mmc, req);
1788 return;
1789 }
1790 }
1791 }
1792
examine_cyclic_buffer(struct vub300_mmc_host * vub300,struct mmc_command * cmd,u8 Function)1793 static int examine_cyclic_buffer(struct vub300_mmc_host *vub300,
1794 struct mmc_command *cmd, u8 Function)
1795 {
1796 /* cmd_mutex is held by vub300_mmc_request */
1797 u8 cmd0 = 0xFF & (cmd->arg >> 24);
1798 u8 cmd1 = 0xFF & (cmd->arg >> 16);
1799 u8 cmd2 = 0xFF & (cmd->arg >> 8);
1800 u8 cmd3 = 0xFF & (cmd->arg >> 0);
1801 int first = MAXREGMASK & vub300->fn[Function].offload_point;
1802 struct offload_registers_access *rf = &vub300->fn[Function].reg[first];
1803 if (cmd0 == rf->command_byte[0] &&
1804 cmd1 == rf->command_byte[1] &&
1805 cmd2 == rf->command_byte[2] &&
1806 cmd3 == rf->command_byte[3]) {
1807 u8 checksum = 0x00;
1808 cmd->resp[1] = checksum << 24;
1809 cmd->resp[0] = (rf->Respond_Byte[0] << 24)
1810 | (rf->Respond_Byte[1] << 16)
1811 | (rf->Respond_Byte[2] << 8)
1812 | (rf->Respond_Byte[3] << 0);
1813 vub300->fn[Function].offload_point += 1;
1814 vub300->fn[Function].offload_count -= 1;
1815 vub300->total_offload_count -= 1;
1816 return 1;
1817 } else {
1818 int delta = 1; /* because it does not match the first one */
1819 u8 register_count = vub300->fn[Function].offload_count - 1;
1820 u32 register_point = vub300->fn[Function].offload_point + 1;
1821 while (0 < register_count) {
1822 int point = MAXREGMASK & register_point;
1823 struct offload_registers_access *r =
1824 &vub300->fn[Function].reg[point];
1825 if (cmd0 == r->command_byte[0] &&
1826 cmd1 == r->command_byte[1] &&
1827 cmd2 == r->command_byte[2] &&
1828 cmd3 == r->command_byte[3]) {
1829 u8 checksum = 0x00;
1830 cmd->resp[1] = checksum << 24;
1831 cmd->resp[0] = (r->Respond_Byte[0] << 24)
1832 | (r->Respond_Byte[1] << 16)
1833 | (r->Respond_Byte[2] << 8)
1834 | (r->Respond_Byte[3] << 0);
1835 vub300->fn[Function].offload_point += delta;
1836 vub300->fn[Function].offload_count -= delta;
1837 vub300->total_offload_count -= delta;
1838 return 1;
1839 } else {
1840 register_point += 1;
1841 register_count -= 1;
1842 delta += 1;
1843 continue;
1844 }
1845 }
1846 return 0;
1847 }
1848 }
1849
satisfy_request_from_offloaded_data(struct vub300_mmc_host * vub300,struct mmc_command * cmd)1850 static int satisfy_request_from_offloaded_data(struct vub300_mmc_host *vub300,
1851 struct mmc_command *cmd)
1852 {
1853 /* cmd_mutex is held by vub300_mmc_request */
1854 u8 regs = vub300->dynamic_register_count;
1855 u8 i = 0;
1856 u8 func = FUN(cmd);
1857 u32 reg = REG(cmd);
1858 while (0 < regs--) {
1859 if ((vub300->sdio_register[i].func_num == func) &&
1860 (vub300->sdio_register[i].sdio_reg == reg)) {
1861 if (!vub300->sdio_register[i].prepared) {
1862 return 0;
1863 } else if ((0x80000000 & cmd->arg) == 0x80000000) {
1864 /*
1865 * a write to a dynamic register
1866 * nullifies our offloaded value
1867 */
1868 vub300->sdio_register[i].prepared = 0;
1869 return 0;
1870 } else {
1871 u8 checksum = 0x00;
1872 u8 rsp0 = 0x00;
1873 u8 rsp1 = 0x00;
1874 u8 rsp2 = vub300->sdio_register[i].response;
1875 u8 rsp3 = vub300->sdio_register[i].regvalue;
1876 vub300->sdio_register[i].prepared = 0;
1877 cmd->resp[1] = checksum << 24;
1878 cmd->resp[0] = (rsp0 << 24)
1879 | (rsp1 << 16)
1880 | (rsp2 << 8)
1881 | (rsp3 << 0);
1882 return 1;
1883 }
1884 } else {
1885 i += 1;
1886 continue;
1887 }
1888 }
1889 if (vub300->total_offload_count == 0)
1890 return 0;
1891 else if (vub300->fn[func].offload_count == 0)
1892 return 0;
1893 else
1894 return examine_cyclic_buffer(vub300, cmd, func);
1895 }
1896
vub300_mmc_request(struct mmc_host * mmc,struct mmc_request * req)1897 static void vub300_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1898 { /* NOT irq */
1899 struct mmc_command *cmd = req->cmd;
1900 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
1901 if (!vub300->interface) {
1902 cmd->error = -ESHUTDOWN;
1903 mmc_request_done(mmc, req);
1904 return;
1905 } else {
1906 struct mmc_data *data = req->data;
1907 if (!vub300->card_powered) {
1908 cmd->error = -ENOMEDIUM;
1909 mmc_request_done(mmc, req);
1910 return;
1911 }
1912 if (!vub300->card_present) {
1913 cmd->error = -ENOMEDIUM;
1914 mmc_request_done(mmc, req);
1915 return;
1916 }
1917 if (vub300->usb_transport_fail) {
1918 cmd->error = vub300->usb_transport_fail;
1919 mmc_request_done(mmc, req);
1920 return;
1921 }
1922 if (!vub300->interface) {
1923 cmd->error = -ENODEV;
1924 mmc_request_done(mmc, req);
1925 return;
1926 }
1927 kref_get(&vub300->kref);
1928 mutex_lock(&vub300->cmd_mutex);
1929 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
1930 /*
1931 * for performance we have to return immediately
1932 * if the requested data has been offloaded
1933 */
1934 if (cmd->opcode == 52 &&
1935 satisfy_request_from_offloaded_data(vub300, cmd)) {
1936 cmd->error = 0;
1937 mutex_unlock(&vub300->cmd_mutex);
1938 kref_put(&vub300->kref, vub300_delete);
1939 mmc_request_done(mmc, req);
1940 return;
1941 } else {
1942 vub300->cmd = cmd;
1943 vub300->req = req;
1944 vub300->data = data;
1945 if (data)
1946 vub300->datasize = data->blksz * data->blocks;
1947 else
1948 vub300->datasize = 0;
1949 vub300_queue_cmnd_work(vub300);
1950 mutex_unlock(&vub300->cmd_mutex);
1951 kref_put(&vub300->kref, vub300_delete);
1952 /*
1953 * the kernel lock diagnostics complain
1954 * if the cmd_mutex * is "passed on"
1955 * to the cmndwork thread,
1956 * so we must release it now
1957 * and re-acquire it in the cmndwork thread
1958 */
1959 }
1960 }
1961 }
1962
__set_clock_speed(struct vub300_mmc_host * vub300,u8 buf[8],struct mmc_ios * ios)1963 static void __set_clock_speed(struct vub300_mmc_host *vub300, u8 buf[8],
1964 struct mmc_ios *ios)
1965 {
1966 int buf_array_size = 8; /* ARRAY_SIZE(buf) does not work !!! */
1967 int retval;
1968 u32 kHzClock;
1969 if (ios->clock >= 48000000)
1970 kHzClock = 48000;
1971 else if (ios->clock >= 24000000)
1972 kHzClock = 24000;
1973 else if (ios->clock >= 20000000)
1974 kHzClock = 20000;
1975 else if (ios->clock >= 15000000)
1976 kHzClock = 15000;
1977 else if (ios->clock >= 200000)
1978 kHzClock = 200;
1979 else
1980 kHzClock = 0;
1981 {
1982 int i;
1983 u64 c = kHzClock;
1984 for (i = 0; i < buf_array_size; i++) {
1985 buf[i] = c;
1986 c >>= 8;
1987 }
1988 }
1989 retval =
1990 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
1991 SET_CLOCK_SPEED,
1992 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1993 0x00, 0x00, buf, buf_array_size, 1000);
1994 if (retval != 8) {
1995 dev_err(&vub300->udev->dev, "SET_CLOCK_SPEED"
1996 " %dkHz failed with retval=%d\n", kHzClock, retval);
1997 } else {
1998 dev_dbg(&vub300->udev->dev, "SET_CLOCK_SPEED"
1999 " %dkHz\n", kHzClock);
2000 }
2001 }
2002
vub300_mmc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)2003 static void vub300_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2004 { /* NOT irq */
2005 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2006 if (!vub300->interface)
2007 return;
2008 kref_get(&vub300->kref);
2009 mutex_lock(&vub300->cmd_mutex);
2010 if ((ios->power_mode == MMC_POWER_OFF) && vub300->card_powered) {
2011 vub300->card_powered = 0;
2012 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2013 SET_SD_POWER,
2014 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2015 0x0000, 0x0000, NULL, 0, 1000);
2016 /* must wait for the VUB300 u-proc to boot up */
2017 msleep(600);
2018 } else if ((ios->power_mode == MMC_POWER_UP) && !vub300->card_powered) {
2019 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2020 SET_SD_POWER,
2021 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2022 0x0001, 0x0000, NULL, 0, 1000);
2023 msleep(600);
2024 vub300->card_powered = 1;
2025 } else if (ios->power_mode == MMC_POWER_ON) {
2026 u8 *buf = kmalloc(8, GFP_KERNEL);
2027 if (buf) {
2028 __set_clock_speed(vub300, buf, ios);
2029 kfree(buf);
2030 }
2031 } else {
2032 /* this should mean no change of state */
2033 }
2034 mutex_unlock(&vub300->cmd_mutex);
2035 kref_put(&vub300->kref, vub300_delete);
2036 }
2037
vub300_mmc_get_ro(struct mmc_host * mmc)2038 static int vub300_mmc_get_ro(struct mmc_host *mmc)
2039 {
2040 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2041 return vub300->read_only;
2042 }
2043
vub300_enable_sdio_irq(struct mmc_host * mmc,int enable)2044 static void vub300_enable_sdio_irq(struct mmc_host *mmc, int enable)
2045 { /* NOT irq */
2046 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2047 if (!vub300->interface)
2048 return;
2049 kref_get(&vub300->kref);
2050 if (enable) {
2051 set_current_state(TASK_RUNNING);
2052 mutex_lock(&vub300->irq_mutex);
2053 if (vub300->irqs_queued) {
2054 vub300->irqs_queued -= 1;
2055 mmc_signal_sdio_irq(vub300->mmc);
2056 } else if (vub300->irq_disabled) {
2057 vub300->irq_disabled = 0;
2058 vub300->irq_enabled = 1;
2059 vub300_queue_poll_work(vub300, 0);
2060 } else if (vub300->irq_enabled) {
2061 /* this should not happen, so we will just ignore it */
2062 } else {
2063 vub300->irq_enabled = 1;
2064 vub300_queue_poll_work(vub300, 0);
2065 }
2066 mutex_unlock(&vub300->irq_mutex);
2067 set_current_state(TASK_INTERRUPTIBLE);
2068 } else {
2069 vub300->irq_enabled = 0;
2070 }
2071 kref_put(&vub300->kref, vub300_delete);
2072 }
2073
2074 static const struct mmc_host_ops vub300_mmc_ops = {
2075 .request = vub300_mmc_request,
2076 .set_ios = vub300_mmc_set_ios,
2077 .get_ro = vub300_mmc_get_ro,
2078 .enable_sdio_irq = vub300_enable_sdio_irq,
2079 };
2080
vub300_probe(struct usb_interface * interface,const struct usb_device_id * id)2081 static int vub300_probe(struct usb_interface *interface,
2082 const struct usb_device_id *id)
2083 { /* NOT irq */
2084 struct vub300_mmc_host *vub300;
2085 struct usb_host_interface *iface_desc;
2086 struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
2087 int i;
2088 int retval = -ENOMEM;
2089 struct urb *command_out_urb;
2090 struct urb *command_res_urb;
2091 struct mmc_host *mmc;
2092 char manufacturer[48];
2093 char product[32];
2094 char serial_number[32];
2095 usb_string(udev, udev->descriptor.iManufacturer, manufacturer,
2096 sizeof(manufacturer));
2097 usb_string(udev, udev->descriptor.iProduct, product, sizeof(product));
2098 usb_string(udev, udev->descriptor.iSerialNumber, serial_number,
2099 sizeof(serial_number));
2100 dev_info(&udev->dev, "probing VID:PID(%04X:%04X) %s %s %s\n",
2101 le16_to_cpu(udev->descriptor.idVendor),
2102 le16_to_cpu(udev->descriptor.idProduct),
2103 manufacturer, product, serial_number);
2104 command_out_urb = usb_alloc_urb(0, GFP_KERNEL);
2105 if (!command_out_urb) {
2106 retval = -ENOMEM;
2107 goto error0;
2108 }
2109 command_res_urb = usb_alloc_urb(0, GFP_KERNEL);
2110 if (!command_res_urb) {
2111 retval = -ENOMEM;
2112 goto error1;
2113 }
2114 /* this also allocates memory for our VUB300 mmc host device */
2115 mmc = devm_mmc_alloc_host(&udev->dev, sizeof(*vub300));
2116 if (!mmc) {
2117 retval = -ENOMEM;
2118 dev_err(&udev->dev, "not enough memory for the mmc_host\n");
2119 goto error4;
2120 }
2121 /* MMC core transfer sizes tunable parameters */
2122 mmc->caps = 0;
2123 if (!force_1_bit_data_xfers)
2124 mmc->caps |= MMC_CAP_4_BIT_DATA;
2125 if (!force_polling_for_irqs)
2126 mmc->caps |= MMC_CAP_SDIO_IRQ;
2127 mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2128 /*
2129 * MMC_CAP_NEEDS_POLL causes core.c:mmc_rescan() to poll
2130 * for devices which results in spurious CMD7's being
2131 * issued which stops some SDIO cards from working
2132 */
2133 if (limit_speed_to_24_MHz) {
2134 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2135 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2136 mmc->f_max = 24000000;
2137 dev_info(&udev->dev, "limiting SDIO speed to 24_MHz\n");
2138 } else {
2139 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2140 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2141 mmc->f_max = 48000000;
2142 }
2143 mmc->f_min = 200000;
2144 mmc->max_blk_count = 511;
2145 mmc->max_blk_size = 512;
2146 mmc->max_segs = 128;
2147 if (force_max_req_size)
2148 mmc->max_req_size = force_max_req_size * 1024;
2149 else
2150 mmc->max_req_size = 64 * 1024;
2151 mmc->max_seg_size = mmc->max_req_size;
2152 mmc->ocr_avail = 0;
2153 mmc->ocr_avail |= MMC_VDD_165_195;
2154 mmc->ocr_avail |= MMC_VDD_20_21;
2155 mmc->ocr_avail |= MMC_VDD_21_22;
2156 mmc->ocr_avail |= MMC_VDD_22_23;
2157 mmc->ocr_avail |= MMC_VDD_23_24;
2158 mmc->ocr_avail |= MMC_VDD_24_25;
2159 mmc->ocr_avail |= MMC_VDD_25_26;
2160 mmc->ocr_avail |= MMC_VDD_26_27;
2161 mmc->ocr_avail |= MMC_VDD_27_28;
2162 mmc->ocr_avail |= MMC_VDD_28_29;
2163 mmc->ocr_avail |= MMC_VDD_29_30;
2164 mmc->ocr_avail |= MMC_VDD_30_31;
2165 mmc->ocr_avail |= MMC_VDD_31_32;
2166 mmc->ocr_avail |= MMC_VDD_32_33;
2167 mmc->ocr_avail |= MMC_VDD_33_34;
2168 mmc->ocr_avail |= MMC_VDD_34_35;
2169 mmc->ocr_avail |= MMC_VDD_35_36;
2170 mmc->ops = &vub300_mmc_ops;
2171 vub300 = mmc_priv(mmc);
2172 vub300->mmc = mmc;
2173 vub300->card_powered = 0;
2174 vub300->bus_width = 0;
2175 vub300->cmnd.head.block_size[0] = 0x00;
2176 vub300->cmnd.head.block_size[1] = 0x00;
2177 vub300->app_spec = 0;
2178 mutex_init(&vub300->cmd_mutex);
2179 mutex_init(&vub300->irq_mutex);
2180 vub300->command_out_urb = command_out_urb;
2181 vub300->command_res_urb = command_res_urb;
2182 vub300->usb_timed_out = 0;
2183 vub300->dynamic_register_count = 0;
2184
2185 for (i = 0; i < ARRAY_SIZE(vub300->fn); i++) {
2186 vub300->fn[i].offload_point = 0;
2187 vub300->fn[i].offload_count = 0;
2188 }
2189
2190 vub300->total_offload_count = 0;
2191 vub300->irq_enabled = 0;
2192 vub300->irq_disabled = 0;
2193 vub300->irqs_queued = 0;
2194
2195 for (i = 0; i < ARRAY_SIZE(vub300->sdio_register); i++)
2196 vub300->sdio_register[i++].activate = 0;
2197
2198 vub300->udev = udev;
2199 vub300->interface = interface;
2200 vub300->cmnd_res_ep = 0;
2201 vub300->cmnd_out_ep = 0;
2202 vub300->data_inp_ep = 0;
2203 vub300->data_out_ep = 0;
2204
2205 for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
2206 vub300->fbs[i] = 512;
2207
2208 /*
2209 * set up the endpoint information
2210 *
2211 * use the first pair of bulk-in and bulk-out
2212 * endpoints for Command/Response+Interrupt
2213 *
2214 * use the second pair of bulk-in and bulk-out
2215 * endpoints for Data In/Out
2216 */
2217 vub300->large_usb_packets = 0;
2218 iface_desc = interface->cur_altsetting;
2219 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2220 struct usb_endpoint_descriptor *endpoint =
2221 &iface_desc->endpoint[i].desc;
2222 dev_info(&vub300->udev->dev,
2223 "vub300 testing %s EndPoint(%d) %02X\n",
2224 usb_endpoint_is_bulk_in(endpoint) ? "BULK IN" :
2225 usb_endpoint_is_bulk_out(endpoint) ? "BULK OUT" :
2226 "UNKNOWN", i, endpoint->bEndpointAddress);
2227 if (endpoint->wMaxPacketSize > 64)
2228 vub300->large_usb_packets = 1;
2229 if (usb_endpoint_is_bulk_in(endpoint)) {
2230 if (!vub300->cmnd_res_ep) {
2231 vub300->cmnd_res_ep =
2232 endpoint->bEndpointAddress;
2233 } else if (!vub300->data_inp_ep) {
2234 vub300->data_inp_ep =
2235 endpoint->bEndpointAddress;
2236 } else {
2237 dev_warn(&vub300->udev->dev,
2238 "ignoring"
2239 " unexpected bulk_in endpoint");
2240 }
2241 } else if (usb_endpoint_is_bulk_out(endpoint)) {
2242 if (!vub300->cmnd_out_ep) {
2243 vub300->cmnd_out_ep =
2244 endpoint->bEndpointAddress;
2245 } else if (!vub300->data_out_ep) {
2246 vub300->data_out_ep =
2247 endpoint->bEndpointAddress;
2248 } else {
2249 dev_warn(&vub300->udev->dev,
2250 "ignoring"
2251 " unexpected bulk_out endpoint");
2252 }
2253 } else {
2254 dev_warn(&vub300->udev->dev,
2255 "vub300 ignoring EndPoint(%d) %02X", i,
2256 endpoint->bEndpointAddress);
2257 }
2258 }
2259 if (vub300->cmnd_res_ep && vub300->cmnd_out_ep &&
2260 vub300->data_inp_ep && vub300->data_out_ep) {
2261 dev_info(&vub300->udev->dev,
2262 "vub300 %s packets"
2263 " using EndPoints %02X %02X %02X %02X\n",
2264 vub300->large_usb_packets ? "LARGE" : "SMALL",
2265 vub300->cmnd_out_ep, vub300->cmnd_res_ep,
2266 vub300->data_out_ep, vub300->data_inp_ep);
2267 /* we have the expected EndPoints */
2268 } else {
2269 dev_err(&vub300->udev->dev,
2270 "Could not find two sets of bulk-in/out endpoint pairs\n");
2271 retval = -EINVAL;
2272 goto error4;
2273 }
2274 retval =
2275 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2276 GET_HC_INF0,
2277 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2278 0x0000, 0x0000, &vub300->hc_info,
2279 sizeof(vub300->hc_info), 1000);
2280 if (retval < 0)
2281 goto error4;
2282 retval =
2283 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2284 SET_ROM_WAIT_STATES,
2285 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2286 firmware_rom_wait_states, 0x0000, NULL, 0, 1000);
2287 if (retval < 0)
2288 goto error4;
2289 dev_info(&vub300->udev->dev,
2290 "operating_mode = %s %s %d MHz %s %d byte USB packets\n",
2291 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "IRQs" : "POLL",
2292 (mmc->caps & MMC_CAP_4_BIT_DATA) ? "4-bit" : "1-bit",
2293 mmc->f_max / 1000000,
2294 pad_input_to_usb_pkt ? "padding input data to" : "with",
2295 vub300->large_usb_packets ? 512 : 64);
2296 retval =
2297 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2298 GET_SYSTEM_PORT_STATUS,
2299 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2300 0x0000, 0x0000, &vub300->system_port_status,
2301 sizeof(vub300->system_port_status), 1000);
2302 if (retval < 0) {
2303 goto error4;
2304 } else if (sizeof(vub300->system_port_status) == retval) {
2305 vub300->card_present =
2306 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
2307 vub300->read_only =
2308 (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
2309 } else {
2310 retval = -EINVAL;
2311 goto error4;
2312 }
2313 usb_set_intfdata(interface, vub300);
2314 INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread);
2315 INIT_WORK(&vub300->cmndwork, vub300_cmndwork_thread);
2316 INIT_WORK(&vub300->deadwork, vub300_deadwork_thread);
2317 kref_init(&vub300->kref);
2318 timer_setup(&vub300->sg_transfer_timer, vub300_sg_timed_out, 0);
2319 kref_get(&vub300->kref);
2320 timer_setup(&vub300->inactivity_timer,
2321 vub300_inactivity_timer_expired, 0);
2322 vub300->inactivity_timer.expires = jiffies + HZ;
2323 add_timer(&vub300->inactivity_timer);
2324 if (vub300->card_present)
2325 dev_info(&vub300->udev->dev,
2326 "USB vub300 remote SDIO host controller[%d]"
2327 "connected with SD/SDIO card inserted\n",
2328 interface_to_InterfaceNumber(interface));
2329 else
2330 dev_info(&vub300->udev->dev,
2331 "USB vub300 remote SDIO host controller[%d]"
2332 "connected with no SD/SDIO card inserted\n",
2333 interface_to_InterfaceNumber(interface));
2334 retval = mmc_add_host(mmc);
2335 if (retval)
2336 goto error6;
2337
2338 return 0;
2339 error6:
2340 timer_delete_sync(&vub300->inactivity_timer);
2341 /*
2342 * and hence also frees vub300
2343 * which is contained at the end of struct mmc
2344 */
2345 error4:
2346 usb_free_urb(command_res_urb);
2347 error1:
2348 usb_free_urb(command_out_urb);
2349 error0:
2350 usb_put_dev(udev);
2351 return retval;
2352 }
2353
vub300_disconnect(struct usb_interface * interface)2354 static void vub300_disconnect(struct usb_interface *interface)
2355 { /* NOT irq */
2356 struct vub300_mmc_host *vub300 = usb_get_intfdata(interface);
2357 if (!vub300 || !vub300->mmc) {
2358 return;
2359 } else {
2360 struct mmc_host *mmc = vub300->mmc;
2361 if (!vub300->mmc) {
2362 return;
2363 } else {
2364 int ifnum = interface_to_InterfaceNumber(interface);
2365 usb_set_intfdata(interface, NULL);
2366 /* prevent more I/O from starting */
2367 vub300->interface = NULL;
2368 kref_put(&vub300->kref, vub300_delete);
2369 mmc_remove_host(mmc);
2370 pr_info("USB vub300 remote SDIO host controller[%d]"
2371 " now disconnected", ifnum);
2372 return;
2373 }
2374 }
2375 }
2376
2377 #ifdef CONFIG_PM
vub300_suspend(struct usb_interface * intf,pm_message_t message)2378 static int vub300_suspend(struct usb_interface *intf, pm_message_t message)
2379 {
2380 return 0;
2381 }
2382
vub300_resume(struct usb_interface * intf)2383 static int vub300_resume(struct usb_interface *intf)
2384 {
2385 return 0;
2386 }
2387 #else
2388 #define vub300_suspend NULL
2389 #define vub300_resume NULL
2390 #endif
vub300_pre_reset(struct usb_interface * intf)2391 static int vub300_pre_reset(struct usb_interface *intf)
2392 { /* NOT irq */
2393 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2394 mutex_lock(&vub300->cmd_mutex);
2395 return 0;
2396 }
2397
vub300_post_reset(struct usb_interface * intf)2398 static int vub300_post_reset(struct usb_interface *intf)
2399 { /* NOT irq */
2400 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2401 /* we are sure no URBs are active - no locking needed */
2402 vub300->errors = -EPIPE;
2403 mutex_unlock(&vub300->cmd_mutex);
2404 return 0;
2405 }
2406
2407 static struct usb_driver vub300_driver = {
2408 .name = "vub300",
2409 .probe = vub300_probe,
2410 .disconnect = vub300_disconnect,
2411 .suspend = vub300_suspend,
2412 .resume = vub300_resume,
2413 .pre_reset = vub300_pre_reset,
2414 .post_reset = vub300_post_reset,
2415 .id_table = vub300_table,
2416 .supports_autosuspend = 1,
2417 };
2418
vub300_init(void)2419 static int __init vub300_init(void)
2420 { /* NOT irq */
2421 int result;
2422
2423 pr_info("VUB300 Driver rom wait states = %02X irqpoll timeout = %04X",
2424 firmware_rom_wait_states, 0x0FFFF & firmware_irqpoll_timeout);
2425 cmndworkqueue = create_singlethread_workqueue("kvub300c");
2426 if (!cmndworkqueue) {
2427 pr_err("not enough memory for the REQUEST workqueue");
2428 result = -ENOMEM;
2429 goto out1;
2430 }
2431 pollworkqueue = create_singlethread_workqueue("kvub300p");
2432 if (!pollworkqueue) {
2433 pr_err("not enough memory for the IRQPOLL workqueue");
2434 result = -ENOMEM;
2435 goto out2;
2436 }
2437 deadworkqueue = create_singlethread_workqueue("kvub300d");
2438 if (!deadworkqueue) {
2439 pr_err("not enough memory for the EXPIRED workqueue");
2440 result = -ENOMEM;
2441 goto out3;
2442 }
2443 result = usb_register(&vub300_driver);
2444 if (result) {
2445 pr_err("usb_register failed. Error number %d", result);
2446 goto out4;
2447 }
2448 return 0;
2449 out4:
2450 destroy_workqueue(deadworkqueue);
2451 out3:
2452 destroy_workqueue(pollworkqueue);
2453 out2:
2454 destroy_workqueue(cmndworkqueue);
2455 out1:
2456 return result;
2457 }
2458
vub300_exit(void)2459 static void __exit vub300_exit(void)
2460 {
2461 usb_deregister(&vub300_driver);
2462 flush_workqueue(cmndworkqueue);
2463 flush_workqueue(pollworkqueue);
2464 flush_workqueue(deadworkqueue);
2465 destroy_workqueue(cmndworkqueue);
2466 destroy_workqueue(pollworkqueue);
2467 destroy_workqueue(deadworkqueue);
2468 }
2469
2470 module_init(vub300_init);
2471 module_exit(vub300_exit);
2472
2473 MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
2474 MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
2475 MODULE_LICENSE("GPL");
2476