msm72k_udc.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980
  1. /*
  2. * Driver for HighSpeed USB Client Controller in MSM7K
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
  6. * Author: Mike Lockwood <lockwood@android.com>
  7. * Brian Swetland <swetland@google.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/delay.h>
  24. #include <linux/timer.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/switch.h>
  32. #include <linux/pm_runtime.h>
  33. #include <mach/msm72k_otg.h>
  34. #include <linux/io.h>
  35. #include <asm/mach-types.h>
  36. #include <mach/board.h>
  37. #include <mach/msm_hsusb.h>
  38. #include <linux/device.h>
  39. #include <mach/msm_hsusb_hw.h>
  40. #include <mach/clk.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/wakelock.h>
  43. static const char driver_name[] = "msm72k_udc";
  44. /* #define DEBUG */
  45. /* #define VERBOSE */
  46. #define MSM_USB_BASE ((unsigned) ui->addr)
  47. #define DRIVER_DESC "MSM 72K USB Peripheral Controller"
  48. #define DRIVER_NAME "MSM72K_UDC"
  49. #define EPT_FLAG_IN 0x0001
  50. #define SETUP_BUF_SIZE 8
  51. static const char *const ep_name[] = {
  52. "ep0out", "ep1out", "ep2out", "ep3out",
  53. "ep4out", "ep5out", "ep6out", "ep7out",
  54. "ep8out", "ep9out", "ep10out", "ep11out",
  55. "ep12out", "ep13out", "ep14out", "ep15out",
  56. "ep0in", "ep1in", "ep2in", "ep3in",
  57. "ep4in", "ep5in", "ep6in", "ep7in",
  58. "ep8in", "ep9in", "ep10in", "ep11in",
  59. "ep12in", "ep13in", "ep14in", "ep15in"
  60. };
  61. /*To release the wakelock from debugfs*/
  62. static int release_wlocks;
  63. struct msm_request {
  64. struct usb_request req;
  65. /* saved copy of req.complete */
  66. void (*gadget_complete)(struct usb_ep *ep,
  67. struct usb_request *req);
  68. struct usb_info *ui;
  69. struct msm_request *next;
  70. struct msm_request *prev;
  71. unsigned busy:1;
  72. unsigned live:1;
  73. unsigned alloced:1;
  74. dma_addr_t dma;
  75. dma_addr_t item_dma;
  76. struct ept_queue_item *item;
  77. };
  78. #define to_msm_request(r) container_of(r, struct msm_request, req)
  79. #define to_msm_endpoint(r) container_of(r, struct msm_endpoint, ep)
  80. #define to_msm_otg(xceiv) container_of(xceiv, struct msm_otg, phy)
  81. #define is_b_sess_vld() ((OTGSC_BSV & readl(USB_OTGSC)) ? 1 : 0)
  82. #define is_usb_online(ui) (ui->usb_state != USB_STATE_NOTATTACHED)
  83. struct msm_endpoint {
  84. struct usb_ep ep;
  85. struct usb_info *ui;
  86. struct msm_request *req; /* head of pending requests */
  87. struct msm_request *last;
  88. unsigned flags;
  89. /* bit number (0-31) in various status registers
  90. ** as well as the index into the usb_info's array
  91. ** of all endpoints
  92. */
  93. unsigned char bit;
  94. unsigned char num;
  95. unsigned long dTD_update_fail_count;
  96. unsigned long false_prime_fail_count;
  97. unsigned actual_prime_fail_count;
  98. unsigned long dTD_workaround_fail_count;
  99. unsigned wedged:1;
  100. /* pointers to DMA transfer list area */
  101. /* these are allocated from the usb_info dma space */
  102. struct ept_queue_head *head;
  103. struct timer_list prime_timer;
  104. };
  105. /* PHY status check timer to monitor phy stuck up on reset */
  106. static struct timer_list phy_status_timer;
  107. static void ept_prime_timer_func(unsigned long data);
  108. static void usb_do_work(struct work_struct *w);
  109. static void usb_do_remote_wakeup(struct work_struct *w);
  110. #define USB_STATE_IDLE 0
  111. #define USB_STATE_ONLINE 1
  112. #define USB_STATE_OFFLINE 2
  113. #define USB_FLAG_START 0x0001
  114. #define USB_FLAG_VBUS_ONLINE 0x0002
  115. #define USB_FLAG_VBUS_OFFLINE 0x0004
  116. #define USB_FLAG_RESET 0x0008
  117. #define USB_FLAG_SUSPEND 0x0010
  118. #define USB_FLAG_CONFIGURED 0x0020
  119. #define USB_CHG_DET_DELAY msecs_to_jiffies(1000)
  120. #define REMOTE_WAKEUP_DELAY msecs_to_jiffies(1000)
  121. #define PHY_STATUS_CHECK_DELAY (jiffies + msecs_to_jiffies(1000))
  122. #define EPT_PRIME_CHECK_DELAY (jiffies + msecs_to_jiffies(1000))
  123. struct usb_info {
  124. /* lock for register/queue/device state changes */
  125. spinlock_t lock;
  126. /* single request used for handling setup transactions */
  127. struct usb_request *setup_req;
  128. struct platform_device *pdev;
  129. int irq;
  130. void *addr;
  131. unsigned state;
  132. unsigned flags;
  133. atomic_t configured;
  134. atomic_t running;
  135. struct dma_pool *pool;
  136. /* dma page to back the queue heads and items */
  137. unsigned char *buf;
  138. dma_addr_t dma;
  139. struct ept_queue_head *head;
  140. /* used for allocation */
  141. unsigned next_item;
  142. unsigned next_ifc_num;
  143. /* endpoints are ordered based on their status bits,
  144. ** so they are OUT0, OUT1, ... OUT15, IN0, IN1, ... IN15
  145. */
  146. struct msm_endpoint ept[32];
  147. /* max power requested by selected configuration */
  148. unsigned b_max_pow;
  149. unsigned chg_current;
  150. unsigned chg_type_retry_cnt;
  151. bool proprietary_chg;
  152. struct delayed_work chg_det;
  153. struct delayed_work chg_stop;
  154. struct msm_hsusb_gadget_platform_data *pdata;
  155. struct work_struct phy_status_check;
  156. struct work_struct work;
  157. unsigned phy_status;
  158. unsigned phy_fail_count;
  159. unsigned prime_fail_count;
  160. unsigned long dTD_update_fail_count;
  161. unsigned long dTD_workaround_fail_count;
  162. struct usb_gadget gadget;
  163. struct usb_gadget_driver *driver;
  164. struct switch_dev sdev;
  165. #define ep0out ept[0]
  166. #define ep0in ept[16]
  167. atomic_t ep0_dir;
  168. atomic_t test_mode;
  169. atomic_t offline_pending;
  170. atomic_t softconnect;
  171. #ifdef CONFIG_USB_OTG
  172. u8 hnp_avail;
  173. #endif
  174. atomic_t remote_wakeup;
  175. atomic_t self_powered;
  176. struct delayed_work rw_work;
  177. struct usb_phy *xceiv;
  178. enum usb_device_state usb_state;
  179. struct wake_lock wlock;
  180. };
  181. static const struct usb_ep_ops msm72k_ep_ops;
  182. static struct usb_info *the_usb_info;
  183. static int msm72k_wakeup(struct usb_gadget *_gadget);
  184. static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active);
  185. static int msm72k_set_halt(struct usb_ep *_ep, int value);
  186. static void flush_endpoint(struct msm_endpoint *ept);
  187. static void usb_reset(struct usb_info *ui);
  188. static int usb_ept_set_halt(struct usb_ep *_ep, int value);
  189. static void msm_hsusb_set_speed(struct usb_info *ui)
  190. {
  191. unsigned long flags;
  192. spin_lock_irqsave(&ui->lock, flags);
  193. switch (readl(USB_PORTSC) & PORTSC_PSPD_MASK) {
  194. case PORTSC_PSPD_FS:
  195. dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_FULL\n");
  196. ui->gadget.speed = USB_SPEED_FULL;
  197. break;
  198. case PORTSC_PSPD_LS:
  199. dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_LOW\n");
  200. ui->gadget.speed = USB_SPEED_LOW;
  201. break;
  202. case PORTSC_PSPD_HS:
  203. dev_dbg(&ui->pdev->dev, "portchange USB_SPEED_HIGH\n");
  204. ui->gadget.speed = USB_SPEED_HIGH;
  205. break;
  206. }
  207. spin_unlock_irqrestore(&ui->lock, flags);
  208. }
  209. static void msm_hsusb_set_state(enum usb_device_state state)
  210. {
  211. unsigned long flags;
  212. spin_lock_irqsave(&the_usb_info->lock, flags);
  213. the_usb_info->usb_state = state;
  214. spin_unlock_irqrestore(&the_usb_info->lock, flags);
  215. }
  216. static enum usb_device_state msm_hsusb_get_state(void)
  217. {
  218. unsigned long flags;
  219. enum usb_device_state state;
  220. spin_lock_irqsave(&the_usb_info->lock, flags);
  221. state = the_usb_info->usb_state;
  222. spin_unlock_irqrestore(&the_usb_info->lock, flags);
  223. return state;
  224. }
  225. static ssize_t print_switch_name(struct switch_dev *sdev, char *buf)
  226. {
  227. return snprintf(buf, PAGE_SIZE, "%s\n", DRIVER_NAME);
  228. }
  229. static ssize_t print_switch_state(struct switch_dev *sdev, char *buf)
  230. {
  231. return snprintf(buf, PAGE_SIZE, "%s\n",
  232. sdev->state ? "online" : "offline");
  233. }
  234. static inline enum chg_type usb_get_chg_type(struct usb_info *ui)
  235. {
  236. if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
  237. return USB_CHG_TYPE__WALLCHARGER;
  238. } else if (ui->pdata->prop_chg) {
  239. if (ui->gadget.speed == USB_SPEED_LOW ||
  240. ui->gadget.speed == USB_SPEED_FULL ||
  241. ui->gadget.speed == USB_SPEED_HIGH)
  242. return USB_CHG_TYPE__SDP;
  243. else
  244. return USB_CHG_TYPE__INVALID;
  245. } else {
  246. return USB_CHG_TYPE__SDP;
  247. }
  248. }
  249. #define USB_WALLCHARGER_CHG_CURRENT 1800
  250. #define USB_PROPRIETARY_CHG_CURRENT 500
  251. static int usb_get_max_power(struct usb_info *ui)
  252. {
  253. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  254. unsigned long flags;
  255. enum chg_type temp;
  256. int suspended;
  257. int configured;
  258. unsigned bmaxpow;
  259. if (ui->gadget.is_a_peripheral)
  260. return -EINVAL;
  261. temp = atomic_read(&otg->chg_type);
  262. spin_lock_irqsave(&ui->lock, flags);
  263. suspended = ui->usb_state == USB_STATE_SUSPENDED ? 1 : 0;
  264. configured = atomic_read(&ui->configured);
  265. bmaxpow = ui->b_max_pow;
  266. spin_unlock_irqrestore(&ui->lock, flags);
  267. if (temp == USB_CHG_TYPE__INVALID)
  268. return -ENODEV;
  269. if (temp == USB_CHG_TYPE__WALLCHARGER && !ui->proprietary_chg)
  270. return USB_WALLCHARGER_CHG_CURRENT;
  271. else if (ui->pdata->prop_chg)
  272. return USB_PROPRIETARY_CHG_CURRENT;
  273. if (suspended || !configured)
  274. return 0;
  275. return bmaxpow;
  276. }
  277. static int usb_phy_stuck_check(struct usb_info *ui)
  278. {
  279. /*
  280. * write some value (0xAA) into scratch reg (0x16) and read it back,
  281. * If the read value is same as written value, means PHY is normal
  282. * otherwise, PHY seems to have stuck.
  283. */
  284. if (usb_phy_io_write(ui->xceiv, 0xAA, 0x16) == -1) {
  285. dev_dbg(&ui->pdev->dev,
  286. "%s(): ulpi write timeout\n", __func__);
  287. return -EIO;
  288. }
  289. if (usb_phy_io_read(ui->xceiv, 0x16) != 0xAA) {
  290. dev_dbg(&ui->pdev->dev,
  291. "%s(): read value is incorrect\n", __func__);
  292. return -EIO;
  293. }
  294. return 0;
  295. }
  296. /*
  297. * This function checks the phy status by reading/writing to the
  298. * phy scratch register. If the phy is stuck resets the HW
  299. * */
  300. static void usb_phy_stuck_recover(struct work_struct *w)
  301. {
  302. struct usb_info *ui = the_usb_info;
  303. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  304. unsigned long flags;
  305. spin_lock_irqsave(&ui->lock, flags);
  306. if (ui->gadget.speed != USB_SPEED_UNKNOWN ||
  307. ui->usb_state == USB_STATE_NOTATTACHED ||
  308. ui->driver == NULL) {
  309. spin_unlock_irqrestore(&ui->lock, flags);
  310. return;
  311. }
  312. spin_unlock_irqrestore(&ui->lock, flags);
  313. disable_irq(otg->irq);
  314. if (usb_phy_stuck_check(ui)) {
  315. #ifdef CONFIG_USB_MSM_ACA
  316. del_timer_sync(&otg->id_timer);
  317. #endif
  318. ui->phy_fail_count++;
  319. dev_err(&ui->pdev->dev,
  320. "%s():PHY stuck, resetting HW\n", __func__);
  321. /*
  322. * PHY seems to have stuck,
  323. * reset the PHY and HW link to recover the PHY
  324. */
  325. usb_reset(ui);
  326. #ifdef CONFIG_USB_MSM_ACA
  327. mod_timer(&otg->id_timer, jiffies +
  328. msecs_to_jiffies(OTG_ID_POLL_MS));
  329. #endif
  330. msm72k_pullup_internal(&ui->gadget, 1);
  331. }
  332. enable_irq(otg->irq);
  333. }
  334. static void usb_phy_status_check_timer(unsigned long data)
  335. {
  336. struct usb_info *ui = the_usb_info;
  337. schedule_work(&ui->phy_status_check);
  338. }
  339. static void usb_chg_stop(struct work_struct *w)
  340. {
  341. struct usb_info *ui = container_of(w, struct usb_info, chg_stop.work);
  342. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  343. enum chg_type temp;
  344. temp = atomic_read(&otg->chg_type);
  345. if (temp == USB_CHG_TYPE__SDP)
  346. usb_phy_set_power(ui->xceiv, 0);
  347. }
  348. static void usb_chg_detect(struct work_struct *w)
  349. {
  350. struct usb_info *ui = container_of(w, struct usb_info, chg_det.work);
  351. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  352. enum chg_type temp = USB_CHG_TYPE__INVALID;
  353. unsigned long flags;
  354. int maxpower;
  355. spin_lock_irqsave(&ui->lock, flags);
  356. if (ui->usb_state == USB_STATE_NOTATTACHED) {
  357. spin_unlock_irqrestore(&ui->lock, flags);
  358. return;
  359. }
  360. temp = usb_get_chg_type(ui);
  361. if (temp != USB_CHG_TYPE__WALLCHARGER && temp != USB_CHG_TYPE__SDP
  362. && !ui->chg_type_retry_cnt) {
  363. schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
  364. ui->chg_type_retry_cnt++;
  365. spin_unlock_irqrestore(&ui->lock, flags);
  366. return;
  367. }
  368. if (temp == USB_CHG_TYPE__INVALID) {
  369. temp = USB_CHG_TYPE__WALLCHARGER;
  370. ui->proprietary_chg = true;
  371. }
  372. spin_unlock_irqrestore(&ui->lock, flags);
  373. atomic_set(&otg->chg_type, temp);
  374. maxpower = usb_get_max_power(ui);
  375. if (maxpower > 0)
  376. usb_phy_set_power(ui->xceiv, maxpower);
  377. /* USB driver prevents idle and suspend power collapse(pc)
  378. * while USB cable is connected. But when dedicated charger is
  379. * connected, driver can vote for idle and suspend pc.
  380. * OTG driver handles idle pc as part of above usb_phy_set_power call
  381. * when wallcharger is attached. To allow suspend pc, release the
  382. * wakelock which will be re-acquired for any sub-sequent usb interrupts
  383. * */
  384. if (temp == USB_CHG_TYPE__WALLCHARGER) {
  385. pm_runtime_put_sync(&ui->pdev->dev);
  386. wake_unlock(&ui->wlock);
  387. }
  388. }
  389. static int usb_ep_get_stall(struct msm_endpoint *ept)
  390. {
  391. unsigned int n;
  392. struct usb_info *ui = ept->ui;
  393. n = readl(USB_ENDPTCTRL(ept->num));
  394. if (ept->flags & EPT_FLAG_IN)
  395. return (CTRL_TXS & n) ? 1 : 0;
  396. else
  397. return (CTRL_RXS & n) ? 1 : 0;
  398. }
  399. static void init_endpoints(struct usb_info *ui)
  400. {
  401. unsigned n;
  402. for (n = 0; n < 32; n++) {
  403. struct msm_endpoint *ept = ui->ept + n;
  404. ept->ui = ui;
  405. ept->bit = n;
  406. ept->num = n & 15;
  407. ept->ep.name = ep_name[n];
  408. ept->ep.ops = &msm72k_ep_ops;
  409. if (ept->bit > 15) {
  410. /* IN endpoint */
  411. ept->head = ui->head + (ept->num << 1) + 1;
  412. ept->flags = EPT_FLAG_IN;
  413. } else {
  414. /* OUT endpoint */
  415. ept->head = ui->head + (ept->num << 1);
  416. ept->flags = 0;
  417. }
  418. setup_timer(&ept->prime_timer, ept_prime_timer_func,
  419. (unsigned long) ept);
  420. }
  421. }
  422. static void config_ept(struct msm_endpoint *ept)
  423. {
  424. struct usb_info *ui = ept->ui;
  425. unsigned cfg = CONFIG_MAX_PKT(ept->ep.maxpacket) | CONFIG_ZLT;
  426. const struct usb_endpoint_descriptor *desc = ept->ep.desc;
  427. unsigned mult = 0;
  428. if (desc && ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  429. == USB_ENDPOINT_XFER_ISOC)) {
  430. cfg &= ~(CONFIG_MULT);
  431. mult = ((ept->ep.maxpacket >> CONFIG_MULT_SHIFT) + 1) & 0x03;
  432. cfg |= (mult << (ffs(CONFIG_MULT) - 1));
  433. }
  434. /* ep0 out needs interrupt-on-setup */
  435. if (ept->bit == 0)
  436. cfg |= CONFIG_IOS;
  437. ept->head->config = cfg;
  438. ept->head->next = TERMINATE;
  439. if (ept->ep.maxpacket)
  440. dev_dbg(&ui->pdev->dev,
  441. "ept #%d %s max:%d head:%pK bit:%d\n",
  442. ept->num,
  443. (ept->flags & EPT_FLAG_IN) ? "in" : "out",
  444. ept->ep.maxpacket, ept->head, ept->bit);
  445. }
  446. static void configure_endpoints(struct usb_info *ui)
  447. {
  448. unsigned n;
  449. for (n = 0; n < 32; n++)
  450. config_ept(ui->ept + n);
  451. }
  452. struct usb_request *usb_ept_alloc_req(struct msm_endpoint *ept,
  453. unsigned bufsize, gfp_t gfp_flags)
  454. {
  455. struct usb_info *ui = ept->ui;
  456. struct msm_request *req;
  457. req = kzalloc(sizeof(*req), gfp_flags);
  458. if (!req)
  459. goto fail1;
  460. req->item = dma_pool_alloc(ui->pool, gfp_flags, &req->item_dma);
  461. if (!req->item)
  462. goto fail2;
  463. if (bufsize) {
  464. req->req.buf = kmalloc(bufsize, gfp_flags);
  465. if (!req->req.buf)
  466. goto fail3;
  467. req->alloced = 1;
  468. }
  469. return &req->req;
  470. fail3:
  471. dma_pool_free(ui->pool, req->item, req->item_dma);
  472. fail2:
  473. kfree(req);
  474. fail1:
  475. return 0;
  476. }
  477. static void usb_ept_enable(struct msm_endpoint *ept, int yes,
  478. unsigned char ep_type)
  479. {
  480. struct usb_info *ui = ept->ui;
  481. int in = ept->flags & EPT_FLAG_IN;
  482. unsigned n;
  483. n = readl(USB_ENDPTCTRL(ept->num));
  484. if (in) {
  485. if (yes) {
  486. n = (n & (~CTRL_TXT_MASK)) |
  487. (ep_type << CTRL_TXT_EP_TYPE_SHIFT);
  488. n |= CTRL_TXE | CTRL_TXR;
  489. } else
  490. n &= (~CTRL_TXE);
  491. } else {
  492. if (yes) {
  493. n = (n & (~CTRL_RXT_MASK)) |
  494. (ep_type << CTRL_RXT_EP_TYPE_SHIFT);
  495. n |= CTRL_RXE | CTRL_RXR;
  496. } else
  497. n &= ~(CTRL_RXE);
  498. }
  499. /* complete all the updates to ept->head before enabling endpoint*/
  500. mb();
  501. writel(n, USB_ENDPTCTRL(ept->num));
  502. /* Ensure endpoint is enabled before returning */
  503. mb();
  504. dev_dbg(&ui->pdev->dev, "ept %d %s %s\n",
  505. ept->num, in ? "in" : "out", yes ? "enabled" : "disabled");
  506. }
  507. static void ept_prime_timer_func(unsigned long data)
  508. {
  509. struct msm_endpoint *ept = (struct msm_endpoint *)data;
  510. struct usb_info *ui = ept->ui;
  511. unsigned n = 1 << ept->bit;
  512. unsigned long flags;
  513. spin_lock_irqsave(&ui->lock, flags);
  514. ept->false_prime_fail_count++;
  515. if ((readl_relaxed(USB_ENDPTPRIME) & n)) {
  516. /*
  517. * ---- UNLIKELY ---
  518. * May be hardware is taking long time to process the
  519. * prime request. Or could be intermittent priming and
  520. * previous dTD is not fired yet.
  521. */
  522. mod_timer(&ept->prime_timer, EPT_PRIME_CHECK_DELAY);
  523. goto out;
  524. }
  525. if (readl_relaxed(USB_ENDPTSTAT) & n)
  526. goto out;
  527. /* clear speculative loads on item->info */
  528. rmb();
  529. if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
  530. ui->prime_fail_count++;
  531. ept->actual_prime_fail_count++;
  532. pr_err("%s(): ept%d%s prime failed. ept: config: %x"
  533. "active: %x next: %x info: %x\n",
  534. __func__, ept->num,
  535. ept->flags & EPT_FLAG_IN ? "in" : "out",
  536. ept->head->config, ept->head->active,
  537. ept->head->next, ept->head->info);
  538. writel_relaxed(n, USB_ENDPTPRIME);
  539. mod_timer(&ept->prime_timer, EPT_PRIME_CHECK_DELAY);
  540. }
  541. out:
  542. spin_unlock_irqrestore(&ui->lock, flags);
  543. }
  544. static void usb_ept_start(struct msm_endpoint *ept)
  545. {
  546. struct usb_info *ui = ept->ui;
  547. struct msm_request *req = ept->req;
  548. unsigned n = 1 << ept->bit;
  549. BUG_ON(req->live);
  550. while (req) {
  551. req->live = 1;
  552. /* prepare the transaction descriptor item for the hardware */
  553. req->item->info =
  554. INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE;
  555. req->item->page0 = req->dma;
  556. req->item->page1 = (req->dma + 0x1000) & 0xfffff000;
  557. req->item->page2 = (req->dma + 0x2000) & 0xfffff000;
  558. req->item->page3 = (req->dma + 0x3000) & 0xfffff000;
  559. if (req->next == NULL) {
  560. req->item->next = TERMINATE;
  561. break;
  562. }
  563. req->item->next = req->next->item_dma;
  564. req = req->next;
  565. }
  566. rmb();
  567. /* link the hw queue head to the request's transaction item */
  568. ept->head->next = ept->req->item_dma;
  569. ept->head->info = 0;
  570. /* flush buffers before priming ept */
  571. mb();
  572. /* during high throughput testing it is observed that
  573. * ept stat bit is not set even though all the data
  574. * structures are updated properly and ept prime bit
  575. * is set. To workaround the issue, kick a timer and
  576. * make decision on re-prime. We can do a busy loop here
  577. * but it leads to high cpu usage.
  578. */
  579. writel_relaxed(n, USB_ENDPTPRIME);
  580. mod_timer(&ept->prime_timer, EPT_PRIME_CHECK_DELAY);
  581. }
  582. int usb_ept_queue_xfer(struct msm_endpoint *ept, struct usb_request *_req)
  583. {
  584. unsigned long flags;
  585. struct msm_request *req = to_msm_request(_req);
  586. struct msm_request *last;
  587. struct usb_info *ui = ept->ui;
  588. unsigned length = req->req.length;
  589. if (length > 0x4000)
  590. return -EMSGSIZE;
  591. spin_lock_irqsave(&ui->lock, flags);
  592. if (ept->num != 0 && ept->ep.desc == NULL) {
  593. req->req.status = -EINVAL;
  594. spin_unlock_irqrestore(&ui->lock, flags);
  595. dev_err(&ui->pdev->dev,
  596. "%s: called for disabled endpoint\n", __func__);
  597. return -EINVAL;
  598. }
  599. if (req->busy) {
  600. req->req.status = -EBUSY;
  601. spin_unlock_irqrestore(&ui->lock, flags);
  602. dev_err(&ui->pdev->dev,
  603. "usb_ept_queue_xfer() tried to queue busy request\n");
  604. return -EBUSY;
  605. }
  606. if (!atomic_read(&ui->configured) && (ept->num != 0)) {
  607. req->req.status = -ESHUTDOWN;
  608. spin_unlock_irqrestore(&ui->lock, flags);
  609. if (printk_ratelimit())
  610. dev_err(&ui->pdev->dev,
  611. "%s: called while offline\n", __func__);
  612. return -ESHUTDOWN;
  613. }
  614. if (ui->usb_state == USB_STATE_SUSPENDED) {
  615. if (!atomic_read(&ui->remote_wakeup)) {
  616. req->req.status = -EAGAIN;
  617. spin_unlock_irqrestore(&ui->lock, flags);
  618. if (printk_ratelimit())
  619. dev_err(&ui->pdev->dev,
  620. "%s: cannot queue as bus is suspended "
  621. "ept #%d %s max:%d head:%pK bit:%d\n",
  622. __func__, ept->num,
  623. (ept->flags & EPT_FLAG_IN) ? "in" : "out",
  624. ept->ep.maxpacket, ept->head, ept->bit);
  625. return -EAGAIN;
  626. }
  627. wake_lock(&ui->wlock);
  628. usb_phy_set_suspend(ui->xceiv, 0);
  629. schedule_delayed_work(&ui->rw_work, REMOTE_WAKEUP_DELAY);
  630. }
  631. req->busy = 1;
  632. req->live = 0;
  633. req->next = 0;
  634. req->req.status = -EBUSY;
  635. req->dma = dma_map_single(NULL, req->req.buf, length,
  636. (ept->flags & EPT_FLAG_IN) ?
  637. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  638. /* Add the new request to the end of the queue */
  639. last = ept->last;
  640. if (last) {
  641. /* Already requests in the queue. add us to the
  642. * end, but let the completion interrupt actually
  643. * start things going, to avoid hw issues
  644. */
  645. last->next = req;
  646. req->prev = last;
  647. } else {
  648. /* queue was empty -- kick the hardware */
  649. ept->req = req;
  650. req->prev = NULL;
  651. usb_ept_start(ept);
  652. }
  653. ept->last = req;
  654. spin_unlock_irqrestore(&ui->lock, flags);
  655. return 0;
  656. }
  657. /* --- endpoint 0 handling --- */
  658. static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  659. {
  660. struct msm_request *r = to_msm_request(req);
  661. struct msm_endpoint *ept = to_msm_endpoint(ep);
  662. struct usb_info *ui = ept->ui;
  663. req->complete = r->gadget_complete;
  664. r->gadget_complete = 0;
  665. if (req->complete)
  666. req->complete(&ui->ep0in.ep, req);
  667. }
  668. static void ep0_status_complete(struct usb_ep *ep, struct usb_request *_req)
  669. {
  670. struct usb_request *req = _req->context;
  671. struct msm_request *r;
  672. struct msm_endpoint *ept;
  673. struct usb_info *ui;
  674. pr_debug("%s:\n", __func__);
  675. if (!req)
  676. return;
  677. r = to_msm_request(req);
  678. ept = to_msm_endpoint(ep);
  679. ui = ept->ui;
  680. _req->context = 0;
  681. req->complete = r->gadget_complete;
  682. req->zero = 0;
  683. r->gadget_complete = 0;
  684. if (req->complete)
  685. req->complete(&ui->ep0in.ep, req);
  686. }
  687. static void ep0_status_phase(struct usb_ep *ep, struct usb_request *req)
  688. {
  689. struct msm_endpoint *ept = to_msm_endpoint(ep);
  690. struct usb_info *ui = ept->ui;
  691. pr_debug("%s:\n", __func__);
  692. req->length = 0;
  693. req->complete = ep0_status_complete;
  694. /* status phase */
  695. if (atomic_read(&ui->ep0_dir) == USB_DIR_IN)
  696. usb_ept_queue_xfer(&ui->ep0out, req);
  697. else
  698. usb_ept_queue_xfer(&ui->ep0in, req);
  699. }
  700. static void ep0in_send_zero_leng_pkt(struct msm_endpoint *ept)
  701. {
  702. struct usb_info *ui = ept->ui;
  703. struct usb_request *req = ui->setup_req;
  704. pr_debug("%s:\n", __func__);
  705. req->length = 0;
  706. req->complete = ep0_status_phase;
  707. usb_ept_queue_xfer(&ui->ep0in, req);
  708. }
  709. static void ep0_queue_ack_complete(struct usb_ep *ep,
  710. struct usb_request *_req)
  711. {
  712. struct msm_endpoint *ept = to_msm_endpoint(ep);
  713. struct usb_info *ui = ept->ui;
  714. struct usb_request *req = ui->setup_req;
  715. pr_debug("%s: _req:%pK actual:%d length:%d zero:%d\n",
  716. __func__, _req, _req->actual,
  717. _req->length, _req->zero);
  718. /* queue up the receive of the ACK response from the host */
  719. if (_req->status == 0 && _req->actual == _req->length) {
  720. req->context = _req;
  721. if (atomic_read(&ui->ep0_dir) == USB_DIR_IN) {
  722. if (_req->zero && _req->length &&
  723. !(_req->length % ep->maxpacket)) {
  724. ep0in_send_zero_leng_pkt(&ui->ep0in);
  725. return;
  726. }
  727. }
  728. ep0_status_phase(ep, req);
  729. } else
  730. ep0_complete(ep, _req);
  731. }
  732. static void ep0_setup_ack_complete(struct usb_ep *ep, struct usb_request *req)
  733. {
  734. struct msm_endpoint *ept = to_msm_endpoint(ep);
  735. struct usb_info *ui = ept->ui;
  736. unsigned int temp;
  737. int test_mode = atomic_read(&ui->test_mode);
  738. if (!test_mode)
  739. return;
  740. switch (test_mode) {
  741. case J_TEST:
  742. dev_info(&ui->pdev->dev, "usb electrical test mode: (J)\n");
  743. temp = readl(USB_PORTSC) & (~PORTSC_PTC);
  744. writel(temp | PORTSC_PTC_J_STATE, USB_PORTSC);
  745. break;
  746. case K_TEST:
  747. dev_info(&ui->pdev->dev, "usb electrical test mode: (K)\n");
  748. temp = readl(USB_PORTSC) & (~PORTSC_PTC);
  749. writel(temp | PORTSC_PTC_K_STATE, USB_PORTSC);
  750. break;
  751. case SE0_NAK_TEST:
  752. dev_info(&ui->pdev->dev,
  753. "usb electrical test mode: (SE0-NAK)\n");
  754. temp = readl(USB_PORTSC) & (~PORTSC_PTC);
  755. writel(temp | PORTSC_PTC_SE0_NAK, USB_PORTSC);
  756. break;
  757. case TST_PKT_TEST:
  758. dev_info(&ui->pdev->dev,
  759. "usb electrical test mode: (TEST_PKT)\n");
  760. temp = readl(USB_PORTSC) & (~PORTSC_PTC);
  761. writel(temp | PORTSC_PTC_TST_PKT, USB_PORTSC);
  762. break;
  763. }
  764. }
  765. static void ep0_setup_ack(struct usb_info *ui)
  766. {
  767. struct usb_request *req = ui->setup_req;
  768. req->length = 0;
  769. req->complete = ep0_setup_ack_complete;
  770. usb_ept_queue_xfer(&ui->ep0in, req);
  771. }
  772. static void ep0_setup_stall(struct usb_info *ui)
  773. {
  774. writel((1<<16) | (1<<0), USB_ENDPTCTRL(0));
  775. }
  776. static void ep0_setup_send(struct usb_info *ui, unsigned length)
  777. {
  778. struct usb_request *req = ui->setup_req;
  779. struct msm_request *r = to_msm_request(req);
  780. struct msm_endpoint *ept = &ui->ep0in;
  781. req->length = length;
  782. req->complete = ep0_queue_ack_complete;
  783. r->gadget_complete = 0;
  784. usb_ept_queue_xfer(ept, req);
  785. }
  786. static void handle_setup(struct usb_info *ui)
  787. {
  788. struct usb_ctrlrequest ctl;
  789. struct usb_request *req = ui->setup_req;
  790. int ret;
  791. #ifdef CONFIG_USB_OTG
  792. u8 hnp;
  793. unsigned long flags;
  794. #endif
  795. /* USB hardware sometimes generate interrupt before
  796. * 8 bytes of SETUP packet are written to system memory.
  797. * This results in fetching wrong setup_data sometimes.
  798. * TODO: Remove below workaround of adding 1us delay once
  799. * it gets fixed in hardware.
  800. */
  801. udelay(10);
  802. memcpy(&ctl, ui->ep0out.head->setup_data, sizeof(ctl));
  803. /* Ensure buffer is read before acknowledging to h/w */
  804. mb();
  805. writel(EPT_RX(0), USB_ENDPTSETUPSTAT);
  806. if (ctl.bRequestType & USB_DIR_IN)
  807. atomic_set(&ui->ep0_dir, USB_DIR_IN);
  808. else
  809. atomic_set(&ui->ep0_dir, USB_DIR_OUT);
  810. /* any pending ep0 transactions must be canceled */
  811. flush_endpoint(&ui->ep0out);
  812. flush_endpoint(&ui->ep0in);
  813. dev_dbg(&ui->pdev->dev,
  814. "setup: type=%02x req=%02x val=%04x idx=%04x len=%04x\n",
  815. ctl.bRequestType, ctl.bRequest, ctl.wValue,
  816. ctl.wIndex, ctl.wLength);
  817. if ((ctl.bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) ==
  818. (USB_DIR_IN | USB_TYPE_STANDARD)) {
  819. if (ctl.bRequest == USB_REQ_GET_STATUS) {
  820. /* OTG supplement Rev 2.0 introduces another device
  821. * GET_STATUS request for HNP polling with length = 1.
  822. */
  823. u8 len = 2;
  824. switch (ctl.bRequestType & USB_RECIP_MASK) {
  825. case USB_RECIP_ENDPOINT:
  826. {
  827. struct msm_endpoint *ept;
  828. unsigned num =
  829. ctl.wIndex & USB_ENDPOINT_NUMBER_MASK;
  830. u16 temp = 0;
  831. if (num == 0) {
  832. memset(req->buf, 0, 2);
  833. break;
  834. }
  835. if (ctl.wIndex & USB_ENDPOINT_DIR_MASK)
  836. num += 16;
  837. ept = &ui->ep0out + num;
  838. temp = usb_ep_get_stall(ept);
  839. temp = temp << USB_ENDPOINT_HALT;
  840. memcpy(req->buf, &temp, 2);
  841. break;
  842. }
  843. case USB_RECIP_DEVICE:
  844. {
  845. u16 temp = 0;
  846. if (ctl.wIndex == OTG_STATUS_SELECTOR) {
  847. #ifdef CONFIG_USB_OTG
  848. spin_lock_irqsave(&ui->lock, flags);
  849. hnp = (ui->gadget.host_request <<
  850. HOST_REQUEST_FLAG);
  851. ui->hnp_avail = 1;
  852. spin_unlock_irqrestore(&ui->lock,
  853. flags);
  854. memcpy(req->buf, &hnp, 1);
  855. len = 1;
  856. #else
  857. goto stall;
  858. #endif
  859. } else {
  860. temp = (atomic_read(&ui->self_powered)
  861. << USB_DEVICE_SELF_POWERED);
  862. temp |= (atomic_read(&ui->remote_wakeup)
  863. << USB_DEVICE_REMOTE_WAKEUP);
  864. memcpy(req->buf, &temp, 2);
  865. }
  866. break;
  867. }
  868. case USB_RECIP_INTERFACE:
  869. memset(req->buf, 0, 2);
  870. break;
  871. default:
  872. goto stall;
  873. }
  874. ep0_setup_send(ui, len);
  875. return;
  876. }
  877. }
  878. if (ctl.bRequestType ==
  879. (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)) {
  880. if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) ||
  881. (ctl.bRequest == USB_REQ_SET_FEATURE)) {
  882. if ((ctl.wValue == 0) && (ctl.wLength == 0)) {
  883. unsigned num = ctl.wIndex & 0x0f;
  884. if (num != 0) {
  885. struct msm_endpoint *ept;
  886. if (ctl.wIndex & 0x80)
  887. num += 16;
  888. ept = &ui->ep0out + num;
  889. if (ept->wedged)
  890. goto ack;
  891. if (ctl.bRequest == USB_REQ_SET_FEATURE)
  892. usb_ept_set_halt(&ept->ep, 1);
  893. else
  894. usb_ept_set_halt(&ept->ep, 0);
  895. }
  896. goto ack;
  897. }
  898. }
  899. }
  900. if (ctl.bRequestType == (USB_DIR_OUT | USB_TYPE_STANDARD)) {
  901. if (ctl.bRequest == USB_REQ_SET_CONFIGURATION) {
  902. atomic_set(&ui->configured, !!ctl.wValue);
  903. msm_hsusb_set_state(USB_STATE_CONFIGURED);
  904. } else if (ctl.bRequest == USB_REQ_SET_ADDRESS) {
  905. /*
  906. * Gadget speed should be set when PCI interrupt
  907. * occurs. But sometimes, PCI interrupt is not
  908. * occuring after reset. Hence update the gadget
  909. * speed here.
  910. */
  911. if (ui->gadget.speed == USB_SPEED_UNKNOWN) {
  912. dev_info(&ui->pdev->dev,
  913. "PCI intr missed"
  914. "set speed explictly\n");
  915. msm_hsusb_set_speed(ui);
  916. }
  917. msm_hsusb_set_state(USB_STATE_ADDRESS);
  918. /* write address delayed (will take effect
  919. ** after the next IN txn)
  920. */
  921. writel((ctl.wValue << 25) | (1 << 24), USB_DEVICEADDR);
  922. goto ack;
  923. } else if (ctl.bRequest == USB_REQ_SET_FEATURE) {
  924. switch (ctl.wValue) {
  925. case USB_DEVICE_TEST_MODE:
  926. switch (ctl.wIndex) {
  927. case J_TEST:
  928. case K_TEST:
  929. case SE0_NAK_TEST:
  930. case TST_PKT_TEST:
  931. atomic_set(&ui->test_mode, ctl.wIndex);
  932. goto ack;
  933. }
  934. goto stall;
  935. case USB_DEVICE_REMOTE_WAKEUP:
  936. atomic_set(&ui->remote_wakeup, 1);
  937. goto ack;
  938. #ifdef CONFIG_USB_OTG
  939. case USB_DEVICE_B_HNP_ENABLE:
  940. ui->gadget.b_hnp_enable = 1;
  941. goto ack;
  942. case USB_DEVICE_A_HNP_SUPPORT:
  943. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  944. /* B-devices compliant to OTG spec
  945. * Rev 2.0 are not required to
  946. * suppport these features.
  947. */
  948. goto stall;
  949. #endif
  950. }
  951. } else if ((ctl.bRequest == USB_REQ_CLEAR_FEATURE) &&
  952. (ctl.wValue == USB_DEVICE_REMOTE_WAKEUP)) {
  953. atomic_set(&ui->remote_wakeup, 0);
  954. goto ack;
  955. }
  956. }
  957. /* delegate if we get here */
  958. if (ui->driver) {
  959. ret = ui->driver->setup(&ui->gadget, &ctl);
  960. if (ret >= 0)
  961. return;
  962. }
  963. stall:
  964. /* stall ep0 on error */
  965. ep0_setup_stall(ui);
  966. return;
  967. ack:
  968. ep0_setup_ack(ui);
  969. }
  970. static void handle_endpoint(struct usb_info *ui, unsigned bit)
  971. {
  972. struct msm_endpoint *ept = ui->ept + bit;
  973. struct msm_request *req;
  974. unsigned long flags;
  975. int req_dequeue = 1;
  976. int dtd_update_fail_count_chk = 10;
  977. int check_bit = 0;
  978. unsigned info;
  979. /*
  980. INFO("handle_endpoint() %d %s req=%pK(%08x)\n",
  981. ept->num, (ept->flags & EPT_FLAG_IN) ? "in" : "out",
  982. ept->req, ept->req ? ept->req->item_dma : 0);
  983. */
  984. /* expire all requests that are no longer active */
  985. spin_lock_irqsave(&ui->lock, flags);
  986. while ((req = ept->req)) {
  987. /* if we've processed all live requests, time to
  988. * restart the hardware on the next non-live request
  989. */
  990. if (!req->live) {
  991. usb_ept_start(ept);
  992. break;
  993. }
  994. dequeue:
  995. /* clean speculative fetches on req->item->info */
  996. dma_coherent_post_ops();
  997. info = req->item->info;
  998. /* if the transaction is still in-flight, stop here */
  999. if (info & INFO_ACTIVE) {
  1000. if (req_dequeue) {
  1001. ui->dTD_update_fail_count++;
  1002. ept->dTD_update_fail_count++;
  1003. udelay(1);
  1004. if (!dtd_update_fail_count_chk--) {
  1005. req_dequeue = 0;
  1006. check_bit = 1;
  1007. }
  1008. goto dequeue;
  1009. } else {
  1010. if (check_bit) {
  1011. pr_debug("%s: Delay Workaround Failed\n",
  1012. __func__);
  1013. check_bit = 0;
  1014. ui->dTD_workaround_fail_count++;
  1015. ept->dTD_workaround_fail_count++;
  1016. }
  1017. break;
  1018. }
  1019. }
  1020. req_dequeue = 0;
  1021. del_timer(&ept->prime_timer);
  1022. /* advance ept queue to the next request */
  1023. ept->req = req->next;
  1024. if (ept->req == 0)
  1025. ept->last = 0;
  1026. dma_unmap_single(NULL, req->dma, req->req.length,
  1027. (ept->flags & EPT_FLAG_IN) ?
  1028. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1029. if (info & (INFO_HALTED | INFO_BUFFER_ERROR | INFO_TXN_ERROR)) {
  1030. /* XXX pass on more specific error code */
  1031. req->req.status = -EIO;
  1032. req->req.actual = 0;
  1033. dev_err(&ui->pdev->dev,
  1034. "ept %d %s error. info=%08x\n",
  1035. ept->num,
  1036. (ept->flags & EPT_FLAG_IN) ? "in" : "out",
  1037. info);
  1038. } else {
  1039. req->req.status = 0;
  1040. req->req.actual =
  1041. req->req.length - ((info >> 16) & 0x7FFF);
  1042. }
  1043. req->busy = 0;
  1044. req->live = 0;
  1045. if (req->req.complete) {
  1046. spin_unlock_irqrestore(&ui->lock, flags);
  1047. req->req.complete(&ept->ep, &req->req);
  1048. spin_lock_irqsave(&ui->lock, flags);
  1049. }
  1050. }
  1051. spin_unlock_irqrestore(&ui->lock, flags);
  1052. }
  1053. static void flush_endpoint_hw(struct usb_info *ui, unsigned bits)
  1054. {
  1055. /* flush endpoint, canceling transactions
  1056. ** - this can take a "large amount of time" (per databook)
  1057. ** - the flush can fail in some cases, thus we check STAT
  1058. ** and repeat if we're still operating
  1059. ** (does the fact that this doesn't use the tripwire matter?!)
  1060. */
  1061. do {
  1062. writel(bits, USB_ENDPTFLUSH);
  1063. while (readl(USB_ENDPTFLUSH) & bits)
  1064. udelay(100);
  1065. } while (readl(USB_ENDPTSTAT) & bits);
  1066. }
  1067. static void flush_endpoint_sw(struct msm_endpoint *ept)
  1068. {
  1069. struct usb_info *ui = ept->ui;
  1070. struct msm_request *req, *next_req = NULL;
  1071. unsigned long flags;
  1072. if (!ept->req)
  1073. return;
  1074. /* inactive endpoints have nothing to do here */
  1075. if (ept->ep.maxpacket == 0)
  1076. return;
  1077. /* put the queue head in a sane state */
  1078. ept->head->info = 0;
  1079. ept->head->next = TERMINATE;
  1080. /* cancel any pending requests */
  1081. spin_lock_irqsave(&ui->lock, flags);
  1082. req = ept->req;
  1083. ept->req = 0;
  1084. ept->last = 0;
  1085. while (req != 0) {
  1086. req->busy = 0;
  1087. req->live = 0;
  1088. req->req.status = -ESHUTDOWN;
  1089. req->req.actual = 0;
  1090. /* Gadget driver may free the request in completion
  1091. * handler. So keep a copy of next req pointer
  1092. * before calling completion handler.
  1093. */
  1094. next_req = req->next;
  1095. if (req->req.complete) {
  1096. spin_unlock_irqrestore(&ui->lock, flags);
  1097. req->req.complete(&ept->ep, &req->req);
  1098. spin_lock_irqsave(&ui->lock, flags);
  1099. }
  1100. req = next_req;
  1101. }
  1102. spin_unlock_irqrestore(&ui->lock, flags);
  1103. }
  1104. static void flush_endpoint(struct msm_endpoint *ept)
  1105. {
  1106. del_timer(&ept->prime_timer);
  1107. flush_endpoint_hw(ept->ui, (1 << ept->bit));
  1108. flush_endpoint_sw(ept);
  1109. }
  1110. static irqreturn_t usb_interrupt(int irq, void *data)
  1111. {
  1112. struct usb_info *ui = data;
  1113. struct msm_otg *dev = to_msm_otg(ui->xceiv);
  1114. unsigned n;
  1115. unsigned long flags;
  1116. if (atomic_read(&dev->in_lpm))
  1117. return IRQ_NONE;
  1118. n = readl(USB_USBSTS);
  1119. writel(n, USB_USBSTS);
  1120. /* somehow we got an IRQ while in the reset sequence: ignore it */
  1121. if (!atomic_read(&ui->running))
  1122. return IRQ_HANDLED;
  1123. if (n & STS_PCI) {
  1124. msm_hsusb_set_speed(ui);
  1125. if (atomic_read(&ui->configured)) {
  1126. wake_lock(&ui->wlock);
  1127. spin_lock_irqsave(&ui->lock, flags);
  1128. ui->usb_state = USB_STATE_CONFIGURED;
  1129. ui->flags = USB_FLAG_CONFIGURED;
  1130. spin_unlock_irqrestore(&ui->lock, flags);
  1131. ui->driver->resume(&ui->gadget);
  1132. schedule_work(&ui->work);
  1133. } else {
  1134. msm_hsusb_set_state(USB_STATE_DEFAULT);
  1135. }
  1136. #ifdef CONFIG_USB_OTG
  1137. /* notify otg to clear A_BIDL_ADIS timer */
  1138. if (ui->gadget.is_a_peripheral)
  1139. usb_phy_set_suspend(ui->xceiv, 0);
  1140. #endif
  1141. }
  1142. if (n & STS_URI) {
  1143. dev_dbg(&ui->pdev->dev, "reset\n");
  1144. spin_lock_irqsave(&ui->lock, flags);
  1145. ui->gadget.speed = USB_SPEED_UNKNOWN;
  1146. spin_unlock_irqrestore(&ui->lock, flags);
  1147. #ifdef CONFIG_USB_OTG
  1148. /* notify otg to clear A_BIDL_ADIS timer */
  1149. if (ui->gadget.is_a_peripheral)
  1150. usb_phy_set_suspend(ui->xceiv, 0);
  1151. spin_lock_irqsave(&ui->lock, flags);
  1152. /* Host request is persistent across reset */
  1153. ui->gadget.b_hnp_enable = 0;
  1154. ui->hnp_avail = 0;
  1155. spin_unlock_irqrestore(&ui->lock, flags);
  1156. #endif
  1157. msm_hsusb_set_state(USB_STATE_DEFAULT);
  1158. atomic_set(&ui->remote_wakeup, 0);
  1159. if (!ui->gadget.is_a_peripheral)
  1160. schedule_delayed_work(&ui->chg_stop, 0);
  1161. writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
  1162. writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
  1163. writel(0xffffffff, USB_ENDPTFLUSH);
  1164. writel(0, USB_ENDPTCTRL(1));
  1165. wake_lock(&ui->wlock);
  1166. if (atomic_read(&ui->configured)) {
  1167. /* marking us offline will cause ept queue attempts
  1168. ** to fail
  1169. */
  1170. atomic_set(&ui->configured, 0);
  1171. /* Defer sending offline uevent to userspace */
  1172. atomic_set(&ui->offline_pending, 1);
  1173. /* XXX: we can't seem to detect going offline,
  1174. * XXX: so deconfigure on reset for the time being
  1175. */
  1176. dev_dbg(&ui->pdev->dev,
  1177. "usb: notify offline\n");
  1178. ui->driver->disconnect(&ui->gadget);
  1179. /* cancel pending ep0 transactions */
  1180. flush_endpoint(&ui->ep0out);
  1181. flush_endpoint(&ui->ep0in);
  1182. }
  1183. /* Start phy stuck timer */
  1184. if (ui->pdata && ui->pdata->is_phy_status_timer_on)
  1185. mod_timer(&phy_status_timer, PHY_STATUS_CHECK_DELAY);
  1186. }
  1187. if (n & STS_SLI) {
  1188. dev_dbg(&ui->pdev->dev, "suspend\n");
  1189. spin_lock_irqsave(&ui->lock, flags);
  1190. ui->usb_state = USB_STATE_SUSPENDED;
  1191. ui->flags = USB_FLAG_SUSPEND;
  1192. spin_unlock_irqrestore(&ui->lock, flags);
  1193. ui->driver->suspend(&ui->gadget);
  1194. schedule_work(&ui->work);
  1195. #ifdef CONFIG_USB_OTG
  1196. /* notify otg for
  1197. * 1. kicking A_BIDL_ADIS timer in case of A-peripheral
  1198. * 2. disabling pull-up and kicking B_ASE0_RST timer
  1199. */
  1200. if (ui->gadget.b_hnp_enable || ui->gadget.is_a_peripheral)
  1201. usb_phy_set_suspend(ui->xceiv, 1);
  1202. #endif
  1203. }
  1204. if (n & STS_UI) {
  1205. n = readl(USB_ENDPTSETUPSTAT);
  1206. if (n & EPT_RX(0))
  1207. handle_setup(ui);
  1208. n = readl(USB_ENDPTCOMPLETE);
  1209. writel(n, USB_ENDPTCOMPLETE);
  1210. while (n) {
  1211. unsigned bit = __ffs(n);
  1212. handle_endpoint(ui, bit);
  1213. n = n & (~(1 << bit));
  1214. }
  1215. }
  1216. return IRQ_HANDLED;
  1217. }
  1218. static void usb_prepare(struct usb_info *ui)
  1219. {
  1220. spin_lock_init(&ui->lock);
  1221. memset(ui->buf, 0, 4096);
  1222. ui->head = (void *) (ui->buf + 0);
  1223. /* only important for reset/reinit */
  1224. memset(ui->ept, 0, sizeof(ui->ept));
  1225. ui->next_item = 0;
  1226. ui->next_ifc_num = 0;
  1227. init_endpoints(ui);
  1228. ui->ep0in.ep.maxpacket = 64;
  1229. ui->ep0out.ep.maxpacket = 64;
  1230. ui->setup_req =
  1231. usb_ept_alloc_req(&ui->ep0in, SETUP_BUF_SIZE, GFP_KERNEL);
  1232. INIT_WORK(&ui->work, usb_do_work);
  1233. INIT_DELAYED_WORK(&ui->chg_det, usb_chg_detect);
  1234. INIT_DELAYED_WORK(&ui->chg_stop, usb_chg_stop);
  1235. INIT_DELAYED_WORK(&ui->rw_work, usb_do_remote_wakeup);
  1236. if (ui->pdata && ui->pdata->is_phy_status_timer_on)
  1237. INIT_WORK(&ui->phy_status_check, usb_phy_stuck_recover);
  1238. }
  1239. static void usb_reset(struct usb_info *ui)
  1240. {
  1241. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  1242. dev_dbg(&ui->pdev->dev, "reset controller\n");
  1243. atomic_set(&ui->running, 0);
  1244. /*
  1245. * PHY reset takes minimum 100 msec. Hence reset only link
  1246. * during HNP. Reset PHY and link in B-peripheral mode.
  1247. */
  1248. if (ui->gadget.is_a_peripheral)
  1249. otg->reset(ui->xceiv, 0);
  1250. else
  1251. otg->reset(ui->xceiv, 1);
  1252. /* set usb controller interrupt threshold to zero*/
  1253. writel((readl(USB_USBCMD) & ~USBCMD_ITC_MASK) | USBCMD_ITC(0),
  1254. USB_USBCMD);
  1255. writel(ui->dma, USB_ENDPOINTLISTADDR);
  1256. configure_endpoints(ui);
  1257. /* marking us offline will cause ept queue attempts to fail */
  1258. atomic_set(&ui->configured, 0);
  1259. if (ui->driver) {
  1260. dev_dbg(&ui->pdev->dev, "usb: notify offline\n");
  1261. ui->driver->disconnect(&ui->gadget);
  1262. }
  1263. /* cancel pending ep0 transactions */
  1264. flush_endpoint(&ui->ep0out);
  1265. flush_endpoint(&ui->ep0in);
  1266. /* enable interrupts */
  1267. writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR);
  1268. /* Ensure that h/w RESET is completed before returning */
  1269. mb();
  1270. atomic_set(&ui->running, 1);
  1271. }
  1272. static void usb_start(struct usb_info *ui)
  1273. {
  1274. unsigned long flags;
  1275. spin_lock_irqsave(&ui->lock, flags);
  1276. ui->flags |= USB_FLAG_START;
  1277. schedule_work(&ui->work);
  1278. spin_unlock_irqrestore(&ui->lock, flags);
  1279. }
  1280. static int usb_free(struct usb_info *ui, int ret)
  1281. {
  1282. if (ret)
  1283. dev_dbg(&ui->pdev->dev, "usb_free(%d)\n", ret);
  1284. usb_del_gadget_udc(&ui->gadget);
  1285. if (ui->xceiv)
  1286. usb_put_transceiver(ui->xceiv);
  1287. if (ui->irq)
  1288. free_irq(ui->irq, 0);
  1289. if (ui->pool)
  1290. dma_pool_destroy(ui->pool);
  1291. if (ui->dma)
  1292. dma_free_coherent(&ui->pdev->dev, 4096, ui->buf, ui->dma);
  1293. kfree(ui);
  1294. return ret;
  1295. }
  1296. static void usb_do_work_check_vbus(struct usb_info *ui)
  1297. {
  1298. unsigned long iflags;
  1299. spin_lock_irqsave(&ui->lock, iflags);
  1300. if (is_usb_online(ui))
  1301. ui->flags |= USB_FLAG_VBUS_ONLINE;
  1302. else
  1303. ui->flags |= USB_FLAG_VBUS_OFFLINE;
  1304. spin_unlock_irqrestore(&ui->lock, iflags);
  1305. }
  1306. static void usb_do_work(struct work_struct *w)
  1307. {
  1308. struct usb_info *ui = container_of(w, struct usb_info, work);
  1309. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  1310. unsigned long iflags;
  1311. unsigned flags, _vbus;
  1312. for (;;) {
  1313. spin_lock_irqsave(&ui->lock, iflags);
  1314. flags = ui->flags;
  1315. ui->flags = 0;
  1316. _vbus = is_usb_online(ui);
  1317. spin_unlock_irqrestore(&ui->lock, iflags);
  1318. /* give up if we have nothing to do */
  1319. if (flags == 0)
  1320. break;
  1321. switch (ui->state) {
  1322. case USB_STATE_IDLE:
  1323. if (flags & USB_FLAG_START) {
  1324. int ret;
  1325. if (!_vbus) {
  1326. ui->state = USB_STATE_OFFLINE;
  1327. break;
  1328. }
  1329. pm_runtime_get_noresume(&ui->pdev->dev);
  1330. pm_runtime_resume(&ui->pdev->dev);
  1331. dev_dbg(&ui->pdev->dev,
  1332. "msm72k_udc: IDLE -> ONLINE\n");
  1333. usb_reset(ui);
  1334. ret = request_irq(otg->irq, usb_interrupt,
  1335. IRQF_SHARED,
  1336. ui->pdev->name, ui);
  1337. /* FIXME: should we call BUG_ON when
  1338. * requst irq fails
  1339. */
  1340. if (ret) {
  1341. dev_err(&ui->pdev->dev,
  1342. "hsusb: peripheral: request irq"
  1343. " failed:(%d)", ret);
  1344. break;
  1345. }
  1346. ui->irq = otg->irq;
  1347. ui->state = USB_STATE_ONLINE;
  1348. usb_do_work_check_vbus(ui);
  1349. if (!atomic_read(&ui->softconnect))
  1350. break;
  1351. msm72k_pullup_internal(&ui->gadget, 1);
  1352. if (!ui->gadget.is_a_peripheral)
  1353. schedule_delayed_work(
  1354. &ui->chg_det,
  1355. USB_CHG_DET_DELAY);
  1356. }
  1357. break;
  1358. case USB_STATE_ONLINE:
  1359. if (atomic_read(&ui->offline_pending)) {
  1360. switch_set_state(&ui->sdev, 0);
  1361. atomic_set(&ui->offline_pending, 0);
  1362. }
  1363. /* If at any point when we were online, we received
  1364. * the signal to go offline, we must honor it
  1365. */
  1366. if (flags & USB_FLAG_VBUS_OFFLINE) {
  1367. ui->chg_current = 0;
  1368. /* wait incase chg_detect is running */
  1369. if (!ui->gadget.is_a_peripheral)
  1370. cancel_delayed_work_sync(&ui->chg_det);
  1371. dev_dbg(&ui->pdev->dev,
  1372. "msm72k_udc: ONLINE -> OFFLINE\n");
  1373. atomic_set(&ui->running, 0);
  1374. atomic_set(&ui->remote_wakeup, 0);
  1375. atomic_set(&ui->configured, 0);
  1376. if (ui->driver) {
  1377. dev_dbg(&ui->pdev->dev,
  1378. "usb: notify offline\n");
  1379. ui->driver->disconnect(&ui->gadget);
  1380. }
  1381. /* cancel pending ep0 transactions */
  1382. flush_endpoint(&ui->ep0out);
  1383. flush_endpoint(&ui->ep0in);
  1384. /* synchronize with irq context */
  1385. spin_lock_irqsave(&ui->lock, iflags);
  1386. #ifdef CONFIG_USB_OTG
  1387. ui->gadget.host_request = 0;
  1388. ui->gadget.b_hnp_enable = 0;
  1389. ui->hnp_avail = 0;
  1390. #endif
  1391. msm72k_pullup_internal(&ui->gadget, 0);
  1392. spin_unlock_irqrestore(&ui->lock, iflags);
  1393. /* if charger is initialized to known type
  1394. * we must let modem know about charger
  1395. * disconnection
  1396. */
  1397. usb_phy_set_power(ui->xceiv, 0);
  1398. if (ui->irq) {
  1399. /* Disable and acknowledge all
  1400. * USB interrupts before freeing
  1401. * irq, so that no USB spurious
  1402. * interrupt occurs during USB cable
  1403. * disconnect which may lead to
  1404. * IRQ nobody cared error.
  1405. */
  1406. writel_relaxed(0, USB_USBINTR);
  1407. writel_relaxed(readl_relaxed(USB_USBSTS)
  1408. , USB_USBSTS);
  1409. /* Ensure that above STOREs are
  1410. * completed before enabling
  1411. * interrupts */
  1412. wmb();
  1413. free_irq(ui->irq, ui);
  1414. ui->irq = 0;
  1415. }
  1416. switch_set_state(&ui->sdev, 0);
  1417. ui->state = USB_STATE_OFFLINE;
  1418. usb_do_work_check_vbus(ui);
  1419. pm_runtime_put_noidle(&ui->pdev->dev);
  1420. pm_runtime_suspend(&ui->pdev->dev);
  1421. wake_unlock(&ui->wlock);
  1422. break;
  1423. }
  1424. if (flags & USB_FLAG_SUSPEND) {
  1425. int maxpower = usb_get_max_power(ui);
  1426. if (maxpower < 0)
  1427. break;
  1428. usb_phy_set_power(ui->xceiv, 0);
  1429. /* To support TCXO during bus suspend
  1430. * This might be dummy check since bus suspend
  1431. * is not implemented as of now
  1432. * */
  1433. if (release_wlocks)
  1434. wake_unlock(&ui->wlock);
  1435. /* TBD: Initiate LPM at usb bus suspend */
  1436. break;
  1437. }
  1438. if (flags & USB_FLAG_CONFIGURED) {
  1439. int maxpower = usb_get_max_power(ui);
  1440. /* We may come here even when no configuration
  1441. * is selected. Send online/offline event
  1442. * accordingly.
  1443. */
  1444. switch_set_state(&ui->sdev,
  1445. atomic_read(&ui->configured));
  1446. if (maxpower < 0)
  1447. break;
  1448. ui->chg_current = maxpower;
  1449. usb_phy_set_power(ui->xceiv, maxpower);
  1450. break;
  1451. }
  1452. if (flags & USB_FLAG_RESET) {
  1453. dev_dbg(&ui->pdev->dev,
  1454. "msm72k_udc: ONLINE -> RESET\n");
  1455. msm72k_pullup_internal(&ui->gadget, 0);
  1456. usb_reset(ui);
  1457. msm72k_pullup_internal(&ui->gadget, 1);
  1458. dev_dbg(&ui->pdev->dev,
  1459. "msm72k_udc: RESET -> ONLINE\n");
  1460. break;
  1461. }
  1462. break;
  1463. case USB_STATE_OFFLINE:
  1464. /* If we were signaled to go online and vbus is still
  1465. * present when we received the signal, go online.
  1466. */
  1467. if ((flags & USB_FLAG_VBUS_ONLINE) && _vbus) {
  1468. int ret;
  1469. pm_runtime_get_noresume(&ui->pdev->dev);
  1470. pm_runtime_resume(&ui->pdev->dev);
  1471. dev_dbg(&ui->pdev->dev,
  1472. "msm72k_udc: OFFLINE -> ONLINE\n");
  1473. usb_reset(ui);
  1474. ui->state = USB_STATE_ONLINE;
  1475. usb_do_work_check_vbus(ui);
  1476. ret = request_irq(otg->irq, usb_interrupt,
  1477. IRQF_SHARED,
  1478. ui->pdev->name, ui);
  1479. /* FIXME: should we call BUG_ON when
  1480. * requst irq fails
  1481. */
  1482. if (ret) {
  1483. dev_err(&ui->pdev->dev,
  1484. "hsusb: peripheral: request irq"
  1485. " failed:(%d)", ret);
  1486. break;
  1487. }
  1488. ui->irq = otg->irq;
  1489. enable_irq_wake(otg->irq);
  1490. if (!atomic_read(&ui->softconnect))
  1491. break;
  1492. msm72k_pullup_internal(&ui->gadget, 1);
  1493. if (!ui->gadget.is_a_peripheral)
  1494. schedule_delayed_work(
  1495. &ui->chg_det,
  1496. USB_CHG_DET_DELAY);
  1497. }
  1498. break;
  1499. }
  1500. }
  1501. }
  1502. /* FIXME - the callers of this function should use a gadget API instead.
  1503. * This is called from htc_battery.c and board-halibut.c
  1504. * WARNING - this can get called before this driver is initialized.
  1505. */
  1506. void msm_hsusb_set_vbus_state(int online)
  1507. {
  1508. unsigned long flags;
  1509. struct usb_info *ui = the_usb_info;
  1510. if (!ui) {
  1511. pr_err("%s called before driver initialized\n", __func__);
  1512. return;
  1513. }
  1514. spin_lock_irqsave(&ui->lock, flags);
  1515. if (is_usb_online(ui) == online)
  1516. goto out;
  1517. if (online) {
  1518. ui->usb_state = USB_STATE_POWERED;
  1519. ui->flags |= USB_FLAG_VBUS_ONLINE;
  1520. } else {
  1521. ui->gadget.speed = USB_SPEED_UNKNOWN;
  1522. ui->usb_state = USB_STATE_NOTATTACHED;
  1523. ui->flags |= USB_FLAG_VBUS_OFFLINE;
  1524. ui->chg_type_retry_cnt = 0;
  1525. ui->proprietary_chg = false;
  1526. }
  1527. if (in_interrupt()) {
  1528. schedule_work(&ui->work);
  1529. } else {
  1530. spin_unlock_irqrestore(&ui->lock, flags);
  1531. usb_do_work(&ui->work);
  1532. return;
  1533. }
  1534. out:
  1535. spin_unlock_irqrestore(&ui->lock, flags);
  1536. }
  1537. #if defined(CONFIG_DEBUG_FS)
  1538. void usb_function_reenumerate(void)
  1539. {
  1540. struct usb_info *ui = the_usb_info;
  1541. /* disable and re-enable the D+ pullup */
  1542. dev_dbg(&ui->pdev->dev, "disable pullup\n");
  1543. writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
  1544. msleep(10);
  1545. dev_dbg(&ui->pdev->dev, "enable pullup\n");
  1546. writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
  1547. }
  1548. static char debug_buffer[PAGE_SIZE];
  1549. static ssize_t debug_read_status(struct file *file, char __user *ubuf,
  1550. size_t count, loff_t *ppos)
  1551. {
  1552. struct usb_info *ui = file->private_data;
  1553. char *buf = debug_buffer;
  1554. unsigned long flags;
  1555. struct msm_endpoint *ept;
  1556. struct msm_request *req;
  1557. int n;
  1558. int i = 0;
  1559. spin_lock_irqsave(&ui->lock, flags);
  1560. i += scnprintf(buf + i, PAGE_SIZE - i,
  1561. "regs: setup=%08x prime=%08x stat=%08x done=%08x\n",
  1562. readl(USB_ENDPTSETUPSTAT),
  1563. readl(USB_ENDPTPRIME),
  1564. readl(USB_ENDPTSTAT),
  1565. readl(USB_ENDPTCOMPLETE));
  1566. i += scnprintf(buf + i, PAGE_SIZE - i,
  1567. "regs: cmd=%08x sts=%08x intr=%08x port=%08x\n\n",
  1568. readl(USB_USBCMD),
  1569. readl(USB_USBSTS),
  1570. readl(USB_USBINTR),
  1571. readl(USB_PORTSC));
  1572. for (n = 0; n < 32; n++) {
  1573. ept = ui->ept + n;
  1574. if (ept->ep.maxpacket == 0)
  1575. continue;
  1576. i += scnprintf(buf + i, PAGE_SIZE - i,
  1577. "ept%d %s cfg=%08x active=%08x next=%08x info=%08x\n",
  1578. ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
  1579. ept->head->config, ept->head->active,
  1580. ept->head->next, ept->head->info);
  1581. for (req = ept->req; req; req = req->next)
  1582. i += scnprintf(buf + i, PAGE_SIZE - i,
  1583. " req @%08x next=%08x info=%08x page0=%08x %c %c\n",
  1584. req->item_dma, req->item->next,
  1585. req->item->info, req->item->page0,
  1586. req->busy ? 'B' : ' ',
  1587. req->live ? 'L' : ' ');
  1588. }
  1589. i += scnprintf(buf + i, PAGE_SIZE - i,
  1590. "phy failure count: %d\n", ui->phy_fail_count);
  1591. spin_unlock_irqrestore(&ui->lock, flags);
  1592. return simple_read_from_buffer(ubuf, count, ppos, buf, i);
  1593. }
  1594. static ssize_t debug_write_reset(struct file *file, const char __user *buf,
  1595. size_t count, loff_t *ppos)
  1596. {
  1597. struct usb_info *ui = file->private_data;
  1598. unsigned long flags;
  1599. spin_lock_irqsave(&ui->lock, flags);
  1600. ui->flags |= USB_FLAG_RESET;
  1601. schedule_work(&ui->work);
  1602. spin_unlock_irqrestore(&ui->lock, flags);
  1603. return count;
  1604. }
  1605. static ssize_t debug_write_cycle(struct file *file, const char __user *buf,
  1606. size_t count, loff_t *ppos)
  1607. {
  1608. usb_function_reenumerate();
  1609. return count;
  1610. }
  1611. static int debug_open(struct inode *inode, struct file *file)
  1612. {
  1613. file->private_data = inode->i_private;
  1614. return 0;
  1615. }
  1616. const struct file_operations debug_stat_ops = {
  1617. .open = debug_open,
  1618. .read = debug_read_status,
  1619. };
  1620. const struct file_operations debug_reset_ops = {
  1621. .open = debug_open,
  1622. .write = debug_write_reset,
  1623. };
  1624. const struct file_operations debug_cycle_ops = {
  1625. .open = debug_open,
  1626. .write = debug_write_cycle,
  1627. };
  1628. static ssize_t debug_read_release_wlocks(struct file *file, char __user *ubuf,
  1629. size_t count, loff_t *ppos)
  1630. {
  1631. char kbuf[10];
  1632. size_t c = 0;
  1633. memset(kbuf, 0, 10);
  1634. c = scnprintf(kbuf, 10, "%d", release_wlocks);
  1635. if (copy_to_user(ubuf, kbuf, c))
  1636. return -EFAULT;
  1637. return c;
  1638. }
  1639. static ssize_t debug_write_release_wlocks(struct file *file,
  1640. const char __user *buf, size_t count, loff_t *ppos)
  1641. {
  1642. char kbuf[10];
  1643. long temp;
  1644. memset(kbuf, 0, 10);
  1645. if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
  1646. return -EFAULT;
  1647. if (strict_strtol(kbuf, 10, &temp))
  1648. return -EINVAL;
  1649. if (temp)
  1650. release_wlocks = 1;
  1651. else
  1652. release_wlocks = 0;
  1653. return count;
  1654. }
  1655. static int debug_wake_lock_open(struct inode *inode, struct file *file)
  1656. {
  1657. file->private_data = inode->i_private;
  1658. return 0;
  1659. }
  1660. const struct file_operations debug_wlocks_ops = {
  1661. .open = debug_wake_lock_open,
  1662. .read = debug_read_release_wlocks,
  1663. .write = debug_write_release_wlocks,
  1664. };
  1665. static ssize_t debug_reprime_ep(struct file *file, const char __user *ubuf,
  1666. size_t count, loff_t *ppos)
  1667. {
  1668. struct usb_info *ui = file->private_data;
  1669. struct msm_endpoint *ept;
  1670. char kbuf[10];
  1671. unsigned int ep_num, dir;
  1672. unsigned long flags;
  1673. unsigned n, i;
  1674. memset(kbuf, 0, 10);
  1675. if (copy_from_user(kbuf, ubuf, count > 10 ? 10 : count))
  1676. return -EFAULT;
  1677. if (sscanf(kbuf, "%u %u", &ep_num, &dir) != 2)
  1678. return -EINVAL;
  1679. if (dir)
  1680. i = ep_num + 16;
  1681. else
  1682. i = ep_num;
  1683. spin_lock_irqsave(&ui->lock, flags);
  1684. ept = ui->ept + i;
  1685. n = 1 << ept->bit;
  1686. if ((readl_relaxed(USB_ENDPTPRIME) & n))
  1687. goto out;
  1688. if (readl_relaxed(USB_ENDPTSTAT) & n)
  1689. goto out;
  1690. /* clear speculative loads on item->info */
  1691. rmb();
  1692. if (ept->req && (ept->req->item->info & INFO_ACTIVE)) {
  1693. pr_err("%s(): ept%d%s prime failed. ept: config: %x"
  1694. "active: %x next: %x info: %x\n",
  1695. __func__, ept->num,
  1696. ept->flags & EPT_FLAG_IN ? "in" : "out",
  1697. ept->head->config, ept->head->active,
  1698. ept->head->next, ept->head->info);
  1699. writel_relaxed(n, USB_ENDPTPRIME);
  1700. }
  1701. out:
  1702. spin_unlock_irqrestore(&ui->lock, flags);
  1703. return count;
  1704. }
  1705. static char buffer[512];
  1706. static ssize_t debug_prime_fail_read(struct file *file, char __user *ubuf,
  1707. size_t count, loff_t *ppos)
  1708. {
  1709. struct usb_info *ui = file->private_data;
  1710. char *buf = buffer;
  1711. unsigned long flags;
  1712. struct msm_endpoint *ept;
  1713. int n;
  1714. int i = 0;
  1715. spin_lock_irqsave(&ui->lock, flags);
  1716. for (n = 0; n < 32; n++) {
  1717. ept = ui->ept + n;
  1718. if (ept->ep.maxpacket == 0)
  1719. continue;
  1720. i += scnprintf(buf + i, PAGE_SIZE - i,
  1721. "ept%d %s false_prime_count=%lu prime_fail_count=%d "
  1722. "dtd_fail_count=%lu "
  1723. "dTD_workaround_fail_count=%lu\n",
  1724. ept->num, (ept->flags & EPT_FLAG_IN) ? "in " : "out",
  1725. ept->false_prime_fail_count,
  1726. ept->actual_prime_fail_count,
  1727. ept->dTD_update_fail_count,
  1728. ept->dTD_workaround_fail_count);
  1729. }
  1730. i += scnprintf(buf + i, PAGE_SIZE - i,
  1731. "dTD_update_fail count: %lu\n",
  1732. ui->dTD_update_fail_count);
  1733. i += scnprintf(buf + i, PAGE_SIZE - i,
  1734. "prime_fail count: %d\n", ui->prime_fail_count);
  1735. i += scnprintf(buf + i, PAGE_SIZE - i,
  1736. "dtd_workaround_fail count: %lu\n",
  1737. ui->dTD_workaround_fail_count);
  1738. spin_unlock_irqrestore(&ui->lock, flags);
  1739. return simple_read_from_buffer(ubuf, count, ppos, buf, i);
  1740. }
  1741. static int debug_prime_fail_open(struct inode *inode, struct file *file)
  1742. {
  1743. file->private_data = inode->i_private;
  1744. return 0;
  1745. }
  1746. const struct file_operations prime_fail_ops = {
  1747. .open = debug_prime_fail_open,
  1748. .read = debug_prime_fail_read,
  1749. .write = debug_reprime_ep,
  1750. };
  1751. static ssize_t debug_prop_chg_write(struct file *file,
  1752. const char __user *buf, size_t count, loff_t *ppos)
  1753. {
  1754. struct usb_info *ui = file->private_data;
  1755. char kbuf[2];
  1756. memset(kbuf, 0, sizeof(kbuf));
  1757. if (copy_from_user(kbuf, buf, sizeof(kbuf)))
  1758. return -EFAULT;
  1759. if (!strncmp(kbuf, "1", 1))
  1760. ui->pdata->prop_chg = 1;
  1761. else
  1762. ui->pdata->prop_chg = 0;
  1763. return count;
  1764. }
  1765. static ssize_t debug_prop_chg_read(struct file *file, char __user *ubuf,
  1766. size_t count, loff_t *ppos)
  1767. {
  1768. struct usb_info *ui = file->private_data;
  1769. char kbuf[2];
  1770. size_t c = 0;
  1771. memset(kbuf, 0, sizeof(kbuf));
  1772. c = scnprintf(kbuf, sizeof(kbuf), "%d\n", ui->pdata->prop_chg);
  1773. if (copy_to_user(ubuf, kbuf, c))
  1774. return -EFAULT;
  1775. return simple_read_from_buffer(ubuf, count, ppos, kbuf, c);
  1776. }
  1777. static int debug_prop_chg_open(struct inode *inode, struct file *file)
  1778. {
  1779. file->private_data = inode->i_private;
  1780. return 0;
  1781. }
  1782. const struct file_operations debug_prop_chg_ops = {
  1783. .open = debug_prop_chg_open,
  1784. .read = debug_prop_chg_read,
  1785. .write = debug_prop_chg_write,
  1786. };
  1787. static void usb_debugfs_init(struct usb_info *ui)
  1788. {
  1789. struct dentry *dent;
  1790. dent = debugfs_create_dir(dev_name(&ui->pdev->dev), 0);
  1791. if (IS_ERR(dent))
  1792. return;
  1793. debugfs_create_file("status", 0444, dent, ui, &debug_stat_ops);
  1794. debugfs_create_file("reset", 0222, dent, ui, &debug_reset_ops);
  1795. debugfs_create_file("cycle", 0222, dent, ui, &debug_cycle_ops);
  1796. debugfs_create_file("release_wlocks", 0666, dent, ui,
  1797. &debug_wlocks_ops);
  1798. debugfs_create_file("prime_fail_countt", 0666, dent, ui,
  1799. &prime_fail_ops);
  1800. debugfs_create_file("proprietary_chg", 0666, dent, ui,
  1801. &debug_prop_chg_ops);
  1802. }
  1803. #else
  1804. static void usb_debugfs_init(struct usb_info *ui) {}
  1805. #endif
  1806. static int
  1807. msm72k_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  1808. {
  1809. struct msm_endpoint *ept;
  1810. unsigned char ep_type;
  1811. if (_ep == NULL || desc == NULL)
  1812. return -EINVAL;
  1813. ept = to_msm_endpoint(_ep);
  1814. ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  1815. _ep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
  1816. config_ept(ept);
  1817. ept->wedged = 0;
  1818. usb_ept_enable(ept, 1, ep_type);
  1819. return 0;
  1820. }
  1821. static int msm72k_disable(struct usb_ep *_ep)
  1822. {
  1823. struct msm_endpoint *ept = to_msm_endpoint(_ep);
  1824. usb_ept_enable(ept, 0, 0);
  1825. flush_endpoint(ept);
  1826. /*
  1827. * Clear descriptors here. Otherwise previous descriptors
  1828. * will be used by function drivers upon next enumeration.
  1829. */
  1830. _ep->desc = NULL;
  1831. return 0;
  1832. }
  1833. static struct usb_request *
  1834. msm72k_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  1835. {
  1836. return usb_ept_alloc_req(to_msm_endpoint(_ep), 0, gfp_flags);
  1837. }
  1838. static void
  1839. msm72k_free_request(struct usb_ep *_ep, struct usb_request *_req)
  1840. {
  1841. struct msm_request *req = to_msm_request(_req);
  1842. struct msm_endpoint *ept = to_msm_endpoint(_ep);
  1843. struct usb_info *ui = ept->ui;
  1844. /* request should not be busy */
  1845. BUG_ON(req->busy);
  1846. if (req->alloced)
  1847. kfree(req->req.buf);
  1848. dma_pool_free(ui->pool, req->item, req->item_dma);
  1849. kfree(req);
  1850. }
  1851. static int
  1852. msm72k_queue(struct usb_ep *_ep, struct usb_request *req, gfp_t gfp_flags)
  1853. {
  1854. struct msm_endpoint *ep = to_msm_endpoint(_ep);
  1855. struct usb_info *ui = ep->ui;
  1856. if (!atomic_read(&ui->softconnect))
  1857. return -ENODEV;
  1858. if (ep == &ui->ep0in) {
  1859. struct msm_request *r = to_msm_request(req);
  1860. if (!req->length)
  1861. goto ep_queue_done;
  1862. r->gadget_complete = req->complete;
  1863. /* ep0_queue_ack_complete queue a receive for ACK before
  1864. ** calling req->complete
  1865. */
  1866. req->complete = ep0_queue_ack_complete;
  1867. if (atomic_read(&ui->ep0_dir) == USB_DIR_OUT)
  1868. ep = &ui->ep0out;
  1869. goto ep_queue_done;
  1870. }
  1871. ep_queue_done:
  1872. return usb_ept_queue_xfer(ep, req);
  1873. }
  1874. static int msm72k_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1875. {
  1876. struct msm_endpoint *ep = to_msm_endpoint(_ep);
  1877. struct msm_request *req = to_msm_request(_req);
  1878. struct usb_info *ui = ep->ui;
  1879. struct msm_request *temp_req;
  1880. unsigned long flags;
  1881. if (ep->num == 0) {
  1882. /* Flush both out and in control endpoints */
  1883. flush_endpoint(&ui->ep0out);
  1884. flush_endpoint(&ui->ep0in);
  1885. return 0;
  1886. }
  1887. if (!(ui && req && ep->req))
  1888. return -EINVAL;
  1889. spin_lock_irqsave(&ui->lock, flags);
  1890. if (!req->busy) {
  1891. dev_dbg(&ui->pdev->dev, "%s: !req->busy\n", __func__);
  1892. spin_unlock_irqrestore(&ui->lock, flags);
  1893. return -EINVAL;
  1894. }
  1895. del_timer(&ep->prime_timer);
  1896. /* Stop the transfer */
  1897. do {
  1898. writel((1 << ep->bit), USB_ENDPTFLUSH);
  1899. while (readl(USB_ENDPTFLUSH) & (1 << ep->bit))
  1900. udelay(100);
  1901. } while (readl(USB_ENDPTSTAT) & (1 << ep->bit));
  1902. req->req.status = 0;
  1903. req->busy = 0;
  1904. if (ep->req == req) {
  1905. ep->req = req->next;
  1906. ep->head->next = req->item->next;
  1907. } else {
  1908. req->prev->next = req->next;
  1909. if (req->next)
  1910. req->next->prev = req->prev;
  1911. req->prev->item->next = req->item->next;
  1912. }
  1913. if (!req->next)
  1914. ep->last = req->prev;
  1915. /* initialize request to default */
  1916. req->item->next = TERMINATE;
  1917. req->item->info = 0;
  1918. req->live = 0;
  1919. dma_unmap_single(NULL, req->dma, req->req.length,
  1920. (ep->flags & EPT_FLAG_IN) ?
  1921. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1922. if (req->req.complete) {
  1923. req->req.status = -ECONNRESET;
  1924. spin_unlock_irqrestore(&ui->lock, flags);
  1925. req->req.complete(&ep->ep, &req->req);
  1926. spin_lock_irqsave(&ui->lock, flags);
  1927. }
  1928. if (!req->live) {
  1929. /* Reprime the endpoint for the remaining transfers */
  1930. for (temp_req = ep->req ; temp_req ; temp_req = temp_req->next)
  1931. temp_req->live = 0;
  1932. if (ep->req)
  1933. usb_ept_start(ep);
  1934. spin_unlock_irqrestore(&ui->lock, flags);
  1935. return 0;
  1936. }
  1937. spin_unlock_irqrestore(&ui->lock, flags);
  1938. return 0;
  1939. }
  1940. static int
  1941. usb_ept_set_halt(struct usb_ep *_ep, int value)
  1942. {
  1943. struct msm_endpoint *ept = to_msm_endpoint(_ep);
  1944. struct usb_info *ui = ept->ui;
  1945. unsigned int in = ept->flags & EPT_FLAG_IN;
  1946. unsigned int n;
  1947. unsigned long flags;
  1948. spin_lock_irqsave(&ui->lock, flags);
  1949. n = readl(USB_ENDPTCTRL(ept->num));
  1950. if (in) {
  1951. if (value)
  1952. n |= CTRL_TXS;
  1953. else {
  1954. n &= ~CTRL_TXS;
  1955. n |= CTRL_TXR;
  1956. }
  1957. } else {
  1958. if (value)
  1959. n |= CTRL_RXS;
  1960. else {
  1961. n &= ~CTRL_RXS;
  1962. n |= CTRL_RXR;
  1963. }
  1964. }
  1965. writel(n, USB_ENDPTCTRL(ept->num));
  1966. if (!value)
  1967. ept->wedged = 0;
  1968. spin_unlock_irqrestore(&ui->lock, flags);
  1969. return 0;
  1970. }
  1971. static int
  1972. msm72k_set_halt(struct usb_ep *_ep, int value)
  1973. {
  1974. struct msm_endpoint *ept = to_msm_endpoint(_ep);
  1975. unsigned int in = ept->flags & EPT_FLAG_IN;
  1976. if (value && in && ept->req)
  1977. return -EAGAIN;
  1978. usb_ept_set_halt(_ep, value);
  1979. return 0;
  1980. }
  1981. static int
  1982. msm72k_fifo_status(struct usb_ep *_ep)
  1983. {
  1984. return -EOPNOTSUPP;
  1985. }
  1986. static void
  1987. msm72k_fifo_flush(struct usb_ep *_ep)
  1988. {
  1989. flush_endpoint(to_msm_endpoint(_ep));
  1990. }
  1991. static int msm72k_set_wedge(struct usb_ep *_ep)
  1992. {
  1993. struct msm_endpoint *ept = to_msm_endpoint(_ep);
  1994. if (ept->num == 0)
  1995. return -EINVAL;
  1996. ept->wedged = 1;
  1997. return msm72k_set_halt(_ep, 1);
  1998. }
  1999. static const struct usb_ep_ops msm72k_ep_ops = {
  2000. .enable = msm72k_enable,
  2001. .disable = msm72k_disable,
  2002. .alloc_request = msm72k_alloc_request,
  2003. .free_request = msm72k_free_request,
  2004. .queue = msm72k_queue,
  2005. .dequeue = msm72k_dequeue,
  2006. .set_halt = msm72k_set_halt,
  2007. .set_wedge = msm72k_set_wedge,
  2008. .fifo_status = msm72k_fifo_status,
  2009. .fifo_flush = msm72k_fifo_flush,
  2010. };
  2011. static int msm72k_get_frame(struct usb_gadget *_gadget)
  2012. {
  2013. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2014. /* frame number is in bits 13:3 */
  2015. return (readl(USB_FRINDEX) >> 3) & 0x000007FF;
  2016. }
  2017. /* VBUS reporting logically comes from a transceiver */
  2018. static int msm72k_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  2019. {
  2020. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2021. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  2022. if (is_active || atomic_read(&otg->chg_type)
  2023. == USB_CHG_TYPE__WALLCHARGER)
  2024. wake_lock(&ui->wlock);
  2025. msm_hsusb_set_vbus_state(is_active);
  2026. return 0;
  2027. }
  2028. /* SW workarounds
  2029. Issue #1 - USB Spoof Disconnect Failure
  2030. Symptom - Writing 0 to run/stop bit of USBCMD doesn't cause disconnect
  2031. SW workaround - Making opmode non-driving and SuspendM set in function
  2032. register of SMSC phy
  2033. */
  2034. /* drivers may have software control over D+ pullup */
  2035. static int msm72k_pullup_internal(struct usb_gadget *_gadget, int is_active)
  2036. {
  2037. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2038. unsigned long flags;
  2039. if (is_active) {
  2040. spin_lock_irqsave(&ui->lock, flags);
  2041. if (is_usb_online(ui) && ui->driver)
  2042. writel(readl(USB_USBCMD) | USBCMD_RS, USB_USBCMD);
  2043. spin_unlock_irqrestore(&ui->lock, flags);
  2044. } else {
  2045. writel(readl(USB_USBCMD) & ~USBCMD_RS, USB_USBCMD);
  2046. /* S/W workaround, Issue#1 */
  2047. usb_phy_io_write(ui->xceiv, 0x48, 0x04);
  2048. }
  2049. /* Ensure pull-up operation is completed before returning */
  2050. mb();
  2051. return 0;
  2052. }
  2053. static int msm72k_pullup(struct usb_gadget *_gadget, int is_active)
  2054. {
  2055. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2056. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  2057. unsigned long flags;
  2058. atomic_set(&ui->softconnect, is_active);
  2059. spin_lock_irqsave(&ui->lock, flags);
  2060. if (ui->usb_state == USB_STATE_NOTATTACHED || ui->driver == NULL ||
  2061. atomic_read(&otg->chg_type) == USB_CHG_TYPE__WALLCHARGER) {
  2062. spin_unlock_irqrestore(&ui->lock, flags);
  2063. return 0;
  2064. }
  2065. spin_unlock_irqrestore(&ui->lock, flags);
  2066. msm72k_pullup_internal(_gadget, is_active);
  2067. if (is_active && !ui->gadget.is_a_peripheral)
  2068. schedule_delayed_work(&ui->chg_det, USB_CHG_DET_DELAY);
  2069. return 0;
  2070. }
  2071. static int msm72k_wakeup(struct usb_gadget *_gadget)
  2072. {
  2073. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2074. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  2075. if (!atomic_read(&ui->remote_wakeup)) {
  2076. dev_err(&ui->pdev->dev,
  2077. "%s: remote wakeup not supported\n", __func__);
  2078. return -ENOTSUPP;
  2079. }
  2080. if (!atomic_read(&ui->configured)) {
  2081. dev_err(&ui->pdev->dev,
  2082. "%s: device is not configured\n", __func__);
  2083. return -ENODEV;
  2084. }
  2085. usb_phy_set_suspend(ui->xceiv, 0);
  2086. disable_irq(otg->irq);
  2087. if (!is_usb_active())
  2088. writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
  2089. /* Ensure that USB port is resumed before enabling the IRQ */
  2090. mb();
  2091. enable_irq(otg->irq);
  2092. return 0;
  2093. }
  2094. /* when Gadget is configured, it will indicate how much power
  2095. * can be pulled from vbus, as specified in configuiration descriptor
  2096. */
  2097. static int msm72k_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  2098. {
  2099. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2100. unsigned long flags;
  2101. spin_lock_irqsave(&ui->lock, flags);
  2102. ui->b_max_pow = mA;
  2103. ui->flags = USB_FLAG_CONFIGURED;
  2104. spin_unlock_irqrestore(&ui->lock, flags);
  2105. schedule_work(&ui->work);
  2106. return 0;
  2107. }
  2108. static int msm72k_set_selfpowered(struct usb_gadget *_gadget, int set)
  2109. {
  2110. struct usb_info *ui = container_of(_gadget, struct usb_info, gadget);
  2111. unsigned long flags;
  2112. int ret = 0;
  2113. spin_lock_irqsave(&ui->lock, flags);
  2114. if (set) {
  2115. if (ui->pdata && ui->pdata->self_powered)
  2116. atomic_set(&ui->self_powered, 1);
  2117. else
  2118. ret = -EOPNOTSUPP;
  2119. } else {
  2120. /* We can always work as a bus powered device */
  2121. atomic_set(&ui->self_powered, 0);
  2122. }
  2123. spin_unlock_irqrestore(&ui->lock, flags);
  2124. return ret;
  2125. }
  2126. static int msm72k_gadget_start(struct usb_gadget_driver *driver,
  2127. int (*bind)(struct usb_gadget *));
  2128. static int msm72k_gadget_stop(struct usb_gadget_driver *driver);
  2129. static const struct usb_gadget_ops msm72k_ops = {
  2130. .get_frame = msm72k_get_frame,
  2131. .vbus_session = msm72k_udc_vbus_session,
  2132. .vbus_draw = msm72k_udc_vbus_draw,
  2133. .pullup = msm72k_pullup,
  2134. .wakeup = msm72k_wakeup,
  2135. .set_selfpowered = msm72k_set_selfpowered,
  2136. .start = msm72k_gadget_start,
  2137. .stop = msm72k_gadget_stop
  2138. };
  2139. static void usb_do_remote_wakeup(struct work_struct *w)
  2140. {
  2141. struct usb_info *ui = the_usb_info;
  2142. msm72k_wakeup(&ui->gadget);
  2143. }
  2144. static ssize_t usb_remote_wakeup(struct device *dev,
  2145. struct device_attribute *attr, const char *buf, size_t count)
  2146. {
  2147. struct usb_info *ui = the_usb_info;
  2148. msm72k_wakeup(&ui->gadget);
  2149. return count;
  2150. }
  2151. static ssize_t show_usb_state(struct device *dev, struct device_attribute *attr,
  2152. char *buf)
  2153. {
  2154. size_t i;
  2155. char *state[] = {"USB_STATE_NOTATTACHED", "USB_STATE_ATTACHED",
  2156. "USB_STATE_POWERED", "USB_STATE_UNAUTHENTICATED",
  2157. "USB_STATE_RECONNECTING", "USB_STATE_DEFAULT",
  2158. "USB_STATE_ADDRESS", "USB_STATE_CONFIGURED",
  2159. "USB_STATE_SUSPENDED"
  2160. };
  2161. i = scnprintf(buf, PAGE_SIZE, "%s\n", state[msm_hsusb_get_state()]);
  2162. return i;
  2163. }
  2164. static ssize_t show_usb_speed(struct device *dev, struct device_attribute *attr,
  2165. char *buf)
  2166. {
  2167. struct usb_info *ui = the_usb_info;
  2168. size_t i;
  2169. char *speed[] = {"USB_SPEED_UNKNOWN", "USB_SPEED_LOW",
  2170. "USB_SPEED_FULL", "USB_SPEED_HIGH"};
  2171. i = scnprintf(buf, PAGE_SIZE, "%s\n", speed[ui->gadget.speed]);
  2172. return i;
  2173. }
  2174. static ssize_t store_usb_chg_current(struct device *dev,
  2175. struct device_attribute *attr, const char *buf, size_t count)
  2176. {
  2177. struct usb_info *ui = the_usb_info;
  2178. unsigned long mA;
  2179. if (ui->gadget.is_a_peripheral)
  2180. return -EINVAL;
  2181. if (strict_strtoul(buf, 10, &mA))
  2182. return -EINVAL;
  2183. ui->chg_current = mA;
  2184. usb_phy_set_power(ui->xceiv, mA);
  2185. return count;
  2186. }
  2187. static ssize_t show_usb_chg_current(struct device *dev,
  2188. struct device_attribute *attr, char *buf)
  2189. {
  2190. struct usb_info *ui = the_usb_info;
  2191. size_t count;
  2192. count = snprintf(buf, PAGE_SIZE, "%d", ui->chg_current);
  2193. return count;
  2194. }
  2195. static ssize_t show_usb_chg_type(struct device *dev,
  2196. struct device_attribute *attr, char *buf)
  2197. {
  2198. struct usb_info *ui = the_usb_info;
  2199. struct msm_otg *otg = to_msm_otg(ui->xceiv);
  2200. size_t count;
  2201. char *chg_type[] = {"STD DOWNSTREAM PORT",
  2202. "CARKIT",
  2203. "DEDICATED CHARGER",
  2204. "INVALID"};
  2205. count = snprintf(buf, PAGE_SIZE, "%s",
  2206. chg_type[atomic_read(&otg->chg_type)]);
  2207. return count;
  2208. }
  2209. static DEVICE_ATTR(wakeup, S_IWUSR, 0, usb_remote_wakeup);
  2210. static DEVICE_ATTR(usb_state, S_IRUSR, show_usb_state, 0);
  2211. static DEVICE_ATTR(usb_speed, S_IRUSR, show_usb_speed, 0);
  2212. static DEVICE_ATTR(chg_type, S_IRUSR, show_usb_chg_type, 0);
  2213. static DEVICE_ATTR(chg_current, S_IWUSR | S_IRUSR,
  2214. show_usb_chg_current, store_usb_chg_current);
  2215. #ifdef CONFIG_USB_OTG
  2216. static ssize_t store_host_req(struct device *dev,
  2217. struct device_attribute *attr, const char *buf, size_t count)
  2218. {
  2219. struct usb_info *ui = the_usb_info;
  2220. unsigned long val, flags;
  2221. if (strict_strtoul(buf, 10, &val))
  2222. return -EINVAL;
  2223. dev_dbg(&ui->pdev->dev, "%s host request\n",
  2224. val ? "set" : "clear");
  2225. spin_lock_irqsave(&ui->lock, flags);
  2226. if (ui->hnp_avail)
  2227. ui->gadget.host_request = !!val;
  2228. spin_unlock_irqrestore(&ui->lock, flags);
  2229. return count;
  2230. }
  2231. static DEVICE_ATTR(host_request, S_IWUSR, NULL, store_host_req);
  2232. /* How do we notify user space about HNP availability?
  2233. * As we are compliant to Rev 2.0, Host will not set a_hnp_support.
  2234. * Introduce hnp_avail flag and set when HNP polling request arrives.
  2235. * The expectation is that user space checks hnp availability before
  2236. * requesting host role via above sysfs node.
  2237. */
  2238. static ssize_t show_host_avail(struct device *dev,
  2239. struct device_attribute *attr, char *buf)
  2240. {
  2241. struct usb_info *ui = the_usb_info;
  2242. size_t count;
  2243. unsigned long flags;
  2244. spin_lock_irqsave(&ui->lock, flags);
  2245. count = snprintf(buf, PAGE_SIZE, "%d\n", ui->hnp_avail);
  2246. spin_unlock_irqrestore(&ui->lock, flags);
  2247. return count;
  2248. }
  2249. static DEVICE_ATTR(host_avail, S_IRUSR, show_host_avail, NULL);
  2250. static struct attribute *otg_attrs[] = {
  2251. &dev_attr_host_request.attr,
  2252. &dev_attr_host_avail.attr,
  2253. NULL,
  2254. };
  2255. static struct attribute_group otg_attr_grp = {
  2256. .name = "otg",
  2257. .attrs = otg_attrs,
  2258. };
  2259. #endif
  2260. static int msm72k_probe(struct platform_device *pdev)
  2261. {
  2262. struct usb_info *ui;
  2263. struct msm_otg *otg;
  2264. int retval;
  2265. dev_dbg(&pdev->dev, "msm72k_probe\n");
  2266. ui = kzalloc(sizeof(struct usb_info), GFP_KERNEL);
  2267. if (!ui)
  2268. return -ENOMEM;
  2269. ui->pdev = pdev;
  2270. ui->pdata = pdev->dev.platform_data;
  2271. ui->buf = dma_alloc_coherent(&pdev->dev, 4096, &ui->dma, GFP_KERNEL);
  2272. if (!ui->buf)
  2273. return usb_free(ui, -ENOMEM);
  2274. ui->pool = dma_pool_create("msm72k_udc", NULL, 32, 32, 0);
  2275. if (!ui->pool)
  2276. return usb_free(ui, -ENOMEM);
  2277. ui->xceiv = usb_get_transceiver();
  2278. if (!ui->xceiv)
  2279. return usb_free(ui, -ENODEV);
  2280. otg = to_msm_otg(ui->xceiv);
  2281. ui->addr = otg->regs;
  2282. ui->gadget.ops = &msm72k_ops;
  2283. ui->gadget.max_speed = USB_SPEED_HIGH;
  2284. device_initialize(&ui->gadget.dev);
  2285. dev_set_name(&ui->gadget.dev, "gadget");
  2286. ui->gadget.dev.parent = &pdev->dev;
  2287. ui->gadget.dev.dma_mask = pdev->dev.dma_mask;
  2288. #ifdef CONFIG_USB_OTG
  2289. ui->gadget.is_otg = 1;
  2290. #endif
  2291. retval = usb_add_gadget_udc(&pdev->dev, &ui->gadget);
  2292. if (retval)
  2293. return usb_free(ui, retval);
  2294. ui->sdev.name = DRIVER_NAME;
  2295. ui->sdev.print_name = print_switch_name;
  2296. ui->sdev.print_state = print_switch_state;
  2297. retval = switch_dev_register(&ui->sdev);
  2298. if (retval)
  2299. return usb_free(ui, retval);
  2300. the_usb_info = ui;
  2301. wake_lock_init(&ui->wlock,
  2302. WAKE_LOCK_SUSPEND, "usb_bus_active");
  2303. usb_debugfs_init(ui);
  2304. usb_prepare(ui);
  2305. #ifdef CONFIG_USB_OTG
  2306. retval = sysfs_create_group(&pdev->dev.kobj, &otg_attr_grp);
  2307. if (retval) {
  2308. dev_err(&ui->pdev->dev,
  2309. "failed to create otg sysfs directory:"
  2310. "err:(%d)\n", retval);
  2311. }
  2312. #endif
  2313. retval = otg_set_peripheral(ui->xceiv->otg, &ui->gadget);
  2314. if (retval) {
  2315. dev_err(&ui->pdev->dev,
  2316. "%s: Cannot bind the transceiver, retval:(%d)\n",
  2317. __func__, retval);
  2318. switch_dev_unregister(&ui->sdev);
  2319. wake_lock_destroy(&ui->wlock);
  2320. return usb_free(ui, retval);
  2321. }
  2322. pm_runtime_enable(&pdev->dev);
  2323. /* Setup phy stuck timer */
  2324. if (ui->pdata && ui->pdata->is_phy_status_timer_on)
  2325. setup_timer(&phy_status_timer, usb_phy_status_check_timer, 0);
  2326. return 0;
  2327. }
  2328. static int msm72k_gadget_start(struct usb_gadget_driver *driver,
  2329. int (*bind)(struct usb_gadget *))
  2330. {
  2331. struct usb_info *ui = the_usb_info;
  2332. int retval, n;
  2333. if (!driver
  2334. || driver->max_speed < USB_SPEED_FULL
  2335. || !bind
  2336. || !driver->disconnect
  2337. || !driver->setup)
  2338. return -EINVAL;
  2339. if (!ui)
  2340. return -ENODEV;
  2341. if (ui->driver)
  2342. return -EBUSY;
  2343. /* first hook up the driver ... */
  2344. ui->driver = driver;
  2345. ui->gadget.dev.driver = &driver->driver;
  2346. ui->gadget.name = driver_name;
  2347. INIT_LIST_HEAD(&ui->gadget.ep_list);
  2348. ui->gadget.ep0 = &ui->ep0in.ep;
  2349. INIT_LIST_HEAD(&ui->gadget.ep0->ep_list);
  2350. ui->gadget.speed = USB_SPEED_UNKNOWN;
  2351. atomic_set(&ui->softconnect, 1);
  2352. for (n = 1; n < 16; n++) {
  2353. struct msm_endpoint *ept = ui->ept + n;
  2354. list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
  2355. ept->ep.maxpacket = 512;
  2356. }
  2357. for (n = 17; n < 32; n++) {
  2358. struct msm_endpoint *ept = ui->ept + n;
  2359. list_add_tail(&ept->ep.ep_list, &ui->gadget.ep_list);
  2360. ept->ep.maxpacket = 512;
  2361. }
  2362. retval = device_add(&ui->gadget.dev);
  2363. if (retval)
  2364. goto fail;
  2365. retval = bind(&ui->gadget);
  2366. if (retval) {
  2367. dev_err(&ui->pdev->dev, "bind to driver %s --> error %d\n",
  2368. driver->driver.name, retval);
  2369. device_del(&ui->gadget.dev);
  2370. goto fail;
  2371. }
  2372. retval = device_create_file(&ui->gadget.dev, &dev_attr_wakeup);
  2373. if (retval != 0)
  2374. dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
  2375. "(wakeup) error: (%d)\n", retval);
  2376. retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_state);
  2377. if (retval != 0)
  2378. dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
  2379. " (usb_state) error: (%d)\n", retval);
  2380. retval = device_create_file(&ui->gadget.dev, &dev_attr_usb_speed);
  2381. if (retval != 0)
  2382. dev_err(&ui->pdev->dev, "failed to create sysfs entry:"
  2383. " (usb_speed) error: (%d)\n", retval);
  2384. retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_type);
  2385. if (retval != 0)
  2386. dev_err(&ui->pdev->dev,
  2387. "failed to create sysfs entry(chg_type): err:(%d)\n",
  2388. retval);
  2389. retval = device_create_file(&ui->gadget.dev, &dev_attr_chg_current);
  2390. if (retval != 0)
  2391. dev_err(&ui->pdev->dev,
  2392. "failed to create sysfs entry(chg_current):"
  2393. "err:(%d)\n", retval);
  2394. dev_dbg(&ui->pdev->dev, "registered gadget driver '%s'\n",
  2395. driver->driver.name);
  2396. usb_start(ui);
  2397. return 0;
  2398. fail:
  2399. ui->driver = NULL;
  2400. ui->gadget.dev.driver = NULL;
  2401. return retval;
  2402. }
  2403. static int msm72k_gadget_stop(struct usb_gadget_driver *driver)
  2404. {
  2405. struct usb_info *dev = the_usb_info;
  2406. if (!dev)
  2407. return -ENODEV;
  2408. if (!driver || driver != dev->driver || !driver->unbind)
  2409. return -EINVAL;
  2410. msm72k_pullup_internal(&dev->gadget, 0);
  2411. if (dev->irq) {
  2412. free_irq(dev->irq, dev);
  2413. dev->irq = 0;
  2414. }
  2415. dev->state = USB_STATE_IDLE;
  2416. atomic_set(&dev->configured, 0);
  2417. switch_set_state(&dev->sdev, 0);
  2418. /* cancel pending ep0 transactions */
  2419. flush_endpoint(&dev->ep0out);
  2420. flush_endpoint(&dev->ep0in);
  2421. device_remove_file(&dev->gadget.dev, &dev_attr_wakeup);
  2422. device_remove_file(&dev->gadget.dev, &dev_attr_usb_state);
  2423. device_remove_file(&dev->gadget.dev, &dev_attr_usb_speed);
  2424. device_remove_file(&dev->gadget.dev, &dev_attr_chg_type);
  2425. device_remove_file(&dev->gadget.dev, &dev_attr_chg_current);
  2426. driver->disconnect(&dev->gadget);
  2427. driver->unbind(&dev->gadget);
  2428. dev->gadget.dev.driver = NULL;
  2429. dev->driver = NULL;
  2430. device_del(&dev->gadget.dev);
  2431. dev_dbg(&dev->pdev->dev,
  2432. "unregistered gadget driver '%s'\n", driver->driver.name);
  2433. return 0;
  2434. }
  2435. static int msm72k_udc_runtime_suspend(struct device *dev)
  2436. {
  2437. dev_dbg(dev, "pm_runtime: suspending...\n");
  2438. return 0;
  2439. }
  2440. static int msm72k_udc_runtime_resume(struct device *dev)
  2441. {
  2442. dev_dbg(dev, "pm_runtime: resuming...\n");
  2443. return 0;
  2444. }
  2445. static int msm72k_udc_runtime_idle(struct device *dev)
  2446. {
  2447. dev_dbg(dev, "pm_runtime: idling...\n");
  2448. return 0;
  2449. }
  2450. static struct dev_pm_ops msm72k_udc_dev_pm_ops = {
  2451. .runtime_suspend = msm72k_udc_runtime_suspend,
  2452. .runtime_resume = msm72k_udc_runtime_resume,
  2453. .runtime_idle = msm72k_udc_runtime_idle
  2454. };
  2455. static int __exit msm72k_remove(struct platform_device *pdev)
  2456. {
  2457. struct usb_info *ui = container_of(&pdev, struct usb_info, pdev);
  2458. return usb_free(ui, 0);
  2459. }
  2460. static struct platform_driver usb_driver = {
  2461. .probe = msm72k_probe,
  2462. .remove = msm72k_remove,
  2463. .driver = { .name = "msm_hsusb",
  2464. .pm = &msm72k_udc_dev_pm_ops, },
  2465. };
  2466. static int __init init(void)
  2467. {
  2468. return platform_driver_register(&usb_driver);
  2469. }
  2470. module_init(init);
  2471. static void __exit cleanup(void)
  2472. {
  2473. platform_driver_unregister(&usb_driver);
  2474. }
  2475. module_exit(cleanup);
  2476. MODULE_DESCRIPTION(DRIVER_DESC);
  2477. MODULE_AUTHOR("Mike Lockwood, Brian Swetland");
  2478. MODULE_LICENSE("GPL");