r8152.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. /*
  2. * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/signal.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/mii.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/usb.h>
  17. #include <linux/crc32.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/list.h>
  21. #include <linux/ip.h>
  22. #include <linux/ipv6.h>
  23. #include <net/ip6_checksum.h>
  24. /* Version Information */
  25. #define DRIVER_VERSION "v1.06.0 (2014/03/03)"
  26. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  27. #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
  28. #define MODULENAME "r8152"
  29. #define R8152_PHY_ID 32
  30. #define PLA_IDR 0xc000
  31. #define PLA_RCR 0xc010
  32. #define PLA_RMS 0xc016
  33. #define PLA_RXFIFO_CTRL0 0xc0a0
  34. #define PLA_RXFIFO_CTRL1 0xc0a4
  35. #define PLA_RXFIFO_CTRL2 0xc0a8
  36. #define PLA_FMC 0xc0b4
  37. #define PLA_CFG_WOL 0xc0b6
  38. #define PLA_TEREDO_CFG 0xc0bc
  39. #define PLA_MAR 0xcd00
  40. #define PLA_BACKUP 0xd000
  41. #define PAL_BDC_CR 0xd1a0
  42. #define PLA_TEREDO_TIMER 0xd2cc
  43. #define PLA_REALWOW_TIMER 0xd2e8
  44. #define PLA_LEDSEL 0xdd90
  45. #define PLA_LED_FEATURE 0xdd92
  46. #define PLA_PHYAR 0xde00
  47. #define PLA_BOOT_CTRL 0xe004
  48. #define PLA_GPHY_INTR_IMR 0xe022
  49. #define PLA_EEE_CR 0xe040
  50. #define PLA_EEEP_CR 0xe080
  51. #define PLA_MAC_PWR_CTRL 0xe0c0
  52. #define PLA_MAC_PWR_CTRL2 0xe0ca
  53. #define PLA_MAC_PWR_CTRL3 0xe0cc
  54. #define PLA_MAC_PWR_CTRL4 0xe0ce
  55. #define PLA_WDT6_CTRL 0xe428
  56. #define PLA_TCR0 0xe610
  57. #define PLA_TCR1 0xe612
  58. #define PLA_TXFIFO_CTRL 0xe618
  59. #define PLA_RSTTALLY 0xe800
  60. #define PLA_CR 0xe813
  61. #define PLA_CRWECR 0xe81c
  62. #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
  63. #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
  64. #define PLA_CONFIG5 0xe822
  65. #define PLA_PHY_PWR 0xe84c
  66. #define PLA_OOB_CTRL 0xe84f
  67. #define PLA_CPCR 0xe854
  68. #define PLA_MISC_0 0xe858
  69. #define PLA_MISC_1 0xe85a
  70. #define PLA_OCP_GPHY_BASE 0xe86c
  71. #define PLA_TALLYCNT 0xe890
  72. #define PLA_SFF_STS_7 0xe8de
  73. #define PLA_PHYSTATUS 0xe908
  74. #define PLA_BP_BA 0xfc26
  75. #define PLA_BP_0 0xfc28
  76. #define PLA_BP_1 0xfc2a
  77. #define PLA_BP_2 0xfc2c
  78. #define PLA_BP_3 0xfc2e
  79. #define PLA_BP_4 0xfc30
  80. #define PLA_BP_5 0xfc32
  81. #define PLA_BP_6 0xfc34
  82. #define PLA_BP_7 0xfc36
  83. #define PLA_BP_EN 0xfc38
  84. #define USB_U2P3_CTRL 0xb460
  85. #define USB_DEV_STAT 0xb808
  86. #define USB_USB_CTRL 0xd406
  87. #define USB_PHY_CTRL 0xd408
  88. #define USB_TX_AGG 0xd40a
  89. #define USB_RX_BUF_TH 0xd40c
  90. #define USB_USB_TIMER 0xd428
  91. #define USB_RX_EARLY_AGG 0xd42c
  92. #define USB_PM_CTRL_STATUS 0xd432
  93. #define USB_TX_DMA 0xd434
  94. #define USB_TOLERANCE 0xd490
  95. #define USB_LPM_CTRL 0xd41a
  96. #define USB_UPS_CTRL 0xd800
  97. #define USB_MISC_0 0xd81a
  98. #define USB_POWER_CUT 0xd80a
  99. #define USB_AFE_CTRL2 0xd824
  100. #define USB_WDT11_CTRL 0xe43c
  101. #define USB_BP_BA 0xfc26
  102. #define USB_BP_0 0xfc28
  103. #define USB_BP_1 0xfc2a
  104. #define USB_BP_2 0xfc2c
  105. #define USB_BP_3 0xfc2e
  106. #define USB_BP_4 0xfc30
  107. #define USB_BP_5 0xfc32
  108. #define USB_BP_6 0xfc34
  109. #define USB_BP_7 0xfc36
  110. #define USB_BP_EN 0xfc38
  111. /* OCP Registers */
  112. #define OCP_ALDPS_CONFIG 0x2010
  113. #define OCP_EEE_CONFIG1 0x2080
  114. #define OCP_EEE_CONFIG2 0x2092
  115. #define OCP_EEE_CONFIG3 0x2094
  116. #define OCP_BASE_MII 0xa400
  117. #define OCP_EEE_AR 0xa41a
  118. #define OCP_EEE_DATA 0xa41c
  119. #define OCP_PHY_STATUS 0xa420
  120. #define OCP_POWER_CFG 0xa430
  121. #define OCP_EEE_CFG 0xa432
  122. #define OCP_SRAM_ADDR 0xa436
  123. #define OCP_SRAM_DATA 0xa438
  124. #define OCP_DOWN_SPEED 0xa442
  125. #define OCP_EEE_CFG2 0xa5d0
  126. #define OCP_ADC_CFG 0xbc06
  127. /* SRAM Register */
  128. #define SRAM_LPF_CFG 0x8012
  129. #define SRAM_10M_AMP1 0x8080
  130. #define SRAM_10M_AMP2 0x8082
  131. #define SRAM_IMPEDANCE 0x8084
  132. /* PLA_RCR */
  133. #define RCR_AAP 0x00000001
  134. #define RCR_APM 0x00000002
  135. #define RCR_AM 0x00000004
  136. #define RCR_AB 0x00000008
  137. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  138. /* PLA_RXFIFO_CTRL0 */
  139. #define RXFIFO_THR1_NORMAL 0x00080002
  140. #define RXFIFO_THR1_OOB 0x01800003
  141. /* PLA_RXFIFO_CTRL1 */
  142. #define RXFIFO_THR2_FULL 0x00000060
  143. #define RXFIFO_THR2_HIGH 0x00000038
  144. #define RXFIFO_THR2_OOB 0x0000004a
  145. #define RXFIFO_THR2_NORMAL 0x00a0
  146. /* PLA_RXFIFO_CTRL2 */
  147. #define RXFIFO_THR3_FULL 0x00000078
  148. #define RXFIFO_THR3_HIGH 0x00000048
  149. #define RXFIFO_THR3_OOB 0x0000005a
  150. #define RXFIFO_THR3_NORMAL 0x0110
  151. /* PLA_TXFIFO_CTRL */
  152. #define TXFIFO_THR_NORMAL 0x00400008
  153. #define TXFIFO_THR_NORMAL2 0x01000008
  154. /* PLA_FMC */
  155. #define FMC_FCR_MCU_EN 0x0001
  156. /* PLA_EEEP_CR */
  157. #define EEEP_CR_EEEP_TX 0x0002
  158. /* PLA_WDT6_CTRL */
  159. #define WDT6_SET_MODE 0x0010
  160. /* PLA_TCR0 */
  161. #define TCR0_TX_EMPTY 0x0800
  162. #define TCR0_AUTO_FIFO 0x0080
  163. /* PLA_TCR1 */
  164. #define VERSION_MASK 0x7cf0
  165. /* PLA_RSTTALLY */
  166. #define TALLY_RESET 0x0001
  167. /* PLA_CR */
  168. #define CR_RST 0x10
  169. #define CR_RE 0x08
  170. #define CR_TE 0x04
  171. /* PLA_CRWECR */
  172. #define CRWECR_NORAML 0x00
  173. #define CRWECR_CONFIG 0xc0
  174. /* PLA_OOB_CTRL */
  175. #define NOW_IS_OOB 0x80
  176. #define TXFIFO_EMPTY 0x20
  177. #define RXFIFO_EMPTY 0x10
  178. #define LINK_LIST_READY 0x02
  179. #define DIS_MCU_CLROOB 0x01
  180. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  181. /* PLA_MISC_1 */
  182. #define RXDY_GATED_EN 0x0008
  183. /* PLA_SFF_STS_7 */
  184. #define RE_INIT_LL 0x8000
  185. #define MCU_BORW_EN 0x4000
  186. /* PLA_CPCR */
  187. #define CPCR_RX_VLAN 0x0040
  188. /* PLA_CFG_WOL */
  189. #define MAGIC_EN 0x0001
  190. /* PLA_TEREDO_CFG */
  191. #define TEREDO_SEL 0x8000
  192. #define TEREDO_WAKE_MASK 0x7f00
  193. #define TEREDO_RS_EVENT_MASK 0x00fe
  194. #define OOB_TEREDO_EN 0x0001
  195. /* PAL_BDC_CR */
  196. #define ALDPS_PROXY_MODE 0x0001
  197. /* PLA_CONFIG34 */
  198. #define LINK_ON_WAKE_EN 0x0010
  199. #define LINK_OFF_WAKE_EN 0x0008
  200. /* PLA_CONFIG5 */
  201. #define BWF_EN 0x0040
  202. #define MWF_EN 0x0020
  203. #define UWF_EN 0x0010
  204. #define LAN_WAKE_EN 0x0002
  205. /* PLA_LED_FEATURE */
  206. #define LED_MODE_MASK 0x0700
  207. /* PLA_PHY_PWR */
  208. #define TX_10M_IDLE_EN 0x0080
  209. #define PFM_PWM_SWITCH 0x0040
  210. /* PLA_MAC_PWR_CTRL */
  211. #define D3_CLK_GATED_EN 0x00004000
  212. #define MCU_CLK_RATIO 0x07010f07
  213. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  214. #define ALDPS_SPDWN_RATIO 0x0f87
  215. /* PLA_MAC_PWR_CTRL2 */
  216. #define EEE_SPDWN_RATIO 0x8007
  217. /* PLA_MAC_PWR_CTRL3 */
  218. #define PKT_AVAIL_SPDWN_EN 0x0100
  219. #define SUSPEND_SPDWN_EN 0x0004
  220. #define U1U2_SPDWN_EN 0x0002
  221. #define L1_SPDWN_EN 0x0001
  222. /* PLA_MAC_PWR_CTRL4 */
  223. #define PWRSAVE_SPDWN_EN 0x1000
  224. #define RXDV_SPDWN_EN 0x0800
  225. #define TX10MIDLE_EN 0x0100
  226. #define TP100_SPDWN_EN 0x0020
  227. #define TP500_SPDWN_EN 0x0010
  228. #define TP1000_SPDWN_EN 0x0008
  229. #define EEE_SPDWN_EN 0x0001
  230. /* PLA_GPHY_INTR_IMR */
  231. #define GPHY_STS_MSK 0x0001
  232. #define SPEED_DOWN_MSK 0x0002
  233. #define SPDWN_RXDV_MSK 0x0004
  234. #define SPDWN_LINKCHG_MSK 0x0008
  235. /* PLA_PHYAR */
  236. #define PHYAR_FLAG 0x80000000
  237. /* PLA_EEE_CR */
  238. #define EEE_RX_EN 0x0001
  239. #define EEE_TX_EN 0x0002
  240. /* PLA_BOOT_CTRL */
  241. #define AUTOLOAD_DONE 0x0002
  242. /* USB_DEV_STAT */
  243. #define STAT_SPEED_MASK 0x0006
  244. #define STAT_SPEED_HIGH 0x0000
  245. #define STAT_SPEED_FULL 0x0001
  246. /* USB_TX_AGG */
  247. #define TX_AGG_MAX_THRESHOLD 0x03
  248. /* USB_RX_BUF_TH */
  249. #define RX_THR_SUPPER 0x0c350180
  250. #define RX_THR_HIGH 0x7a120180
  251. #define RX_THR_SLOW 0xffff0180
  252. /* USB_TX_DMA */
  253. #define TEST_MODE_DISABLE 0x00000001
  254. #define TX_SIZE_ADJUST1 0x00000100
  255. /* USB_UPS_CTRL */
  256. #define POWER_CUT 0x0100
  257. /* USB_PM_CTRL_STATUS */
  258. #define RESUME_INDICATE 0x0001
  259. /* USB_USB_CTRL */
  260. #define RX_AGG_DISABLE 0x0010
  261. /* USB_U2P3_CTRL */
  262. #define U2P3_ENABLE 0x0001
  263. /* USB_POWER_CUT */
  264. #define PWR_EN 0x0001
  265. #define PHASE2_EN 0x0008
  266. /* USB_MISC_0 */
  267. #define PCUT_STATUS 0x0001
  268. /* USB_RX_EARLY_AGG */
  269. #define EARLY_AGG_SUPPER 0x0e832981
  270. #define EARLY_AGG_HIGH 0x0e837a12
  271. #define EARLY_AGG_SLOW 0x0e83ffff
  272. /* USB_WDT11_CTRL */
  273. #define TIMER11_EN 0x0001
  274. /* USB_LPM_CTRL */
  275. #define LPM_TIMER_MASK 0x0c
  276. #define LPM_TIMER_500MS 0x04 /* 500 ms */
  277. #define LPM_TIMER_500US 0x0c /* 500 us */
  278. /* USB_AFE_CTRL2 */
  279. #define SEN_VAL_MASK 0xf800
  280. #define SEN_VAL_NORMAL 0xa000
  281. #define SEL_RXIDLE 0x0100
  282. /* OCP_ALDPS_CONFIG */
  283. #define ENPWRSAVE 0x8000
  284. #define ENPDNPS 0x0200
  285. #define LINKENA 0x0100
  286. #define DIS_SDSAVE 0x0010
  287. /* OCP_PHY_STATUS */
  288. #define PHY_STAT_MASK 0x0007
  289. #define PHY_STAT_LAN_ON 3
  290. #define PHY_STAT_PWRDN 5
  291. /* OCP_POWER_CFG */
  292. #define EEE_CLKDIV_EN 0x8000
  293. #define EN_ALDPS 0x0004
  294. #define EN_10M_PLLOFF 0x0001
  295. /* OCP_EEE_CONFIG1 */
  296. #define RG_TXLPI_MSK_HFDUP 0x8000
  297. #define RG_MATCLR_EN 0x4000
  298. #define EEE_10_CAP 0x2000
  299. #define EEE_NWAY_EN 0x1000
  300. #define TX_QUIET_EN 0x0200
  301. #define RX_QUIET_EN 0x0100
  302. #define SDRISETIME 0x0010 /* bit 4 ~ 6 */
  303. #define RG_RXLPI_MSK_HFDUP 0x0008
  304. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  305. /* OCP_EEE_CONFIG2 */
  306. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  307. #define RG_DACQUIET_EN 0x0400
  308. #define RG_LDVQUIET_EN 0x0200
  309. #define RG_CKRSEL 0x0020
  310. #define RG_EEEPRG_EN 0x0010
  311. /* OCP_EEE_CONFIG3 */
  312. #define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
  313. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  314. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  315. /* OCP_EEE_AR */
  316. /* bit[15:14] function */
  317. #define FUN_ADDR 0x0000
  318. #define FUN_DATA 0x4000
  319. /* bit[4:0] device addr */
  320. #define DEVICE_ADDR 0x0007
  321. /* OCP_EEE_DATA */
  322. #define EEE_ADDR 0x003C
  323. #define EEE_DATA 0x0002
  324. /* OCP_EEE_CFG */
  325. #define CTAP_SHORT_EN 0x0040
  326. #define EEE10_EN 0x0010
  327. /* OCP_DOWN_SPEED */
  328. #define EN_10M_BGOFF 0x0080
  329. /* OCP_EEE_CFG2 */
  330. #define MY1000_EEE 0x0004
  331. #define MY100_EEE 0x0002
  332. /* OCP_ADC_CFG */
  333. #define CKADSEL_L 0x0100
  334. #define ADC_EN 0x0080
  335. #define EN_EMI_L 0x0040
  336. /* SRAM_LPF_CFG */
  337. #define LPF_AUTO_TUNE 0x8000
  338. /* SRAM_10M_AMP1 */
  339. #define GDAC_IB_UPALL 0x0008
  340. /* SRAM_10M_AMP2 */
  341. #define AMP_DN 0x0200
  342. /* SRAM_IMPEDANCE */
  343. #define RX_DRIVING_MASK 0x6000
  344. enum rtl_register_content {
  345. _1000bps = 0x10,
  346. _100bps = 0x08,
  347. _10bps = 0x04,
  348. LINK_STATUS = 0x02,
  349. FULL_DUP = 0x01,
  350. };
  351. #define RTL8152_MAX_TX 10
  352. #define RTL8152_MAX_RX 10
  353. #define INTBUFSIZE 2
  354. #define CRC_SIZE 4
  355. #define TX_ALIGN 4
  356. #define RX_ALIGN 8
  357. #define INTR_LINK 0x0004
  358. #define RTL8152_REQT_READ 0xc0
  359. #define RTL8152_REQT_WRITE 0x40
  360. #define RTL8152_REQ_GET_REGS 0x05
  361. #define RTL8152_REQ_SET_REGS 0x05
  362. #define BYTE_EN_DWORD 0xff
  363. #define BYTE_EN_WORD 0x33
  364. #define BYTE_EN_BYTE 0x11
  365. #define BYTE_EN_SIX_BYTES 0x3f
  366. #define BYTE_EN_START_MASK 0x0f
  367. #define BYTE_EN_END_MASK 0xf0
  368. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
  369. #define RTL8152_TX_TIMEOUT (HZ)
  370. /* rtl8152 flags */
  371. enum rtl8152_flags {
  372. RTL8152_UNPLUG = 0,
  373. RTL8152_SET_RX_MODE,
  374. WORK_ENABLE,
  375. RTL8152_LINK_CHG,
  376. SELECTIVE_SUSPEND,
  377. PHY_RESET,
  378. SCHEDULE_TASKLET,
  379. };
  380. /* Define these values to match your device */
  381. #define VENDOR_ID_REALTEK 0x0bda
  382. #define PRODUCT_ID_RTL8152 0x8152
  383. #define PRODUCT_ID_RTL8153 0x8153
  384. #define VENDOR_ID_SAMSUNG 0x04e8
  385. #define PRODUCT_ID_SAMSUNG 0xa101
  386. #define MCU_TYPE_PLA 0x0100
  387. #define MCU_TYPE_USB 0x0000
  388. #define REALTEK_USB_DEVICE(vend, prod) \
  389. USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
  390. struct tally_counter {
  391. __le64 tx_packets;
  392. __le64 rx_packets;
  393. __le64 tx_errors;
  394. __le32 rx_errors;
  395. __le16 rx_missed;
  396. __le16 align_errors;
  397. __le32 tx_one_collision;
  398. __le32 tx_multi_collision;
  399. __le64 rx_unicast;
  400. __le64 rx_broadcast;
  401. __le32 rx_multicast;
  402. __le16 tx_aborted;
  403. __le16 tx_underun;
  404. };
  405. struct rx_desc {
  406. __le32 opts1;
  407. #define RX_LEN_MASK 0x7fff
  408. __le32 opts2;
  409. #define RD_UDP_CS (1 << 23)
  410. #define RD_TCP_CS (1 << 22)
  411. #define RD_IPV6_CS (1 << 20)
  412. #define RD_IPV4_CS (1 << 19)
  413. __le32 opts3;
  414. #define IPF (1 << 23) /* IP checksum fail */
  415. #define UDPF (1 << 22) /* UDP checksum fail */
  416. #define TCPF (1 << 21) /* TCP checksum fail */
  417. __le32 opts4;
  418. __le32 opts5;
  419. __le32 opts6;
  420. };
  421. struct tx_desc {
  422. __le32 opts1;
  423. #define TX_FS (1 << 31) /* First segment of a packet */
  424. #define TX_LS (1 << 30) /* Final segment of a packet */
  425. #define GTSENDV4 (1 << 28)
  426. #define GTSENDV6 (1 << 27)
  427. #define GTTCPHO_SHIFT 18
  428. #define GTTCPHO_MAX 0x7fU
  429. #define TX_LEN_MAX 0x3ffffU
  430. __le32 opts2;
  431. #define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
  432. #define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
  433. #define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
  434. #define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
  435. #define MSS_SHIFT 17
  436. #define MSS_MAX 0x7ffU
  437. #define TCPHO_SHIFT 17
  438. #define TCPHO_MAX 0x7ffU
  439. };
  440. struct r8152;
  441. struct rx_agg {
  442. struct list_head list;
  443. struct urb *urb;
  444. struct r8152 *context;
  445. void *buffer;
  446. void *head;
  447. };
  448. struct tx_agg {
  449. struct list_head list;
  450. struct urb *urb;
  451. struct r8152 *context;
  452. void *buffer;
  453. void *head;
  454. u32 skb_num;
  455. u32 skb_len;
  456. };
  457. struct r8152 {
  458. unsigned long flags;
  459. struct usb_device *udev;
  460. struct tasklet_struct tl;
  461. struct usb_interface *intf;
  462. struct net_device *netdev;
  463. struct urb *intr_urb;
  464. struct tx_agg tx_info[RTL8152_MAX_TX];
  465. struct rx_agg rx_info[RTL8152_MAX_RX];
  466. struct list_head rx_done, tx_free;
  467. struct sk_buff_head tx_queue;
  468. spinlock_t rx_lock, tx_lock;
  469. struct delayed_work schedule;
  470. struct mii_if_info mii;
  471. struct rtl_ops {
  472. void (*init)(struct r8152 *);
  473. int (*enable)(struct r8152 *);
  474. void (*disable)(struct r8152 *);
  475. void (*up)(struct r8152 *);
  476. void (*down)(struct r8152 *);
  477. void (*unload)(struct r8152 *);
  478. } rtl_ops;
  479. int intr_interval;
  480. u32 saved_wolopts;
  481. u32 msg_enable;
  482. u32 tx_qlen;
  483. u16 ocp_base;
  484. u8 *intr_buff;
  485. u8 version;
  486. u8 speed;
  487. };
  488. enum rtl_version {
  489. RTL_VER_UNKNOWN = 0,
  490. RTL_VER_01,
  491. RTL_VER_02,
  492. RTL_VER_03,
  493. RTL_VER_04,
  494. RTL_VER_05,
  495. RTL_VER_MAX
  496. };
  497. enum tx_csum_stat {
  498. TX_CSUM_SUCCESS = 0,
  499. TX_CSUM_TSO,
  500. TX_CSUM_NONE
  501. };
  502. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  503. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  504. */
  505. static const int multicast_filter_limit = 32;
  506. static unsigned int rx_buf_sz = 16384;
  507. #define RTL_LIMITED_TSO_SIZE (rx_buf_sz - sizeof(struct tx_desc) - \
  508. VLAN_ETH_HLEN - VLAN_HLEN)
  509. static
  510. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  511. {
  512. int ret;
  513. void *tmp;
  514. tmp = kmalloc(size, GFP_KERNEL);
  515. if (!tmp)
  516. return -ENOMEM;
  517. ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  518. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  519. value, index, tmp, size, 500);
  520. memcpy(data, tmp, size);
  521. kfree(tmp);
  522. return ret;
  523. }
  524. static
  525. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  526. {
  527. int ret;
  528. void *tmp;
  529. tmp = kmemdup(data, size, GFP_KERNEL);
  530. if (!tmp)
  531. return -ENOMEM;
  532. ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  533. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  534. value, index, tmp, size, 500);
  535. kfree(tmp);
  536. return ret;
  537. }
  538. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  539. void *data, u16 type)
  540. {
  541. u16 limit = 64;
  542. int ret = 0;
  543. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  544. return -ENODEV;
  545. /* both size and indix must be 4 bytes align */
  546. if ((size & 3) || !size || (index & 3) || !data)
  547. return -EPERM;
  548. if ((u32)index + (u32)size > 0xffff)
  549. return -EPERM;
  550. while (size) {
  551. if (size > limit) {
  552. ret = get_registers(tp, index, type, limit, data);
  553. if (ret < 0)
  554. break;
  555. index += limit;
  556. data += limit;
  557. size -= limit;
  558. } else {
  559. ret = get_registers(tp, index, type, size, data);
  560. if (ret < 0)
  561. break;
  562. index += size;
  563. data += size;
  564. size = 0;
  565. break;
  566. }
  567. }
  568. return ret;
  569. }
  570. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  571. u16 size, void *data, u16 type)
  572. {
  573. int ret;
  574. u16 byteen_start, byteen_end, byen;
  575. u16 limit = 512;
  576. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  577. return -ENODEV;
  578. /* both size and indix must be 4 bytes align */
  579. if ((size & 3) || !size || (index & 3) || !data)
  580. return -EPERM;
  581. if ((u32)index + (u32)size > 0xffff)
  582. return -EPERM;
  583. byteen_start = byteen & BYTE_EN_START_MASK;
  584. byteen_end = byteen & BYTE_EN_END_MASK;
  585. byen = byteen_start | (byteen_start << 4);
  586. ret = set_registers(tp, index, type | byen, 4, data);
  587. if (ret < 0)
  588. goto error1;
  589. index += 4;
  590. data += 4;
  591. size -= 4;
  592. if (size) {
  593. size -= 4;
  594. while (size) {
  595. if (size > limit) {
  596. ret = set_registers(tp, index,
  597. type | BYTE_EN_DWORD,
  598. limit, data);
  599. if (ret < 0)
  600. goto error1;
  601. index += limit;
  602. data += limit;
  603. size -= limit;
  604. } else {
  605. ret = set_registers(tp, index,
  606. type | BYTE_EN_DWORD,
  607. size, data);
  608. if (ret < 0)
  609. goto error1;
  610. index += size;
  611. data += size;
  612. size = 0;
  613. break;
  614. }
  615. }
  616. byen = byteen_end | (byteen_end >> 4);
  617. ret = set_registers(tp, index, type | byen, 4, data);
  618. if (ret < 0)
  619. goto error1;
  620. }
  621. error1:
  622. return ret;
  623. }
  624. static inline
  625. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  626. {
  627. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  628. }
  629. static inline
  630. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  631. {
  632. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  633. }
  634. static inline
  635. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  636. {
  637. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  638. }
  639. static inline
  640. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  641. {
  642. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  643. }
  644. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  645. {
  646. __le32 data;
  647. generic_ocp_read(tp, index, sizeof(data), &data, type);
  648. return __le32_to_cpu(data);
  649. }
  650. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  651. {
  652. __le32 tmp = __cpu_to_le32(data);
  653. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  654. }
  655. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  656. {
  657. u32 data;
  658. __le32 tmp;
  659. u8 shift = index & 2;
  660. index &= ~3;
  661. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  662. data = __le32_to_cpu(tmp);
  663. data >>= (shift * 8);
  664. data &= 0xffff;
  665. return (u16)data;
  666. }
  667. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  668. {
  669. u32 mask = 0xffff;
  670. __le32 tmp;
  671. u16 byen = BYTE_EN_WORD;
  672. u8 shift = index & 2;
  673. data &= mask;
  674. if (index & 2) {
  675. byen <<= shift;
  676. mask <<= (shift * 8);
  677. data <<= (shift * 8);
  678. index &= ~3;
  679. }
  680. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  681. data |= __le32_to_cpu(tmp) & ~mask;
  682. tmp = __cpu_to_le32(data);
  683. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  684. }
  685. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  686. {
  687. u32 data;
  688. __le32 tmp;
  689. u8 shift = index & 3;
  690. index &= ~3;
  691. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  692. data = __le32_to_cpu(tmp);
  693. data >>= (shift * 8);
  694. data &= 0xff;
  695. return (u8)data;
  696. }
  697. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  698. {
  699. u32 mask = 0xff;
  700. __le32 tmp;
  701. u16 byen = BYTE_EN_BYTE;
  702. u8 shift = index & 3;
  703. data &= mask;
  704. if (index & 3) {
  705. byen <<= shift;
  706. mask <<= (shift * 8);
  707. data <<= (shift * 8);
  708. index &= ~3;
  709. }
  710. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  711. data |= __le32_to_cpu(tmp) & ~mask;
  712. tmp = __cpu_to_le32(data);
  713. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  714. }
  715. static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
  716. {
  717. u16 ocp_base, ocp_index;
  718. ocp_base = addr & 0xf000;
  719. if (ocp_base != tp->ocp_base) {
  720. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  721. tp->ocp_base = ocp_base;
  722. }
  723. ocp_index = (addr & 0x0fff) | 0xb000;
  724. return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
  725. }
  726. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  727. {
  728. u16 ocp_base, ocp_index;
  729. ocp_base = addr & 0xf000;
  730. if (ocp_base != tp->ocp_base) {
  731. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  732. tp->ocp_base = ocp_base;
  733. }
  734. ocp_index = (addr & 0x0fff) | 0xb000;
  735. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  736. }
  737. static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  738. {
  739. ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
  740. }
  741. static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  742. {
  743. return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
  744. }
  745. static void sram_write(struct r8152 *tp, u16 addr, u16 data)
  746. {
  747. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  748. ocp_reg_write(tp, OCP_SRAM_DATA, data);
  749. }
  750. static u16 sram_read(struct r8152 *tp, u16 addr)
  751. {
  752. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  753. return ocp_reg_read(tp, OCP_SRAM_DATA);
  754. }
  755. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  756. {
  757. struct r8152 *tp = netdev_priv(netdev);
  758. int ret;
  759. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  760. return -ENODEV;
  761. if (phy_id != R8152_PHY_ID)
  762. return -EINVAL;
  763. ret = usb_autopm_get_interface(tp->intf);
  764. if (ret < 0)
  765. goto out;
  766. ret = r8152_mdio_read(tp, reg);
  767. usb_autopm_put_interface(tp->intf);
  768. out:
  769. return ret;
  770. }
  771. static
  772. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  773. {
  774. struct r8152 *tp = netdev_priv(netdev);
  775. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  776. return;
  777. if (phy_id != R8152_PHY_ID)
  778. return;
  779. if (usb_autopm_get_interface(tp->intf) < 0)
  780. return;
  781. r8152_mdio_write(tp, reg, val);
  782. usb_autopm_put_interface(tp->intf);
  783. }
  784. static
  785. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
  786. static inline void set_ethernet_addr(struct r8152 *tp)
  787. {
  788. struct net_device *dev = tp->netdev;
  789. int ret;
  790. u8 node_id[8] = {0};
  791. if (tp->version == RTL_VER_01)
  792. ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
  793. else
  794. ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);
  795. if (ret < 0) {
  796. netif_notice(tp, probe, dev, "inet addr fail\n");
  797. } else {
  798. if (tp->version != RTL_VER_01) {
  799. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
  800. CRWECR_CONFIG);
  801. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
  802. sizeof(node_id), node_id);
  803. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
  804. CRWECR_NORAML);
  805. }
  806. memcpy(dev->dev_addr, node_id, dev->addr_len);
  807. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  808. }
  809. }
  810. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  811. {
  812. struct r8152 *tp = netdev_priv(netdev);
  813. struct sockaddr *addr = p;
  814. if (!is_valid_ether_addr(addr->sa_data))
  815. return -EADDRNOTAVAIL;
  816. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  817. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  818. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  819. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  820. return 0;
  821. }
  822. static void read_bulk_callback(struct urb *urb)
  823. {
  824. struct net_device *netdev;
  825. int status = urb->status;
  826. struct rx_agg *agg;
  827. struct r8152 *tp;
  828. int result;
  829. agg = urb->context;
  830. if (!agg)
  831. return;
  832. tp = agg->context;
  833. if (!tp)
  834. return;
  835. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  836. return;
  837. if (!test_bit(WORK_ENABLE, &tp->flags))
  838. return;
  839. netdev = tp->netdev;
  840. /* When link down, the driver would cancel all bulks. */
  841. /* This avoid the re-submitting bulk */
  842. if (!netif_carrier_ok(netdev))
  843. return;
  844. usb_mark_last_busy(tp->udev);
  845. switch (status) {
  846. case 0:
  847. if (urb->actual_length < ETH_ZLEN)
  848. break;
  849. spin_lock(&tp->rx_lock);
  850. list_add_tail(&agg->list, &tp->rx_done);
  851. spin_unlock(&tp->rx_lock);
  852. tasklet_schedule(&tp->tl);
  853. return;
  854. case -ESHUTDOWN:
  855. set_bit(RTL8152_UNPLUG, &tp->flags);
  856. netif_device_detach(tp->netdev);
  857. return;
  858. case -ENOENT:
  859. return; /* the urb is in unlink state */
  860. case -ETIME:
  861. if (net_ratelimit())
  862. netdev_warn(netdev, "maybe reset is needed?\n");
  863. break;
  864. default:
  865. if (net_ratelimit())
  866. netdev_warn(netdev, "Rx status %d\n", status);
  867. break;
  868. }
  869. result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  870. if (result == -ENODEV) {
  871. netif_device_detach(tp->netdev);
  872. } else if (result) {
  873. spin_lock(&tp->rx_lock);
  874. list_add_tail(&agg->list, &tp->rx_done);
  875. spin_unlock(&tp->rx_lock);
  876. tasklet_schedule(&tp->tl);
  877. }
  878. }
  879. static void write_bulk_callback(struct urb *urb)
  880. {
  881. struct net_device_stats *stats;
  882. struct net_device *netdev;
  883. struct tx_agg *agg;
  884. struct r8152 *tp;
  885. int status = urb->status;
  886. agg = urb->context;
  887. if (!agg)
  888. return;
  889. tp = agg->context;
  890. if (!tp)
  891. return;
  892. netdev = tp->netdev;
  893. stats = &netdev->stats;
  894. if (status) {
  895. if (net_ratelimit())
  896. netdev_warn(netdev, "Tx status %d\n", status);
  897. stats->tx_errors += agg->skb_num;
  898. } else {
  899. stats->tx_packets += agg->skb_num;
  900. stats->tx_bytes += agg->skb_len;
  901. }
  902. spin_lock(&tp->tx_lock);
  903. list_add_tail(&agg->list, &tp->tx_free);
  904. spin_unlock(&tp->tx_lock);
  905. usb_autopm_put_interface_async(tp->intf);
  906. if (!netif_carrier_ok(netdev))
  907. return;
  908. if (!test_bit(WORK_ENABLE, &tp->flags))
  909. return;
  910. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  911. return;
  912. if (!skb_queue_empty(&tp->tx_queue))
  913. tasklet_schedule(&tp->tl);
  914. }
  915. static void intr_callback(struct urb *urb)
  916. {
  917. struct r8152 *tp;
  918. __le16 *d;
  919. int status = urb->status;
  920. int res;
  921. tp = urb->context;
  922. if (!tp)
  923. return;
  924. if (!test_bit(WORK_ENABLE, &tp->flags))
  925. return;
  926. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  927. return;
  928. switch (status) {
  929. case 0: /* success */
  930. break;
  931. case -ECONNRESET: /* unlink */
  932. case -ESHUTDOWN:
  933. netif_device_detach(tp->netdev);
  934. case -ENOENT:
  935. return;
  936. case -EOVERFLOW:
  937. netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
  938. goto resubmit;
  939. /* -EPIPE: should clear the halt */
  940. default:
  941. netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
  942. goto resubmit;
  943. }
  944. d = urb->transfer_buffer;
  945. if (INTR_LINK & __le16_to_cpu(d[0])) {
  946. if (!(tp->speed & LINK_STATUS)) {
  947. set_bit(RTL8152_LINK_CHG, &tp->flags);
  948. schedule_delayed_work(&tp->schedule, 0);
  949. }
  950. } else {
  951. if (tp->speed & LINK_STATUS) {
  952. set_bit(RTL8152_LINK_CHG, &tp->flags);
  953. schedule_delayed_work(&tp->schedule, 0);
  954. }
  955. }
  956. resubmit:
  957. res = usb_submit_urb(urb, GFP_ATOMIC);
  958. if (res == -ENODEV)
  959. netif_device_detach(tp->netdev);
  960. else if (res)
  961. netif_err(tp, intr, tp->netdev,
  962. "can't resubmit intr, status %d\n", res);
  963. }
  964. static inline void *rx_agg_align(void *data)
  965. {
  966. return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
  967. }
  968. static inline void *tx_agg_align(void *data)
  969. {
  970. return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
  971. }
  972. static void free_all_mem(struct r8152 *tp)
  973. {
  974. int i;
  975. for (i = 0; i < RTL8152_MAX_RX; i++) {
  976. usb_free_urb(tp->rx_info[i].urb);
  977. tp->rx_info[i].urb = NULL;
  978. kfree(tp->rx_info[i].buffer);
  979. tp->rx_info[i].buffer = NULL;
  980. tp->rx_info[i].head = NULL;
  981. }
  982. for (i = 0; i < RTL8152_MAX_TX; i++) {
  983. usb_free_urb(tp->tx_info[i].urb);
  984. tp->tx_info[i].urb = NULL;
  985. kfree(tp->tx_info[i].buffer);
  986. tp->tx_info[i].buffer = NULL;
  987. tp->tx_info[i].head = NULL;
  988. }
  989. usb_free_urb(tp->intr_urb);
  990. tp->intr_urb = NULL;
  991. kfree(tp->intr_buff);
  992. tp->intr_buff = NULL;
  993. }
  994. static int alloc_all_mem(struct r8152 *tp)
  995. {
  996. struct net_device *netdev = tp->netdev;
  997. struct usb_interface *intf = tp->intf;
  998. struct usb_host_interface *alt = intf->cur_altsetting;
  999. struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
  1000. struct urb *urb;
  1001. int node, i;
  1002. u8 *buf;
  1003. node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  1004. spin_lock_init(&tp->rx_lock);
  1005. spin_lock_init(&tp->tx_lock);
  1006. INIT_LIST_HEAD(&tp->rx_done);
  1007. INIT_LIST_HEAD(&tp->tx_free);
  1008. skb_queue_head_init(&tp->tx_queue);
  1009. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1010. buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  1011. if (!buf)
  1012. goto err1;
  1013. if (buf != rx_agg_align(buf)) {
  1014. kfree(buf);
  1015. buf = kmalloc_node(rx_buf_sz + RX_ALIGN, GFP_KERNEL,
  1016. node);
  1017. if (!buf)
  1018. goto err1;
  1019. }
  1020. urb = usb_alloc_urb(0, GFP_KERNEL);
  1021. if (!urb) {
  1022. kfree(buf);
  1023. goto err1;
  1024. }
  1025. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1026. tp->rx_info[i].context = tp;
  1027. tp->rx_info[i].urb = urb;
  1028. tp->rx_info[i].buffer = buf;
  1029. tp->rx_info[i].head = rx_agg_align(buf);
  1030. }
  1031. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1032. buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  1033. if (!buf)
  1034. goto err1;
  1035. if (buf != tx_agg_align(buf)) {
  1036. kfree(buf);
  1037. buf = kmalloc_node(rx_buf_sz + TX_ALIGN, GFP_KERNEL,
  1038. node);
  1039. if (!buf)
  1040. goto err1;
  1041. }
  1042. urb = usb_alloc_urb(0, GFP_KERNEL);
  1043. if (!urb) {
  1044. kfree(buf);
  1045. goto err1;
  1046. }
  1047. INIT_LIST_HEAD(&tp->tx_info[i].list);
  1048. tp->tx_info[i].context = tp;
  1049. tp->tx_info[i].urb = urb;
  1050. tp->tx_info[i].buffer = buf;
  1051. tp->tx_info[i].head = tx_agg_align(buf);
  1052. list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
  1053. }
  1054. tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  1055. if (!tp->intr_urb)
  1056. goto err1;
  1057. tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  1058. if (!tp->intr_buff)
  1059. goto err1;
  1060. tp->intr_interval = (int)ep_intr->desc.bInterval;
  1061. usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
  1062. tp->intr_buff, INTBUFSIZE, intr_callback,
  1063. tp, tp->intr_interval);
  1064. return 0;
  1065. err1:
  1066. free_all_mem(tp);
  1067. return -ENOMEM;
  1068. }
  1069. static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
  1070. {
  1071. struct tx_agg *agg = NULL;
  1072. unsigned long flags;
  1073. if (list_empty(&tp->tx_free))
  1074. return NULL;
  1075. spin_lock_irqsave(&tp->tx_lock, flags);
  1076. if (!list_empty(&tp->tx_free)) {
  1077. struct list_head *cursor;
  1078. cursor = tp->tx_free.next;
  1079. list_del_init(cursor);
  1080. agg = list_entry(cursor, struct tx_agg, list);
  1081. }
  1082. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1083. return agg;
  1084. }
  1085. static inline __be16 get_protocol(struct sk_buff *skb)
  1086. {
  1087. __be16 protocol;
  1088. if (skb->protocol == htons(ETH_P_8021Q))
  1089. protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  1090. else
  1091. protocol = skb->protocol;
  1092. return protocol;
  1093. }
  1094. /*
  1095. * r8152_csum_workaround()
  1096. * The hw limites the value the transport offset. When the offset is out of the
  1097. * range, calculate the checksum by sw.
  1098. */
  1099. static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
  1100. struct sk_buff_head *list)
  1101. {
  1102. if (skb_shinfo(skb)->gso_size) {
  1103. netdev_features_t features = tp->netdev->features;
  1104. struct sk_buff_head seg_list;
  1105. struct sk_buff *segs, *nskb;
  1106. features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
  1107. segs = skb_gso_segment(skb, features);
  1108. if (IS_ERR(segs) || !segs)
  1109. goto drop;
  1110. __skb_queue_head_init(&seg_list);
  1111. do {
  1112. nskb = segs;
  1113. segs = segs->next;
  1114. nskb->next = NULL;
  1115. __skb_queue_tail(&seg_list, nskb);
  1116. } while (segs);
  1117. skb_queue_splice(&seg_list, list);
  1118. dev_kfree_skb(skb);
  1119. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1120. if (skb_checksum_help(skb) < 0)
  1121. goto drop;
  1122. __skb_queue_head(list, skb);
  1123. } else {
  1124. struct net_device_stats *stats;
  1125. drop:
  1126. stats = &tp->netdev->stats;
  1127. stats->tx_dropped++;
  1128. dev_kfree_skb(skb);
  1129. }
  1130. }
  1131. /*
  1132. * msdn_giant_send_check()
  1133. * According to the document of microsoft, the TCP Pseudo Header excludes the
  1134. * packet length for IPv6 TCP large packets.
  1135. */
  1136. static int msdn_giant_send_check(struct sk_buff *skb)
  1137. {
  1138. const struct ipv6hdr *ipv6h;
  1139. struct tcphdr *th;
  1140. int ret;
  1141. ret = skb_cow_head(skb, 0);
  1142. if (ret)
  1143. return ret;
  1144. ipv6h = ipv6_hdr(skb);
  1145. th = tcp_hdr(skb);
  1146. th->check = 0;
  1147. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  1148. return ret;
  1149. }
  1150. static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
  1151. struct sk_buff *skb, u32 len, u32 transport_offset)
  1152. {
  1153. u32 mss = skb_shinfo(skb)->gso_size;
  1154. u32 opts1, opts2 = 0;
  1155. int ret = TX_CSUM_SUCCESS;
  1156. WARN_ON_ONCE(len > TX_LEN_MAX);
  1157. opts1 = len | TX_FS | TX_LS;
  1158. if (mss) {
  1159. if (transport_offset > GTTCPHO_MAX) {
  1160. netif_warn(tp, tx_err, tp->netdev,
  1161. "Invalid transport offset 0x%x for TSO\n",
  1162. transport_offset);
  1163. ret = TX_CSUM_TSO;
  1164. goto unavailable;
  1165. }
  1166. switch (get_protocol(skb)) {
  1167. case htons(ETH_P_IP):
  1168. opts1 |= GTSENDV4;
  1169. break;
  1170. case htons(ETH_P_IPV6):
  1171. if (msdn_giant_send_check(skb)) {
  1172. ret = TX_CSUM_TSO;
  1173. goto unavailable;
  1174. }
  1175. opts1 |= GTSENDV6;
  1176. break;
  1177. default:
  1178. WARN_ON_ONCE(1);
  1179. break;
  1180. }
  1181. opts1 |= transport_offset << GTTCPHO_SHIFT;
  1182. opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
  1183. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1184. u8 ip_protocol;
  1185. if (transport_offset > TCPHO_MAX) {
  1186. netif_warn(tp, tx_err, tp->netdev,
  1187. "Invalid transport offset 0x%x\n",
  1188. transport_offset);
  1189. ret = TX_CSUM_NONE;
  1190. goto unavailable;
  1191. }
  1192. switch (get_protocol(skb)) {
  1193. case htons(ETH_P_IP):
  1194. opts2 |= IPV4_CS;
  1195. ip_protocol = ip_hdr(skb)->protocol;
  1196. break;
  1197. case htons(ETH_P_IPV6):
  1198. opts2 |= IPV6_CS;
  1199. ip_protocol = ipv6_hdr(skb)->nexthdr;
  1200. break;
  1201. default:
  1202. ip_protocol = IPPROTO_RAW;
  1203. break;
  1204. }
  1205. if (ip_protocol == IPPROTO_TCP)
  1206. opts2 |= TCP_CS;
  1207. else if (ip_protocol == IPPROTO_UDP)
  1208. opts2 |= UDP_CS;
  1209. else
  1210. WARN_ON_ONCE(1);
  1211. opts2 |= transport_offset << TCPHO_SHIFT;
  1212. }
  1213. desc->opts2 = cpu_to_le32(opts2);
  1214. desc->opts1 = cpu_to_le32(opts1);
  1215. unavailable:
  1216. return ret;
  1217. }
  1218. static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
  1219. {
  1220. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1221. int remain, ret;
  1222. u8 *tx_data;
  1223. __skb_queue_head_init(&skb_head);
  1224. spin_lock(&tx_queue->lock);
  1225. skb_queue_splice_init(tx_queue, &skb_head);
  1226. spin_unlock(&tx_queue->lock);
  1227. tx_data = agg->head;
  1228. agg->skb_num = agg->skb_len = 0;
  1229. remain = rx_buf_sz;
  1230. while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
  1231. struct tx_desc *tx_desc;
  1232. struct sk_buff *skb;
  1233. unsigned int len;
  1234. u32 offset;
  1235. skb = __skb_dequeue(&skb_head);
  1236. if (!skb)
  1237. break;
  1238. len = skb->len + sizeof(*tx_desc);
  1239. if (len > remain) {
  1240. __skb_queue_head(&skb_head, skb);
  1241. break;
  1242. }
  1243. tx_data = tx_agg_align(tx_data);
  1244. tx_desc = (struct tx_desc *)tx_data;
  1245. offset = (u32)skb_transport_offset(skb);
  1246. if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
  1247. r8152_csum_workaround(tp, skb, &skb_head);
  1248. continue;
  1249. }
  1250. tx_data += sizeof(*tx_desc);
  1251. len = skb->len;
  1252. if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
  1253. struct net_device_stats *stats = &tp->netdev->stats;
  1254. stats->tx_dropped++;
  1255. dev_kfree_skb_any(skb);
  1256. tx_data -= sizeof(*tx_desc);
  1257. continue;
  1258. }
  1259. tx_data += len;
  1260. agg->skb_len += len;
  1261. agg->skb_num++;
  1262. dev_kfree_skb_any(skb);
  1263. remain = rx_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
  1264. }
  1265. if (!skb_queue_empty(&skb_head)) {
  1266. spin_lock(&tx_queue->lock);
  1267. skb_queue_splice(&skb_head, tx_queue);
  1268. spin_unlock(&tx_queue->lock);
  1269. }
  1270. netif_tx_lock(tp->netdev);
  1271. if (netif_queue_stopped(tp->netdev) &&
  1272. skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
  1273. netif_wake_queue(tp->netdev);
  1274. netif_tx_unlock(tp->netdev);
  1275. ret = usb_autopm_get_interface_async(tp->intf);
  1276. if (ret < 0)
  1277. goto out_tx_fill;
  1278. usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  1279. agg->head, (int)(tx_data - (u8 *)agg->head),
  1280. (usb_complete_t)write_bulk_callback, agg);
  1281. ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
  1282. if (ret < 0)
  1283. usb_autopm_put_interface_async(tp->intf);
  1284. out_tx_fill:
  1285. return ret;
  1286. }
  1287. static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
  1288. {
  1289. u8 checksum = CHECKSUM_NONE;
  1290. u32 opts2, opts3;
  1291. if (tp->version == RTL_VER_01)
  1292. goto return_result;
  1293. opts2 = le32_to_cpu(rx_desc->opts2);
  1294. opts3 = le32_to_cpu(rx_desc->opts3);
  1295. if (opts2 & RD_IPV4_CS) {
  1296. if (opts3 & IPF)
  1297. checksum = CHECKSUM_NONE;
  1298. else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
  1299. checksum = CHECKSUM_NONE;
  1300. else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
  1301. checksum = CHECKSUM_NONE;
  1302. else
  1303. checksum = CHECKSUM_UNNECESSARY;
  1304. } else if (RD_IPV6_CS) {
  1305. if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
  1306. checksum = CHECKSUM_UNNECESSARY;
  1307. else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
  1308. checksum = CHECKSUM_UNNECESSARY;
  1309. }
  1310. return_result:
  1311. return checksum;
  1312. }
  1313. static void rx_bottom(struct r8152 *tp)
  1314. {
  1315. unsigned long flags;
  1316. struct list_head *cursor, *next, rx_queue;
  1317. if (list_empty(&tp->rx_done))
  1318. return;
  1319. INIT_LIST_HEAD(&rx_queue);
  1320. spin_lock_irqsave(&tp->rx_lock, flags);
  1321. list_splice_init(&tp->rx_done, &rx_queue);
  1322. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1323. list_for_each_safe(cursor, next, &rx_queue) {
  1324. struct rx_desc *rx_desc;
  1325. struct rx_agg *agg;
  1326. int len_used = 0;
  1327. struct urb *urb;
  1328. u8 *rx_data;
  1329. int ret;
  1330. list_del_init(cursor);
  1331. agg = list_entry(cursor, struct rx_agg, list);
  1332. urb = agg->urb;
  1333. if (urb->actual_length < ETH_ZLEN)
  1334. goto submit;
  1335. rx_desc = agg->head;
  1336. rx_data = agg->head;
  1337. len_used += sizeof(struct rx_desc);
  1338. while (urb->actual_length > len_used) {
  1339. struct net_device *netdev = tp->netdev;
  1340. struct net_device_stats *stats = &netdev->stats;
  1341. unsigned int pkt_len;
  1342. struct sk_buff *skb;
  1343. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  1344. if (pkt_len < ETH_ZLEN)
  1345. break;
  1346. len_used += pkt_len;
  1347. if (urb->actual_length < len_used)
  1348. break;
  1349. pkt_len -= CRC_SIZE;
  1350. rx_data += sizeof(struct rx_desc);
  1351. skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
  1352. if (!skb) {
  1353. stats->rx_dropped++;
  1354. goto find_next_rx;
  1355. }
  1356. skb->ip_summed = r8152_rx_csum(tp, rx_desc);
  1357. memcpy(skb->data, rx_data, pkt_len);
  1358. skb_put(skb, pkt_len);
  1359. skb->protocol = eth_type_trans(skb, netdev);
  1360. netif_receive_skb(skb);
  1361. stats->rx_packets++;
  1362. stats->rx_bytes += pkt_len;
  1363. find_next_rx:
  1364. rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
  1365. rx_desc = (struct rx_desc *)rx_data;
  1366. len_used = (int)(rx_data - (u8 *)agg->head);
  1367. len_used += sizeof(struct rx_desc);
  1368. }
  1369. submit:
  1370. ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  1371. if (ret && ret != -ENODEV) {
  1372. spin_lock_irqsave(&tp->rx_lock, flags);
  1373. list_add_tail(&agg->list, &tp->rx_done);
  1374. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1375. tasklet_schedule(&tp->tl);
  1376. }
  1377. }
  1378. }
  1379. static void tx_bottom(struct r8152 *tp)
  1380. {
  1381. int res;
  1382. do {
  1383. struct tx_agg *agg;
  1384. if (skb_queue_empty(&tp->tx_queue))
  1385. break;
  1386. agg = r8152_get_tx_agg(tp);
  1387. if (!agg)
  1388. break;
  1389. res = r8152_tx_agg_fill(tp, agg);
  1390. if (res) {
  1391. struct net_device *netdev = tp->netdev;
  1392. if (res == -ENODEV) {
  1393. netif_device_detach(netdev);
  1394. } else {
  1395. struct net_device_stats *stats = &netdev->stats;
  1396. unsigned long flags;
  1397. netif_warn(tp, tx_err, netdev,
  1398. "failed tx_urb %d\n", res);
  1399. stats->tx_dropped += agg->skb_num;
  1400. spin_lock_irqsave(&tp->tx_lock, flags);
  1401. list_add_tail(&agg->list, &tp->tx_free);
  1402. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1403. }
  1404. }
  1405. } while (res == 0);
  1406. }
  1407. static void bottom_half(unsigned long data)
  1408. {
  1409. struct r8152 *tp;
  1410. tp = (struct r8152 *)data;
  1411. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1412. return;
  1413. if (!test_bit(WORK_ENABLE, &tp->flags))
  1414. return;
  1415. /* When link down, the driver would cancel all bulks. */
  1416. /* This avoid the re-submitting bulk */
  1417. if (!netif_carrier_ok(tp->netdev))
  1418. return;
  1419. rx_bottom(tp);
  1420. tx_bottom(tp);
  1421. }
  1422. static
  1423. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
  1424. {
  1425. usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  1426. agg->head, rx_buf_sz,
  1427. (usb_complete_t)read_bulk_callback, agg);
  1428. return usb_submit_urb(agg->urb, mem_flags);
  1429. }
  1430. static void rtl_drop_queued_tx(struct r8152 *tp)
  1431. {
  1432. struct net_device_stats *stats = &tp->netdev->stats;
  1433. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1434. struct sk_buff *skb;
  1435. if (skb_queue_empty(tx_queue))
  1436. return;
  1437. __skb_queue_head_init(&skb_head);
  1438. spin_lock_bh(&tx_queue->lock);
  1439. skb_queue_splice_init(tx_queue, &skb_head);
  1440. spin_unlock_bh(&tx_queue->lock);
  1441. while ((skb = __skb_dequeue(&skb_head))) {
  1442. dev_kfree_skb(skb);
  1443. stats->tx_dropped++;
  1444. }
  1445. }
  1446. static void rtl8152_tx_timeout(struct net_device *netdev)
  1447. {
  1448. struct r8152 *tp = netdev_priv(netdev);
  1449. int i;
  1450. netif_warn(tp, tx_err, netdev, "Tx timeout\n");
  1451. for (i = 0; i < RTL8152_MAX_TX; i++)
  1452. usb_unlink_urb(tp->tx_info[i].urb);
  1453. }
  1454. static void rtl8152_set_rx_mode(struct net_device *netdev)
  1455. {
  1456. struct r8152 *tp = netdev_priv(netdev);
  1457. if (tp->speed & LINK_STATUS) {
  1458. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1459. schedule_delayed_work(&tp->schedule, 0);
  1460. }
  1461. }
  1462. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  1463. {
  1464. struct r8152 *tp = netdev_priv(netdev);
  1465. u32 mc_filter[2]; /* Multicast hash filter */
  1466. __le32 tmp[2];
  1467. u32 ocp_data;
  1468. clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1469. netif_stop_queue(netdev);
  1470. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1471. ocp_data &= ~RCR_ACPT_ALL;
  1472. ocp_data |= RCR_AB | RCR_APM;
  1473. if (netdev->flags & IFF_PROMISC) {
  1474. /* Unconditionally log net taps. */
  1475. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  1476. ocp_data |= RCR_AM | RCR_AAP;
  1477. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1478. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  1479. (netdev->flags & IFF_ALLMULTI)) {
  1480. /* Too many to filter perfectly -- accept all multicasts. */
  1481. ocp_data |= RCR_AM;
  1482. mc_filter[1] = mc_filter[0] = 0xffffffff;
  1483. } else {
  1484. struct netdev_hw_addr *ha;
  1485. mc_filter[1] = mc_filter[0] = 0;
  1486. netdev_for_each_mc_addr(ha, netdev) {
  1487. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1488. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1489. ocp_data |= RCR_AM;
  1490. }
  1491. }
  1492. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  1493. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  1494. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  1495. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1496. netif_wake_queue(netdev);
  1497. }
  1498. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  1499. struct net_device *netdev)
  1500. {
  1501. struct r8152 *tp = netdev_priv(netdev);
  1502. skb_tx_timestamp(skb);
  1503. skb_queue_tail(&tp->tx_queue, skb);
  1504. if (!list_empty(&tp->tx_free)) {
  1505. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  1506. set_bit(SCHEDULE_TASKLET, &tp->flags);
  1507. schedule_delayed_work(&tp->schedule, 0);
  1508. } else {
  1509. usb_mark_last_busy(tp->udev);
  1510. tasklet_schedule(&tp->tl);
  1511. }
  1512. } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen)
  1513. netif_stop_queue(netdev);
  1514. return NETDEV_TX_OK;
  1515. }
  1516. static void r8152b_reset_packet_filter(struct r8152 *tp)
  1517. {
  1518. u32 ocp_data;
  1519. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  1520. ocp_data &= ~FMC_FCR_MCU_EN;
  1521. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1522. ocp_data |= FMC_FCR_MCU_EN;
  1523. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1524. }
  1525. static void rtl8152_nic_reset(struct r8152 *tp)
  1526. {
  1527. int i;
  1528. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  1529. for (i = 0; i < 1000; i++) {
  1530. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  1531. break;
  1532. udelay(100);
  1533. }
  1534. }
  1535. static void set_tx_qlen(struct r8152 *tp)
  1536. {
  1537. struct net_device *netdev = tp->netdev;
  1538. tp->tx_qlen = rx_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
  1539. sizeof(struct tx_desc));
  1540. }
  1541. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  1542. {
  1543. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  1544. }
  1545. static void rtl_set_eee_plus(struct r8152 *tp)
  1546. {
  1547. u32 ocp_data;
  1548. u8 speed;
  1549. speed = rtl8152_get_speed(tp);
  1550. if (speed & _10bps) {
  1551. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1552. ocp_data |= EEEP_CR_EEEP_TX;
  1553. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1554. } else {
  1555. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1556. ocp_data &= ~EEEP_CR_EEEP_TX;
  1557. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1558. }
  1559. }
  1560. static void rxdy_gated_en(struct r8152 *tp, bool enable)
  1561. {
  1562. u32 ocp_data;
  1563. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1564. if (enable)
  1565. ocp_data |= RXDY_GATED_EN;
  1566. else
  1567. ocp_data &= ~RXDY_GATED_EN;
  1568. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1569. }
  1570. static int rtl_enable(struct r8152 *tp)
  1571. {
  1572. u32 ocp_data;
  1573. int i, ret;
  1574. r8152b_reset_packet_filter(tp);
  1575. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  1576. ocp_data |= CR_RE | CR_TE;
  1577. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  1578. rxdy_gated_en(tp, false);
  1579. INIT_LIST_HEAD(&tp->rx_done);
  1580. ret = 0;
  1581. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1582. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1583. ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
  1584. }
  1585. return ret;
  1586. }
  1587. static int rtl8152_enable(struct r8152 *tp)
  1588. {
  1589. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1590. return -ENODEV;
  1591. set_tx_qlen(tp);
  1592. rtl_set_eee_plus(tp);
  1593. return rtl_enable(tp);
  1594. }
  1595. static void r8153_set_rx_agg(struct r8152 *tp)
  1596. {
  1597. u8 speed;
  1598. speed = rtl8152_get_speed(tp);
  1599. if (speed & _1000bps) {
  1600. if (tp->udev->speed == USB_SPEED_SUPER) {
  1601. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
  1602. RX_THR_SUPPER);
  1603. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1604. EARLY_AGG_SUPPER);
  1605. } else {
  1606. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
  1607. RX_THR_HIGH);
  1608. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1609. EARLY_AGG_HIGH);
  1610. }
  1611. } else {
  1612. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
  1613. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
  1614. EARLY_AGG_SLOW);
  1615. }
  1616. }
  1617. static int rtl8153_enable(struct r8152 *tp)
  1618. {
  1619. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1620. return -ENODEV;
  1621. set_tx_qlen(tp);
  1622. rtl_set_eee_plus(tp);
  1623. r8153_set_rx_agg(tp);
  1624. return rtl_enable(tp);
  1625. }
  1626. static void rtl8152_disable(struct r8152 *tp)
  1627. {
  1628. u32 ocp_data;
  1629. int i;
  1630. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  1631. rtl_drop_queued_tx(tp);
  1632. return;
  1633. }
  1634. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1635. ocp_data &= ~RCR_ACPT_ALL;
  1636. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1637. rtl_drop_queued_tx(tp);
  1638. for (i = 0; i < RTL8152_MAX_TX; i++)
  1639. usb_kill_urb(tp->tx_info[i].urb);
  1640. rxdy_gated_en(tp, true);
  1641. for (i = 0; i < 1000; i++) {
  1642. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1643. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  1644. break;
  1645. mdelay(1);
  1646. }
  1647. for (i = 0; i < 1000; i++) {
  1648. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  1649. break;
  1650. mdelay(1);
  1651. }
  1652. for (i = 0; i < RTL8152_MAX_RX; i++)
  1653. usb_kill_urb(tp->rx_info[i].urb);
  1654. rtl8152_nic_reset(tp);
  1655. }
  1656. static void r8152_power_cut_en(struct r8152 *tp, bool enable)
  1657. {
  1658. u32 ocp_data;
  1659. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1660. if (enable)
  1661. ocp_data |= POWER_CUT;
  1662. else
  1663. ocp_data &= ~POWER_CUT;
  1664. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1665. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1666. ocp_data &= ~RESUME_INDICATE;
  1667. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1668. }
  1669. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1670. static u32 __rtl_get_wol(struct r8152 *tp)
  1671. {
  1672. u32 ocp_data;
  1673. u32 wolopts = 0;
  1674. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1675. if (!(ocp_data & LAN_WAKE_EN))
  1676. return 0;
  1677. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1678. if (ocp_data & LINK_ON_WAKE_EN)
  1679. wolopts |= WAKE_PHY;
  1680. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1681. if (ocp_data & UWF_EN)
  1682. wolopts |= WAKE_UCAST;
  1683. if (ocp_data & BWF_EN)
  1684. wolopts |= WAKE_BCAST;
  1685. if (ocp_data & MWF_EN)
  1686. wolopts |= WAKE_MCAST;
  1687. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1688. if (ocp_data & MAGIC_EN)
  1689. wolopts |= WAKE_MAGIC;
  1690. return wolopts;
  1691. }
  1692. static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
  1693. {
  1694. u32 ocp_data;
  1695. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1696. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1697. ocp_data &= ~LINK_ON_WAKE_EN;
  1698. if (wolopts & WAKE_PHY)
  1699. ocp_data |= LINK_ON_WAKE_EN;
  1700. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1701. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1702. ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
  1703. if (wolopts & WAKE_UCAST)
  1704. ocp_data |= UWF_EN;
  1705. if (wolopts & WAKE_BCAST)
  1706. ocp_data |= BWF_EN;
  1707. if (wolopts & WAKE_MCAST)
  1708. ocp_data |= MWF_EN;
  1709. if (wolopts & WAKE_ANY)
  1710. ocp_data |= LAN_WAKE_EN;
  1711. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
  1712. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1713. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1714. ocp_data &= ~MAGIC_EN;
  1715. if (wolopts & WAKE_MAGIC)
  1716. ocp_data |= MAGIC_EN;
  1717. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  1718. if (wolopts & WAKE_ANY)
  1719. device_set_wakeup_enable(&tp->udev->dev, true);
  1720. else
  1721. device_set_wakeup_enable(&tp->udev->dev, false);
  1722. }
  1723. static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
  1724. {
  1725. if (enable) {
  1726. u32 ocp_data;
  1727. __rtl_set_wol(tp, WAKE_ANY);
  1728. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1729. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1730. ocp_data |= LINK_OFF_WAKE_EN;
  1731. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1732. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1733. } else {
  1734. __rtl_set_wol(tp, tp->saved_wolopts);
  1735. }
  1736. }
  1737. static void rtl_phy_reset(struct r8152 *tp)
  1738. {
  1739. u16 data;
  1740. int i;
  1741. clear_bit(PHY_RESET, &tp->flags);
  1742. data = r8152_mdio_read(tp, MII_BMCR);
  1743. /* don't reset again before the previous one complete */
  1744. if (data & BMCR_RESET)
  1745. return;
  1746. data |= BMCR_RESET;
  1747. r8152_mdio_write(tp, MII_BMCR, data);
  1748. for (i = 0; i < 50; i++) {
  1749. msleep(20);
  1750. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  1751. break;
  1752. }
  1753. }
  1754. static void rtl_clear_bp(struct r8152 *tp)
  1755. {
  1756. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
  1757. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
  1758. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
  1759. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
  1760. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
  1761. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
  1762. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
  1763. ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
  1764. mdelay(3);
  1765. ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
  1766. ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
  1767. }
  1768. static void r8153_clear_bp(struct r8152 *tp)
  1769. {
  1770. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
  1771. ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
  1772. rtl_clear_bp(tp);
  1773. }
  1774. static void r8153_teredo_off(struct r8152 *tp)
  1775. {
  1776. u32 ocp_data;
  1777. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  1778. ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
  1779. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  1780. ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
  1781. ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
  1782. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
  1783. }
  1784. static void r8152b_disable_aldps(struct r8152 *tp)
  1785. {
  1786. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
  1787. msleep(20);
  1788. }
  1789. static inline void r8152b_enable_aldps(struct r8152 *tp)
  1790. {
  1791. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  1792. LINKENA | DIS_SDSAVE);
  1793. }
  1794. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  1795. {
  1796. u16 data;
  1797. data = r8152_mdio_read(tp, MII_BMCR);
  1798. if (data & BMCR_PDOWN) {
  1799. data &= ~BMCR_PDOWN;
  1800. r8152_mdio_write(tp, MII_BMCR, data);
  1801. }
  1802. r8152b_disable_aldps(tp);
  1803. rtl_clear_bp(tp);
  1804. r8152b_enable_aldps(tp);
  1805. set_bit(PHY_RESET, &tp->flags);
  1806. }
  1807. static void r8152b_exit_oob(struct r8152 *tp)
  1808. {
  1809. u32 ocp_data;
  1810. int i;
  1811. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1812. return;
  1813. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1814. ocp_data &= ~RCR_ACPT_ALL;
  1815. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1816. rxdy_gated_en(tp, true);
  1817. r8153_teredo_off(tp);
  1818. r8152b_hw_phy_cfg(tp);
  1819. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1820. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  1821. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1822. ocp_data &= ~NOW_IS_OOB;
  1823. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1824. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1825. ocp_data &= ~MCU_BORW_EN;
  1826. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1827. for (i = 0; i < 1000; i++) {
  1828. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1829. if (ocp_data & LINK_LIST_READY)
  1830. break;
  1831. mdelay(1);
  1832. }
  1833. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1834. ocp_data |= RE_INIT_LL;
  1835. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1836. for (i = 0; i < 1000; i++) {
  1837. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1838. if (ocp_data & LINK_LIST_READY)
  1839. break;
  1840. mdelay(1);
  1841. }
  1842. rtl8152_nic_reset(tp);
  1843. /* rx share fifo credit full threshold */
  1844. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  1845. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
  1846. ocp_data &= STAT_SPEED_MASK;
  1847. if (ocp_data == STAT_SPEED_FULL) {
  1848. /* rx share fifo credit near full threshold */
  1849. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  1850. RXFIFO_THR2_FULL);
  1851. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  1852. RXFIFO_THR3_FULL);
  1853. } else {
  1854. /* rx share fifo credit near full threshold */
  1855. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  1856. RXFIFO_THR2_HIGH);
  1857. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  1858. RXFIFO_THR3_HIGH);
  1859. }
  1860. /* TX share fifo free credit full threshold */
  1861. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  1862. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  1863. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
  1864. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  1865. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  1866. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1867. ocp_data &= ~CPCR_RX_VLAN;
  1868. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1869. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1870. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  1871. ocp_data |= TCR0_AUTO_FIFO;
  1872. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  1873. }
  1874. static void r8152b_enter_oob(struct r8152 *tp)
  1875. {
  1876. u32 ocp_data;
  1877. int i;
  1878. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1879. ocp_data &= ~NOW_IS_OOB;
  1880. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1881. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  1882. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  1883. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  1884. rtl8152_disable(tp);
  1885. for (i = 0; i < 1000; i++) {
  1886. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1887. if (ocp_data & LINK_LIST_READY)
  1888. break;
  1889. mdelay(1);
  1890. }
  1891. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  1892. ocp_data |= RE_INIT_LL;
  1893. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  1894. for (i = 0; i < 1000; i++) {
  1895. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1896. if (ocp_data & LINK_LIST_READY)
  1897. break;
  1898. mdelay(1);
  1899. }
  1900. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  1901. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1902. ocp_data |= CPCR_RX_VLAN;
  1903. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1904. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  1905. ocp_data |= ALDPS_PROXY_MODE;
  1906. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  1907. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1908. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  1909. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  1910. rxdy_gated_en(tp, false);
  1911. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1912. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  1913. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1914. }
  1915. static void r8153_hw_phy_cfg(struct r8152 *tp)
  1916. {
  1917. u32 ocp_data;
  1918. u16 data;
  1919. ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
  1920. data = r8152_mdio_read(tp, MII_BMCR);
  1921. if (data & BMCR_PDOWN) {
  1922. data &= ~BMCR_PDOWN;
  1923. r8152_mdio_write(tp, MII_BMCR, data);
  1924. }
  1925. r8153_clear_bp(tp);
  1926. if (tp->version == RTL_VER_03) {
  1927. data = ocp_reg_read(tp, OCP_EEE_CFG);
  1928. data &= ~CTAP_SHORT_EN;
  1929. ocp_reg_write(tp, OCP_EEE_CFG, data);
  1930. }
  1931. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1932. data |= EEE_CLKDIV_EN;
  1933. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1934. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  1935. data |= EN_10M_BGOFF;
  1936. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  1937. data = ocp_reg_read(tp, OCP_POWER_CFG);
  1938. data |= EN_10M_PLLOFF;
  1939. ocp_reg_write(tp, OCP_POWER_CFG, data);
  1940. data = sram_read(tp, SRAM_IMPEDANCE);
  1941. data &= ~RX_DRIVING_MASK;
  1942. sram_write(tp, SRAM_IMPEDANCE, data);
  1943. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  1944. ocp_data |= PFM_PWM_SWITCH;
  1945. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  1946. data = sram_read(tp, SRAM_LPF_CFG);
  1947. data |= LPF_AUTO_TUNE;
  1948. sram_write(tp, SRAM_LPF_CFG, data);
  1949. data = sram_read(tp, SRAM_10M_AMP1);
  1950. data |= GDAC_IB_UPALL;
  1951. sram_write(tp, SRAM_10M_AMP1, data);
  1952. data = sram_read(tp, SRAM_10M_AMP2);
  1953. data |= AMP_DN;
  1954. sram_write(tp, SRAM_10M_AMP2, data);
  1955. set_bit(PHY_RESET, &tp->flags);
  1956. }
  1957. static void r8153_u1u2en(struct r8152 *tp, bool enable)
  1958. {
  1959. u8 u1u2[8];
  1960. if (enable)
  1961. memset(u1u2, 0xff, sizeof(u1u2));
  1962. else
  1963. memset(u1u2, 0x00, sizeof(u1u2));
  1964. usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
  1965. }
  1966. static void r8153_u2p3en(struct r8152 *tp, bool enable)
  1967. {
  1968. u32 ocp_data;
  1969. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  1970. if (enable)
  1971. ocp_data |= U2P3_ENABLE;
  1972. else
  1973. ocp_data &= ~U2P3_ENABLE;
  1974. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  1975. }
  1976. static void r8153_power_cut_en(struct r8152 *tp, bool enable)
  1977. {
  1978. u32 ocp_data;
  1979. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  1980. if (enable)
  1981. ocp_data |= PWR_EN | PHASE2_EN;
  1982. else
  1983. ocp_data &= ~(PWR_EN | PHASE2_EN);
  1984. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  1985. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  1986. ocp_data &= ~PCUT_STATUS;
  1987. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  1988. }
  1989. static void r8153_first_init(struct r8152 *tp)
  1990. {
  1991. u32 ocp_data;
  1992. int i;
  1993. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1994. return;
  1995. rxdy_gated_en(tp, true);
  1996. r8153_teredo_off(tp);
  1997. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1998. ocp_data &= ~RCR_ACPT_ALL;
  1999. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2000. r8153_hw_phy_cfg(tp);
  2001. rtl8152_nic_reset(tp);
  2002. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2003. ocp_data &= ~NOW_IS_OOB;
  2004. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2005. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2006. ocp_data &= ~MCU_BORW_EN;
  2007. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2008. for (i = 0; i < 1000; i++) {
  2009. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2010. if (ocp_data & LINK_LIST_READY)
  2011. break;
  2012. mdelay(1);
  2013. }
  2014. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2015. ocp_data |= RE_INIT_LL;
  2016. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2017. for (i = 0; i < 1000; i++) {
  2018. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2019. if (ocp_data & LINK_LIST_READY)
  2020. break;
  2021. mdelay(1);
  2022. }
  2023. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  2024. ocp_data &= ~CPCR_RX_VLAN;
  2025. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  2026. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2027. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  2028. ocp_data |= TCR0_AUTO_FIFO;
  2029. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  2030. rtl8152_nic_reset(tp);
  2031. /* rx share fifo credit full threshold */
  2032. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  2033. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  2034. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  2035. /* TX share fifo free credit full threshold */
  2036. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  2037. /* rx aggregation */
  2038. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2039. ocp_data &= ~RX_AGG_DISABLE;
  2040. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2041. }
  2042. static void r8153_enter_oob(struct r8152 *tp)
  2043. {
  2044. u32 ocp_data;
  2045. int i;
  2046. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2047. ocp_data &= ~NOW_IS_OOB;
  2048. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2049. rtl8152_disable(tp);
  2050. for (i = 0; i < 1000; i++) {
  2051. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2052. if (ocp_data & LINK_LIST_READY)
  2053. break;
  2054. mdelay(1);
  2055. }
  2056. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2057. ocp_data |= RE_INIT_LL;
  2058. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2059. for (i = 0; i < 1000; i++) {
  2060. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2061. if (ocp_data & LINK_LIST_READY)
  2062. break;
  2063. mdelay(1);
  2064. }
  2065. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2066. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  2067. ocp_data &= ~TEREDO_WAKE_MASK;
  2068. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  2069. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  2070. ocp_data |= CPCR_RX_VLAN;
  2071. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  2072. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  2073. ocp_data |= ALDPS_PROXY_MODE;
  2074. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  2075. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2076. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  2077. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2078. rxdy_gated_en(tp, false);
  2079. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2080. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  2081. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2082. }
  2083. static void r8153_disable_aldps(struct r8152 *tp)
  2084. {
  2085. u16 data;
  2086. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2087. data &= ~EN_ALDPS;
  2088. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2089. msleep(20);
  2090. }
  2091. static void r8153_enable_aldps(struct r8152 *tp)
  2092. {
  2093. u16 data;
  2094. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2095. data |= EN_ALDPS;
  2096. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2097. }
  2098. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  2099. {
  2100. u16 bmcr, anar, gbcr;
  2101. int ret = 0;
  2102. cancel_delayed_work_sync(&tp->schedule);
  2103. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2104. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  2105. ADVERTISE_100HALF | ADVERTISE_100FULL);
  2106. if (tp->mii.supports_gmii) {
  2107. gbcr = r8152_mdio_read(tp, MII_CTRL1000);
  2108. gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  2109. } else {
  2110. gbcr = 0;
  2111. }
  2112. if (autoneg == AUTONEG_DISABLE) {
  2113. if (speed == SPEED_10) {
  2114. bmcr = 0;
  2115. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2116. } else if (speed == SPEED_100) {
  2117. bmcr = BMCR_SPEED100;
  2118. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2119. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2120. bmcr = BMCR_SPEED1000;
  2121. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2122. } else {
  2123. ret = -EINVAL;
  2124. goto out;
  2125. }
  2126. if (duplex == DUPLEX_FULL)
  2127. bmcr |= BMCR_FULLDPLX;
  2128. } else {
  2129. if (speed == SPEED_10) {
  2130. if (duplex == DUPLEX_FULL)
  2131. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2132. else
  2133. anar |= ADVERTISE_10HALF;
  2134. } else if (speed == SPEED_100) {
  2135. if (duplex == DUPLEX_FULL) {
  2136. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2137. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2138. } else {
  2139. anar |= ADVERTISE_10HALF;
  2140. anar |= ADVERTISE_100HALF;
  2141. }
  2142. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2143. if (duplex == DUPLEX_FULL) {
  2144. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2145. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2146. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2147. } else {
  2148. anar |= ADVERTISE_10HALF;
  2149. anar |= ADVERTISE_100HALF;
  2150. gbcr |= ADVERTISE_1000HALF;
  2151. }
  2152. } else {
  2153. ret = -EINVAL;
  2154. goto out;
  2155. }
  2156. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  2157. }
  2158. if (test_bit(PHY_RESET, &tp->flags))
  2159. bmcr |= BMCR_RESET;
  2160. if (tp->mii.supports_gmii)
  2161. r8152_mdio_write(tp, MII_CTRL1000, gbcr);
  2162. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2163. r8152_mdio_write(tp, MII_BMCR, bmcr);
  2164. if (test_bit(PHY_RESET, &tp->flags)) {
  2165. int i;
  2166. clear_bit(PHY_RESET, &tp->flags);
  2167. for (i = 0; i < 50; i++) {
  2168. msleep(20);
  2169. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  2170. break;
  2171. }
  2172. }
  2173. out:
  2174. return ret;
  2175. }
  2176. static void rtl8152_down(struct r8152 *tp)
  2177. {
  2178. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2179. rtl_drop_queued_tx(tp);
  2180. return;
  2181. }
  2182. r8152_power_cut_en(tp, false);
  2183. r8152b_disable_aldps(tp);
  2184. r8152b_enter_oob(tp);
  2185. r8152b_enable_aldps(tp);
  2186. }
  2187. static void rtl8153_down(struct r8152 *tp)
  2188. {
  2189. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2190. rtl_drop_queued_tx(tp);
  2191. return;
  2192. }
  2193. r8153_u1u2en(tp, false);
  2194. r8153_power_cut_en(tp, false);
  2195. r8153_disable_aldps(tp);
  2196. r8153_enter_oob(tp);
  2197. r8153_enable_aldps(tp);
  2198. }
  2199. static void set_carrier(struct r8152 *tp)
  2200. {
  2201. struct net_device *netdev = tp->netdev;
  2202. u8 speed;
  2203. clear_bit(RTL8152_LINK_CHG, &tp->flags);
  2204. speed = rtl8152_get_speed(tp);
  2205. if (speed & LINK_STATUS) {
  2206. if (!(tp->speed & LINK_STATUS)) {
  2207. tp->rtl_ops.enable(tp);
  2208. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  2209. netif_carrier_on(netdev);
  2210. }
  2211. } else {
  2212. if (tp->speed & LINK_STATUS) {
  2213. netif_carrier_off(netdev);
  2214. tasklet_disable(&tp->tl);
  2215. tp->rtl_ops.disable(tp);
  2216. tasklet_enable(&tp->tl);
  2217. }
  2218. }
  2219. tp->speed = speed;
  2220. }
  2221. static void rtl_work_func_t(struct work_struct *work)
  2222. {
  2223. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  2224. if (usb_autopm_get_interface(tp->intf) < 0)
  2225. return;
  2226. if (!test_bit(WORK_ENABLE, &tp->flags))
  2227. goto out1;
  2228. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2229. goto out1;
  2230. if (test_bit(RTL8152_LINK_CHG, &tp->flags))
  2231. set_carrier(tp);
  2232. if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
  2233. _rtl8152_set_rx_mode(tp->netdev);
  2234. if (test_bit(SCHEDULE_TASKLET, &tp->flags) &&
  2235. (tp->speed & LINK_STATUS)) {
  2236. clear_bit(SCHEDULE_TASKLET, &tp->flags);
  2237. tasklet_schedule(&tp->tl);
  2238. }
  2239. if (test_bit(PHY_RESET, &tp->flags))
  2240. rtl_phy_reset(tp);
  2241. out1:
  2242. usb_autopm_put_interface(tp->intf);
  2243. }
  2244. static int rtl8152_open(struct net_device *netdev)
  2245. {
  2246. struct r8152 *tp = netdev_priv(netdev);
  2247. int res = 0;
  2248. res = alloc_all_mem(tp);
  2249. if (res)
  2250. goto out;
  2251. res = usb_autopm_get_interface(tp->intf);
  2252. if (res < 0) {
  2253. free_all_mem(tp);
  2254. goto out;
  2255. }
  2256. /* The WORK_ENABLE may be set when autoresume occurs */
  2257. if (test_bit(WORK_ENABLE, &tp->flags)) {
  2258. clear_bit(WORK_ENABLE, &tp->flags);
  2259. usb_kill_urb(tp->intr_urb);
  2260. cancel_delayed_work_sync(&tp->schedule);
  2261. if (tp->speed & LINK_STATUS)
  2262. tp->rtl_ops.disable(tp);
  2263. }
  2264. tp->rtl_ops.up(tp);
  2265. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  2266. tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
  2267. DUPLEX_FULL);
  2268. tp->speed = 0;
  2269. netif_carrier_off(netdev);
  2270. netif_start_queue(netdev);
  2271. set_bit(WORK_ENABLE, &tp->flags);
  2272. res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2273. if (res) {
  2274. if (res == -ENODEV)
  2275. netif_device_detach(tp->netdev);
  2276. netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
  2277. res);
  2278. free_all_mem(tp);
  2279. }
  2280. usb_autopm_put_interface(tp->intf);
  2281. out:
  2282. return res;
  2283. }
  2284. static int rtl8152_close(struct net_device *netdev)
  2285. {
  2286. struct r8152 *tp = netdev_priv(netdev);
  2287. int res = 0;
  2288. clear_bit(WORK_ENABLE, &tp->flags);
  2289. usb_kill_urb(tp->intr_urb);
  2290. cancel_delayed_work_sync(&tp->schedule);
  2291. netif_stop_queue(netdev);
  2292. res = usb_autopm_get_interface(tp->intf);
  2293. if (res < 0) {
  2294. rtl_drop_queued_tx(tp);
  2295. } else {
  2296. /*
  2297. * The autosuspend may have been enabled and wouldn't
  2298. * be disable when autoresume occurs, because the
  2299. * netif_running() would be false.
  2300. */
  2301. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2302. rtl_runtime_suspend_enable(tp, false);
  2303. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2304. }
  2305. tasklet_disable(&tp->tl);
  2306. tp->rtl_ops.down(tp);
  2307. tasklet_enable(&tp->tl);
  2308. usb_autopm_put_interface(tp->intf);
  2309. }
  2310. free_all_mem(tp);
  2311. return res;
  2312. }
  2313. static void r8152b_enable_eee(struct r8152 *tp)
  2314. {
  2315. u32 ocp_data;
  2316. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2317. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2318. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2319. ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
  2320. EEE_10_CAP | EEE_NWAY_EN |
  2321. TX_QUIET_EN | RX_QUIET_EN |
  2322. SDRISETIME | RG_RXLPI_MSK_HFDUP |
  2323. SDFALLTIME);
  2324. ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
  2325. RG_LDVQUIET_EN | RG_CKRSEL |
  2326. RG_EEEPRG_EN);
  2327. ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
  2328. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
  2329. ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
  2330. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
  2331. ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
  2332. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  2333. }
  2334. static void r8153_enable_eee(struct r8152 *tp)
  2335. {
  2336. u32 ocp_data;
  2337. u16 data;
  2338. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2339. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2340. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2341. data = ocp_reg_read(tp, OCP_EEE_CFG);
  2342. data |= EEE10_EN;
  2343. ocp_reg_write(tp, OCP_EEE_CFG, data);
  2344. data = ocp_reg_read(tp, OCP_EEE_CFG2);
  2345. data |= MY1000_EEE | MY100_EEE;
  2346. ocp_reg_write(tp, OCP_EEE_CFG2, data);
  2347. }
  2348. static void r8152b_enable_fc(struct r8152 *tp)
  2349. {
  2350. u16 anar;
  2351. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2352. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  2353. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2354. }
  2355. static void rtl_tally_reset(struct r8152 *tp)
  2356. {
  2357. u32 ocp_data;
  2358. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
  2359. ocp_data |= TALLY_RESET;
  2360. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
  2361. }
  2362. static void r8152b_init(struct r8152 *tp)
  2363. {
  2364. u32 ocp_data;
  2365. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2366. return;
  2367. if (tp->version == RTL_VER_01) {
  2368. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2369. ocp_data &= ~LED_MODE_MASK;
  2370. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2371. }
  2372. r8152_power_cut_en(tp, false);
  2373. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  2374. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  2375. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  2376. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  2377. ocp_data &= ~MCU_CLK_RATIO_MASK;
  2378. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  2379. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  2380. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  2381. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  2382. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  2383. r8152b_enable_eee(tp);
  2384. r8152b_enable_aldps(tp);
  2385. r8152b_enable_fc(tp);
  2386. rtl_tally_reset(tp);
  2387. /* enable rx aggregation */
  2388. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2389. ocp_data &= ~RX_AGG_DISABLE;
  2390. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2391. }
  2392. static void r8153_init(struct r8152 *tp)
  2393. {
  2394. u32 ocp_data;
  2395. int i;
  2396. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2397. return;
  2398. r8153_u1u2en(tp, false);
  2399. for (i = 0; i < 500; i++) {
  2400. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  2401. AUTOLOAD_DONE)
  2402. break;
  2403. msleep(20);
  2404. }
  2405. for (i = 0; i < 500; i++) {
  2406. ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
  2407. if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
  2408. break;
  2409. msleep(20);
  2410. }
  2411. r8153_u2p3en(tp, false);
  2412. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
  2413. ocp_data &= ~TIMER11_EN;
  2414. ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
  2415. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2416. ocp_data &= ~LED_MODE_MASK;
  2417. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2418. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
  2419. ocp_data &= ~LPM_TIMER_MASK;
  2420. if (tp->udev->speed == USB_SPEED_SUPER)
  2421. ocp_data |= LPM_TIMER_500US;
  2422. else
  2423. ocp_data |= LPM_TIMER_500MS;
  2424. ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
  2425. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
  2426. ocp_data &= ~SEN_VAL_MASK;
  2427. ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
  2428. ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
  2429. r8153_power_cut_en(tp, false);
  2430. r8153_u1u2en(tp, true);
  2431. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
  2432. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
  2433. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
  2434. PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
  2435. U1U2_SPDWN_EN | L1_SPDWN_EN);
  2436. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
  2437. PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
  2438. TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
  2439. EEE_SPDWN_EN);
  2440. r8153_enable_eee(tp);
  2441. r8153_enable_aldps(tp);
  2442. r8152b_enable_fc(tp);
  2443. rtl_tally_reset(tp);
  2444. }
  2445. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  2446. {
  2447. struct r8152 *tp = usb_get_intfdata(intf);
  2448. if (PMSG_IS_AUTO(message))
  2449. set_bit(SELECTIVE_SUSPEND, &tp->flags);
  2450. else
  2451. netif_device_detach(tp->netdev);
  2452. if (netif_running(tp->netdev)) {
  2453. clear_bit(WORK_ENABLE, &tp->flags);
  2454. usb_kill_urb(tp->intr_urb);
  2455. cancel_delayed_work_sync(&tp->schedule);
  2456. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2457. rtl_runtime_suspend_enable(tp, true);
  2458. } else {
  2459. tasklet_disable(&tp->tl);
  2460. tp->rtl_ops.down(tp);
  2461. tasklet_enable(&tp->tl);
  2462. }
  2463. }
  2464. return 0;
  2465. }
  2466. static int rtl8152_resume(struct usb_interface *intf)
  2467. {
  2468. struct r8152 *tp = usb_get_intfdata(intf);
  2469. if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2470. tp->rtl_ops.init(tp);
  2471. netif_device_attach(tp->netdev);
  2472. }
  2473. if (netif_running(tp->netdev)) {
  2474. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2475. rtl_runtime_suspend_enable(tp, false);
  2476. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2477. if (tp->speed & LINK_STATUS)
  2478. tp->rtl_ops.disable(tp);
  2479. } else {
  2480. tp->rtl_ops.up(tp);
  2481. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  2482. tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
  2483. DUPLEX_FULL);
  2484. }
  2485. tp->speed = 0;
  2486. netif_carrier_off(tp->netdev);
  2487. set_bit(WORK_ENABLE, &tp->flags);
  2488. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2489. }
  2490. return 0;
  2491. }
  2492. static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2493. {
  2494. struct r8152 *tp = netdev_priv(dev);
  2495. if (usb_autopm_get_interface(tp->intf) < 0)
  2496. return;
  2497. wol->supported = WAKE_ANY;
  2498. wol->wolopts = __rtl_get_wol(tp);
  2499. usb_autopm_put_interface(tp->intf);
  2500. }
  2501. static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2502. {
  2503. struct r8152 *tp = netdev_priv(dev);
  2504. int ret;
  2505. ret = usb_autopm_get_interface(tp->intf);
  2506. if (ret < 0)
  2507. goto out_set_wol;
  2508. __rtl_set_wol(tp, wol->wolopts);
  2509. tp->saved_wolopts = wol->wolopts & WAKE_ANY;
  2510. usb_autopm_put_interface(tp->intf);
  2511. out_set_wol:
  2512. return ret;
  2513. }
  2514. static u32 rtl8152_get_msglevel(struct net_device *dev)
  2515. {
  2516. struct r8152 *tp = netdev_priv(dev);
  2517. return tp->msg_enable;
  2518. }
  2519. static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
  2520. {
  2521. struct r8152 *tp = netdev_priv(dev);
  2522. tp->msg_enable = value;
  2523. }
  2524. static void rtl8152_get_drvinfo(struct net_device *netdev,
  2525. struct ethtool_drvinfo *info)
  2526. {
  2527. struct r8152 *tp = netdev_priv(netdev);
  2528. strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
  2529. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  2530. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  2531. }
  2532. static
  2533. int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  2534. {
  2535. struct r8152 *tp = netdev_priv(netdev);
  2536. if (!tp->mii.mdio_read)
  2537. return -EOPNOTSUPP;
  2538. return mii_ethtool_gset(&tp->mii, cmd);
  2539. }
  2540. static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2541. {
  2542. struct r8152 *tp = netdev_priv(dev);
  2543. int ret;
  2544. ret = usb_autopm_get_interface(tp->intf);
  2545. if (ret < 0)
  2546. goto out;
  2547. ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
  2548. usb_autopm_put_interface(tp->intf);
  2549. out:
  2550. return ret;
  2551. }
  2552. static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
  2553. "tx_packets",
  2554. "rx_packets",
  2555. "tx_errors",
  2556. "rx_errors",
  2557. "rx_missed",
  2558. "align_errors",
  2559. "tx_single_collisions",
  2560. "tx_multi_collisions",
  2561. "rx_unicast",
  2562. "rx_broadcast",
  2563. "rx_multicast",
  2564. "tx_aborted",
  2565. "tx_underrun",
  2566. };
  2567. static int rtl8152_get_sset_count(struct net_device *dev, int sset)
  2568. {
  2569. switch (sset) {
  2570. case ETH_SS_STATS:
  2571. return ARRAY_SIZE(rtl8152_gstrings);
  2572. default:
  2573. return -EOPNOTSUPP;
  2574. }
  2575. }
  2576. static void rtl8152_get_ethtool_stats(struct net_device *dev,
  2577. struct ethtool_stats *stats, u64 *data)
  2578. {
  2579. struct r8152 *tp = netdev_priv(dev);
  2580. struct tally_counter tally;
  2581. generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
  2582. data[0] = le64_to_cpu(tally.tx_packets);
  2583. data[1] = le64_to_cpu(tally.rx_packets);
  2584. data[2] = le64_to_cpu(tally.tx_errors);
  2585. data[3] = le32_to_cpu(tally.rx_errors);
  2586. data[4] = le16_to_cpu(tally.rx_missed);
  2587. data[5] = le16_to_cpu(tally.align_errors);
  2588. data[6] = le32_to_cpu(tally.tx_one_collision);
  2589. data[7] = le32_to_cpu(tally.tx_multi_collision);
  2590. data[8] = le64_to_cpu(tally.rx_unicast);
  2591. data[9] = le64_to_cpu(tally.rx_broadcast);
  2592. data[10] = le32_to_cpu(tally.rx_multicast);
  2593. data[11] = le16_to_cpu(tally.tx_aborted);
  2594. data[12] = le16_to_cpu(tally.tx_underun);
  2595. }
  2596. static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  2597. {
  2598. switch (stringset) {
  2599. case ETH_SS_STATS:
  2600. memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
  2601. break;
  2602. }
  2603. }
  2604. static struct ethtool_ops ops = {
  2605. .get_drvinfo = rtl8152_get_drvinfo,
  2606. .get_settings = rtl8152_get_settings,
  2607. .set_settings = rtl8152_set_settings,
  2608. .get_link = ethtool_op_get_link,
  2609. .get_msglevel = rtl8152_get_msglevel,
  2610. .set_msglevel = rtl8152_set_msglevel,
  2611. .get_wol = rtl8152_get_wol,
  2612. .set_wol = rtl8152_set_wol,
  2613. .get_strings = rtl8152_get_strings,
  2614. .get_sset_count = rtl8152_get_sset_count,
  2615. .get_ethtool_stats = rtl8152_get_ethtool_stats,
  2616. };
  2617. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  2618. {
  2619. struct r8152 *tp = netdev_priv(netdev);
  2620. struct mii_ioctl_data *data = if_mii(rq);
  2621. int res;
  2622. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2623. return -ENODEV;
  2624. res = usb_autopm_get_interface(tp->intf);
  2625. if (res < 0)
  2626. goto out;
  2627. switch (cmd) {
  2628. case SIOCGMIIPHY:
  2629. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  2630. break;
  2631. case SIOCGMIIREG:
  2632. data->val_out = r8152_mdio_read(tp, data->reg_num);
  2633. break;
  2634. case SIOCSMIIREG:
  2635. if (!capable(CAP_NET_ADMIN)) {
  2636. res = -EPERM;
  2637. break;
  2638. }
  2639. r8152_mdio_write(tp, data->reg_num, data->val_in);
  2640. break;
  2641. default:
  2642. res = -EOPNOTSUPP;
  2643. }
  2644. usb_autopm_put_interface(tp->intf);
  2645. out:
  2646. return res;
  2647. }
  2648. static const struct net_device_ops rtl8152_netdev_ops = {
  2649. .ndo_open = rtl8152_open,
  2650. .ndo_stop = rtl8152_close,
  2651. .ndo_do_ioctl = rtl8152_ioctl,
  2652. .ndo_start_xmit = rtl8152_start_xmit,
  2653. .ndo_tx_timeout = rtl8152_tx_timeout,
  2654. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  2655. .ndo_set_mac_address = rtl8152_set_mac_address,
  2656. .ndo_change_mtu = eth_change_mtu,
  2657. .ndo_validate_addr = eth_validate_addr,
  2658. };
  2659. static void r8152b_get_version(struct r8152 *tp)
  2660. {
  2661. u32 ocp_data;
  2662. u16 version;
  2663. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  2664. version = (u16)(ocp_data & VERSION_MASK);
  2665. switch (version) {
  2666. case 0x4c00:
  2667. tp->version = RTL_VER_01;
  2668. break;
  2669. case 0x4c10:
  2670. tp->version = RTL_VER_02;
  2671. break;
  2672. case 0x5c00:
  2673. tp->version = RTL_VER_03;
  2674. tp->mii.supports_gmii = 1;
  2675. break;
  2676. case 0x5c10:
  2677. tp->version = RTL_VER_04;
  2678. tp->mii.supports_gmii = 1;
  2679. break;
  2680. case 0x5c20:
  2681. tp->version = RTL_VER_05;
  2682. tp->mii.supports_gmii = 1;
  2683. break;
  2684. default:
  2685. netif_info(tp, probe, tp->netdev,
  2686. "Unknown version 0x%04x\n", version);
  2687. break;
  2688. }
  2689. }
  2690. static void rtl8152_unload(struct r8152 *tp)
  2691. {
  2692. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2693. return;
  2694. if (tp->version != RTL_VER_01)
  2695. r8152_power_cut_en(tp, true);
  2696. }
  2697. static void rtl8153_unload(struct r8152 *tp)
  2698. {
  2699. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2700. return;
  2701. r8153_power_cut_en(tp, true);
  2702. }
  2703. static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
  2704. {
  2705. struct rtl_ops *ops = &tp->rtl_ops;
  2706. int ret = -ENODEV;
  2707. switch (id->idVendor) {
  2708. case VENDOR_ID_REALTEK:
  2709. switch (id->idProduct) {
  2710. case PRODUCT_ID_RTL8152:
  2711. ops->init = r8152b_init;
  2712. ops->enable = rtl8152_enable;
  2713. ops->disable = rtl8152_disable;
  2714. ops->up = r8152b_exit_oob;
  2715. ops->down = rtl8152_down;
  2716. ops->unload = rtl8152_unload;
  2717. ret = 0;
  2718. break;
  2719. case PRODUCT_ID_RTL8153:
  2720. ops->init = r8153_init;
  2721. ops->enable = rtl8153_enable;
  2722. ops->disable = rtl8152_disable;
  2723. ops->up = r8153_first_init;
  2724. ops->down = rtl8153_down;
  2725. ops->unload = rtl8153_unload;
  2726. ret = 0;
  2727. break;
  2728. default:
  2729. break;
  2730. }
  2731. break;
  2732. case VENDOR_ID_SAMSUNG:
  2733. switch (id->idProduct) {
  2734. case PRODUCT_ID_SAMSUNG:
  2735. ops->init = r8153_init;
  2736. ops->enable = rtl8153_enable;
  2737. ops->disable = rtl8152_disable;
  2738. ops->up = r8153_first_init;
  2739. ops->down = rtl8153_down;
  2740. ops->unload = rtl8153_unload;
  2741. ret = 0;
  2742. break;
  2743. default:
  2744. break;
  2745. }
  2746. break;
  2747. default:
  2748. break;
  2749. }
  2750. if (ret)
  2751. netif_err(tp, probe, tp->netdev, "Unknown Device\n");
  2752. return ret;
  2753. }
  2754. static int rtl8152_probe(struct usb_interface *intf,
  2755. const struct usb_device_id *id)
  2756. {
  2757. struct usb_device *udev = interface_to_usbdev(intf);
  2758. struct r8152 *tp;
  2759. struct net_device *netdev;
  2760. int ret;
  2761. if (udev->actconfig->desc.bConfigurationValue != 1) {
  2762. usb_driver_set_configuration(udev, 1);
  2763. return -ENODEV;
  2764. }
  2765. usb_reset_device(udev);
  2766. netdev = alloc_etherdev(sizeof(struct r8152));
  2767. if (!netdev) {
  2768. dev_err(&intf->dev, "Out of memory\n");
  2769. return -ENOMEM;
  2770. }
  2771. SET_NETDEV_DEV(netdev, &intf->dev);
  2772. tp = netdev_priv(netdev);
  2773. tp->msg_enable = 0x7FFF;
  2774. tp->udev = udev;
  2775. tp->netdev = netdev;
  2776. tp->intf = intf;
  2777. ret = rtl_ops_init(tp, id);
  2778. if (ret)
  2779. goto out;
  2780. tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
  2781. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  2782. netdev->netdev_ops = &rtl8152_netdev_ops;
  2783. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  2784. netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  2785. NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
  2786. NETIF_F_TSO6;
  2787. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  2788. NETIF_F_TSO | NETIF_F_FRAGLIST |
  2789. NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  2790. netdev->ethtool_ops = &ops;
  2791. netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
  2792. tp->mii.dev = netdev;
  2793. tp->mii.mdio_read = read_mii_word;
  2794. tp->mii.mdio_write = write_mii_word;
  2795. tp->mii.phy_id_mask = 0x3f;
  2796. tp->mii.reg_num_mask = 0x1f;
  2797. tp->mii.phy_id = R8152_PHY_ID;
  2798. tp->mii.supports_gmii = 0;
  2799. intf->needs_remote_wakeup = 1;
  2800. r8152b_get_version(tp);
  2801. tp->rtl_ops.init(tp);
  2802. set_ethernet_addr(tp);
  2803. usb_set_intfdata(intf, tp);
  2804. ret = register_netdev(netdev);
  2805. if (ret != 0) {
  2806. netif_err(tp, probe, netdev, "couldn't register the device\n");
  2807. goto out1;
  2808. }
  2809. tp->saved_wolopts = __rtl_get_wol(tp);
  2810. if (tp->saved_wolopts)
  2811. device_set_wakeup_enable(&udev->dev, true);
  2812. else
  2813. device_set_wakeup_enable(&udev->dev, false);
  2814. netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
  2815. return 0;
  2816. out1:
  2817. usb_set_intfdata(intf, NULL);
  2818. out:
  2819. free_netdev(netdev);
  2820. return ret;
  2821. }
  2822. static void rtl8152_disconnect(struct usb_interface *intf)
  2823. {
  2824. struct r8152 *tp = usb_get_intfdata(intf);
  2825. usb_set_intfdata(intf, NULL);
  2826. if (tp) {
  2827. set_bit(RTL8152_UNPLUG, &tp->flags);
  2828. tasklet_kill(&tp->tl);
  2829. unregister_netdev(tp->netdev);
  2830. tp->rtl_ops.unload(tp);
  2831. free_netdev(tp->netdev);
  2832. }
  2833. }
  2834. /* table of devices that work with this driver */
  2835. static struct usb_device_id rtl8152_table[] = {
  2836. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
  2837. {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
  2838. {USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
  2839. {}
  2840. };
  2841. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  2842. static struct usb_driver rtl8152_driver = {
  2843. .name = MODULENAME,
  2844. .id_table = rtl8152_table,
  2845. .probe = rtl8152_probe,
  2846. .disconnect = rtl8152_disconnect,
  2847. .suspend = rtl8152_suspend,
  2848. .resume = rtl8152_resume,
  2849. .reset_resume = rtl8152_resume,
  2850. .supports_autosuspend = 1,
  2851. .disable_hub_initiated_lpm = 1,
  2852. };
  2853. module_usb_driver(rtl8152_driver);
  2854. MODULE_AUTHOR(DRIVER_AUTHOR);
  2855. MODULE_DESCRIPTION(DRIVER_DESC);
  2856. MODULE_LICENSE("GPL");