vub300.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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. unsigned 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 { /* commretval > 0 */
  629. __vub300_irqpoll_response(vub300);
  630. }
  631. }
  632. /* this thread runs only when the driver
  633. * is trying to poll the device for an IRQ
  634. */
  635. static void vub300_pollwork_thread(struct work_struct *work)
  636. { /* NOT irq */
  637. struct vub300_mmc_host *vub300 = container_of(work,
  638. struct vub300_mmc_host, pollwork.work);
  639. if (!vub300->interface) {
  640. kref_put(&vub300->kref, vub300_delete);
  641. return;
  642. }
  643. mutex_lock(&vub300->cmd_mutex);
  644. if (vub300->cmd) {
  645. vub300_queue_poll_work(vub300, 1);
  646. } else if (!vub300->card_present) {
  647. /* no need to do anything */
  648. } else { /* vub300->card_present */
  649. mutex_lock(&vub300->irq_mutex);
  650. if (!vub300->irq_enabled) {
  651. mutex_unlock(&vub300->irq_mutex);
  652. } else if (vub300->irqs_queued) {
  653. vub300->irqs_queued -= 1;
  654. mmc_signal_sdio_irq(vub300->mmc);
  655. mod_timer(&vub300->inactivity_timer, jiffies + HZ);
  656. mutex_unlock(&vub300->irq_mutex);
  657. } else { /* NOT vub300->irqs_queued */
  658. mutex_unlock(&vub300->irq_mutex);
  659. __do_poll(vub300);
  660. }
  661. }
  662. mutex_unlock(&vub300->cmd_mutex);
  663. kref_put(&vub300->kref, vub300_delete);
  664. }
  665. static void vub300_deadwork_thread(struct work_struct *work)
  666. { /* NOT irq */
  667. struct vub300_mmc_host *vub300 =
  668. container_of(work, struct vub300_mmc_host, deadwork);
  669. if (!vub300->interface) {
  670. kref_put(&vub300->kref, vub300_delete);
  671. return;
  672. }
  673. mutex_lock(&vub300->cmd_mutex);
  674. if (vub300->cmd) {
  675. /*
  676. * a command got in as the inactivity
  677. * timer expired - so we just let the
  678. * processing of the command show if
  679. * the device is dead
  680. */
  681. } else if (vub300->card_present) {
  682. check_vub300_port_status(vub300);
  683. } else if (vub300->mmc && vub300->mmc->card &&
  684. mmc_card_present(vub300->mmc->card)) {
  685. /*
  686. * the MMC core must not have responded
  687. * to the previous indication - lets
  688. * hope that it eventually does so we
  689. * will just ignore this for now
  690. */
  691. } else {
  692. check_vub300_port_status(vub300);
  693. }
  694. mod_timer(&vub300->inactivity_timer, jiffies + HZ);
  695. mutex_unlock(&vub300->cmd_mutex);
  696. kref_put(&vub300->kref, vub300_delete);
  697. }
  698. static void vub300_inactivity_timer_expired(unsigned long data)
  699. { /* softirq */
  700. struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
  701. if (!vub300->interface) {
  702. kref_put(&vub300->kref, vub300_delete);
  703. } else if (vub300->cmd) {
  704. mod_timer(&vub300->inactivity_timer, jiffies + HZ);
  705. } else {
  706. vub300_queue_dead_work(vub300);
  707. mod_timer(&vub300->inactivity_timer, jiffies + HZ);
  708. }
  709. }
  710. static int vub300_response_error(u8 error_code)
  711. {
  712. switch (error_code) {
  713. case SD_ERROR_PIO_TIMEOUT:
  714. case SD_ERROR_1BIT_TIMEOUT:
  715. case SD_ERROR_4BIT_TIMEOUT:
  716. return -ETIMEDOUT;
  717. case SD_ERROR_STAT_DATA:
  718. case SD_ERROR_OVERRUN:
  719. case SD_ERROR_STAT_CMD:
  720. case SD_ERROR_STAT_CMD_TIMEOUT:
  721. case SD_ERROR_SDCRDY_STUCK:
  722. case SD_ERROR_UNHANDLED:
  723. case SD_ERROR_1BIT_CRC_WRONG:
  724. case SD_ERROR_4BIT_CRC_WRONG:
  725. case SD_ERROR_1BIT_CRC_ERROR:
  726. case SD_ERROR_4BIT_CRC_ERROR:
  727. case SD_ERROR_NO_CMD_ENDBIT:
  728. case SD_ERROR_NO_1BIT_DATEND:
  729. case SD_ERROR_NO_4BIT_DATEND:
  730. case SD_ERROR_1BIT_DATA_TIMEOUT:
  731. case SD_ERROR_4BIT_DATA_TIMEOUT:
  732. case SD_ERROR_1BIT_UNEXPECTED_TIMEOUT:
  733. case SD_ERROR_4BIT_UNEXPECTED_TIMEOUT:
  734. return -EILSEQ;
  735. case 33:
  736. return -EILSEQ;
  737. case SD_ERROR_ILLEGAL_COMMAND:
  738. return -EINVAL;
  739. case SD_ERROR_NO_DEVICE:
  740. return -ENOMEDIUM;
  741. default:
  742. return -ENODEV;
  743. }
  744. }
  745. static void command_res_completed(struct urb *urb)
  746. { /* urb completion handler - hardirq */
  747. struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
  748. if (urb->status) {
  749. /* we have to let the initiator handle the error */
  750. } else if (vub300->command_res_urb->actual_length == 0) {
  751. /*
  752. * we have seen this happen once or twice and
  753. * we suspect a buggy USB host controller
  754. */
  755. } else if (!vub300->data) {
  756. /* this means that the command (typically CMD52) succeeded */
  757. } else if (vub300->resp.common.header_type != 0x02) {
  758. /*
  759. * this is an error response from the VUB300 chip
  760. * and we let the initiator handle it
  761. */
  762. } else if (vub300->urb) {
  763. vub300->cmd->error =
  764. vub300_response_error(vub300->resp.error.error_code);
  765. usb_unlink_urb(vub300->urb);
  766. } else {
  767. vub300->cmd->error =
  768. vub300_response_error(vub300->resp.error.error_code);
  769. usb_sg_cancel(&vub300->sg_request);
  770. }
  771. complete(&vub300->command_complete); /* got_response_in */
  772. }
  773. static void command_out_completed(struct urb *urb)
  774. { /* urb completion handler - hardirq */
  775. struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
  776. if (urb->status) {
  777. complete(&vub300->command_complete);
  778. } else {
  779. int ret;
  780. unsigned int pipe =
  781. usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
  782. usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
  783. &vub300->resp, sizeof(vub300->resp),
  784. command_res_completed, vub300);
  785. vub300->command_res_urb->actual_length = 0;
  786. ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
  787. if (ret == 0) {
  788. /*
  789. * the urb completion handler will call
  790. * our completion handler
  791. */
  792. } else {
  793. /*
  794. * and thus we only call it directly
  795. * when it will not be called
  796. */
  797. complete(&vub300->command_complete);
  798. }
  799. }
  800. }
  801. /*
  802. * the STUFF bits are masked out for the comparisons
  803. */
  804. static void snoop_block_size_and_bus_width(struct vub300_mmc_host *vub300,
  805. u32 cmd_arg)
  806. {
  807. if ((0xFBFFFE00 & cmd_arg) == 0x80022200)
  808. vub300->fbs[1] = (cmd_arg << 8) | (0x00FF & vub300->fbs[1]);
  809. else if ((0xFBFFFE00 & cmd_arg) == 0x80022000)
  810. vub300->fbs[1] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[1]);
  811. else if ((0xFBFFFE00 & cmd_arg) == 0x80042200)
  812. vub300->fbs[2] = (cmd_arg << 8) | (0x00FF & vub300->fbs[2]);
  813. else if ((0xFBFFFE00 & cmd_arg) == 0x80042000)
  814. vub300->fbs[2] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[2]);
  815. else if ((0xFBFFFE00 & cmd_arg) == 0x80062200)
  816. vub300->fbs[3] = (cmd_arg << 8) | (0x00FF & vub300->fbs[3]);
  817. else if ((0xFBFFFE00 & cmd_arg) == 0x80062000)
  818. vub300->fbs[3] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[3]);
  819. else if ((0xFBFFFE00 & cmd_arg) == 0x80082200)
  820. vub300->fbs[4] = (cmd_arg << 8) | (0x00FF & vub300->fbs[4]);
  821. else if ((0xFBFFFE00 & cmd_arg) == 0x80082000)
  822. vub300->fbs[4] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[4]);
  823. else if ((0xFBFFFE00 & cmd_arg) == 0x800A2200)
  824. vub300->fbs[5] = (cmd_arg << 8) | (0x00FF & vub300->fbs[5]);
  825. else if ((0xFBFFFE00 & cmd_arg) == 0x800A2000)
  826. vub300->fbs[5] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[5]);
  827. else if ((0xFBFFFE00 & cmd_arg) == 0x800C2200)
  828. vub300->fbs[6] = (cmd_arg << 8) | (0x00FF & vub300->fbs[6]);
  829. else if ((0xFBFFFE00 & cmd_arg) == 0x800C2000)
  830. vub300->fbs[6] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[6]);
  831. else if ((0xFBFFFE00 & cmd_arg) == 0x800E2200)
  832. vub300->fbs[7] = (cmd_arg << 8) | (0x00FF & vub300->fbs[7]);
  833. else if ((0xFBFFFE00 & cmd_arg) == 0x800E2000)
  834. vub300->fbs[7] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[7]);
  835. else if ((0xFBFFFE03 & cmd_arg) == 0x80000E00)
  836. vub300->bus_width = 1;
  837. else if ((0xFBFFFE03 & cmd_arg) == 0x80000E02)
  838. vub300->bus_width = 4;
  839. }
  840. static void send_command(struct vub300_mmc_host *vub300)
  841. {
  842. /* cmd_mutex is held by vub300_cmndwork_thread */
  843. struct mmc_command *cmd = vub300->cmd;
  844. struct mmc_data *data = vub300->data;
  845. int retval;
  846. int i;
  847. u8 response_type;
  848. if (vub300->app_spec) {
  849. switch (cmd->opcode) {
  850. case 6:
  851. response_type = SDRT_1;
  852. vub300->resp_len = 6;
  853. if (0x00000000 == (0x00000003 & cmd->arg))
  854. vub300->bus_width = 1;
  855. else if (0x00000002 == (0x00000003 & cmd->arg))
  856. vub300->bus_width = 4;
  857. else
  858. dev_err(&vub300->udev->dev,
  859. "unexpected ACMD6 bus_width=%d\n",
  860. 0x00000003 & cmd->arg);
  861. break;
  862. case 13:
  863. response_type = SDRT_1;
  864. vub300->resp_len = 6;
  865. break;
  866. case 22:
  867. response_type = SDRT_1;
  868. vub300->resp_len = 6;
  869. break;
  870. case 23:
  871. response_type = SDRT_1;
  872. vub300->resp_len = 6;
  873. break;
  874. case 41:
  875. response_type = SDRT_3;
  876. vub300->resp_len = 6;
  877. break;
  878. case 42:
  879. response_type = SDRT_1;
  880. vub300->resp_len = 6;
  881. break;
  882. case 51:
  883. response_type = SDRT_1;
  884. vub300->resp_len = 6;
  885. break;
  886. case 55:
  887. response_type = SDRT_1;
  888. vub300->resp_len = 6;
  889. break;
  890. default:
  891. vub300->resp_len = 0;
  892. cmd->error = -EINVAL;
  893. complete(&vub300->command_complete);
  894. return;
  895. }
  896. vub300->app_spec = 0;
  897. } else {
  898. switch (cmd->opcode) {
  899. case 0:
  900. response_type = SDRT_NONE;
  901. vub300->resp_len = 0;
  902. break;
  903. case 1:
  904. response_type = SDRT_3;
  905. vub300->resp_len = 6;
  906. break;
  907. case 2:
  908. response_type = SDRT_2;
  909. vub300->resp_len = 17;
  910. break;
  911. case 3:
  912. response_type = SDRT_6;
  913. vub300->resp_len = 6;
  914. break;
  915. case 4:
  916. response_type = SDRT_NONE;
  917. vub300->resp_len = 0;
  918. break;
  919. case 5:
  920. response_type = SDRT_4;
  921. vub300->resp_len = 6;
  922. break;
  923. case 6:
  924. response_type = SDRT_1;
  925. vub300->resp_len = 6;
  926. break;
  927. case 7:
  928. response_type = SDRT_1B;
  929. vub300->resp_len = 6;
  930. break;
  931. case 8:
  932. response_type = SDRT_7;
  933. vub300->resp_len = 6;
  934. break;
  935. case 9:
  936. response_type = SDRT_2;
  937. vub300->resp_len = 17;
  938. break;
  939. case 10:
  940. response_type = SDRT_2;
  941. vub300->resp_len = 17;
  942. break;
  943. case 12:
  944. response_type = SDRT_1B;
  945. vub300->resp_len = 6;
  946. break;
  947. case 13:
  948. response_type = SDRT_1;
  949. vub300->resp_len = 6;
  950. break;
  951. case 15:
  952. response_type = SDRT_NONE;
  953. vub300->resp_len = 0;
  954. break;
  955. case 16:
  956. for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
  957. vub300->fbs[i] = 0xFFFF & cmd->arg;
  958. response_type = SDRT_1;
  959. vub300->resp_len = 6;
  960. break;
  961. case 17:
  962. case 18:
  963. case 24:
  964. case 25:
  965. case 27:
  966. response_type = SDRT_1;
  967. vub300->resp_len = 6;
  968. break;
  969. case 28:
  970. case 29:
  971. response_type = SDRT_1B;
  972. vub300->resp_len = 6;
  973. break;
  974. case 30:
  975. case 32:
  976. case 33:
  977. response_type = SDRT_1;
  978. vub300->resp_len = 6;
  979. break;
  980. case 38:
  981. response_type = SDRT_1B;
  982. vub300->resp_len = 6;
  983. break;
  984. case 42:
  985. response_type = SDRT_1;
  986. vub300->resp_len = 6;
  987. break;
  988. case 52:
  989. response_type = SDRT_5;
  990. vub300->resp_len = 6;
  991. snoop_block_size_and_bus_width(vub300, cmd->arg);
  992. break;
  993. case 53:
  994. response_type = SDRT_5;
  995. vub300->resp_len = 6;
  996. break;
  997. case 55:
  998. response_type = SDRT_1;
  999. vub300->resp_len = 6;
  1000. vub300->app_spec = 1;
  1001. break;
  1002. case 56:
  1003. response_type = SDRT_1;
  1004. vub300->resp_len = 6;
  1005. break;
  1006. default:
  1007. vub300->resp_len = 0;
  1008. cmd->error = -EINVAL;
  1009. complete(&vub300->command_complete);
  1010. return;
  1011. }
  1012. }
  1013. /*
  1014. * it is a shame that we can not use "sizeof(struct sd_command_header)"
  1015. * this is because the packet _must_ be padded to 64 bytes
  1016. */
  1017. vub300->cmnd.head.header_size = 20;
  1018. vub300->cmnd.head.header_type = 0x00;
  1019. vub300->cmnd.head.port_number = 0; /* "0" means port 1 */
  1020. vub300->cmnd.head.command_type = 0x00; /* standard read command */
  1021. vub300->cmnd.head.response_type = response_type;
  1022. vub300->cmnd.head.command_index = cmd->opcode;
  1023. vub300->cmnd.head.arguments[0] = cmd->arg >> 24;
  1024. vub300->cmnd.head.arguments[1] = cmd->arg >> 16;
  1025. vub300->cmnd.head.arguments[2] = cmd->arg >> 8;
  1026. vub300->cmnd.head.arguments[3] = cmd->arg >> 0;
  1027. if (cmd->opcode == 52) {
  1028. int fn = 0x7 & (cmd->arg >> 28);
  1029. vub300->cmnd.head.block_count[0] = 0;
  1030. vub300->cmnd.head.block_count[1] = 0;
  1031. vub300->cmnd.head.block_size[0] = (vub300->fbs[fn] >> 8) & 0xFF;
  1032. vub300->cmnd.head.block_size[1] = (vub300->fbs[fn] >> 0) & 0xFF;
  1033. vub300->cmnd.head.command_type = 0x00;
  1034. vub300->cmnd.head.transfer_size[0] = 0;
  1035. vub300->cmnd.head.transfer_size[1] = 0;
  1036. vub300->cmnd.head.transfer_size[2] = 0;
  1037. vub300->cmnd.head.transfer_size[3] = 0;
  1038. } else if (!data) {
  1039. vub300->cmnd.head.block_count[0] = 0;
  1040. vub300->cmnd.head.block_count[1] = 0;
  1041. vub300->cmnd.head.block_size[0] = (vub300->fbs[0] >> 8) & 0xFF;
  1042. vub300->cmnd.head.block_size[1] = (vub300->fbs[0] >> 0) & 0xFF;
  1043. vub300->cmnd.head.command_type = 0x00;
  1044. vub300->cmnd.head.transfer_size[0] = 0;
  1045. vub300->cmnd.head.transfer_size[1] = 0;
  1046. vub300->cmnd.head.transfer_size[2] = 0;
  1047. vub300->cmnd.head.transfer_size[3] = 0;
  1048. } else if (cmd->opcode == 53) {
  1049. int fn = 0x7 & (cmd->arg >> 28);
  1050. if (0x08 & vub300->cmnd.head.arguments[0]) { /* BLOCK MODE */
  1051. vub300->cmnd.head.block_count[0] =
  1052. (data->blocks >> 8) & 0xFF;
  1053. vub300->cmnd.head.block_count[1] =
  1054. (data->blocks >> 0) & 0xFF;
  1055. vub300->cmnd.head.block_size[0] =
  1056. (data->blksz >> 8) & 0xFF;
  1057. vub300->cmnd.head.block_size[1] =
  1058. (data->blksz >> 0) & 0xFF;
  1059. } else { /* BYTE MODE */
  1060. vub300->cmnd.head.block_count[0] = 0;
  1061. vub300->cmnd.head.block_count[1] = 0;
  1062. vub300->cmnd.head.block_size[0] =
  1063. (vub300->datasize >> 8) & 0xFF;
  1064. vub300->cmnd.head.block_size[1] =
  1065. (vub300->datasize >> 0) & 0xFF;
  1066. }
  1067. vub300->cmnd.head.command_type =
  1068. (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
  1069. vub300->cmnd.head.transfer_size[0] =
  1070. (vub300->datasize >> 24) & 0xFF;
  1071. vub300->cmnd.head.transfer_size[1] =
  1072. (vub300->datasize >> 16) & 0xFF;
  1073. vub300->cmnd.head.transfer_size[2] =
  1074. (vub300->datasize >> 8) & 0xFF;
  1075. vub300->cmnd.head.transfer_size[3] =
  1076. (vub300->datasize >> 0) & 0xFF;
  1077. if (vub300->datasize < vub300->fbs[fn]) {
  1078. vub300->cmnd.head.block_count[0] = 0;
  1079. vub300->cmnd.head.block_count[1] = 0;
  1080. }
  1081. } else {
  1082. vub300->cmnd.head.block_count[0] = (data->blocks >> 8) & 0xFF;
  1083. vub300->cmnd.head.block_count[1] = (data->blocks >> 0) & 0xFF;
  1084. vub300->cmnd.head.block_size[0] = (data->blksz >> 8) & 0xFF;
  1085. vub300->cmnd.head.block_size[1] = (data->blksz >> 0) & 0xFF;
  1086. vub300->cmnd.head.command_type =
  1087. (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
  1088. vub300->cmnd.head.transfer_size[0] =
  1089. (vub300->datasize >> 24) & 0xFF;
  1090. vub300->cmnd.head.transfer_size[1] =
  1091. (vub300->datasize >> 16) & 0xFF;
  1092. vub300->cmnd.head.transfer_size[2] =
  1093. (vub300->datasize >> 8) & 0xFF;
  1094. vub300->cmnd.head.transfer_size[3] =
  1095. (vub300->datasize >> 0) & 0xFF;
  1096. if (vub300->datasize < vub300->fbs[0]) {
  1097. vub300->cmnd.head.block_count[0] = 0;
  1098. vub300->cmnd.head.block_count[1] = 0;
  1099. }
  1100. }
  1101. if (vub300->cmnd.head.block_size[0] || vub300->cmnd.head.block_size[1]) {
  1102. u16 block_size = vub300->cmnd.head.block_size[1] |
  1103. (vub300->cmnd.head.block_size[0] << 8);
  1104. u16 block_boundary = FIRMWARE_BLOCK_BOUNDARY -
  1105. (FIRMWARE_BLOCK_BOUNDARY % block_size);
  1106. vub300->cmnd.head.block_boundary[0] =
  1107. (block_boundary >> 8) & 0xFF;
  1108. vub300->cmnd.head.block_boundary[1] =
  1109. (block_boundary >> 0) & 0xFF;
  1110. } else {
  1111. vub300->cmnd.head.block_boundary[0] = 0;
  1112. vub300->cmnd.head.block_boundary[1] = 0;
  1113. }
  1114. usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
  1115. usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep),
  1116. &vub300->cmnd, sizeof(vub300->cmnd),
  1117. command_out_completed, vub300);
  1118. retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
  1119. if (retval < 0) {
  1120. cmd->error = retval;
  1121. complete(&vub300->command_complete);
  1122. return;
  1123. } else {
  1124. return;
  1125. }
  1126. }
  1127. /*
  1128. * timer callback runs in atomic mode
  1129. * so it cannot call usb_kill_urb()
  1130. */
  1131. static void vub300_sg_timed_out(unsigned long data)
  1132. {
  1133. struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
  1134. vub300->usb_timed_out = 1;
  1135. usb_sg_cancel(&vub300->sg_request);
  1136. usb_unlink_urb(vub300->command_out_urb);
  1137. usb_unlink_urb(vub300->command_res_urb);
  1138. }
  1139. static u16 roundup_to_multiple_of_64(u16 number)
  1140. {
  1141. return 0xFFC0 & (0x3F + number);
  1142. }
  1143. /*
  1144. * this is a separate function to solve the 80 column width restriction
  1145. */
  1146. static void __download_offload_pseudocode(struct vub300_mmc_host *vub300,
  1147. const struct firmware *fw)
  1148. {
  1149. u8 register_count = 0;
  1150. u16 ts = 0;
  1151. u16 interrupt_size = 0;
  1152. const u8 *data = fw->data;
  1153. int size = fw->size;
  1154. u8 c;
  1155. dev_info(&vub300->udev->dev, "using %s for SDIO offload processing\n",
  1156. vub300->vub_name);
  1157. do {
  1158. c = *data++;
  1159. } while (size-- && c); /* skip comment */
  1160. dev_info(&vub300->udev->dev, "using offload firmware %s %s\n", fw->data,
  1161. vub300->vub_name);
  1162. if (size < 4) {
  1163. dev_err(&vub300->udev->dev,
  1164. "corrupt offload pseudocode in firmware %s\n",
  1165. vub300->vub_name);
  1166. strncpy(vub300->vub_name, "corrupt offload pseudocode",
  1167. sizeof(vub300->vub_name));
  1168. return;
  1169. }
  1170. interrupt_size += *data++;
  1171. size -= 1;
  1172. interrupt_size <<= 8;
  1173. interrupt_size += *data++;
  1174. size -= 1;
  1175. if (interrupt_size < size) {
  1176. u16 xfer_length = roundup_to_multiple_of_64(interrupt_size);
  1177. u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
  1178. if (xfer_buffer) {
  1179. int retval;
  1180. memcpy(xfer_buffer, data, interrupt_size);
  1181. memset(xfer_buffer + interrupt_size, 0,
  1182. xfer_length - interrupt_size);
  1183. size -= interrupt_size;
  1184. data += interrupt_size;
  1185. retval =
  1186. usb_control_msg(vub300->udev,
  1187. usb_sndctrlpipe(vub300->udev, 0),
  1188. SET_INTERRUPT_PSEUDOCODE,
  1189. USB_DIR_OUT | USB_TYPE_VENDOR |
  1190. USB_RECIP_DEVICE, 0x0000, 0x0000,
  1191. xfer_buffer, xfer_length, HZ);
  1192. kfree(xfer_buffer);
  1193. if (retval < 0) {
  1194. strncpy(vub300->vub_name,
  1195. "SDIO pseudocode download failed",
  1196. sizeof(vub300->vub_name));
  1197. return;
  1198. }
  1199. } else {
  1200. dev_err(&vub300->udev->dev,
  1201. "not enough memory for xfer buffer to send"
  1202. " INTERRUPT_PSEUDOCODE for %s %s\n", fw->data,
  1203. vub300->vub_name);
  1204. strncpy(vub300->vub_name,
  1205. "SDIO interrupt pseudocode download failed",
  1206. sizeof(vub300->vub_name));
  1207. return;
  1208. }
  1209. } else {
  1210. dev_err(&vub300->udev->dev,
  1211. "corrupt interrupt pseudocode in firmware %s %s\n",
  1212. fw->data, vub300->vub_name);
  1213. strncpy(vub300->vub_name, "corrupt interrupt pseudocode",
  1214. sizeof(vub300->vub_name));
  1215. return;
  1216. }
  1217. ts += *data++;
  1218. size -= 1;
  1219. ts <<= 8;
  1220. ts += *data++;
  1221. size -= 1;
  1222. if (ts < size) {
  1223. u16 xfer_length = roundup_to_multiple_of_64(ts);
  1224. u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
  1225. if (xfer_buffer) {
  1226. int retval;
  1227. memcpy(xfer_buffer, data, ts);
  1228. memset(xfer_buffer + ts, 0,
  1229. xfer_length - ts);
  1230. size -= ts;
  1231. data += ts;
  1232. retval =
  1233. usb_control_msg(vub300->udev,
  1234. usb_sndctrlpipe(vub300->udev, 0),
  1235. SET_TRANSFER_PSEUDOCODE,
  1236. USB_DIR_OUT | USB_TYPE_VENDOR |
  1237. USB_RECIP_DEVICE, 0x0000, 0x0000,
  1238. xfer_buffer, xfer_length, HZ);
  1239. kfree(xfer_buffer);
  1240. if (retval < 0) {
  1241. strncpy(vub300->vub_name,
  1242. "SDIO pseudocode download failed",
  1243. sizeof(vub300->vub_name));
  1244. return;
  1245. }
  1246. } else {
  1247. dev_err(&vub300->udev->dev,
  1248. "not enough memory for xfer buffer to send"
  1249. " TRANSFER_PSEUDOCODE for %s %s\n", fw->data,
  1250. vub300->vub_name);
  1251. strncpy(vub300->vub_name,
  1252. "SDIO transfer pseudocode download failed",
  1253. sizeof(vub300->vub_name));
  1254. return;
  1255. }
  1256. } else {
  1257. dev_err(&vub300->udev->dev,
  1258. "corrupt transfer pseudocode in firmware %s %s\n",
  1259. fw->data, vub300->vub_name);
  1260. strncpy(vub300->vub_name, "corrupt transfer pseudocode",
  1261. sizeof(vub300->vub_name));
  1262. return;
  1263. }
  1264. register_count += *data++;
  1265. size -= 1;
  1266. if (register_count * 4 == size) {
  1267. int I = vub300->dynamic_register_count = register_count;
  1268. int i = 0;
  1269. while (I--) {
  1270. unsigned int func_num = 0;
  1271. vub300->sdio_register[i].func_num = *data++;
  1272. size -= 1;
  1273. func_num += *data++;
  1274. size -= 1;
  1275. func_num <<= 8;
  1276. func_num += *data++;
  1277. size -= 1;
  1278. func_num <<= 8;
  1279. func_num += *data++;
  1280. size -= 1;
  1281. vub300->sdio_register[i].sdio_reg = func_num;
  1282. vub300->sdio_register[i].activate = 1;
  1283. vub300->sdio_register[i].prepared = 0;
  1284. i += 1;
  1285. }
  1286. dev_info(&vub300->udev->dev,
  1287. "initialized %d dynamic pseudocode registers\n",
  1288. vub300->dynamic_register_count);
  1289. return;
  1290. } else {
  1291. dev_err(&vub300->udev->dev,
  1292. "corrupt dynamic registers in firmware %s\n",
  1293. vub300->vub_name);
  1294. strncpy(vub300->vub_name, "corrupt dynamic registers",
  1295. sizeof(vub300->vub_name));
  1296. return;
  1297. }
  1298. }
  1299. /*
  1300. * if the binary containing the EMPTY PseudoCode can not be found
  1301. * vub300->vub_name is set anyway in order to prevent an automatic retry
  1302. */
  1303. static void download_offload_pseudocode(struct vub300_mmc_host *vub300)
  1304. {
  1305. struct mmc_card *card = vub300->mmc->card;
  1306. int sdio_funcs = card->sdio_funcs;
  1307. const struct firmware *fw = NULL;
  1308. int l = snprintf(vub300->vub_name, sizeof(vub300->vub_name),
  1309. "/*(DEBLOBBED)*/", card->cis.vendor, card->cis.device);
  1310. int n = 0;
  1311. int retval;
  1312. for (n = 0; n < sdio_funcs; n++) {
  1313. struct sdio_func *sf = card->sdio_func[n];
  1314. l += snprintf(vub300->vub_name + l,
  1315. sizeof(vub300->vub_name) - l, "_%04X%04X",
  1316. sf->vendor, sf->device);
  1317. }
  1318. /*(DEBLOBBED)*/
  1319. dev_info(&vub300->udev->dev, "requesting offload firmware %s\n",
  1320. vub300->vub_name);
  1321. retval = reject_firmware(&fw, vub300->vub_name, &card->dev);
  1322. if (retval < 0) {
  1323. strncpy(vub300->vub_name, "/*(DEBLOBBED)*/",
  1324. sizeof(vub300->vub_name));
  1325. retval = reject_firmware(&fw, vub300->vub_name, &card->dev);
  1326. if (retval < 0) {
  1327. strncpy(vub300->vub_name,
  1328. "no SDIO offload firmware found",
  1329. sizeof(vub300->vub_name));
  1330. } else {
  1331. __download_offload_pseudocode(vub300, fw);
  1332. release_firmware(fw);
  1333. }
  1334. } else {
  1335. __download_offload_pseudocode(vub300, fw);
  1336. release_firmware(fw);
  1337. }
  1338. }
  1339. static void vub300_usb_bulk_msg_completion(struct urb *urb)
  1340. { /* urb completion handler - hardirq */
  1341. complete((struct completion *)urb->context);
  1342. }
  1343. static int vub300_usb_bulk_msg(struct vub300_mmc_host *vub300,
  1344. unsigned int pipe, void *data, int len,
  1345. int *actual_length, int timeout_msecs)
  1346. {
  1347. /* cmd_mutex is held by vub300_cmndwork_thread */
  1348. struct usb_device *usb_dev = vub300->udev;
  1349. struct completion done;
  1350. int retval;
  1351. vub300->urb = usb_alloc_urb(0, GFP_KERNEL);
  1352. if (!vub300->urb)
  1353. return -ENOMEM;
  1354. usb_fill_bulk_urb(vub300->urb, usb_dev, pipe, data, len,
  1355. vub300_usb_bulk_msg_completion, NULL);
  1356. init_completion(&done);
  1357. vub300->urb->context = &done;
  1358. vub300->urb->actual_length = 0;
  1359. retval = usb_submit_urb(vub300->urb, GFP_KERNEL);
  1360. if (unlikely(retval))
  1361. goto out;
  1362. if (!wait_for_completion_timeout
  1363. (&done, msecs_to_jiffies(timeout_msecs))) {
  1364. retval = -ETIMEDOUT;
  1365. usb_kill_urb(vub300->urb);
  1366. } else {
  1367. retval = vub300->urb->status;
  1368. }
  1369. out:
  1370. *actual_length = vub300->urb->actual_length;
  1371. usb_free_urb(vub300->urb);
  1372. vub300->urb = NULL;
  1373. return retval;
  1374. }
  1375. static int __command_read_data(struct vub300_mmc_host *vub300,
  1376. struct mmc_command *cmd, struct mmc_data *data)
  1377. {
  1378. /* cmd_mutex is held by vub300_cmndwork_thread */
  1379. int linear_length = vub300->datasize;
  1380. int padded_length = vub300->large_usb_packets ?
  1381. ((511 + linear_length) >> 9) << 9 :
  1382. ((63 + linear_length) >> 6) << 6;
  1383. if ((padded_length == linear_length) || !pad_input_to_usb_pkt) {
  1384. int result;
  1385. unsigned pipe;
  1386. pipe = usb_rcvbulkpipe(vub300->udev, vub300->data_inp_ep);
  1387. result = usb_sg_init(&vub300->sg_request, vub300->udev,
  1388. pipe, 0, data->sg,
  1389. data->sg_len, 0, GFP_KERNEL);
  1390. if (result < 0) {
  1391. usb_unlink_urb(vub300->command_out_urb);
  1392. usb_unlink_urb(vub300->command_res_urb);
  1393. cmd->error = result;
  1394. data->bytes_xfered = 0;
  1395. return 0;
  1396. } else {
  1397. vub300->sg_transfer_timer.expires =
  1398. jiffies + msecs_to_jiffies(2000 +
  1399. (linear_length / 16384));
  1400. add_timer(&vub300->sg_transfer_timer);
  1401. usb_sg_wait(&vub300->sg_request);
  1402. del_timer(&vub300->sg_transfer_timer);
  1403. if (vub300->sg_request.status < 0) {
  1404. cmd->error = vub300->sg_request.status;
  1405. data->bytes_xfered = 0;
  1406. return 0;
  1407. } else {
  1408. data->bytes_xfered = vub300->datasize;
  1409. return linear_length;
  1410. }
  1411. }
  1412. } else {
  1413. u8 *buf = kmalloc(padded_length, GFP_KERNEL);
  1414. if (buf) {
  1415. int result;
  1416. unsigned pipe = usb_rcvbulkpipe(vub300->udev,
  1417. vub300->data_inp_ep);
  1418. int actual_length = 0;
  1419. result = vub300_usb_bulk_msg(vub300, pipe, buf,
  1420. padded_length, &actual_length,
  1421. 2000 + (padded_length / 16384));
  1422. if (result < 0) {
  1423. cmd->error = result;
  1424. data->bytes_xfered = 0;
  1425. kfree(buf);
  1426. return 0;
  1427. } else if (actual_length < linear_length) {
  1428. cmd->error = -EREMOTEIO;
  1429. data->bytes_xfered = 0;
  1430. kfree(buf);
  1431. return 0;
  1432. } else {
  1433. sg_copy_from_buffer(data->sg, data->sg_len, buf,
  1434. linear_length);
  1435. kfree(buf);
  1436. data->bytes_xfered = vub300->datasize;
  1437. return linear_length;
  1438. }
  1439. } else {
  1440. cmd->error = -ENOMEM;
  1441. data->bytes_xfered = 0;
  1442. return 0;
  1443. }
  1444. }
  1445. }
  1446. static int __command_write_data(struct vub300_mmc_host *vub300,
  1447. struct mmc_command *cmd, struct mmc_data *data)
  1448. {
  1449. /* cmd_mutex is held by vub300_cmndwork_thread */
  1450. unsigned pipe = usb_sndbulkpipe(vub300->udev, vub300->data_out_ep);
  1451. int linear_length = vub300->datasize;
  1452. int modulo_64_length = linear_length & 0x003F;
  1453. int modulo_512_length = linear_length & 0x01FF;
  1454. if (linear_length < 64) {
  1455. int result;
  1456. int actual_length;
  1457. sg_copy_to_buffer(data->sg, data->sg_len,
  1458. vub300->padded_buffer,
  1459. sizeof(vub300->padded_buffer));
  1460. memset(vub300->padded_buffer + linear_length, 0,
  1461. sizeof(vub300->padded_buffer) - linear_length);
  1462. result = vub300_usb_bulk_msg(vub300, pipe, vub300->padded_buffer,
  1463. sizeof(vub300->padded_buffer),
  1464. &actual_length, 2000 +
  1465. (sizeof(vub300->padded_buffer) /
  1466. 16384));
  1467. if (result < 0) {
  1468. cmd->error = result;
  1469. data->bytes_xfered = 0;
  1470. } else {
  1471. data->bytes_xfered = vub300->datasize;
  1472. }
  1473. } else if ((!vub300->large_usb_packets && (0 < modulo_64_length)) ||
  1474. (vub300->large_usb_packets && (64 > modulo_512_length))
  1475. ) { /* don't you just love these work-rounds */
  1476. int padded_length = ((63 + linear_length) >> 6) << 6;
  1477. u8 *buf = kmalloc(padded_length, GFP_KERNEL);
  1478. if (buf) {
  1479. int result;
  1480. int actual_length;
  1481. sg_copy_to_buffer(data->sg, data->sg_len, buf,
  1482. padded_length);
  1483. memset(buf + linear_length, 0,
  1484. padded_length - linear_length);
  1485. result =
  1486. vub300_usb_bulk_msg(vub300, pipe, buf,
  1487. padded_length, &actual_length,
  1488. 2000 + padded_length / 16384);
  1489. kfree(buf);
  1490. if (result < 0) {
  1491. cmd->error = result;
  1492. data->bytes_xfered = 0;
  1493. } else {
  1494. data->bytes_xfered = vub300->datasize;
  1495. }
  1496. } else {
  1497. cmd->error = -ENOMEM;
  1498. data->bytes_xfered = 0;
  1499. }
  1500. } else { /* no data padding required */
  1501. int result;
  1502. unsigned char buf[64 * 4];
  1503. sg_copy_to_buffer(data->sg, data->sg_len, buf, sizeof(buf));
  1504. result = usb_sg_init(&vub300->sg_request, vub300->udev,
  1505. pipe, 0, data->sg,
  1506. data->sg_len, 0, GFP_KERNEL);
  1507. if (result < 0) {
  1508. usb_unlink_urb(vub300->command_out_urb);
  1509. usb_unlink_urb(vub300->command_res_urb);
  1510. cmd->error = result;
  1511. data->bytes_xfered = 0;
  1512. } else {
  1513. vub300->sg_transfer_timer.expires =
  1514. jiffies + msecs_to_jiffies(2000 +
  1515. linear_length / 16384);
  1516. add_timer(&vub300->sg_transfer_timer);
  1517. usb_sg_wait(&vub300->sg_request);
  1518. if (cmd->error) {
  1519. data->bytes_xfered = 0;
  1520. } else {
  1521. del_timer(&vub300->sg_transfer_timer);
  1522. if (vub300->sg_request.status < 0) {
  1523. cmd->error = vub300->sg_request.status;
  1524. data->bytes_xfered = 0;
  1525. } else {
  1526. data->bytes_xfered = vub300->datasize;
  1527. }
  1528. }
  1529. }
  1530. }
  1531. return linear_length;
  1532. }
  1533. static void __vub300_command_response(struct vub300_mmc_host *vub300,
  1534. struct mmc_command *cmd,
  1535. struct mmc_data *data, int data_length)
  1536. {
  1537. /* cmd_mutex is held by vub300_cmndwork_thread */
  1538. long respretval;
  1539. int msec_timeout = 1000 + data_length / 4;
  1540. respretval =
  1541. wait_for_completion_timeout(&vub300->command_complete,
  1542. msecs_to_jiffies(msec_timeout));
  1543. if (respretval == 0) { /* TIMED OUT */
  1544. /* we don't know which of "out" and "res" if any failed */
  1545. int result;
  1546. vub300->usb_timed_out = 1;
  1547. usb_kill_urb(vub300->command_out_urb);
  1548. usb_kill_urb(vub300->command_res_urb);
  1549. cmd->error = -ETIMEDOUT;
  1550. result = usb_lock_device_for_reset(vub300->udev,
  1551. vub300->interface);
  1552. if (result == 0) {
  1553. result = usb_reset_device(vub300->udev);
  1554. usb_unlock_device(vub300->udev);
  1555. }
  1556. } else if (respretval < 0) {
  1557. /* we don't know which of "out" and "res" if any failed */
  1558. usb_kill_urb(vub300->command_out_urb);
  1559. usb_kill_urb(vub300->command_res_urb);
  1560. cmd->error = respretval;
  1561. } else if (cmd->error) {
  1562. /*
  1563. * the error occurred sending the command
  1564. * or receiving the response
  1565. */
  1566. } else if (vub300->command_out_urb->status) {
  1567. vub300->usb_transport_fail = vub300->command_out_urb->status;
  1568. cmd->error = -EPROTO == vub300->command_out_urb->status ?
  1569. -ESHUTDOWN : vub300->command_out_urb->status;
  1570. } else if (vub300->command_res_urb->status) {
  1571. vub300->usb_transport_fail = vub300->command_res_urb->status;
  1572. cmd->error = -EPROTO == vub300->command_res_urb->status ?
  1573. -ESHUTDOWN : vub300->command_res_urb->status;
  1574. } else if (vub300->resp.common.header_type == 0x00) {
  1575. /*
  1576. * the command completed successfully
  1577. * and there was no piggybacked data
  1578. */
  1579. } else if (vub300->resp.common.header_type == RESPONSE_ERROR) {
  1580. cmd->error =
  1581. vub300_response_error(vub300->resp.error.error_code);
  1582. if (vub300->data)
  1583. usb_sg_cancel(&vub300->sg_request);
  1584. } else if (vub300->resp.common.header_type == RESPONSE_PIGGYBACKED) {
  1585. int offloaded_data_length =
  1586. vub300->resp.common.header_size -
  1587. sizeof(struct sd_register_header);
  1588. int register_count = offloaded_data_length >> 3;
  1589. int ri = 0;
  1590. while (register_count--) {
  1591. add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
  1592. ri += 1;
  1593. }
  1594. vub300->resp.common.header_size =
  1595. sizeof(struct sd_register_header);
  1596. vub300->resp.common.header_type = 0x00;
  1597. cmd->error = 0;
  1598. } else if (vub300->resp.common.header_type == RESPONSE_PIG_DISABLED) {
  1599. int offloaded_data_length =
  1600. vub300->resp.common.header_size -
  1601. sizeof(struct sd_register_header);
  1602. int register_count = offloaded_data_length >> 3;
  1603. int ri = 0;
  1604. while (register_count--) {
  1605. add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
  1606. ri += 1;
  1607. }
  1608. mutex_lock(&vub300->irq_mutex);
  1609. if (vub300->irqs_queued) {
  1610. vub300->irqs_queued += 1;
  1611. } else if (vub300->irq_enabled) {
  1612. vub300->irqs_queued += 1;
  1613. vub300_queue_poll_work(vub300, 0);
  1614. } else {
  1615. vub300->irqs_queued += 1;
  1616. }
  1617. vub300->irq_disabled = 1;
  1618. mutex_unlock(&vub300->irq_mutex);
  1619. vub300->resp.common.header_size =
  1620. sizeof(struct sd_register_header);
  1621. vub300->resp.common.header_type = 0x00;
  1622. cmd->error = 0;
  1623. } else if (vub300->resp.common.header_type == RESPONSE_PIG_ENABLED) {
  1624. int offloaded_data_length =
  1625. vub300->resp.common.header_size -
  1626. sizeof(struct sd_register_header);
  1627. int register_count = offloaded_data_length >> 3;
  1628. int ri = 0;
  1629. while (register_count--) {
  1630. add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
  1631. ri += 1;
  1632. }
  1633. mutex_lock(&vub300->irq_mutex);
  1634. if (vub300->irqs_queued) {
  1635. vub300->irqs_queued += 1;
  1636. } else if (vub300->irq_enabled) {
  1637. vub300->irqs_queued += 1;
  1638. vub300_queue_poll_work(vub300, 0);
  1639. } else {
  1640. vub300->irqs_queued += 1;
  1641. }
  1642. vub300->irq_disabled = 0;
  1643. mutex_unlock(&vub300->irq_mutex);
  1644. vub300->resp.common.header_size =
  1645. sizeof(struct sd_register_header);
  1646. vub300->resp.common.header_type = 0x00;
  1647. cmd->error = 0;
  1648. } else {
  1649. cmd->error = -EINVAL;
  1650. }
  1651. }
  1652. static void construct_request_response(struct vub300_mmc_host *vub300,
  1653. struct mmc_command *cmd)
  1654. {
  1655. int resp_len = vub300->resp_len;
  1656. int less_cmd = (17 == resp_len) ? resp_len : resp_len - 1;
  1657. int bytes = 3 & less_cmd;
  1658. int words = less_cmd >> 2;
  1659. u8 *r = vub300->resp.response.command_response;
  1660. if (bytes == 3) {
  1661. cmd->resp[words] = (r[1 + (words << 2)] << 24)
  1662. | (r[2 + (words << 2)] << 16)
  1663. | (r[3 + (words << 2)] << 8);
  1664. } else if (bytes == 2) {
  1665. cmd->resp[words] = (r[1 + (words << 2)] << 24)
  1666. | (r[2 + (words << 2)] << 16);
  1667. } else if (bytes == 1) {
  1668. cmd->resp[words] = (r[1 + (words << 2)] << 24);
  1669. }
  1670. while (words-- > 0) {
  1671. cmd->resp[words] = (r[1 + (words << 2)] << 24)
  1672. | (r[2 + (words << 2)] << 16)
  1673. | (r[3 + (words << 2)] << 8)
  1674. | (r[4 + (words << 2)] << 0);
  1675. }
  1676. if ((cmd->opcode == 53) && (0x000000FF & cmd->resp[0]))
  1677. cmd->resp[0] &= 0xFFFFFF00;
  1678. }
  1679. /* this thread runs only when there is an upper level command req outstanding */
  1680. static void vub300_cmndwork_thread(struct work_struct *work)
  1681. {
  1682. struct vub300_mmc_host *vub300 =
  1683. container_of(work, struct vub300_mmc_host, cmndwork);
  1684. if (!vub300->interface) {
  1685. kref_put(&vub300->kref, vub300_delete);
  1686. return;
  1687. } else {
  1688. struct mmc_request *req = vub300->req;
  1689. struct mmc_command *cmd = vub300->cmd;
  1690. struct mmc_data *data = vub300->data;
  1691. int data_length;
  1692. mutex_lock(&vub300->cmd_mutex);
  1693. init_completion(&vub300->command_complete);
  1694. if (likely(vub300->vub_name[0]) || !vub300->mmc->card ||
  1695. !mmc_card_present(vub300->mmc->card)) {
  1696. /*
  1697. * the name of the EMPTY Pseudo firmware file
  1698. * is used as a flag to indicate that the file
  1699. * has been already downloaded to the VUB300 chip
  1700. */
  1701. } else if (0 == vub300->mmc->card->sdio_funcs) {
  1702. strncpy(vub300->vub_name, "SD memory device",
  1703. sizeof(vub300->vub_name));
  1704. } else {
  1705. download_offload_pseudocode(vub300);
  1706. }
  1707. send_command(vub300);
  1708. if (!data)
  1709. data_length = 0;
  1710. else if (MMC_DATA_READ & data->flags)
  1711. data_length = __command_read_data(vub300, cmd, data);
  1712. else
  1713. data_length = __command_write_data(vub300, cmd, data);
  1714. __vub300_command_response(vub300, cmd, data, data_length);
  1715. vub300->req = NULL;
  1716. vub300->cmd = NULL;
  1717. vub300->data = NULL;
  1718. if (cmd->error) {
  1719. if (cmd->error == -ENOMEDIUM)
  1720. check_vub300_port_status(vub300);
  1721. mutex_unlock(&vub300->cmd_mutex);
  1722. mmc_request_done(vub300->mmc, req);
  1723. kref_put(&vub300->kref, vub300_delete);
  1724. return;
  1725. } else {
  1726. construct_request_response(vub300, cmd);
  1727. vub300->resp_len = 0;
  1728. mutex_unlock(&vub300->cmd_mutex);
  1729. kref_put(&vub300->kref, vub300_delete);
  1730. mmc_request_done(vub300->mmc, req);
  1731. return;
  1732. }
  1733. }
  1734. }
  1735. static int examine_cyclic_buffer(struct vub300_mmc_host *vub300,
  1736. struct mmc_command *cmd, u8 Function)
  1737. {
  1738. /* cmd_mutex is held by vub300_mmc_request */
  1739. u8 cmd0 = 0xFF & (cmd->arg >> 24);
  1740. u8 cmd1 = 0xFF & (cmd->arg >> 16);
  1741. u8 cmd2 = 0xFF & (cmd->arg >> 8);
  1742. u8 cmd3 = 0xFF & (cmd->arg >> 0);
  1743. int first = MAXREGMASK & vub300->fn[Function].offload_point;
  1744. struct offload_registers_access *rf = &vub300->fn[Function].reg[first];
  1745. if (cmd0 == rf->command_byte[0] &&
  1746. cmd1 == rf->command_byte[1] &&
  1747. cmd2 == rf->command_byte[2] &&
  1748. cmd3 == rf->command_byte[3]) {
  1749. u8 checksum = 0x00;
  1750. cmd->resp[1] = checksum << 24;
  1751. cmd->resp[0] = (rf->Respond_Byte[0] << 24)
  1752. | (rf->Respond_Byte[1] << 16)
  1753. | (rf->Respond_Byte[2] << 8)
  1754. | (rf->Respond_Byte[3] << 0);
  1755. vub300->fn[Function].offload_point += 1;
  1756. vub300->fn[Function].offload_count -= 1;
  1757. vub300->total_offload_count -= 1;
  1758. return 1;
  1759. } else {
  1760. int delta = 1; /* because it does not match the first one */
  1761. u8 register_count = vub300->fn[Function].offload_count - 1;
  1762. u32 register_point = vub300->fn[Function].offload_point + 1;
  1763. while (0 < register_count) {
  1764. int point = MAXREGMASK & register_point;
  1765. struct offload_registers_access *r =
  1766. &vub300->fn[Function].reg[point];
  1767. if (cmd0 == r->command_byte[0] &&
  1768. cmd1 == r->command_byte[1] &&
  1769. cmd2 == r->command_byte[2] &&
  1770. cmd3 == r->command_byte[3]) {
  1771. u8 checksum = 0x00;
  1772. cmd->resp[1] = checksum << 24;
  1773. cmd->resp[0] = (r->Respond_Byte[0] << 24)
  1774. | (r->Respond_Byte[1] << 16)
  1775. | (r->Respond_Byte[2] << 8)
  1776. | (r->Respond_Byte[3] << 0);
  1777. vub300->fn[Function].offload_point += delta;
  1778. vub300->fn[Function].offload_count -= delta;
  1779. vub300->total_offload_count -= delta;
  1780. return 1;
  1781. } else {
  1782. register_point += 1;
  1783. register_count -= 1;
  1784. delta += 1;
  1785. continue;
  1786. }
  1787. }
  1788. return 0;
  1789. }
  1790. }
  1791. static int satisfy_request_from_offloaded_data(struct vub300_mmc_host *vub300,
  1792. struct mmc_command *cmd)
  1793. {
  1794. /* cmd_mutex is held by vub300_mmc_request */
  1795. u8 regs = vub300->dynamic_register_count;
  1796. u8 i = 0;
  1797. u8 func = FUN(cmd);
  1798. u32 reg = REG(cmd);
  1799. while (0 < regs--) {
  1800. if ((vub300->sdio_register[i].func_num == func) &&
  1801. (vub300->sdio_register[i].sdio_reg == reg)) {
  1802. if (!vub300->sdio_register[i].prepared) {
  1803. return 0;
  1804. } else if ((0x80000000 & cmd->arg) == 0x80000000) {
  1805. /*
  1806. * a write to a dynamic register
  1807. * nullifies our offloaded value
  1808. */
  1809. vub300->sdio_register[i].prepared = 0;
  1810. return 0;
  1811. } else {
  1812. u8 checksum = 0x00;
  1813. u8 rsp0 = 0x00;
  1814. u8 rsp1 = 0x00;
  1815. u8 rsp2 = vub300->sdio_register[i].response;
  1816. u8 rsp3 = vub300->sdio_register[i].regvalue;
  1817. vub300->sdio_register[i].prepared = 0;
  1818. cmd->resp[1] = checksum << 24;
  1819. cmd->resp[0] = (rsp0 << 24)
  1820. | (rsp1 << 16)
  1821. | (rsp2 << 8)
  1822. | (rsp3 << 0);
  1823. return 1;
  1824. }
  1825. } else {
  1826. i += 1;
  1827. continue;
  1828. }
  1829. }
  1830. if (vub300->total_offload_count == 0)
  1831. return 0;
  1832. else if (vub300->fn[func].offload_count == 0)
  1833. return 0;
  1834. else
  1835. return examine_cyclic_buffer(vub300, cmd, func);
  1836. }
  1837. static void vub300_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1838. { /* NOT irq */
  1839. struct mmc_command *cmd = req->cmd;
  1840. struct vub300_mmc_host *vub300 = mmc_priv(mmc);
  1841. if (!vub300->interface) {
  1842. cmd->error = -ESHUTDOWN;
  1843. mmc_request_done(mmc, req);
  1844. return;
  1845. } else {
  1846. struct mmc_data *data = req->data;
  1847. if (!vub300->card_powered) {
  1848. cmd->error = -ENOMEDIUM;
  1849. mmc_request_done(mmc, req);
  1850. return;
  1851. }
  1852. if (!vub300->card_present) {
  1853. cmd->error = -ENOMEDIUM;
  1854. mmc_request_done(mmc, req);
  1855. return;
  1856. }
  1857. if (vub300->usb_transport_fail) {
  1858. cmd->error = vub300->usb_transport_fail;
  1859. mmc_request_done(mmc, req);
  1860. return;
  1861. }
  1862. if (!vub300->interface) {
  1863. cmd->error = -ENODEV;
  1864. mmc_request_done(mmc, req);
  1865. return;
  1866. }
  1867. kref_get(&vub300->kref);
  1868. mutex_lock(&vub300->cmd_mutex);
  1869. mod_timer(&vub300->inactivity_timer, jiffies + HZ);
  1870. /*
  1871. * for performance we have to return immediately
  1872. * if the requested data has been offloaded
  1873. */
  1874. if (cmd->opcode == 52 &&
  1875. satisfy_request_from_offloaded_data(vub300, cmd)) {
  1876. cmd->error = 0;
  1877. mutex_unlock(&vub300->cmd_mutex);
  1878. kref_put(&vub300->kref, vub300_delete);
  1879. mmc_request_done(mmc, req);
  1880. return;
  1881. } else {
  1882. vub300->cmd = cmd;
  1883. vub300->req = req;
  1884. vub300->data = data;
  1885. if (data)
  1886. vub300->datasize = data->blksz * data->blocks;
  1887. else
  1888. vub300->datasize = 0;
  1889. vub300_queue_cmnd_work(vub300);
  1890. mutex_unlock(&vub300->cmd_mutex);
  1891. kref_put(&vub300->kref, vub300_delete);
  1892. /*
  1893. * the kernel lock diagnostics complain
  1894. * if the cmd_mutex * is "passed on"
  1895. * to the cmndwork thread,
  1896. * so we must release it now
  1897. * and re-acquire it in the cmndwork thread
  1898. */
  1899. }
  1900. }
  1901. }
  1902. static void __set_clock_speed(struct vub300_mmc_host *vub300, u8 buf[8],
  1903. struct mmc_ios *ios)
  1904. {
  1905. int buf_array_size = 8; /* ARRAY_SIZE(buf) does not work !!! */
  1906. int retval;
  1907. u32 kHzClock;
  1908. if (ios->clock >= 48000000)
  1909. kHzClock = 48000;
  1910. else if (ios->clock >= 24000000)
  1911. kHzClock = 24000;
  1912. else if (ios->clock >= 20000000)
  1913. kHzClock = 20000;
  1914. else if (ios->clock >= 15000000)
  1915. kHzClock = 15000;
  1916. else if (ios->clock >= 200000)
  1917. kHzClock = 200;
  1918. else
  1919. kHzClock = 0;
  1920. {
  1921. int i;
  1922. u64 c = kHzClock;
  1923. for (i = 0; i < buf_array_size; i++) {
  1924. buf[i] = c;
  1925. c >>= 8;
  1926. }
  1927. }
  1928. retval =
  1929. usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
  1930. SET_CLOCK_SPEED,
  1931. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1932. 0x00, 0x00, buf, buf_array_size, HZ);
  1933. if (retval != 8) {
  1934. dev_err(&vub300->udev->dev, "SET_CLOCK_SPEED"
  1935. " %dkHz failed with retval=%d\n", kHzClock, retval);
  1936. } else {
  1937. dev_dbg(&vub300->udev->dev, "SET_CLOCK_SPEED"
  1938. " %dkHz\n", kHzClock);
  1939. }
  1940. }
  1941. static void vub300_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1942. { /* NOT irq */
  1943. struct vub300_mmc_host *vub300 = mmc_priv(mmc);
  1944. if (!vub300->interface)
  1945. return;
  1946. kref_get(&vub300->kref);
  1947. mutex_lock(&vub300->cmd_mutex);
  1948. if ((ios->power_mode == MMC_POWER_OFF) && vub300->card_powered) {
  1949. vub300->card_powered = 0;
  1950. usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
  1951. SET_SD_POWER,
  1952. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1953. 0x0000, 0x0000, NULL, 0, HZ);
  1954. /* must wait for the VUB300 u-proc to boot up */
  1955. msleep(600);
  1956. } else if ((ios->power_mode == MMC_POWER_UP) && !vub300->card_powered) {
  1957. usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
  1958. SET_SD_POWER,
  1959. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1960. 0x0001, 0x0000, NULL, 0, HZ);
  1961. msleep(600);
  1962. vub300->card_powered = 1;
  1963. } else if (ios->power_mode == MMC_POWER_ON) {
  1964. u8 *buf = kmalloc(8, GFP_KERNEL);
  1965. if (buf) {
  1966. __set_clock_speed(vub300, buf, ios);
  1967. kfree(buf);
  1968. }
  1969. } else {
  1970. /* this should mean no change of state */
  1971. }
  1972. mutex_unlock(&vub300->cmd_mutex);
  1973. kref_put(&vub300->kref, vub300_delete);
  1974. }
  1975. static int vub300_mmc_get_ro(struct mmc_host *mmc)
  1976. {
  1977. struct vub300_mmc_host *vub300 = mmc_priv(mmc);
  1978. return vub300->read_only;
  1979. }
  1980. static void vub300_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1981. { /* NOT irq */
  1982. struct vub300_mmc_host *vub300 = mmc_priv(mmc);
  1983. if (!vub300->interface)
  1984. return;
  1985. kref_get(&vub300->kref);
  1986. if (enable) {
  1987. mutex_lock(&vub300->irq_mutex);
  1988. if (vub300->irqs_queued) {
  1989. vub300->irqs_queued -= 1;
  1990. mmc_signal_sdio_irq(vub300->mmc);
  1991. } else if (vub300->irq_disabled) {
  1992. vub300->irq_disabled = 0;
  1993. vub300->irq_enabled = 1;
  1994. vub300_queue_poll_work(vub300, 0);
  1995. } else if (vub300->irq_enabled) {
  1996. /* this should not happen, so we will just ignore it */
  1997. } else {
  1998. vub300->irq_enabled = 1;
  1999. vub300_queue_poll_work(vub300, 0);
  2000. }
  2001. mutex_unlock(&vub300->irq_mutex);
  2002. } else {
  2003. vub300->irq_enabled = 0;
  2004. }
  2005. kref_put(&vub300->kref, vub300_delete);
  2006. }
  2007. static void vub300_init_card(struct mmc_host *mmc, struct mmc_card *card)
  2008. { /* NOT irq */
  2009. struct vub300_mmc_host *vub300 = mmc_priv(mmc);
  2010. dev_info(&vub300->udev->dev, "NO host QUIRKS for this card\n");
  2011. }
  2012. static struct mmc_host_ops vub300_mmc_ops = {
  2013. .request = vub300_mmc_request,
  2014. .set_ios = vub300_mmc_set_ios,
  2015. .get_ro = vub300_mmc_get_ro,
  2016. .enable_sdio_irq = vub300_enable_sdio_irq,
  2017. .init_card = vub300_init_card,
  2018. };
  2019. static int vub300_probe(struct usb_interface *interface,
  2020. const struct usb_device_id *id)
  2021. { /* NOT irq */
  2022. struct vub300_mmc_host *vub300;
  2023. struct usb_host_interface *iface_desc;
  2024. struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
  2025. int i;
  2026. int retval = -ENOMEM;
  2027. struct urb *command_out_urb;
  2028. struct urb *command_res_urb;
  2029. struct mmc_host *mmc;
  2030. char manufacturer[48];
  2031. char product[32];
  2032. char serial_number[32];
  2033. usb_string(udev, udev->descriptor.iManufacturer, manufacturer,
  2034. sizeof(manufacturer));
  2035. usb_string(udev, udev->descriptor.iProduct, product, sizeof(product));
  2036. usb_string(udev, udev->descriptor.iSerialNumber, serial_number,
  2037. sizeof(serial_number));
  2038. dev_info(&udev->dev, "probing VID:PID(%04X:%04X) %s %s %s\n",
  2039. udev->descriptor.idVendor, udev->descriptor.idProduct,
  2040. manufacturer, product, serial_number);
  2041. command_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  2042. if (!command_out_urb) {
  2043. retval = -ENOMEM;
  2044. goto error0;
  2045. }
  2046. command_res_urb = usb_alloc_urb(0, GFP_KERNEL);
  2047. if (!command_res_urb) {
  2048. retval = -ENOMEM;
  2049. goto error1;
  2050. }
  2051. /* this also allocates memory for our VUB300 mmc host device */
  2052. mmc = mmc_alloc_host(sizeof(struct vub300_mmc_host), &udev->dev);
  2053. if (!mmc) {
  2054. retval = -ENOMEM;
  2055. dev_err(&udev->dev, "not enough memory for the mmc_host\n");
  2056. goto error4;
  2057. }
  2058. /* MMC core transfer sizes tunable parameters */
  2059. mmc->caps = 0;
  2060. if (!force_1_bit_data_xfers)
  2061. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2062. if (!force_polling_for_irqs)
  2063. mmc->caps |= MMC_CAP_SDIO_IRQ;
  2064. mmc->caps &= ~MMC_CAP_NEEDS_POLL;
  2065. /*
  2066. * MMC_CAP_NEEDS_POLL causes core.c:mmc_rescan() to poll
  2067. * for devices which results in spurious CMD7's being
  2068. * issued which stops some SDIO cards from working
  2069. */
  2070. if (limit_speed_to_24_MHz) {
  2071. mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
  2072. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  2073. mmc->f_max = 24000000;
  2074. dev_info(&udev->dev, "limiting SDIO speed to 24_MHz\n");
  2075. } else {
  2076. mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
  2077. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  2078. mmc->f_max = 48000000;
  2079. }
  2080. mmc->f_min = 200000;
  2081. mmc->max_blk_count = 511;
  2082. mmc->max_blk_size = 512;
  2083. mmc->max_segs = 128;
  2084. if (force_max_req_size)
  2085. mmc->max_req_size = force_max_req_size * 1024;
  2086. else
  2087. mmc->max_req_size = 64 * 1024;
  2088. mmc->max_seg_size = mmc->max_req_size;
  2089. mmc->ocr_avail = 0;
  2090. mmc->ocr_avail |= MMC_VDD_165_195;
  2091. mmc->ocr_avail |= MMC_VDD_20_21;
  2092. mmc->ocr_avail |= MMC_VDD_21_22;
  2093. mmc->ocr_avail |= MMC_VDD_22_23;
  2094. mmc->ocr_avail |= MMC_VDD_23_24;
  2095. mmc->ocr_avail |= MMC_VDD_24_25;
  2096. mmc->ocr_avail |= MMC_VDD_25_26;
  2097. mmc->ocr_avail |= MMC_VDD_26_27;
  2098. mmc->ocr_avail |= MMC_VDD_27_28;
  2099. mmc->ocr_avail |= MMC_VDD_28_29;
  2100. mmc->ocr_avail |= MMC_VDD_29_30;
  2101. mmc->ocr_avail |= MMC_VDD_30_31;
  2102. mmc->ocr_avail |= MMC_VDD_31_32;
  2103. mmc->ocr_avail |= MMC_VDD_32_33;
  2104. mmc->ocr_avail |= MMC_VDD_33_34;
  2105. mmc->ocr_avail |= MMC_VDD_34_35;
  2106. mmc->ocr_avail |= MMC_VDD_35_36;
  2107. mmc->ops = &vub300_mmc_ops;
  2108. vub300 = mmc_priv(mmc);
  2109. vub300->mmc = mmc;
  2110. vub300->card_powered = 0;
  2111. vub300->bus_width = 0;
  2112. vub300->cmnd.head.block_size[0] = 0x00;
  2113. vub300->cmnd.head.block_size[1] = 0x00;
  2114. vub300->app_spec = 0;
  2115. mutex_init(&vub300->cmd_mutex);
  2116. mutex_init(&vub300->irq_mutex);
  2117. vub300->command_out_urb = command_out_urb;
  2118. vub300->command_res_urb = command_res_urb;
  2119. vub300->usb_timed_out = 0;
  2120. vub300->dynamic_register_count = 0;
  2121. for (i = 0; i < ARRAY_SIZE(vub300->fn); i++) {
  2122. vub300->fn[i].offload_point = 0;
  2123. vub300->fn[i].offload_count = 0;
  2124. }
  2125. vub300->total_offload_count = 0;
  2126. vub300->irq_enabled = 0;
  2127. vub300->irq_disabled = 0;
  2128. vub300->irqs_queued = 0;
  2129. for (i = 0; i < ARRAY_SIZE(vub300->sdio_register); i++)
  2130. vub300->sdio_register[i++].activate = 0;
  2131. vub300->udev = udev;
  2132. vub300->interface = interface;
  2133. vub300->cmnd_res_ep = 0;
  2134. vub300->cmnd_out_ep = 0;
  2135. vub300->data_inp_ep = 0;
  2136. vub300->data_out_ep = 0;
  2137. for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
  2138. vub300->fbs[i] = 512;
  2139. /*
  2140. * set up the endpoint information
  2141. *
  2142. * use the first pair of bulk-in and bulk-out
  2143. * endpoints for Command/Response+Interrupt
  2144. *
  2145. * use the second pair of bulk-in and bulk-out
  2146. * endpoints for Data In/Out
  2147. */
  2148. vub300->large_usb_packets = 0;
  2149. iface_desc = interface->cur_altsetting;
  2150. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  2151. struct usb_endpoint_descriptor *endpoint =
  2152. &iface_desc->endpoint[i].desc;
  2153. dev_info(&vub300->udev->dev,
  2154. "vub300 testing %s EndPoint(%d) %02X\n",
  2155. usb_endpoint_is_bulk_in(endpoint) ? "BULK IN" :
  2156. usb_endpoint_is_bulk_out(endpoint) ? "BULK OUT" :
  2157. "UNKNOWN", i, endpoint->bEndpointAddress);
  2158. if (endpoint->wMaxPacketSize > 64)
  2159. vub300->large_usb_packets = 1;
  2160. if (usb_endpoint_is_bulk_in(endpoint)) {
  2161. if (!vub300->cmnd_res_ep) {
  2162. vub300->cmnd_res_ep =
  2163. endpoint->bEndpointAddress;
  2164. } else if (!vub300->data_inp_ep) {
  2165. vub300->data_inp_ep =
  2166. endpoint->bEndpointAddress;
  2167. } else {
  2168. dev_warn(&vub300->udev->dev,
  2169. "ignoring"
  2170. " unexpected bulk_in endpoint");
  2171. }
  2172. } else if (usb_endpoint_is_bulk_out(endpoint)) {
  2173. if (!vub300->cmnd_out_ep) {
  2174. vub300->cmnd_out_ep =
  2175. endpoint->bEndpointAddress;
  2176. } else if (!vub300->data_out_ep) {
  2177. vub300->data_out_ep =
  2178. endpoint->bEndpointAddress;
  2179. } else {
  2180. dev_warn(&vub300->udev->dev,
  2181. "ignoring"
  2182. " unexpected bulk_out endpoint");
  2183. }
  2184. } else {
  2185. dev_warn(&vub300->udev->dev,
  2186. "vub300 ignoring EndPoint(%d) %02X", i,
  2187. endpoint->bEndpointAddress);
  2188. }
  2189. }
  2190. if (vub300->cmnd_res_ep && vub300->cmnd_out_ep &&
  2191. vub300->data_inp_ep && vub300->data_out_ep) {
  2192. dev_info(&vub300->udev->dev,
  2193. "vub300 %s packets"
  2194. " using EndPoints %02X %02X %02X %02X\n",
  2195. vub300->large_usb_packets ? "LARGE" : "SMALL",
  2196. vub300->cmnd_out_ep, vub300->cmnd_res_ep,
  2197. vub300->data_out_ep, vub300->data_inp_ep);
  2198. /* we have the expected EndPoints */
  2199. } else {
  2200. dev_err(&vub300->udev->dev,
  2201. "Could not find two sets of bulk-in/out endpoint pairs\n");
  2202. retval = -EINVAL;
  2203. goto error5;
  2204. }
  2205. retval =
  2206. usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
  2207. GET_HC_INF0,
  2208. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  2209. 0x0000, 0x0000, &vub300->hc_info,
  2210. sizeof(vub300->hc_info), HZ);
  2211. if (retval < 0)
  2212. goto error5;
  2213. retval =
  2214. usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
  2215. SET_ROM_WAIT_STATES,
  2216. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  2217. firmware_rom_wait_states, 0x0000, NULL, 0, HZ);
  2218. if (retval < 0)
  2219. goto error5;
  2220. dev_info(&vub300->udev->dev,
  2221. "operating_mode = %s %s %d MHz %s %d byte USB packets\n",
  2222. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "IRQs" : "POLL",
  2223. (mmc->caps & MMC_CAP_4_BIT_DATA) ? "4-bit" : "1-bit",
  2224. mmc->f_max / 1000000,
  2225. pad_input_to_usb_pkt ? "padding input data to" : "with",
  2226. vub300->large_usb_packets ? 512 : 64);
  2227. retval =
  2228. usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
  2229. GET_SYSTEM_PORT_STATUS,
  2230. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  2231. 0x0000, 0x0000, &vub300->system_port_status,
  2232. sizeof(vub300->system_port_status), HZ);
  2233. if (retval < 0) {
  2234. goto error4;
  2235. } else if (sizeof(vub300->system_port_status) == retval) {
  2236. vub300->card_present =
  2237. (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
  2238. vub300->read_only =
  2239. (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
  2240. } else {
  2241. goto error4;
  2242. }
  2243. usb_set_intfdata(interface, vub300);
  2244. INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread);
  2245. INIT_WORK(&vub300->cmndwork, vub300_cmndwork_thread);
  2246. INIT_WORK(&vub300->deadwork, vub300_deadwork_thread);
  2247. kref_init(&vub300->kref);
  2248. init_timer(&vub300->sg_transfer_timer);
  2249. vub300->sg_transfer_timer.data = (unsigned long)vub300;
  2250. vub300->sg_transfer_timer.function = vub300_sg_timed_out;
  2251. kref_get(&vub300->kref);
  2252. init_timer(&vub300->inactivity_timer);
  2253. vub300->inactivity_timer.data = (unsigned long)vub300;
  2254. vub300->inactivity_timer.function = vub300_inactivity_timer_expired;
  2255. vub300->inactivity_timer.expires = jiffies + HZ;
  2256. add_timer(&vub300->inactivity_timer);
  2257. if (vub300->card_present)
  2258. dev_info(&vub300->udev->dev,
  2259. "USB vub300 remote SDIO host controller[%d]"
  2260. "connected with SD/SDIO card inserted\n",
  2261. interface_to_InterfaceNumber(interface));
  2262. else
  2263. dev_info(&vub300->udev->dev,
  2264. "USB vub300 remote SDIO host controller[%d]"
  2265. "connected with no SD/SDIO card inserted\n",
  2266. interface_to_InterfaceNumber(interface));
  2267. mmc_add_host(mmc);
  2268. return 0;
  2269. error5:
  2270. mmc_free_host(mmc);
  2271. /*
  2272. * and hence also frees vub300
  2273. * which is contained at the end of struct mmc
  2274. */
  2275. error4:
  2276. usb_free_urb(command_res_urb);
  2277. error1:
  2278. usb_free_urb(command_out_urb);
  2279. error0:
  2280. usb_put_dev(udev);
  2281. return retval;
  2282. }
  2283. static void vub300_disconnect(struct usb_interface *interface)
  2284. { /* NOT irq */
  2285. struct vub300_mmc_host *vub300 = usb_get_intfdata(interface);
  2286. if (!vub300 || !vub300->mmc) {
  2287. return;
  2288. } else {
  2289. struct mmc_host *mmc = vub300->mmc;
  2290. if (!vub300->mmc) {
  2291. return;
  2292. } else {
  2293. int ifnum = interface_to_InterfaceNumber(interface);
  2294. usb_set_intfdata(interface, NULL);
  2295. /* prevent more I/O from starting */
  2296. vub300->interface = NULL;
  2297. kref_put(&vub300->kref, vub300_delete);
  2298. mmc_remove_host(mmc);
  2299. pr_info("USB vub300 remote SDIO host controller[%d]"
  2300. " now disconnected", ifnum);
  2301. return;
  2302. }
  2303. }
  2304. }
  2305. #ifdef CONFIG_PM
  2306. static int vub300_suspend(struct usb_interface *intf, pm_message_t message)
  2307. {
  2308. return 0;
  2309. }
  2310. static int vub300_resume(struct usb_interface *intf)
  2311. {
  2312. return 0;
  2313. }
  2314. #else
  2315. #define vub300_suspend NULL
  2316. #define vub300_resume NULL
  2317. #endif
  2318. static int vub300_pre_reset(struct usb_interface *intf)
  2319. { /* NOT irq */
  2320. struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
  2321. mutex_lock(&vub300->cmd_mutex);
  2322. return 0;
  2323. }
  2324. static int vub300_post_reset(struct usb_interface *intf)
  2325. { /* NOT irq */
  2326. struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
  2327. /* we are sure no URBs are active - no locking needed */
  2328. vub300->errors = -EPIPE;
  2329. mutex_unlock(&vub300->cmd_mutex);
  2330. return 0;
  2331. }
  2332. static struct usb_driver vub300_driver = {
  2333. .name = "vub300",
  2334. .probe = vub300_probe,
  2335. .disconnect = vub300_disconnect,
  2336. .suspend = vub300_suspend,
  2337. .resume = vub300_resume,
  2338. .pre_reset = vub300_pre_reset,
  2339. .post_reset = vub300_post_reset,
  2340. .id_table = vub300_table,
  2341. .supports_autosuspend = 1,
  2342. };
  2343. static int __init vub300_init(void)
  2344. { /* NOT irq */
  2345. int result;
  2346. pr_info("VUB300 Driver rom wait states = %02X irqpoll timeout = %04X",
  2347. firmware_rom_wait_states, 0x0FFFF & firmware_irqpoll_timeout);
  2348. cmndworkqueue = create_singlethread_workqueue("kvub300c");
  2349. if (!cmndworkqueue) {
  2350. pr_err("not enough memory for the REQUEST workqueue");
  2351. result = -ENOMEM;
  2352. goto out1;
  2353. }
  2354. pollworkqueue = create_singlethread_workqueue("kvub300p");
  2355. if (!pollworkqueue) {
  2356. pr_err("not enough memory for the IRQPOLL workqueue");
  2357. result = -ENOMEM;
  2358. goto out2;
  2359. }
  2360. deadworkqueue = create_singlethread_workqueue("kvub300d");
  2361. if (!deadworkqueue) {
  2362. pr_err("not enough memory for the EXPIRED workqueue");
  2363. result = -ENOMEM;
  2364. goto out3;
  2365. }
  2366. result = usb_register(&vub300_driver);
  2367. if (result) {
  2368. pr_err("usb_register failed. Error number %d", result);
  2369. goto out4;
  2370. }
  2371. return 0;
  2372. out4:
  2373. destroy_workqueue(deadworkqueue);
  2374. out3:
  2375. destroy_workqueue(pollworkqueue);
  2376. out2:
  2377. destroy_workqueue(cmndworkqueue);
  2378. out1:
  2379. return result;
  2380. }
  2381. static void __exit vub300_exit(void)
  2382. {
  2383. usb_deregister(&vub300_driver);
  2384. flush_workqueue(cmndworkqueue);
  2385. flush_workqueue(pollworkqueue);
  2386. flush_workqueue(deadworkqueue);
  2387. destroy_workqueue(cmndworkqueue);
  2388. destroy_workqueue(pollworkqueue);
  2389. destroy_workqueue(deadworkqueue);
  2390. }
  2391. module_init(vub300_init);
  2392. module_exit(vub300_exit);
  2393. MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
  2394. MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
  2395. MODULE_LICENSE("GPL");