vub300.c 72 KB

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