sisusb.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484
  1. /*
  2. * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
  3. *
  4. * Main part
  5. *
  6. * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria
  7. *
  8. * If distributed as part of the Linux kernel, this code is licensed under the
  9. * terms of the GPL v2.
  10. *
  11. * Otherwise, the following license terms apply:
  12. *
  13. * * Redistribution and use in source and binary forms, with or without
  14. * * modification, are permitted provided that the following conditions
  15. * * are met:
  16. * * 1) Redistributions of source code must retain the above copyright
  17. * * notice, this list of conditions and the following disclaimer.
  18. * * 2) Redistributions in binary form must reproduce the above copyright
  19. * * notice, this list of conditions and the following disclaimer in the
  20. * * documentation and/or other materials provided with the distribution.
  21. * * 3) The name of the author may not be used to endorse or promote products
  22. * * derived from this software without specific psisusbr written permission.
  23. * *
  24. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
  25. * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Author: Thomas Winischhofer <thomas@winischhofer.net>
  36. *
  37. */
  38. #include <linux/mutex.h>
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/signal.h>
  42. #include <linux/sched.h>
  43. #include <linux/errno.h>
  44. #include <linux/poll.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/kref.h>
  49. #include <linux/usb.h>
  50. #include <linux/smp_lock.h>
  51. #include <linux/vmalloc.h>
  52. #include "sisusb.h"
  53. #include "sisusb_init.h"
  54. #ifdef INCL_SISUSB_CON
  55. #include <linux/font.h>
  56. #endif
  57. #define SISUSB_DONTSYNC
  58. /* Forward declarations / clean-up routines */
  59. #ifdef INCL_SISUSB_CON
  60. static int sisusb_first_vc = 0;
  61. static int sisusb_last_vc = 0;
  62. module_param_named(first, sisusb_first_vc, int, 0);
  63. module_param_named(last, sisusb_last_vc, int, 0);
  64. MODULE_PARM_DESC(first, "Number of first console to take over (1 - MAX_NR_CONSOLES)");
  65. MODULE_PARM_DESC(last, "Number of last console to take over (1 - MAX_NR_CONSOLES)");
  66. #endif
  67. static struct usb_driver sisusb_driver;
  68. DEFINE_MUTEX(disconnect_mutex);
  69. static void
  70. sisusb_free_buffers(struct sisusb_usb_data *sisusb)
  71. {
  72. int i;
  73. for (i = 0; i < NUMOBUFS; i++) {
  74. if (sisusb->obuf[i]) {
  75. usb_buffer_free(sisusb->sisusb_dev, sisusb->obufsize,
  76. sisusb->obuf[i], sisusb->transfer_dma_out[i]);
  77. sisusb->obuf[i] = NULL;
  78. }
  79. }
  80. if (sisusb->ibuf) {
  81. usb_buffer_free(sisusb->sisusb_dev, sisusb->ibufsize,
  82. sisusb->ibuf, sisusb->transfer_dma_in);
  83. sisusb->ibuf = NULL;
  84. }
  85. }
  86. static void
  87. sisusb_free_urbs(struct sisusb_usb_data *sisusb)
  88. {
  89. int i;
  90. for (i = 0; i < NUMOBUFS; i++) {
  91. usb_free_urb(sisusb->sisurbout[i]);
  92. sisusb->sisurbout[i] = NULL;
  93. }
  94. usb_free_urb(sisusb->sisurbin);
  95. sisusb->sisurbin = NULL;
  96. }
  97. /* Level 0: USB transport layer */
  98. /* 1. out-bulks */
  99. /* out-urb management */
  100. /* Return 1 if all free, 0 otherwise */
  101. static int
  102. sisusb_all_free(struct sisusb_usb_data *sisusb)
  103. {
  104. int i;
  105. for (i = 0; i < sisusb->numobufs; i++) {
  106. if (sisusb->urbstatus[i] & SU_URB_BUSY)
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. /* Kill all busy URBs */
  112. static void
  113. sisusb_kill_all_busy(struct sisusb_usb_data *sisusb)
  114. {
  115. int i;
  116. if (sisusb_all_free(sisusb))
  117. return;
  118. for (i = 0; i < sisusb->numobufs; i++) {
  119. if (sisusb->urbstatus[i] & SU_URB_BUSY)
  120. usb_kill_urb(sisusb->sisurbout[i]);
  121. }
  122. }
  123. /* Return 1 if ok, 0 if error (not all complete within timeout) */
  124. static int
  125. sisusb_wait_all_out_complete(struct sisusb_usb_data *sisusb)
  126. {
  127. int timeout = 5 * HZ, i = 1;
  128. wait_event_timeout(sisusb->wait_q,
  129. (i = sisusb_all_free(sisusb)),
  130. timeout);
  131. return i;
  132. }
  133. static int
  134. sisusb_outurb_available(struct sisusb_usb_data *sisusb)
  135. {
  136. int i;
  137. for (i = 0; i < sisusb->numobufs; i++) {
  138. if ((sisusb->urbstatus[i] & (SU_URB_BUSY|SU_URB_ALLOC)) == 0)
  139. return i;
  140. }
  141. return -1;
  142. }
  143. static int
  144. sisusb_get_free_outbuf(struct sisusb_usb_data *sisusb)
  145. {
  146. int i, timeout = 5 * HZ;
  147. wait_event_timeout(sisusb->wait_q,
  148. ((i = sisusb_outurb_available(sisusb)) >= 0),
  149. timeout);
  150. return i;
  151. }
  152. static int
  153. sisusb_alloc_outbuf(struct sisusb_usb_data *sisusb)
  154. {
  155. int i;
  156. i = sisusb_outurb_available(sisusb);
  157. if (i >= 0)
  158. sisusb->urbstatus[i] |= SU_URB_ALLOC;
  159. return i;
  160. }
  161. static void
  162. sisusb_free_outbuf(struct sisusb_usb_data *sisusb, int index)
  163. {
  164. if ((index >= 0) && (index < sisusb->numobufs))
  165. sisusb->urbstatus[index] &= ~SU_URB_ALLOC;
  166. }
  167. /* completion callback */
  168. static void
  169. sisusb_bulk_completeout(struct urb *urb)
  170. {
  171. struct sisusb_urb_context *context = urb->context;
  172. struct sisusb_usb_data *sisusb;
  173. if (!context)
  174. return;
  175. sisusb = context->sisusb;
  176. if (!sisusb || !sisusb->sisusb_dev || !sisusb->present)
  177. return;
  178. #ifndef SISUSB_DONTSYNC
  179. if (context->actual_length)
  180. *(context->actual_length) += urb->actual_length;
  181. #endif
  182. sisusb->urbstatus[context->urbindex] &= ~SU_URB_BUSY;
  183. wake_up(&sisusb->wait_q);
  184. }
  185. static int
  186. sisusb_bulkout_msg(struct sisusb_usb_data *sisusb, int index, unsigned int pipe, void *data,
  187. int len, int *actual_length, int timeout, unsigned int tflags,
  188. dma_addr_t transfer_dma)
  189. {
  190. struct urb *urb = sisusb->sisurbout[index];
  191. int retval, byteswritten = 0;
  192. /* Set up URB */
  193. urb->transfer_flags = 0;
  194. usb_fill_bulk_urb(urb, sisusb->sisusb_dev, pipe, data, len,
  195. sisusb_bulk_completeout, &sisusb->urbout_context[index]);
  196. urb->transfer_flags |= tflags;
  197. urb->actual_length = 0;
  198. if ((urb->transfer_dma = transfer_dma))
  199. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  200. /* Set up context */
  201. sisusb->urbout_context[index].actual_length = (timeout) ?
  202. NULL : actual_length;
  203. /* Declare this urb/buffer in use */
  204. sisusb->urbstatus[index] |= SU_URB_BUSY;
  205. /* Submit URB */
  206. retval = usb_submit_urb(urb, GFP_ATOMIC);
  207. /* If OK, and if timeout > 0, wait for completion */
  208. if ((retval == 0) && timeout) {
  209. wait_event_timeout(sisusb->wait_q,
  210. (!(sisusb->urbstatus[index] & SU_URB_BUSY)),
  211. timeout);
  212. if (sisusb->urbstatus[index] & SU_URB_BUSY) {
  213. /* URB timed out... kill it and report error */
  214. usb_kill_urb(urb);
  215. retval = -ETIMEDOUT;
  216. } else {
  217. /* Otherwise, report urb status */
  218. retval = urb->status;
  219. byteswritten = urb->actual_length;
  220. }
  221. }
  222. if (actual_length)
  223. *actual_length = byteswritten;
  224. return retval;
  225. }
  226. /* 2. in-bulks */
  227. /* completion callback */
  228. static void
  229. sisusb_bulk_completein(struct urb *urb)
  230. {
  231. struct sisusb_usb_data *sisusb = urb->context;
  232. if (!sisusb || !sisusb->sisusb_dev || !sisusb->present)
  233. return;
  234. sisusb->completein = 1;
  235. wake_up(&sisusb->wait_q);
  236. }
  237. static int
  238. sisusb_bulkin_msg(struct sisusb_usb_data *sisusb, unsigned int pipe, void *data, int len,
  239. int *actual_length, int timeout, unsigned int tflags, dma_addr_t transfer_dma)
  240. {
  241. struct urb *urb = sisusb->sisurbin;
  242. int retval, readbytes = 0;
  243. urb->transfer_flags = 0;
  244. usb_fill_bulk_urb(urb, sisusb->sisusb_dev, pipe, data, len,
  245. sisusb_bulk_completein, sisusb);
  246. urb->transfer_flags |= tflags;
  247. urb->actual_length = 0;
  248. if ((urb->transfer_dma = transfer_dma))
  249. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  250. sisusb->completein = 0;
  251. retval = usb_submit_urb(urb, GFP_ATOMIC);
  252. if (retval == 0) {
  253. wait_event_timeout(sisusb->wait_q, sisusb->completein, timeout);
  254. if (!sisusb->completein) {
  255. /* URB timed out... kill it and report error */
  256. usb_kill_urb(urb);
  257. retval = -ETIMEDOUT;
  258. } else {
  259. /* URB completed within timout */
  260. retval = urb->status;
  261. readbytes = urb->actual_length;
  262. }
  263. }
  264. if (actual_length)
  265. *actual_length = readbytes;
  266. return retval;
  267. }
  268. /* Level 1: */
  269. /* Send a bulk message of variable size
  270. *
  271. * To copy the data from userspace, give pointer to "userbuffer",
  272. * to copy from (non-DMA) kernel memory, give "kernbuffer". If
  273. * both of these are NULL, it is assumed, that the transfer
  274. * buffer "sisusb->obuf[index]" is set up with the data to send.
  275. * Index is ignored if either kernbuffer or userbuffer is set.
  276. * If async is nonzero, URBs will be sent without waiting for
  277. * completion of the previous URB.
  278. *
  279. * (return 0 on success)
  280. */
  281. static int sisusb_send_bulk_msg(struct sisusb_usb_data *sisusb, int ep, int len,
  282. char *kernbuffer, const char __user *userbuffer, int index,
  283. ssize_t *bytes_written, unsigned int tflags, int async)
  284. {
  285. int result = 0, retry, count = len;
  286. int passsize, thispass, transferred_len = 0;
  287. int fromuser = (userbuffer != NULL) ? 1 : 0;
  288. int fromkern = (kernbuffer != NULL) ? 1 : 0;
  289. unsigned int pipe;
  290. char *buffer;
  291. (*bytes_written) = 0;
  292. /* Sanity check */
  293. if (!sisusb || !sisusb->present || !sisusb->sisusb_dev)
  294. return -ENODEV;
  295. /* If we copy data from kernel or userspace, force the
  296. * allocation of a buffer/urb. If we have the data in
  297. * the transfer buffer[index] already, reuse the buffer/URB
  298. * if the length is > buffer size. (So, transmitting
  299. * large data amounts directly from the transfer buffer
  300. * treats the buffer as a ring buffer. However, we need
  301. * to sync in this case.)
  302. */
  303. if (fromuser || fromkern)
  304. index = -1;
  305. else if (len > sisusb->obufsize)
  306. async = 0;
  307. pipe = usb_sndbulkpipe(sisusb->sisusb_dev, ep);
  308. do {
  309. passsize = thispass = (sisusb->obufsize < count) ?
  310. sisusb->obufsize : count;
  311. if (index < 0)
  312. index = sisusb_get_free_outbuf(sisusb);
  313. if (index < 0)
  314. return -EIO;
  315. buffer = sisusb->obuf[index];
  316. if (fromuser) {
  317. if (copy_from_user(buffer, userbuffer, passsize))
  318. return -EFAULT;
  319. userbuffer += passsize;
  320. } else if (fromkern) {
  321. memcpy(buffer, kernbuffer, passsize);
  322. kernbuffer += passsize;
  323. }
  324. retry = 5;
  325. while (thispass) {
  326. if (!sisusb->sisusb_dev)
  327. return -ENODEV;
  328. result = sisusb_bulkout_msg(sisusb,
  329. index,
  330. pipe,
  331. buffer,
  332. thispass,
  333. &transferred_len,
  334. async ? 0 : 5 * HZ,
  335. tflags,
  336. sisusb->transfer_dma_out[index]);
  337. if (result == -ETIMEDOUT) {
  338. /* Will not happen if async */
  339. if (!retry--)
  340. return -ETIME;
  341. continue;
  342. } else if ((result == 0) && !async && transferred_len) {
  343. thispass -= transferred_len;
  344. if (thispass) {
  345. if (sisusb->transfer_dma_out) {
  346. /* If DMA, copy remaining
  347. * to beginning of buffer
  348. */
  349. memcpy(buffer,
  350. buffer + transferred_len,
  351. thispass);
  352. } else {
  353. /* If not DMA, simply increase
  354. * the pointer
  355. */
  356. buffer += transferred_len;
  357. }
  358. }
  359. } else
  360. break;
  361. };
  362. if (result)
  363. return result;
  364. (*bytes_written) += passsize;
  365. count -= passsize;
  366. /* Force new allocation in next iteration */
  367. if (fromuser || fromkern)
  368. index = -1;
  369. } while (count > 0);
  370. if (async) {
  371. #ifdef SISUSB_DONTSYNC
  372. (*bytes_written) = len;
  373. /* Some URBs/buffers might be busy */
  374. #else
  375. sisusb_wait_all_out_complete(sisusb);
  376. (*bytes_written) = transferred_len;
  377. /* All URBs and all buffers are available */
  378. #endif
  379. }
  380. return ((*bytes_written) == len) ? 0 : -EIO;
  381. }
  382. /* Receive a bulk message of variable size
  383. *
  384. * To copy the data to userspace, give pointer to "userbuffer",
  385. * to copy to kernel memory, give "kernbuffer". One of them
  386. * MUST be set. (There is no technique for letting the caller
  387. * read directly from the ibuf.)
  388. *
  389. */
  390. static int sisusb_recv_bulk_msg(struct sisusb_usb_data *sisusb, int ep, int len,
  391. void *kernbuffer, char __user *userbuffer, ssize_t *bytes_read,
  392. unsigned int tflags)
  393. {
  394. int result = 0, retry, count = len;
  395. int bufsize, thispass, transferred_len;
  396. unsigned int pipe;
  397. char *buffer;
  398. (*bytes_read) = 0;
  399. /* Sanity check */
  400. if (!sisusb || !sisusb->present || !sisusb->sisusb_dev)
  401. return -ENODEV;
  402. pipe = usb_rcvbulkpipe(sisusb->sisusb_dev, ep);
  403. buffer = sisusb->ibuf;
  404. bufsize = sisusb->ibufsize;
  405. retry = 5;
  406. #ifdef SISUSB_DONTSYNC
  407. if (!(sisusb_wait_all_out_complete(sisusb)))
  408. return -EIO;
  409. #endif
  410. while (count > 0) {
  411. if (!sisusb->sisusb_dev)
  412. return -ENODEV;
  413. thispass = (bufsize < count) ? bufsize : count;
  414. result = sisusb_bulkin_msg(sisusb,
  415. pipe,
  416. buffer,
  417. thispass,
  418. &transferred_len,
  419. 5 * HZ,
  420. tflags,
  421. sisusb->transfer_dma_in);
  422. if (transferred_len)
  423. thispass = transferred_len;
  424. else if (result == -ETIMEDOUT) {
  425. if (!retry--)
  426. return -ETIME;
  427. continue;
  428. } else
  429. return -EIO;
  430. if (thispass) {
  431. (*bytes_read) += thispass;
  432. count -= thispass;
  433. if (userbuffer) {
  434. if (copy_to_user(userbuffer, buffer, thispass))
  435. return -EFAULT;
  436. userbuffer += thispass;
  437. } else {
  438. memcpy(kernbuffer, buffer, thispass);
  439. kernbuffer += thispass;
  440. }
  441. }
  442. }
  443. return ((*bytes_read) == len) ? 0 : -EIO;
  444. }
  445. static int sisusb_send_packet(struct sisusb_usb_data *sisusb, int len,
  446. struct sisusb_packet *packet)
  447. {
  448. int ret;
  449. ssize_t bytes_transferred = 0;
  450. __le32 tmp;
  451. if (len == 6)
  452. packet->data = 0;
  453. #ifdef SISUSB_DONTSYNC
  454. if (!(sisusb_wait_all_out_complete(sisusb)))
  455. return 1;
  456. #endif
  457. /* Eventually correct endianness */
  458. SISUSB_CORRECT_ENDIANNESS_PACKET(packet);
  459. /* 1. send the packet */
  460. ret = sisusb_send_bulk_msg(sisusb, SISUSB_EP_GFX_OUT, len,
  461. (char *)packet, NULL, 0, &bytes_transferred, 0, 0);
  462. if ((ret == 0) && (len == 6)) {
  463. /* 2. if packet len == 6, it means we read, so wait for 32bit
  464. * return value and write it to packet->data
  465. */
  466. ret = sisusb_recv_bulk_msg(sisusb, SISUSB_EP_GFX_IN, 4,
  467. (char *)&tmp, NULL, &bytes_transferred, 0);
  468. packet->data = le32_to_cpu(tmp);
  469. }
  470. return ret;
  471. }
  472. static int sisusb_send_bridge_packet(struct sisusb_usb_data *sisusb, int len,
  473. struct sisusb_packet *packet,
  474. unsigned int tflags)
  475. {
  476. int ret;
  477. ssize_t bytes_transferred = 0;
  478. __le32 tmp;
  479. if (len == 6)
  480. packet->data = 0;
  481. #ifdef SISUSB_DONTSYNC
  482. if (!(sisusb_wait_all_out_complete(sisusb)))
  483. return 1;
  484. #endif
  485. /* Eventually correct endianness */
  486. SISUSB_CORRECT_ENDIANNESS_PACKET(packet);
  487. /* 1. send the packet */
  488. ret = sisusb_send_bulk_msg(sisusb, SISUSB_EP_BRIDGE_OUT, len,
  489. (char *)packet, NULL, 0, &bytes_transferred, tflags, 0);
  490. if ((ret == 0) && (len == 6)) {
  491. /* 2. if packet len == 6, it means we read, so wait for 32bit
  492. * return value and write it to packet->data
  493. */
  494. ret = sisusb_recv_bulk_msg(sisusb, SISUSB_EP_BRIDGE_IN, 4,
  495. (char *)&tmp, NULL, &bytes_transferred, 0);
  496. packet->data = le32_to_cpu(tmp);
  497. }
  498. return ret;
  499. }
  500. /* access video memory and mmio (return 0 on success) */
  501. /* Low level */
  502. /* The following routines assume being used to transfer byte, word,
  503. * long etc.
  504. * This means that
  505. * - the write routines expect "data" in machine endianness format.
  506. * The data will be converted to leXX in sisusb_xxx_packet.
  507. * - the read routines can expect read data in machine-endianess.
  508. */
  509. static int sisusb_write_memio_byte(struct sisusb_usb_data *sisusb, int type,
  510. u32 addr, u8 data)
  511. {
  512. struct sisusb_packet packet;
  513. int ret;
  514. packet.header = (1 << (addr & 3)) | (type << 6);
  515. packet.address = addr & ~3;
  516. packet.data = data << ((addr & 3) << 3);
  517. ret = sisusb_send_packet(sisusb, 10, &packet);
  518. return ret;
  519. }
  520. static int sisusb_write_memio_word(struct sisusb_usb_data *sisusb, int type,
  521. u32 addr, u16 data)
  522. {
  523. struct sisusb_packet packet;
  524. int ret = 0;
  525. packet.address = addr & ~3;
  526. switch (addr & 3) {
  527. case 0:
  528. packet.header = (type << 6) | 0x0003;
  529. packet.data = (u32)data;
  530. ret = sisusb_send_packet(sisusb, 10, &packet);
  531. break;
  532. case 1:
  533. packet.header = (type << 6) | 0x0006;
  534. packet.data = (u32)data << 8;
  535. ret = sisusb_send_packet(sisusb, 10, &packet);
  536. break;
  537. case 2:
  538. packet.header = (type << 6) | 0x000c;
  539. packet.data = (u32)data << 16;
  540. ret = sisusb_send_packet(sisusb, 10, &packet);
  541. break;
  542. case 3:
  543. packet.header = (type << 6) | 0x0008;
  544. packet.data = (u32)data << 24;
  545. ret = sisusb_send_packet(sisusb, 10, &packet);
  546. packet.header = (type << 6) | 0x0001;
  547. packet.address = (addr & ~3) + 4;
  548. packet.data = (u32)data >> 8;
  549. ret |= sisusb_send_packet(sisusb, 10, &packet);
  550. }
  551. return ret;
  552. }
  553. static int sisusb_write_memio_24bit(struct sisusb_usb_data *sisusb, int type,
  554. u32 addr, u32 data)
  555. {
  556. struct sisusb_packet packet;
  557. int ret = 0;
  558. packet.address = addr & ~3;
  559. switch (addr & 3) {
  560. case 0:
  561. packet.header = (type << 6) | 0x0007;
  562. packet.data = data & 0x00ffffff;
  563. ret = sisusb_send_packet(sisusb, 10, &packet);
  564. break;
  565. case 1:
  566. packet.header = (type << 6) | 0x000e;
  567. packet.data = data << 8;
  568. ret = sisusb_send_packet(sisusb, 10, &packet);
  569. break;
  570. case 2:
  571. packet.header = (type << 6) | 0x000c;
  572. packet.data = data << 16;
  573. ret = sisusb_send_packet(sisusb, 10, &packet);
  574. packet.header = (type << 6) | 0x0001;
  575. packet.address = (addr & ~3) + 4;
  576. packet.data = (data >> 16) & 0x00ff;
  577. ret |= sisusb_send_packet(sisusb, 10, &packet);
  578. break;
  579. case 3:
  580. packet.header = (type << 6) | 0x0008;
  581. packet.data = data << 24;
  582. ret = sisusb_send_packet(sisusb, 10, &packet);
  583. packet.header = (type << 6) | 0x0003;
  584. packet.address = (addr & ~3) + 4;
  585. packet.data = (data >> 8) & 0xffff;
  586. ret |= sisusb_send_packet(sisusb, 10, &packet);
  587. }
  588. return ret;
  589. }
  590. static int sisusb_write_memio_long(struct sisusb_usb_data *sisusb, int type,
  591. u32 addr, u32 data)
  592. {
  593. struct sisusb_packet packet;
  594. int ret = 0;
  595. packet.address = addr & ~3;
  596. switch (addr & 3) {
  597. case 0:
  598. packet.header = (type << 6) | 0x000f;
  599. packet.data = data;
  600. ret = sisusb_send_packet(sisusb, 10, &packet);
  601. break;
  602. case 1:
  603. packet.header = (type << 6) | 0x000e;
  604. packet.data = data << 8;
  605. ret = sisusb_send_packet(sisusb, 10, &packet);
  606. packet.header = (type << 6) | 0x0001;
  607. packet.address = (addr & ~3) + 4;
  608. packet.data = data >> 24;
  609. ret |= sisusb_send_packet(sisusb, 10, &packet);
  610. break;
  611. case 2:
  612. packet.header = (type << 6) | 0x000c;
  613. packet.data = data << 16;
  614. ret = sisusb_send_packet(sisusb, 10, &packet);
  615. packet.header = (type << 6) | 0x0003;
  616. packet.address = (addr & ~3) + 4;
  617. packet.data = data >> 16;
  618. ret |= sisusb_send_packet(sisusb, 10, &packet);
  619. break;
  620. case 3:
  621. packet.header = (type << 6) | 0x0008;
  622. packet.data = data << 24;
  623. ret = sisusb_send_packet(sisusb, 10, &packet);
  624. packet.header = (type << 6) | 0x0007;
  625. packet.address = (addr & ~3) + 4;
  626. packet.data = data >> 8;
  627. ret |= sisusb_send_packet(sisusb, 10, &packet);
  628. }
  629. return ret;
  630. }
  631. /* The xxx_bulk routines copy a buffer of variable size. They treat the
  632. * buffer as chars, therefore lsb/msb has to be corrected if using the
  633. * byte/word/long/etc routines for speed-up
  634. *
  635. * If data is from userland, set "userbuffer" (and clear "kernbuffer"),
  636. * if data is in kernel space, set "kernbuffer" (and clear "userbuffer");
  637. * if neither "kernbuffer" nor "userbuffer" are given, it is assumed
  638. * that the data already is in the transfer buffer "sisusb->obuf[index]".
  639. */
  640. static int sisusb_write_mem_bulk(struct sisusb_usb_data *sisusb, u32 addr,
  641. char *kernbuffer, int length,
  642. const char __user *userbuffer, int index,
  643. ssize_t *bytes_written)
  644. {
  645. struct sisusb_packet packet;
  646. int ret = 0;
  647. static int msgcount = 0;
  648. u8 swap8, fromkern = kernbuffer ? 1 : 0;
  649. u16 swap16;
  650. u32 swap32, flag = (length >> 28) & 1;
  651. char buf[4];
  652. /* if neither kernbuffer not userbuffer are given, assume
  653. * data in obuf
  654. */
  655. if (!fromkern && !userbuffer)
  656. kernbuffer = sisusb->obuf[index];
  657. (*bytes_written = 0);
  658. length &= 0x00ffffff;
  659. while (length) {
  660. switch (length) {
  661. case 1:
  662. if (userbuffer) {
  663. if (get_user(swap8, (u8 __user *)userbuffer))
  664. return -EFAULT;
  665. } else
  666. swap8 = kernbuffer[0];
  667. ret = sisusb_write_memio_byte(sisusb,
  668. SISUSB_TYPE_MEM,
  669. addr, swap8);
  670. if (!ret)
  671. (*bytes_written)++;
  672. return ret;
  673. case 2:
  674. if (userbuffer) {
  675. if (get_user(swap16, (u16 __user *)userbuffer))
  676. return -EFAULT;
  677. } else
  678. swap16 = *((u16 *)kernbuffer);
  679. ret = sisusb_write_memio_word(sisusb,
  680. SISUSB_TYPE_MEM,
  681. addr,
  682. swap16);
  683. if (!ret)
  684. (*bytes_written) += 2;
  685. return ret;
  686. case 3:
  687. if (userbuffer) {
  688. if (copy_from_user(&buf, userbuffer, 3))
  689. return -EFAULT;
  690. #ifdef __BIG_ENDIAN
  691. swap32 = (buf[0] << 16) |
  692. (buf[1] << 8) |
  693. buf[2];
  694. #else
  695. swap32 = (buf[2] << 16) |
  696. (buf[1] << 8) |
  697. buf[0];
  698. #endif
  699. } else
  700. #ifdef __BIG_ENDIAN
  701. swap32 = (kernbuffer[0] << 16) |
  702. (kernbuffer[1] << 8) |
  703. kernbuffer[2];
  704. #else
  705. swap32 = (kernbuffer[2] << 16) |
  706. (kernbuffer[1] << 8) |
  707. kernbuffer[0];
  708. #endif
  709. ret = sisusb_write_memio_24bit(sisusb,
  710. SISUSB_TYPE_MEM,
  711. addr,
  712. swap32);
  713. if (!ret)
  714. (*bytes_written) += 3;
  715. return ret;
  716. case 4:
  717. if (userbuffer) {
  718. if (get_user(swap32, (u32 __user *)userbuffer))
  719. return -EFAULT;
  720. } else
  721. swap32 = *((u32 *)kernbuffer);
  722. ret = sisusb_write_memio_long(sisusb,
  723. SISUSB_TYPE_MEM,
  724. addr,
  725. swap32);
  726. if (!ret)
  727. (*bytes_written) += 4;
  728. return ret;
  729. default:
  730. if ((length & ~3) > 0x10000) {
  731. packet.header = 0x001f;
  732. packet.address = 0x000001d4;
  733. packet.data = addr;
  734. ret = sisusb_send_bridge_packet(sisusb, 10,
  735. &packet, 0);
  736. packet.header = 0x001f;
  737. packet.address = 0x000001d0;
  738. packet.data = (length & ~3);
  739. ret |= sisusb_send_bridge_packet(sisusb, 10,
  740. &packet, 0);
  741. packet.header = 0x001f;
  742. packet.address = 0x000001c0;
  743. packet.data = flag | 0x16;
  744. ret |= sisusb_send_bridge_packet(sisusb, 10,
  745. &packet, 0);
  746. if (userbuffer) {
  747. ret |= sisusb_send_bulk_msg(sisusb,
  748. SISUSB_EP_GFX_LBULK_OUT,
  749. (length & ~3),
  750. NULL, userbuffer, 0,
  751. bytes_written, 0, 1);
  752. userbuffer += (*bytes_written);
  753. } else if (fromkern) {
  754. ret |= sisusb_send_bulk_msg(sisusb,
  755. SISUSB_EP_GFX_LBULK_OUT,
  756. (length & ~3),
  757. kernbuffer, NULL, 0,
  758. bytes_written, 0, 1);
  759. kernbuffer += (*bytes_written);
  760. } else {
  761. ret |= sisusb_send_bulk_msg(sisusb,
  762. SISUSB_EP_GFX_LBULK_OUT,
  763. (length & ~3),
  764. NULL, NULL, index,
  765. bytes_written, 0, 1);
  766. kernbuffer += ((*bytes_written) &
  767. (sisusb->obufsize-1));
  768. }
  769. } else {
  770. packet.header = 0x001f;
  771. packet.address = 0x00000194;
  772. packet.data = addr;
  773. ret = sisusb_send_bridge_packet(sisusb, 10,
  774. &packet, 0);
  775. packet.header = 0x001f;
  776. packet.address = 0x00000190;
  777. packet.data = (length & ~3);
  778. ret |= sisusb_send_bridge_packet(sisusb, 10,
  779. &packet, 0);
  780. if (sisusb->flagb0 != 0x16) {
  781. packet.header = 0x001f;
  782. packet.address = 0x00000180;
  783. packet.data = flag | 0x16;
  784. ret |= sisusb_send_bridge_packet(sisusb, 10,
  785. &packet, 0);
  786. sisusb->flagb0 = 0x16;
  787. }
  788. if (userbuffer) {
  789. ret |= sisusb_send_bulk_msg(sisusb,
  790. SISUSB_EP_GFX_BULK_OUT,
  791. (length & ~3),
  792. NULL, userbuffer, 0,
  793. bytes_written, 0, 1);
  794. userbuffer += (*bytes_written);
  795. } else if (fromkern) {
  796. ret |= sisusb_send_bulk_msg(sisusb,
  797. SISUSB_EP_GFX_BULK_OUT,
  798. (length & ~3),
  799. kernbuffer, NULL, 0,
  800. bytes_written, 0, 1);
  801. kernbuffer += (*bytes_written);
  802. } else {
  803. ret |= sisusb_send_bulk_msg(sisusb,
  804. SISUSB_EP_GFX_BULK_OUT,
  805. (length & ~3),
  806. NULL, NULL, index,
  807. bytes_written, 0, 1);
  808. kernbuffer += ((*bytes_written) &
  809. (sisusb->obufsize-1));
  810. }
  811. }
  812. if (ret) {
  813. msgcount++;
  814. if (msgcount < 500)
  815. printk(KERN_ERR
  816. "sisusbvga[%d]: Wrote %zd of "
  817. "%d bytes, error %d\n",
  818. sisusb->minor, *bytes_written,
  819. length, ret);
  820. else if (msgcount == 500)
  821. printk(KERN_ERR
  822. "sisusbvga[%d]: Too many errors"
  823. ", logging stopped\n",
  824. sisusb->minor);
  825. }
  826. addr += (*bytes_written);
  827. length -= (*bytes_written);
  828. }
  829. if (ret)
  830. break;
  831. }
  832. return ret ? -EIO : 0;
  833. }
  834. /* Remember: Read data in packet is in machine-endianess! So for
  835. * byte, word, 24bit, long no endian correction is necessary.
  836. */
  837. static int sisusb_read_memio_byte(struct sisusb_usb_data *sisusb, int type,
  838. u32 addr, u8 *data)
  839. {
  840. struct sisusb_packet packet;
  841. int ret;
  842. CLEARPACKET(&packet);
  843. packet.header = (1 << (addr & 3)) | (type << 6);
  844. packet.address = addr & ~3;
  845. ret = sisusb_send_packet(sisusb, 6, &packet);
  846. *data = (u8)(packet.data >> ((addr & 3) << 3));
  847. return ret;
  848. }
  849. static int sisusb_read_memio_word(struct sisusb_usb_data *sisusb, int type,
  850. u32 addr, u16 *data)
  851. {
  852. struct sisusb_packet packet;
  853. int ret = 0;
  854. CLEARPACKET(&packet);
  855. packet.address = addr & ~3;
  856. switch (addr & 3) {
  857. case 0:
  858. packet.header = (type << 6) | 0x0003;
  859. ret = sisusb_send_packet(sisusb, 6, &packet);
  860. *data = (u16)(packet.data);
  861. break;
  862. case 1:
  863. packet.header = (type << 6) | 0x0006;
  864. ret = sisusb_send_packet(sisusb, 6, &packet);
  865. *data = (u16)(packet.data >> 8);
  866. break;
  867. case 2:
  868. packet.header = (type << 6) | 0x000c;
  869. ret = sisusb_send_packet(sisusb, 6, &packet);
  870. *data = (u16)(packet.data >> 16);
  871. break;
  872. case 3:
  873. packet.header = (type << 6) | 0x0008;
  874. ret = sisusb_send_packet(sisusb, 6, &packet);
  875. *data = (u16)(packet.data >> 24);
  876. packet.header = (type << 6) | 0x0001;
  877. packet.address = (addr & ~3) + 4;
  878. ret |= sisusb_send_packet(sisusb, 6, &packet);
  879. *data |= (u16)(packet.data << 8);
  880. }
  881. return ret;
  882. }
  883. static int sisusb_read_memio_24bit(struct sisusb_usb_data *sisusb, int type,
  884. u32 addr, u32 *data)
  885. {
  886. struct sisusb_packet packet;
  887. int ret = 0;
  888. packet.address = addr & ~3;
  889. switch (addr & 3) {
  890. case 0:
  891. packet.header = (type << 6) | 0x0007;
  892. ret = sisusb_send_packet(sisusb, 6, &packet);
  893. *data = packet.data & 0x00ffffff;
  894. break;
  895. case 1:
  896. packet.header = (type << 6) | 0x000e;
  897. ret = sisusb_send_packet(sisusb, 6, &packet);
  898. *data = packet.data >> 8;
  899. break;
  900. case 2:
  901. packet.header = (type << 6) | 0x000c;
  902. ret = sisusb_send_packet(sisusb, 6, &packet);
  903. *data = packet.data >> 16;
  904. packet.header = (type << 6) | 0x0001;
  905. packet.address = (addr & ~3) + 4;
  906. ret |= sisusb_send_packet(sisusb, 6, &packet);
  907. *data |= ((packet.data & 0xff) << 16);
  908. break;
  909. case 3:
  910. packet.header = (type << 6) | 0x0008;
  911. ret = sisusb_send_packet(sisusb, 6, &packet);
  912. *data = packet.data >> 24;
  913. packet.header = (type << 6) | 0x0003;
  914. packet.address = (addr & ~3) + 4;
  915. ret |= sisusb_send_packet(sisusb, 6, &packet);
  916. *data |= ((packet.data & 0xffff) << 8);
  917. }
  918. return ret;
  919. }
  920. static int sisusb_read_memio_long(struct sisusb_usb_data *sisusb, int type,
  921. u32 addr, u32 *data)
  922. {
  923. struct sisusb_packet packet;
  924. int ret = 0;
  925. packet.address = addr & ~3;
  926. switch (addr & 3) {
  927. case 0:
  928. packet.header = (type << 6) | 0x000f;
  929. ret = sisusb_send_packet(sisusb, 6, &packet);
  930. *data = packet.data;
  931. break;
  932. case 1:
  933. packet.header = (type << 6) | 0x000e;
  934. ret = sisusb_send_packet(sisusb, 6, &packet);
  935. *data = packet.data >> 8;
  936. packet.header = (type << 6) | 0x0001;
  937. packet.address = (addr & ~3) + 4;
  938. ret |= sisusb_send_packet(sisusb, 6, &packet);
  939. *data |= (packet.data << 24);
  940. break;
  941. case 2:
  942. packet.header = (type << 6) | 0x000c;
  943. ret = sisusb_send_packet(sisusb, 6, &packet);
  944. *data = packet.data >> 16;
  945. packet.header = (type << 6) | 0x0003;
  946. packet.address = (addr & ~3) + 4;
  947. ret |= sisusb_send_packet(sisusb, 6, &packet);
  948. *data |= (packet.data << 16);
  949. break;
  950. case 3:
  951. packet.header = (type << 6) | 0x0008;
  952. ret = sisusb_send_packet(sisusb, 6, &packet);
  953. *data = packet.data >> 24;
  954. packet.header = (type << 6) | 0x0007;
  955. packet.address = (addr & ~3) + 4;
  956. ret |= sisusb_send_packet(sisusb, 6, &packet);
  957. *data |= (packet.data << 8);
  958. }
  959. return ret;
  960. }
  961. static int sisusb_read_mem_bulk(struct sisusb_usb_data *sisusb, u32 addr,
  962. char *kernbuffer, int length,
  963. char __user *userbuffer, ssize_t *bytes_read)
  964. {
  965. int ret = 0;
  966. char buf[4];
  967. u16 swap16;
  968. u32 swap32;
  969. (*bytes_read = 0);
  970. length &= 0x00ffffff;
  971. while (length) {
  972. switch (length) {
  973. case 1:
  974. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM,
  975. addr, &buf[0]);
  976. if (!ret) {
  977. (*bytes_read)++;
  978. if (userbuffer) {
  979. if (put_user(buf[0],
  980. (u8 __user *)userbuffer)) {
  981. return -EFAULT;
  982. }
  983. } else {
  984. kernbuffer[0] = buf[0];
  985. }
  986. }
  987. return ret;
  988. case 2:
  989. ret |= sisusb_read_memio_word(sisusb, SISUSB_TYPE_MEM,
  990. addr, &swap16);
  991. if (!ret) {
  992. (*bytes_read) += 2;
  993. if (userbuffer) {
  994. if (put_user(swap16,
  995. (u16 __user *)userbuffer))
  996. return -EFAULT;
  997. } else {
  998. *((u16 *)kernbuffer) = swap16;
  999. }
  1000. }
  1001. return ret;
  1002. case 3:
  1003. ret |= sisusb_read_memio_24bit(sisusb, SISUSB_TYPE_MEM,
  1004. addr, &swap32);
  1005. if (!ret) {
  1006. (*bytes_read) += 3;
  1007. #ifdef __BIG_ENDIAN
  1008. buf[0] = (swap32 >> 16) & 0xff;
  1009. buf[1] = (swap32 >> 8) & 0xff;
  1010. buf[2] = swap32 & 0xff;
  1011. #else
  1012. buf[2] = (swap32 >> 16) & 0xff;
  1013. buf[1] = (swap32 >> 8) & 0xff;
  1014. buf[0] = swap32 & 0xff;
  1015. #endif
  1016. if (userbuffer) {
  1017. if (copy_to_user(userbuffer, &buf[0], 3))
  1018. return -EFAULT;
  1019. } else {
  1020. kernbuffer[0] = buf[0];
  1021. kernbuffer[1] = buf[1];
  1022. kernbuffer[2] = buf[2];
  1023. }
  1024. }
  1025. return ret;
  1026. default:
  1027. ret |= sisusb_read_memio_long(sisusb, SISUSB_TYPE_MEM,
  1028. addr, &swap32);
  1029. if (!ret) {
  1030. (*bytes_read) += 4;
  1031. if (userbuffer) {
  1032. if (put_user(swap32,
  1033. (u32 __user *)userbuffer))
  1034. return -EFAULT;
  1035. userbuffer += 4;
  1036. } else {
  1037. *((u32 *)kernbuffer) = swap32;
  1038. kernbuffer += 4;
  1039. }
  1040. addr += 4;
  1041. length -= 4;
  1042. }
  1043. #if 0 /* That does not work, as EP 2 is an OUT EP! */
  1044. default:
  1045. CLEARPACKET(&packet);
  1046. packet.header = 0x001f;
  1047. packet.address = 0x000001a0;
  1048. packet.data = 0x00000006;
  1049. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1050. &packet, 0);
  1051. packet.header = 0x001f;
  1052. packet.address = 0x000001b0;
  1053. packet.data = (length & ~3) | 0x40000000;
  1054. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1055. &packet, 0);
  1056. packet.header = 0x001f;
  1057. packet.address = 0x000001b4;
  1058. packet.data = addr;
  1059. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1060. &packet, 0);
  1061. packet.header = 0x001f;
  1062. packet.address = 0x000001a4;
  1063. packet.data = 0x00000001;
  1064. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1065. &packet, 0);
  1066. if (userbuffer) {
  1067. ret |= sisusb_recv_bulk_msg(sisusb,
  1068. SISUSB_EP_GFX_BULK_IN,
  1069. (length & ~3),
  1070. NULL, userbuffer,
  1071. bytes_read, 0);
  1072. if (!ret) userbuffer += (*bytes_read);
  1073. } else {
  1074. ret |= sisusb_recv_bulk_msg(sisusb,
  1075. SISUSB_EP_GFX_BULK_IN,
  1076. (length & ~3),
  1077. kernbuffer, NULL,
  1078. bytes_read, 0);
  1079. if (!ret) kernbuffer += (*bytes_read);
  1080. }
  1081. addr += (*bytes_read);
  1082. length -= (*bytes_read);
  1083. #endif
  1084. }
  1085. if (ret)
  1086. break;
  1087. }
  1088. return ret;
  1089. }
  1090. /* High level: Gfx (indexed) register access */
  1091. #ifdef INCL_SISUSB_CON
  1092. int
  1093. sisusb_setreg(struct sisusb_usb_data *sisusb, int port, u8 data)
  1094. {
  1095. return sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, data);
  1096. }
  1097. int
  1098. sisusb_getreg(struct sisusb_usb_data *sisusb, int port, u8 *data)
  1099. {
  1100. return sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port, data);
  1101. }
  1102. #endif
  1103. int
  1104. sisusb_setidxreg(struct sisusb_usb_data *sisusb, int port, u8 index, u8 data)
  1105. {
  1106. int ret;
  1107. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, index);
  1108. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, data);
  1109. return ret;
  1110. }
  1111. int
  1112. sisusb_getidxreg(struct sisusb_usb_data *sisusb, int port, u8 index, u8 *data)
  1113. {
  1114. int ret;
  1115. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, index);
  1116. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, data);
  1117. return ret;
  1118. }
  1119. int
  1120. sisusb_setidxregandor(struct sisusb_usb_data *sisusb, int port, u8 idx,
  1121. u8 myand, u8 myor)
  1122. {
  1123. int ret;
  1124. u8 tmp;
  1125. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, idx);
  1126. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, &tmp);
  1127. tmp &= myand;
  1128. tmp |= myor;
  1129. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, tmp);
  1130. return ret;
  1131. }
  1132. static int
  1133. sisusb_setidxregmask(struct sisusb_usb_data *sisusb, int port, u8 idx,
  1134. u8 data, u8 mask)
  1135. {
  1136. int ret;
  1137. u8 tmp;
  1138. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, idx);
  1139. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, &tmp);
  1140. tmp &= ~(mask);
  1141. tmp |= (data & mask);
  1142. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, tmp);
  1143. return ret;
  1144. }
  1145. int
  1146. sisusb_setidxregor(struct sisusb_usb_data *sisusb, int port, u8 index, u8 myor)
  1147. {
  1148. return(sisusb_setidxregandor(sisusb, port, index, 0xff, myor));
  1149. }
  1150. int
  1151. sisusb_setidxregand(struct sisusb_usb_data *sisusb, int port, u8 idx, u8 myand)
  1152. {
  1153. return(sisusb_setidxregandor(sisusb, port, idx, myand, 0x00));
  1154. }
  1155. /* Write/read video ram */
  1156. #ifdef INCL_SISUSB_CON
  1157. int
  1158. sisusb_writeb(struct sisusb_usb_data *sisusb, u32 adr, u8 data)
  1159. {
  1160. return(sisusb_write_memio_byte(sisusb, SISUSB_TYPE_MEM, adr, data));
  1161. }
  1162. int
  1163. sisusb_readb(struct sisusb_usb_data *sisusb, u32 adr, u8 *data)
  1164. {
  1165. return(sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM, adr, data));
  1166. }
  1167. #if 0
  1168. int
  1169. sisusb_writew(struct sisusb_usb_data *sisusb, u32 adr, u16 data)
  1170. {
  1171. return(sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM, adr, data));
  1172. }
  1173. int
  1174. sisusb_readw(struct sisusb_usb_data *sisusb, u32 adr, u16 *data)
  1175. {
  1176. return(sisusb_read_memio_word(sisusb, SISUSB_TYPE_MEM, adr, data));
  1177. }
  1178. #endif /* 0 */
  1179. int
  1180. sisusb_copy_memory(struct sisusb_usb_data *sisusb, char *src,
  1181. u32 dest, int length, size_t *bytes_written)
  1182. {
  1183. return(sisusb_write_mem_bulk(sisusb, dest, src, length, NULL, 0, bytes_written));
  1184. }
  1185. #ifdef SISUSBENDIANTEST
  1186. int
  1187. sisusb_read_memory(struct sisusb_usb_data *sisusb, char *dest,
  1188. u32 src, int length, size_t *bytes_written)
  1189. {
  1190. return(sisusb_read_mem_bulk(sisusb, src, dest, length, NULL, bytes_written));
  1191. }
  1192. #endif
  1193. #endif
  1194. #ifdef SISUSBENDIANTEST
  1195. static void
  1196. sisusb_testreadwrite(struct sisusb_usb_data *sisusb)
  1197. {
  1198. static char srcbuffer[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
  1199. char destbuffer[10];
  1200. size_t dummy;
  1201. int i,j;
  1202. sisusb_copy_memory(sisusb, srcbuffer, sisusb->vrambase, 7, &dummy);
  1203. for(i = 1; i <= 7; i++) {
  1204. printk(KERN_DEBUG "sisusb: rwtest %d bytes\n", i);
  1205. sisusb_read_memory(sisusb, destbuffer, sisusb->vrambase, i, &dummy);
  1206. for(j = 0; j < i; j++) {
  1207. printk(KERN_DEBUG "sisusb: rwtest read[%d] = %x\n", j, destbuffer[j]);
  1208. }
  1209. }
  1210. }
  1211. #endif
  1212. /* access pci config registers (reg numbers 0, 4, 8, etc) */
  1213. static int
  1214. sisusb_write_pci_config(struct sisusb_usb_data *sisusb, int regnum, u32 data)
  1215. {
  1216. struct sisusb_packet packet;
  1217. int ret;
  1218. packet.header = 0x008f;
  1219. packet.address = regnum | 0x10000;
  1220. packet.data = data;
  1221. ret = sisusb_send_packet(sisusb, 10, &packet);
  1222. return ret;
  1223. }
  1224. static int
  1225. sisusb_read_pci_config(struct sisusb_usb_data *sisusb, int regnum, u32 *data)
  1226. {
  1227. struct sisusb_packet packet;
  1228. int ret;
  1229. packet.header = 0x008f;
  1230. packet.address = (u32)regnum | 0x10000;
  1231. ret = sisusb_send_packet(sisusb, 6, &packet);
  1232. *data = packet.data;
  1233. return ret;
  1234. }
  1235. /* Clear video RAM */
  1236. static int
  1237. sisusb_clear_vram(struct sisusb_usb_data *sisusb, u32 address, int length)
  1238. {
  1239. int ret, i;
  1240. ssize_t j;
  1241. if (address < sisusb->vrambase)
  1242. return 1;
  1243. if (address >= sisusb->vrambase + sisusb->vramsize)
  1244. return 1;
  1245. if (address + length > sisusb->vrambase + sisusb->vramsize)
  1246. length = sisusb->vrambase + sisusb->vramsize - address;
  1247. if (length <= 0)
  1248. return 0;
  1249. /* allocate free buffer/urb and clear the buffer */
  1250. if ((i = sisusb_alloc_outbuf(sisusb)) < 0)
  1251. return -EBUSY;
  1252. memset(sisusb->obuf[i], 0, sisusb->obufsize);
  1253. /* We can write a length > buffer size here. The buffer
  1254. * data will simply be re-used (like a ring-buffer).
  1255. */
  1256. ret = sisusb_write_mem_bulk(sisusb, address, NULL, length, NULL, i, &j);
  1257. /* Free the buffer/urb */
  1258. sisusb_free_outbuf(sisusb, i);
  1259. return ret;
  1260. }
  1261. /* Initialize the graphics core (return 0 on success)
  1262. * This resets the graphics hardware and puts it into
  1263. * a defined mode (640x480@60Hz)
  1264. */
  1265. #define GETREG(r,d) sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, r, d)
  1266. #define SETREG(r,d) sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, r, d)
  1267. #define SETIREG(r,i,d) sisusb_setidxreg(sisusb, r, i, d)
  1268. #define GETIREG(r,i,d) sisusb_getidxreg(sisusb, r, i, d)
  1269. #define SETIREGOR(r,i,o) sisusb_setidxregor(sisusb, r, i, o)
  1270. #define SETIREGAND(r,i,a) sisusb_setidxregand(sisusb, r, i, a)
  1271. #define SETIREGANDOR(r,i,a,o) sisusb_setidxregandor(sisusb, r, i, a, o)
  1272. #define READL(a,d) sisusb_read_memio_long(sisusb, SISUSB_TYPE_MEM, a, d)
  1273. #define WRITEL(a,d) sisusb_write_memio_long(sisusb, SISUSB_TYPE_MEM, a, d)
  1274. #define READB(a,d) sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM, a, d)
  1275. #define WRITEB(a,d) sisusb_write_memio_byte(sisusb, SISUSB_TYPE_MEM, a, d)
  1276. static int
  1277. sisusb_triggersr16(struct sisusb_usb_data *sisusb, u8 ramtype)
  1278. {
  1279. int ret;
  1280. u8 tmp8;
  1281. ret = GETIREG(SISSR, 0x16, &tmp8);
  1282. if (ramtype <= 1) {
  1283. tmp8 &= 0x3f;
  1284. ret |= SETIREG(SISSR, 0x16, tmp8);
  1285. tmp8 |= 0x80;
  1286. ret |= SETIREG(SISSR, 0x16, tmp8);
  1287. } else {
  1288. tmp8 |= 0xc0;
  1289. ret |= SETIREG(SISSR, 0x16, tmp8);
  1290. tmp8 &= 0x0f;
  1291. ret |= SETIREG(SISSR, 0x16, tmp8);
  1292. tmp8 |= 0x80;
  1293. ret |= SETIREG(SISSR, 0x16, tmp8);
  1294. tmp8 &= 0x0f;
  1295. ret |= SETIREG(SISSR, 0x16, tmp8);
  1296. tmp8 |= 0xd0;
  1297. ret |= SETIREG(SISSR, 0x16, tmp8);
  1298. tmp8 &= 0x0f;
  1299. ret |= SETIREG(SISSR, 0x16, tmp8);
  1300. tmp8 |= 0xa0;
  1301. ret |= SETIREG(SISSR, 0x16, tmp8);
  1302. }
  1303. return ret;
  1304. }
  1305. static int
  1306. sisusb_getbuswidth(struct sisusb_usb_data *sisusb, int *bw, int *chab)
  1307. {
  1308. int ret;
  1309. u8 ramtype, done = 0;
  1310. u32 t0, t1, t2, t3;
  1311. u32 ramptr = SISUSB_PCI_MEMBASE;
  1312. ret = GETIREG(SISSR, 0x3a, &ramtype);
  1313. ramtype &= 3;
  1314. ret |= SETIREG(SISSR, 0x13, 0x00);
  1315. if (ramtype <= 1) {
  1316. ret |= SETIREG(SISSR, 0x14, 0x12);
  1317. ret |= SETIREGAND(SISSR, 0x15, 0xef);
  1318. } else {
  1319. ret |= SETIREG(SISSR, 0x14, 0x02);
  1320. }
  1321. ret |= sisusb_triggersr16(sisusb, ramtype);
  1322. ret |= WRITEL(ramptr + 0, 0x01234567);
  1323. ret |= WRITEL(ramptr + 4, 0x456789ab);
  1324. ret |= WRITEL(ramptr + 8, 0x89abcdef);
  1325. ret |= WRITEL(ramptr + 12, 0xcdef0123);
  1326. ret |= WRITEL(ramptr + 16, 0x55555555);
  1327. ret |= WRITEL(ramptr + 20, 0x55555555);
  1328. ret |= WRITEL(ramptr + 24, 0xffffffff);
  1329. ret |= WRITEL(ramptr + 28, 0xffffffff);
  1330. ret |= READL(ramptr + 0, &t0);
  1331. ret |= READL(ramptr + 4, &t1);
  1332. ret |= READL(ramptr + 8, &t2);
  1333. ret |= READL(ramptr + 12, &t3);
  1334. if (ramtype <= 1) {
  1335. *chab = 0; *bw = 64;
  1336. if ((t3 != 0xcdef0123) || (t2 != 0x89abcdef)) {
  1337. if ((t1 == 0x456789ab) && (t0 == 0x01234567)) {
  1338. *chab = 0; *bw = 64;
  1339. ret |= SETIREGAND(SISSR, 0x14, 0xfd);
  1340. }
  1341. }
  1342. if ((t1 != 0x456789ab) || (t0 != 0x01234567)) {
  1343. *chab = 1; *bw = 64;
  1344. ret |= SETIREGANDOR(SISSR, 0x14, 0xfc,0x01);
  1345. ret |= sisusb_triggersr16(sisusb, ramtype);
  1346. ret |= WRITEL(ramptr + 0, 0x89abcdef);
  1347. ret |= WRITEL(ramptr + 4, 0xcdef0123);
  1348. ret |= WRITEL(ramptr + 8, 0x55555555);
  1349. ret |= WRITEL(ramptr + 12, 0x55555555);
  1350. ret |= WRITEL(ramptr + 16, 0xaaaaaaaa);
  1351. ret |= WRITEL(ramptr + 20, 0xaaaaaaaa);
  1352. ret |= READL(ramptr + 4, &t1);
  1353. if (t1 != 0xcdef0123) {
  1354. *bw = 32;
  1355. ret |= SETIREGOR(SISSR, 0x15, 0x10);
  1356. }
  1357. }
  1358. } else {
  1359. *chab = 0; *bw = 64; /* default: cha, bw = 64 */
  1360. done = 0;
  1361. if (t1 == 0x456789ab) {
  1362. if (t0 == 0x01234567) {
  1363. *chab = 0; *bw = 64;
  1364. done = 1;
  1365. }
  1366. } else {
  1367. if (t0 == 0x01234567) {
  1368. *chab = 0; *bw = 32;
  1369. ret |= SETIREG(SISSR, 0x14, 0x00);
  1370. done = 1;
  1371. }
  1372. }
  1373. if (!done) {
  1374. ret |= SETIREG(SISSR, 0x14, 0x03);
  1375. ret |= sisusb_triggersr16(sisusb, ramtype);
  1376. ret |= WRITEL(ramptr + 0, 0x01234567);
  1377. ret |= WRITEL(ramptr + 4, 0x456789ab);
  1378. ret |= WRITEL(ramptr + 8, 0x89abcdef);
  1379. ret |= WRITEL(ramptr + 12, 0xcdef0123);
  1380. ret |= WRITEL(ramptr + 16, 0x55555555);
  1381. ret |= WRITEL(ramptr + 20, 0x55555555);
  1382. ret |= WRITEL(ramptr + 24, 0xffffffff);
  1383. ret |= WRITEL(ramptr + 28, 0xffffffff);
  1384. ret |= READL(ramptr + 0, &t0);
  1385. ret |= READL(ramptr + 4, &t1);
  1386. if (t1 == 0x456789ab) {
  1387. if (t0 == 0x01234567) {
  1388. *chab = 1; *bw = 64;
  1389. return ret;
  1390. } /* else error */
  1391. } else {
  1392. if (t0 == 0x01234567) {
  1393. *chab = 1; *bw = 32;
  1394. ret |= SETIREG(SISSR, 0x14, 0x01);
  1395. } /* else error */
  1396. }
  1397. }
  1398. }
  1399. return ret;
  1400. }
  1401. static int
  1402. sisusb_verify_mclk(struct sisusb_usb_data *sisusb)
  1403. {
  1404. int ret = 0;
  1405. u32 ramptr = SISUSB_PCI_MEMBASE;
  1406. u8 tmp1, tmp2, i, j;
  1407. ret |= WRITEB(ramptr, 0xaa);
  1408. ret |= WRITEB(ramptr + 16, 0x55);
  1409. ret |= READB(ramptr, &tmp1);
  1410. ret |= READB(ramptr + 16, &tmp2);
  1411. if ((tmp1 != 0xaa) || (tmp2 != 0x55)) {
  1412. for (i = 0, j = 16; i < 2; i++, j += 16) {
  1413. ret |= GETIREG(SISSR, 0x21, &tmp1);
  1414. ret |= SETIREGAND(SISSR, 0x21, (tmp1 & 0xfb));
  1415. ret |= SETIREGOR(SISSR, 0x3c, 0x01); /* not on 330 */
  1416. ret |= SETIREGAND(SISSR, 0x3c, 0xfe); /* not on 330 */
  1417. ret |= SETIREG(SISSR, 0x21, tmp1);
  1418. ret |= WRITEB(ramptr + 16 + j, j);
  1419. ret |= READB(ramptr + 16 + j, &tmp1);
  1420. if (tmp1 == j) {
  1421. ret |= WRITEB(ramptr + j, j);
  1422. break;
  1423. }
  1424. }
  1425. }
  1426. return ret;
  1427. }
  1428. static int
  1429. sisusb_set_rank(struct sisusb_usb_data *sisusb, int *iret, int index,
  1430. u8 rankno, u8 chab, const u8 dramtype[][5],
  1431. int bw)
  1432. {
  1433. int ret = 0, ranksize;
  1434. u8 tmp;
  1435. *iret = 0;
  1436. if ((rankno == 2) && (dramtype[index][0] == 2))
  1437. return ret;
  1438. ranksize = dramtype[index][3] / 2 * bw / 32;
  1439. if ((ranksize * rankno) > 128)
  1440. return ret;
  1441. tmp = 0;
  1442. while ((ranksize >>= 1) > 0) tmp += 0x10;
  1443. tmp |= ((rankno - 1) << 2);
  1444. tmp |= ((bw / 64) & 0x02);
  1445. tmp |= (chab & 0x01);
  1446. ret = SETIREG(SISSR, 0x14, tmp);
  1447. ret |= sisusb_triggersr16(sisusb, 0); /* sic! */
  1448. *iret = 1;
  1449. return ret;
  1450. }
  1451. static int
  1452. sisusb_check_rbc(struct sisusb_usb_data *sisusb, int *iret, u32 inc, int testn)
  1453. {
  1454. int ret = 0, i;
  1455. u32 j, tmp;
  1456. *iret = 0;
  1457. for (i = 0, j = 0; i < testn; i++) {
  1458. ret |= WRITEL(sisusb->vrambase + j, j);
  1459. j += inc;
  1460. }
  1461. for (i = 0, j = 0; i < testn; i++) {
  1462. ret |= READL(sisusb->vrambase + j, &tmp);
  1463. if (tmp != j) return ret;
  1464. j += inc;
  1465. }
  1466. *iret = 1;
  1467. return ret;
  1468. }
  1469. static int
  1470. sisusb_check_ranks(struct sisusb_usb_data *sisusb, int *iret, int rankno,
  1471. int idx, int bw, const u8 rtype[][5])
  1472. {
  1473. int ret = 0, i, i2ret;
  1474. u32 inc;
  1475. *iret = 0;
  1476. for (i = rankno; i >= 1; i--) {
  1477. inc = 1 << (rtype[idx][2] +
  1478. rtype[idx][1] +
  1479. rtype[idx][0] +
  1480. bw / 64 + i);
  1481. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 2);
  1482. if (!i2ret)
  1483. return ret;
  1484. }
  1485. inc = 1 << (rtype[idx][2] + bw / 64 + 2);
  1486. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 4);
  1487. if (!i2ret)
  1488. return ret;
  1489. inc = 1 << (10 + bw / 64);
  1490. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 2);
  1491. if (!i2ret)
  1492. return ret;
  1493. *iret = 1;
  1494. return ret;
  1495. }
  1496. static int
  1497. sisusb_get_sdram_size(struct sisusb_usb_data *sisusb, int *iret, int bw,
  1498. int chab)
  1499. {
  1500. int ret = 0, i2ret = 0, i, j;
  1501. static const u8 sdramtype[13][5] = {
  1502. { 2, 12, 9, 64, 0x35 },
  1503. { 1, 13, 9, 64, 0x44 },
  1504. { 2, 12, 8, 32, 0x31 },
  1505. { 2, 11, 9, 32, 0x25 },
  1506. { 1, 12, 9, 32, 0x34 },
  1507. { 1, 13, 8, 32, 0x40 },
  1508. { 2, 11, 8, 16, 0x21 },
  1509. { 1, 12, 8, 16, 0x30 },
  1510. { 1, 11, 9, 16, 0x24 },
  1511. { 1, 11, 8, 8, 0x20 },
  1512. { 2, 9, 8, 4, 0x01 },
  1513. { 1, 10, 8, 4, 0x10 },
  1514. { 1, 9, 8, 2, 0x00 }
  1515. };
  1516. *iret = 1; /* error */
  1517. for (i = 0; i < 13; i++) {
  1518. ret |= SETIREGANDOR(SISSR, 0x13, 0x80, sdramtype[i][4]);
  1519. for (j = 2; j > 0; j--) {
  1520. ret |= sisusb_set_rank(sisusb, &i2ret, i, j,
  1521. chab, sdramtype, bw);
  1522. if (!i2ret)
  1523. continue;
  1524. ret |= sisusb_check_ranks(sisusb, &i2ret, j, i,
  1525. bw, sdramtype);
  1526. if (i2ret) {
  1527. *iret = 0; /* ram size found */
  1528. return ret;
  1529. }
  1530. }
  1531. }
  1532. return ret;
  1533. }
  1534. static int
  1535. sisusb_setup_screen(struct sisusb_usb_data *sisusb, int clrall, int drwfr)
  1536. {
  1537. int ret = 0;
  1538. u32 address;
  1539. int i, length, modex, modey, bpp;
  1540. modex = 640; modey = 480; bpp = 2;
  1541. address = sisusb->vrambase; /* Clear video ram */
  1542. if (clrall)
  1543. length = sisusb->vramsize;
  1544. else
  1545. length = modex * bpp * modey;
  1546. ret = sisusb_clear_vram(sisusb, address, length);
  1547. if (!ret && drwfr) {
  1548. for (i = 0; i < modex; i++) {
  1549. address = sisusb->vrambase + (i * bpp);
  1550. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1551. address, 0xf100);
  1552. address += (modex * (modey-1) * bpp);
  1553. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1554. address, 0xf100);
  1555. }
  1556. for (i = 0; i < modey; i++) {
  1557. address = sisusb->vrambase + ((i * modex) * bpp);
  1558. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1559. address, 0xf100);
  1560. address += ((modex - 1) * bpp);
  1561. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1562. address, 0xf100);
  1563. }
  1564. }
  1565. return ret;
  1566. }
  1567. static int
  1568. sisusb_set_default_mode(struct sisusb_usb_data *sisusb, int touchengines)
  1569. {
  1570. int ret = 0, i, j, modex, modey, bpp, du;
  1571. u8 sr31, cr63, tmp8;
  1572. static const char attrdata[] = {
  1573. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1574. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1575. 0x01,0x00,0x00,0x00
  1576. };
  1577. static const char crtcrdata[] = {
  1578. 0x5f,0x4f,0x50,0x82,0x54,0x80,0x0b,0x3e,
  1579. 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
  1580. 0xea,0x8c,0xdf,0x28,0x40,0xe7,0x04,0xa3,
  1581. 0xff
  1582. };
  1583. static const char grcdata[] = {
  1584. 0x00,0x00,0x00,0x00,0x00,0x40,0x05,0x0f,
  1585. 0xff
  1586. };
  1587. static const char crtcdata[] = {
  1588. 0x5f,0x4f,0x4f,0x83,0x55,0x81,0x0b,0x3e,
  1589. 0xe9,0x8b,0xdf,0xe8,0x0c,0x00,0x00,0x05,
  1590. 0x00
  1591. };
  1592. modex = 640; modey = 480; bpp = 2;
  1593. GETIREG(SISSR, 0x31, &sr31);
  1594. GETIREG(SISCR, 0x63, &cr63);
  1595. SETIREGOR(SISSR, 0x01, 0x20);
  1596. SETIREG(SISCR, 0x63, cr63 & 0xbf);
  1597. SETIREGOR(SISCR, 0x17, 0x80);
  1598. SETIREGOR(SISSR, 0x1f, 0x04);
  1599. SETIREGAND(SISSR, 0x07, 0xfb);
  1600. SETIREG(SISSR, 0x00, 0x03); /* seq */
  1601. SETIREG(SISSR, 0x01, 0x21);
  1602. SETIREG(SISSR, 0x02, 0x0f);
  1603. SETIREG(SISSR, 0x03, 0x00);
  1604. SETIREG(SISSR, 0x04, 0x0e);
  1605. SETREG(SISMISCW, 0x23); /* misc */
  1606. for (i = 0; i <= 0x18; i++) { /* crtc */
  1607. SETIREG(SISCR, i, crtcrdata[i]);
  1608. }
  1609. for (i = 0; i <= 0x13; i++) { /* att */
  1610. GETREG(SISINPSTAT, &tmp8);
  1611. SETREG(SISAR, i);
  1612. SETREG(SISAR, attrdata[i]);
  1613. }
  1614. GETREG(SISINPSTAT, &tmp8);
  1615. SETREG(SISAR, 0x14);
  1616. SETREG(SISAR, 0x00);
  1617. GETREG(SISINPSTAT, &tmp8);
  1618. SETREG(SISAR, 0x20);
  1619. GETREG(SISINPSTAT, &tmp8);
  1620. for (i = 0; i <= 0x08; i++) { /* grc */
  1621. SETIREG(SISGR, i, grcdata[i]);
  1622. }
  1623. SETIREGAND(SISGR, 0x05, 0xbf);
  1624. for (i = 0x0A; i <= 0x0E; i++) { /* clr ext */
  1625. SETIREG(SISSR, i, 0x00);
  1626. }
  1627. SETIREGAND(SISSR, 0x37, 0xfe);
  1628. SETREG(SISMISCW, 0xef); /* sync */
  1629. SETIREG(SISCR, 0x11, 0x00); /* crtc */
  1630. for (j = 0x00, i = 0; i <= 7; i++, j++) {
  1631. SETIREG(SISCR, j, crtcdata[i]);
  1632. }
  1633. for (j = 0x10; i <= 10; i++, j++) {
  1634. SETIREG(SISCR, j, crtcdata[i]);
  1635. }
  1636. for (j = 0x15; i <= 12; i++, j++) {
  1637. SETIREG(SISCR, j, crtcdata[i]);
  1638. }
  1639. for (j = 0x0A; i <= 15; i++, j++) {
  1640. SETIREG(SISSR, j, crtcdata[i]);
  1641. }
  1642. SETIREG(SISSR, 0x0E, (crtcdata[16] & 0xE0));
  1643. SETIREGANDOR(SISCR, 0x09, 0x5f, ((crtcdata[16] & 0x01) << 5));
  1644. SETIREG(SISCR, 0x14, 0x4f);
  1645. du = (modex / 16) * (bpp * 2); /* offset/pitch */
  1646. if (modex % 16) du += bpp;
  1647. SETIREGANDOR(SISSR, 0x0e, 0xf0, ((du >> 8) & 0x0f));
  1648. SETIREG(SISCR, 0x13, (du & 0xff));
  1649. du <<= 5;
  1650. tmp8 = du >> 8;
  1651. if (du & 0xff) tmp8++;
  1652. SETIREG(SISSR, 0x10, tmp8);
  1653. SETIREG(SISSR, 0x31, 0x00); /* VCLK */
  1654. SETIREG(SISSR, 0x2b, 0x1b);
  1655. SETIREG(SISSR, 0x2c, 0xe1);
  1656. SETIREG(SISSR, 0x2d, 0x01);
  1657. SETIREGAND(SISSR, 0x3d, 0xfe); /* FIFO */
  1658. SETIREG(SISSR, 0x08, 0xae);
  1659. SETIREGAND(SISSR, 0x09, 0xf0);
  1660. SETIREG(SISSR, 0x08, 0x34);
  1661. SETIREGOR(SISSR, 0x3d, 0x01);
  1662. SETIREGAND(SISSR, 0x1f, 0x3f); /* mode regs */
  1663. SETIREGANDOR(SISSR, 0x06, 0xc0, 0x0a);
  1664. SETIREG(SISCR, 0x19, 0x00);
  1665. SETIREGAND(SISCR, 0x1a, 0xfc);
  1666. SETIREGAND(SISSR, 0x0f, 0xb7);
  1667. SETIREGAND(SISSR, 0x31, 0xfb);
  1668. SETIREGANDOR(SISSR, 0x21, 0x1f, 0xa0);
  1669. SETIREGAND(SISSR, 0x32, 0xf3);
  1670. SETIREGANDOR(SISSR, 0x07, 0xf8, 0x03);
  1671. SETIREG(SISCR, 0x52, 0x6c);
  1672. SETIREG(SISCR, 0x0d, 0x00); /* adjust frame */
  1673. SETIREG(SISCR, 0x0c, 0x00);
  1674. SETIREG(SISSR, 0x0d, 0x00);
  1675. SETIREGAND(SISSR, 0x37, 0xfe);
  1676. SETIREG(SISCR, 0x32, 0x20);
  1677. SETIREGAND(SISSR, 0x01, 0xdf); /* enable display */
  1678. SETIREG(SISCR, 0x63, (cr63 & 0xbf));
  1679. SETIREG(SISSR, 0x31, (sr31 & 0xfb));
  1680. if (touchengines) {
  1681. SETIREG(SISSR, 0x20, 0xa1); /* enable engines */
  1682. SETIREGOR(SISSR, 0x1e, 0x5a);
  1683. SETIREG(SISSR, 0x26, 0x01); /* disable cmdqueue */
  1684. SETIREG(SISSR, 0x27, 0x1f);
  1685. SETIREG(SISSR, 0x26, 0x00);
  1686. }
  1687. SETIREG(SISCR, 0x34, 0x44); /* we just set std mode #44 */
  1688. return ret;
  1689. }
  1690. static int
  1691. sisusb_init_gfxcore(struct sisusb_usb_data *sisusb)
  1692. {
  1693. int ret = 0, i, j, bw, chab, iret, retry = 3;
  1694. u8 tmp8, ramtype;
  1695. u32 tmp32;
  1696. static const char mclktable[] = {
  1697. 0x3b, 0x22, 0x01, 143,
  1698. 0x3b, 0x22, 0x01, 143,
  1699. 0x3b, 0x22, 0x01, 143,
  1700. 0x3b, 0x22, 0x01, 143
  1701. };
  1702. static const char eclktable[] = {
  1703. 0x3b, 0x22, 0x01, 143,
  1704. 0x3b, 0x22, 0x01, 143,
  1705. 0x3b, 0x22, 0x01, 143,
  1706. 0x3b, 0x22, 0x01, 143
  1707. };
  1708. static const char ramtypetable1[] = {
  1709. 0x00, 0x04, 0x60, 0x60,
  1710. 0x0f, 0x0f, 0x1f, 0x1f,
  1711. 0xba, 0xba, 0xba, 0xba,
  1712. 0xa9, 0xa9, 0xac, 0xac,
  1713. 0xa0, 0xa0, 0xa0, 0xa8,
  1714. 0x00, 0x00, 0x02, 0x02,
  1715. 0x30, 0x30, 0x40, 0x40
  1716. };
  1717. static const char ramtypetable2[] = {
  1718. 0x77, 0x77, 0x44, 0x44,
  1719. 0x77, 0x77, 0x44, 0x44,
  1720. 0x00, 0x00, 0x00, 0x00,
  1721. 0x5b, 0x5b, 0xab, 0xab,
  1722. 0x00, 0x00, 0xf0, 0xf8
  1723. };
  1724. while (retry--) {
  1725. /* Enable VGA */
  1726. ret = GETREG(SISVGAEN, &tmp8);
  1727. ret |= SETREG(SISVGAEN, (tmp8 | 0x01));
  1728. /* Enable GPU access to VRAM */
  1729. ret |= GETREG(SISMISCR, &tmp8);
  1730. ret |= SETREG(SISMISCW, (tmp8 | 0x01));
  1731. if (ret) continue;
  1732. /* Reset registers */
  1733. ret |= SETIREGAND(SISCR, 0x5b, 0xdf);
  1734. ret |= SETIREG(SISSR, 0x05, 0x86);
  1735. ret |= SETIREGOR(SISSR, 0x20, 0x01);
  1736. ret |= SETREG(SISMISCW, 0x67);
  1737. for (i = 0x06; i <= 0x1f; i++) {
  1738. ret |= SETIREG(SISSR, i, 0x00);
  1739. }
  1740. for (i = 0x21; i <= 0x27; i++) {
  1741. ret |= SETIREG(SISSR, i, 0x00);
  1742. }
  1743. for (i = 0x31; i <= 0x3d; i++) {
  1744. ret |= SETIREG(SISSR, i, 0x00);
  1745. }
  1746. for (i = 0x12; i <= 0x1b; i++) {
  1747. ret |= SETIREG(SISSR, i, 0x00);
  1748. }
  1749. for (i = 0x79; i <= 0x7c; i++) {
  1750. ret |= SETIREG(SISCR, i, 0x00);
  1751. }
  1752. if (ret) continue;
  1753. ret |= SETIREG(SISCR, 0x63, 0x80);
  1754. ret |= GETIREG(SISSR, 0x3a, &ramtype);
  1755. ramtype &= 0x03;
  1756. ret |= SETIREG(SISSR, 0x28, mclktable[ramtype * 4]);
  1757. ret |= SETIREG(SISSR, 0x29, mclktable[(ramtype * 4) + 1]);
  1758. ret |= SETIREG(SISSR, 0x2a, mclktable[(ramtype * 4) + 2]);
  1759. ret |= SETIREG(SISSR, 0x2e, eclktable[ramtype * 4]);
  1760. ret |= SETIREG(SISSR, 0x2f, eclktable[(ramtype * 4) + 1]);
  1761. ret |= SETIREG(SISSR, 0x30, eclktable[(ramtype * 4) + 2]);
  1762. ret |= SETIREG(SISSR, 0x07, 0x18);
  1763. ret |= SETIREG(SISSR, 0x11, 0x0f);
  1764. if (ret) continue;
  1765. for (i = 0x15, j = 0; i <= 0x1b; i++, j++) {
  1766. ret |= SETIREG(SISSR, i, ramtypetable1[(j*4) + ramtype]);
  1767. }
  1768. for (i = 0x40, j = 0; i <= 0x44; i++, j++) {
  1769. ret |= SETIREG(SISCR, i, ramtypetable2[(j*4) + ramtype]);
  1770. }
  1771. ret |= SETIREG(SISCR, 0x49, 0xaa);
  1772. ret |= SETIREG(SISSR, 0x1f, 0x00);
  1773. ret |= SETIREG(SISSR, 0x20, 0xa0);
  1774. ret |= SETIREG(SISSR, 0x23, 0xf6);
  1775. ret |= SETIREG(SISSR, 0x24, 0x0d);
  1776. ret |= SETIREG(SISSR, 0x25, 0x33);
  1777. ret |= SETIREG(SISSR, 0x11, 0x0f);
  1778. ret |= SETIREGOR(SISPART1, 0x2f, 0x01);
  1779. ret |= SETIREGAND(SISCAP, 0x3f, 0xef);
  1780. if (ret) continue;
  1781. ret |= SETIREG(SISPART1, 0x00, 0x00);
  1782. ret |= GETIREG(SISSR, 0x13, &tmp8);
  1783. tmp8 >>= 4;
  1784. ret |= SETIREG(SISPART1, 0x02, 0x00);
  1785. ret |= SETIREG(SISPART1, 0x2e, 0x08);
  1786. ret |= sisusb_read_pci_config(sisusb, 0x50, &tmp32);
  1787. tmp32 &= 0x00f00000;
  1788. tmp8 = (tmp32 == 0x100000) ? 0x33 : 0x03;
  1789. ret |= SETIREG(SISSR, 0x25, tmp8);
  1790. tmp8 = (tmp32 == 0x100000) ? 0xaa : 0x88;
  1791. ret |= SETIREG(SISCR, 0x49, tmp8);
  1792. ret |= SETIREG(SISSR, 0x27, 0x1f);
  1793. ret |= SETIREG(SISSR, 0x31, 0x00);
  1794. ret |= SETIREG(SISSR, 0x32, 0x11);
  1795. ret |= SETIREG(SISSR, 0x33, 0x00);
  1796. if (ret) continue;
  1797. ret |= SETIREG(SISCR, 0x83, 0x00);
  1798. ret |= sisusb_set_default_mode(sisusb, 0);
  1799. ret |= SETIREGAND(SISSR, 0x21, 0xdf);
  1800. ret |= SETIREGOR(SISSR, 0x01, 0x20);
  1801. ret |= SETIREGOR(SISSR, 0x16, 0x0f);
  1802. ret |= sisusb_triggersr16(sisusb, ramtype);
  1803. /* Disable refresh */
  1804. ret |= SETIREGAND(SISSR, 0x17, 0xf8);
  1805. ret |= SETIREGOR(SISSR, 0x19, 0x03);
  1806. ret |= sisusb_getbuswidth(sisusb, &bw, &chab);
  1807. ret |= sisusb_verify_mclk(sisusb);
  1808. if (ramtype <= 1) {
  1809. ret |= sisusb_get_sdram_size(sisusb, &iret, bw, chab);
  1810. if (iret) {
  1811. printk(KERN_ERR "sisusbvga[%d]: RAM size "
  1812. "detection failed, "
  1813. "assuming 8MB video RAM\n",
  1814. sisusb->minor);
  1815. ret |= SETIREG(SISSR,0x14,0x31);
  1816. /* TODO */
  1817. }
  1818. } else {
  1819. printk(KERN_ERR "sisusbvga[%d]: DDR RAM device found, "
  1820. "assuming 8MB video RAM\n",
  1821. sisusb->minor);
  1822. ret |= SETIREG(SISSR,0x14,0x31);
  1823. /* *** TODO *** */
  1824. }
  1825. /* Enable refresh */
  1826. ret |= SETIREG(SISSR, 0x16, ramtypetable1[4 + ramtype]);
  1827. ret |= SETIREG(SISSR, 0x17, ramtypetable1[8 + ramtype]);
  1828. ret |= SETIREG(SISSR, 0x19, ramtypetable1[16 + ramtype]);
  1829. ret |= SETIREGOR(SISSR, 0x21, 0x20);
  1830. ret |= SETIREG(SISSR, 0x22, 0xfb);
  1831. ret |= SETIREG(SISSR, 0x21, 0xa5);
  1832. if (ret == 0)
  1833. break;
  1834. }
  1835. return ret;
  1836. }
  1837. #undef SETREG
  1838. #undef GETREG
  1839. #undef SETIREG
  1840. #undef GETIREG
  1841. #undef SETIREGOR
  1842. #undef SETIREGAND
  1843. #undef SETIREGANDOR
  1844. #undef READL
  1845. #undef WRITEL
  1846. static void
  1847. sisusb_get_ramconfig(struct sisusb_usb_data *sisusb)
  1848. {
  1849. u8 tmp8, tmp82, ramtype;
  1850. int bw = 0;
  1851. char *ramtypetext1 = NULL;
  1852. const char *ramtypetext2[] = { "SDR SDRAM", "SDR SGRAM",
  1853. "DDR SDRAM", "DDR SGRAM" };
  1854. static const int busSDR[4] = {64, 64, 128, 128};
  1855. static const int busDDR[4] = {32, 32, 64, 64};
  1856. static const int busDDRA[4] = {64+32, 64+32 , (64+32)*2, (64+32)*2};
  1857. sisusb_getidxreg(sisusb, SISSR, 0x14, &tmp8);
  1858. sisusb_getidxreg(sisusb, SISSR, 0x15, &tmp82);
  1859. sisusb_getidxreg(sisusb, SISSR, 0x3a, &ramtype);
  1860. sisusb->vramsize = (1 << ((tmp8 & 0xf0) >> 4)) * 1024 * 1024;
  1861. ramtype &= 0x03;
  1862. switch ((tmp8 >> 2) & 0x03) {
  1863. case 0: ramtypetext1 = "1 ch/1 r";
  1864. if (tmp82 & 0x10) {
  1865. bw = 32;
  1866. } else {
  1867. bw = busSDR[(tmp8 & 0x03)];
  1868. }
  1869. break;
  1870. case 1: ramtypetext1 = "1 ch/2 r";
  1871. sisusb->vramsize <<= 1;
  1872. bw = busSDR[(tmp8 & 0x03)];
  1873. break;
  1874. case 2: ramtypetext1 = "asymmeric";
  1875. sisusb->vramsize += sisusb->vramsize/2;
  1876. bw = busDDRA[(tmp8 & 0x03)];
  1877. break;
  1878. case 3: ramtypetext1 = "2 channel";
  1879. sisusb->vramsize <<= 1;
  1880. bw = busDDR[(tmp8 & 0x03)];
  1881. break;
  1882. }
  1883. printk(KERN_INFO "sisusbvga[%d]: %dMB %s %s, bus width %d\n",
  1884. sisusb->minor, (sisusb->vramsize >> 20), ramtypetext1,
  1885. ramtypetext2[ramtype], bw);
  1886. }
  1887. static int
  1888. sisusb_do_init_gfxdevice(struct sisusb_usb_data *sisusb)
  1889. {
  1890. struct sisusb_packet packet;
  1891. int ret;
  1892. u32 tmp32;
  1893. /* Do some magic */
  1894. packet.header = 0x001f;
  1895. packet.address = 0x00000324;
  1896. packet.data = 0x00000004;
  1897. ret = sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1898. packet.header = 0x001f;
  1899. packet.address = 0x00000364;
  1900. packet.data = 0x00000004;
  1901. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1902. packet.header = 0x001f;
  1903. packet.address = 0x00000384;
  1904. packet.data = 0x00000004;
  1905. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1906. packet.header = 0x001f;
  1907. packet.address = 0x00000100;
  1908. packet.data = 0x00000700;
  1909. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1910. packet.header = 0x000f;
  1911. packet.address = 0x00000004;
  1912. ret |= sisusb_send_bridge_packet(sisusb, 6, &packet, 0);
  1913. packet.data |= 0x17;
  1914. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1915. /* Init BAR 0 (VRAM) */
  1916. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1917. ret |= sisusb_write_pci_config(sisusb, 0x10, 0xfffffff0);
  1918. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1919. tmp32 &= 0x0f;
  1920. tmp32 |= SISUSB_PCI_MEMBASE;
  1921. ret |= sisusb_write_pci_config(sisusb, 0x10, tmp32);
  1922. /* Init BAR 1 (MMIO) */
  1923. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1924. ret |= sisusb_write_pci_config(sisusb, 0x14, 0xfffffff0);
  1925. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1926. tmp32 &= 0x0f;
  1927. tmp32 |= SISUSB_PCI_MMIOBASE;
  1928. ret |= sisusb_write_pci_config(sisusb, 0x14, tmp32);
  1929. /* Init BAR 2 (i/o ports) */
  1930. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1931. ret |= sisusb_write_pci_config(sisusb, 0x18, 0xfffffff0);
  1932. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1933. tmp32 &= 0x0f;
  1934. tmp32 |= SISUSB_PCI_IOPORTBASE;
  1935. ret |= sisusb_write_pci_config(sisusb, 0x18, tmp32);
  1936. /* Enable memory and i/o access */
  1937. ret |= sisusb_read_pci_config(sisusb, 0x04, &tmp32);
  1938. tmp32 |= 0x3;
  1939. ret |= sisusb_write_pci_config(sisusb, 0x04, tmp32);
  1940. if (ret == 0) {
  1941. /* Some further magic */
  1942. packet.header = 0x001f;
  1943. packet.address = 0x00000050;
  1944. packet.data = 0x000000ff;
  1945. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1946. }
  1947. return ret;
  1948. }
  1949. /* Initialize the graphics device (return 0 on success)
  1950. * This initializes the net2280 as well as the PCI registers
  1951. * of the graphics board.
  1952. */
  1953. static int
  1954. sisusb_init_gfxdevice(struct sisusb_usb_data *sisusb, int initscreen)
  1955. {
  1956. int ret = 0, test = 0;
  1957. u32 tmp32;
  1958. if (sisusb->devinit == 1) {
  1959. /* Read PCI BARs and see if they have been set up */
  1960. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1961. if (ret) return ret;
  1962. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_MEMBASE) test++;
  1963. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1964. if (ret) return ret;
  1965. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_MMIOBASE) test++;
  1966. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1967. if (ret) return ret;
  1968. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_IOPORTBASE) test++;
  1969. }
  1970. /* No? So reset the device */
  1971. if ((sisusb->devinit == 0) || (test != 3)) {
  1972. ret |= sisusb_do_init_gfxdevice(sisusb);
  1973. if (ret == 0)
  1974. sisusb->devinit = 1;
  1975. }
  1976. if (sisusb->devinit) {
  1977. /* Initialize the graphics core */
  1978. if (sisusb_init_gfxcore(sisusb) == 0) {
  1979. sisusb->gfxinit = 1;
  1980. sisusb_get_ramconfig(sisusb);
  1981. ret |= sisusb_set_default_mode(sisusb, 1);
  1982. ret |= sisusb_setup_screen(sisusb, 1, initscreen);
  1983. }
  1984. }
  1985. return ret;
  1986. }
  1987. #ifdef INCL_SISUSB_CON
  1988. /* Set up default text mode:
  1989. - Set text mode (0x03)
  1990. - Upload default font
  1991. - Upload user font (if available)
  1992. */
  1993. int
  1994. sisusb_reset_text_mode(struct sisusb_usb_data *sisusb, int init)
  1995. {
  1996. int ret = 0, slot = sisusb->font_slot, i;
  1997. const struct font_desc *myfont;
  1998. u8 *tempbuf;
  1999. u16 *tempbufb;
  2000. size_t written;
  2001. static const char bootstring[] = "SiSUSB VGA text console, (C) 2005 Thomas Winischhofer.";
  2002. static const char bootlogo[] = "(o_ //\\ V_/_";
  2003. /* sisusb->lock is down */
  2004. if (!sisusb->SiS_Pr)
  2005. return 1;
  2006. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2007. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2008. /* Set mode 0x03 */
  2009. SiSUSBSetMode(sisusb->SiS_Pr, 0x03);
  2010. if (!(myfont = find_font("VGA8x16")))
  2011. return 1;
  2012. if (!(tempbuf = vmalloc(8192)))
  2013. return 1;
  2014. for (i = 0; i < 256; i++)
  2015. memcpy(tempbuf + (i * 32), myfont->data + (i * 16), 16);
  2016. /* Upload default font */
  2017. ret = sisusbcon_do_font_op(sisusb, 1, 0, tempbuf, 8192, 0, 1, NULL, 16, 0);
  2018. vfree(tempbuf);
  2019. /* Upload user font (and reset current slot) */
  2020. if (sisusb->font_backup) {
  2021. ret |= sisusbcon_do_font_op(sisusb, 1, 2, sisusb->font_backup,
  2022. 8192, sisusb->font_backup_512, 1, NULL,
  2023. sisusb->font_backup_height, 0);
  2024. if (slot != 2)
  2025. sisusbcon_do_font_op(sisusb, 1, 0, NULL, 0, 0, 1,
  2026. NULL, 16, 0);
  2027. }
  2028. if (init && !sisusb->scrbuf) {
  2029. if ((tempbuf = vmalloc(8192))) {
  2030. i = 4096;
  2031. tempbufb = (u16 *)tempbuf;
  2032. while (i--)
  2033. *(tempbufb++) = 0x0720;
  2034. i = 0;
  2035. tempbufb = (u16 *)tempbuf;
  2036. while (bootlogo[i]) {
  2037. *(tempbufb++) = 0x0700 | bootlogo[i++];
  2038. if (!(i % 4))
  2039. tempbufb += 76;
  2040. }
  2041. i = 0;
  2042. tempbufb = (u16 *)tempbuf + 6;
  2043. while (bootstring[i])
  2044. *(tempbufb++) = 0x0700 | bootstring[i++];
  2045. ret |= sisusb_copy_memory(sisusb, tempbuf,
  2046. sisusb->vrambase, 8192, &written);
  2047. vfree(tempbuf);
  2048. }
  2049. } else if (sisusb->scrbuf) {
  2050. ret |= sisusb_copy_memory(sisusb, (char *)sisusb->scrbuf,
  2051. sisusb->vrambase, sisusb->scrbuf_size, &written);
  2052. }
  2053. if (sisusb->sisusb_cursor_size_from >= 0 &&
  2054. sisusb->sisusb_cursor_size_to >= 0) {
  2055. sisusb_setidxreg(sisusb, SISCR, 0x0a,
  2056. sisusb->sisusb_cursor_size_from);
  2057. sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0,
  2058. sisusb->sisusb_cursor_size_to);
  2059. } else {
  2060. sisusb_setidxreg(sisusb, SISCR, 0x0a, 0x2d);
  2061. sisusb_setidxreg(sisusb, SISCR, 0x0b, 0x0e);
  2062. sisusb->sisusb_cursor_size_to = -1;
  2063. }
  2064. slot = sisusb->sisusb_cursor_loc;
  2065. if(slot < 0) slot = 0;
  2066. sisusb->sisusb_cursor_loc = -1;
  2067. sisusb->bad_cursor_pos = 1;
  2068. sisusb_set_cursor(sisusb, slot);
  2069. sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
  2070. sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
  2071. sisusb->textmodedestroyed = 0;
  2072. /* sisusb->lock is down */
  2073. return ret;
  2074. }
  2075. #endif
  2076. /* fops */
  2077. static int
  2078. sisusb_open(struct inode *inode, struct file *file)
  2079. {
  2080. struct sisusb_usb_data *sisusb;
  2081. struct usb_interface *interface;
  2082. int subminor = iminor(inode);
  2083. mutex_lock(&disconnect_mutex);
  2084. if (!(interface = usb_find_interface(&sisusb_driver, subminor))) {
  2085. printk(KERN_ERR "sisusb[%d]: Failed to find interface\n",
  2086. subminor);
  2087. mutex_unlock(&disconnect_mutex);
  2088. return -ENODEV;
  2089. }
  2090. if (!(sisusb = usb_get_intfdata(interface))) {
  2091. mutex_unlock(&disconnect_mutex);
  2092. return -ENODEV;
  2093. }
  2094. mutex_lock(&sisusb->lock);
  2095. if (!sisusb->present || !sisusb->ready) {
  2096. mutex_unlock(&sisusb->lock);
  2097. mutex_unlock(&disconnect_mutex);
  2098. return -ENODEV;
  2099. }
  2100. if (sisusb->isopen) {
  2101. mutex_unlock(&sisusb->lock);
  2102. mutex_unlock(&disconnect_mutex);
  2103. return -EBUSY;
  2104. }
  2105. if (!sisusb->devinit) {
  2106. if (sisusb->sisusb_dev->speed == USB_SPEED_HIGH) {
  2107. if (sisusb_init_gfxdevice(sisusb, 0)) {
  2108. mutex_unlock(&sisusb->lock);
  2109. mutex_unlock(&disconnect_mutex);
  2110. printk(KERN_ERR
  2111. "sisusbvga[%d]: Failed to initialize "
  2112. "device\n",
  2113. sisusb->minor);
  2114. return -EIO;
  2115. }
  2116. } else {
  2117. mutex_unlock(&sisusb->lock);
  2118. mutex_unlock(&disconnect_mutex);
  2119. printk(KERN_ERR
  2120. "sisusbvga[%d]: Device not attached to "
  2121. "USB 2.0 hub\n",
  2122. sisusb->minor);
  2123. return -EIO;
  2124. }
  2125. }
  2126. /* Increment usage count for our sisusb */
  2127. kref_get(&sisusb->kref);
  2128. sisusb->isopen = 1;
  2129. file->private_data = sisusb;
  2130. mutex_unlock(&sisusb->lock);
  2131. mutex_unlock(&disconnect_mutex);
  2132. return 0;
  2133. }
  2134. void
  2135. sisusb_delete(struct kref *kref)
  2136. {
  2137. struct sisusb_usb_data *sisusb = to_sisusb_dev(kref);
  2138. if (!sisusb)
  2139. return;
  2140. if (sisusb->sisusb_dev)
  2141. usb_put_dev(sisusb->sisusb_dev);
  2142. sisusb->sisusb_dev = NULL;
  2143. sisusb_free_buffers(sisusb);
  2144. sisusb_free_urbs(sisusb);
  2145. #ifdef INCL_SISUSB_CON
  2146. kfree(sisusb->SiS_Pr);
  2147. #endif
  2148. kfree(sisusb);
  2149. }
  2150. static int
  2151. sisusb_release(struct inode *inode, struct file *file)
  2152. {
  2153. struct sisusb_usb_data *sisusb;
  2154. int myminor;
  2155. mutex_lock(&disconnect_mutex);
  2156. if (!(sisusb = (struct sisusb_usb_data *)file->private_data)) {
  2157. mutex_unlock(&disconnect_mutex);
  2158. return -ENODEV;
  2159. }
  2160. mutex_lock(&sisusb->lock);
  2161. if (sisusb->present) {
  2162. /* Wait for all URBs to finish if device still present */
  2163. if (!sisusb_wait_all_out_complete(sisusb))
  2164. sisusb_kill_all_busy(sisusb);
  2165. }
  2166. myminor = sisusb->minor;
  2167. sisusb->isopen = 0;
  2168. file->private_data = NULL;
  2169. mutex_unlock(&sisusb->lock);
  2170. /* decrement the usage count on our device */
  2171. kref_put(&sisusb->kref, sisusb_delete);
  2172. mutex_unlock(&disconnect_mutex);
  2173. return 0;
  2174. }
  2175. static ssize_t
  2176. sisusb_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  2177. {
  2178. struct sisusb_usb_data *sisusb;
  2179. ssize_t bytes_read = 0;
  2180. int errno = 0;
  2181. u8 buf8;
  2182. u16 buf16;
  2183. u32 buf32, address;
  2184. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2185. return -ENODEV;
  2186. mutex_lock(&sisusb->lock);
  2187. /* Sanity check */
  2188. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2189. mutex_unlock(&sisusb->lock);
  2190. return -ENODEV;
  2191. }
  2192. if ((*ppos) >= SISUSB_PCI_PSEUDO_IOPORTBASE &&
  2193. (*ppos) < SISUSB_PCI_PSEUDO_IOPORTBASE + 128) {
  2194. address = (*ppos) -
  2195. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2196. SISUSB_PCI_IOPORTBASE;
  2197. /* Read i/o ports
  2198. * Byte, word and long(32) can be read. As this
  2199. * emulates inX instructions, the data returned is
  2200. * in machine-endianness.
  2201. */
  2202. switch (count) {
  2203. case 1:
  2204. if (sisusb_read_memio_byte(sisusb,
  2205. SISUSB_TYPE_IO,
  2206. address, &buf8))
  2207. errno = -EIO;
  2208. else if (put_user(buf8, (u8 __user *)buffer))
  2209. errno = -EFAULT;
  2210. else
  2211. bytes_read = 1;
  2212. break;
  2213. case 2:
  2214. if (sisusb_read_memio_word(sisusb,
  2215. SISUSB_TYPE_IO,
  2216. address, &buf16))
  2217. errno = -EIO;
  2218. else if (put_user(buf16, (u16 __user *)buffer))
  2219. errno = -EFAULT;
  2220. else
  2221. bytes_read = 2;
  2222. break;
  2223. case 4:
  2224. if (sisusb_read_memio_long(sisusb,
  2225. SISUSB_TYPE_IO,
  2226. address, &buf32))
  2227. errno = -EIO;
  2228. else if (put_user(buf32, (u32 __user *)buffer))
  2229. errno = -EFAULT;
  2230. else
  2231. bytes_read = 4;
  2232. break;
  2233. default:
  2234. errno = -EIO;
  2235. }
  2236. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MEMBASE &&
  2237. (*ppos) < SISUSB_PCI_PSEUDO_MEMBASE + sisusb->vramsize) {
  2238. address = (*ppos) -
  2239. SISUSB_PCI_PSEUDO_MEMBASE +
  2240. SISUSB_PCI_MEMBASE;
  2241. /* Read video ram
  2242. * Remember: Data delivered is never endian-corrected
  2243. */
  2244. errno = sisusb_read_mem_bulk(sisusb, address,
  2245. NULL, count, buffer, &bytes_read);
  2246. if (bytes_read)
  2247. errno = bytes_read;
  2248. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MMIOBASE &&
  2249. (*ppos) < SISUSB_PCI_PSEUDO_MMIOBASE + SISUSB_PCI_MMIOSIZE) {
  2250. address = (*ppos) -
  2251. SISUSB_PCI_PSEUDO_MMIOBASE +
  2252. SISUSB_PCI_MMIOBASE;
  2253. /* Read MMIO
  2254. * Remember: Data delivered is never endian-corrected
  2255. */
  2256. errno = sisusb_read_mem_bulk(sisusb, address,
  2257. NULL, count, buffer, &bytes_read);
  2258. if (bytes_read)
  2259. errno = bytes_read;
  2260. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_PCIBASE &&
  2261. (*ppos) <= SISUSB_PCI_PSEUDO_PCIBASE + 0x5c) {
  2262. if (count != 4) {
  2263. mutex_unlock(&sisusb->lock);
  2264. return -EINVAL;
  2265. }
  2266. address = (*ppos) - SISUSB_PCI_PSEUDO_PCIBASE;
  2267. /* Read PCI config register
  2268. * Return value delivered in machine endianness.
  2269. */
  2270. if (sisusb_read_pci_config(sisusb, address, &buf32))
  2271. errno = -EIO;
  2272. else if (put_user(buf32, (u32 __user *)buffer))
  2273. errno = -EFAULT;
  2274. else
  2275. bytes_read = 4;
  2276. } else {
  2277. errno = -EBADFD;
  2278. }
  2279. (*ppos) += bytes_read;
  2280. mutex_unlock(&sisusb->lock);
  2281. return errno ? errno : bytes_read;
  2282. }
  2283. static ssize_t
  2284. sisusb_write(struct file *file, const char __user *buffer, size_t count,
  2285. loff_t *ppos)
  2286. {
  2287. struct sisusb_usb_data *sisusb;
  2288. int errno = 0;
  2289. ssize_t bytes_written = 0;
  2290. u8 buf8;
  2291. u16 buf16;
  2292. u32 buf32, address;
  2293. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2294. return -ENODEV;
  2295. mutex_lock(&sisusb->lock);
  2296. /* Sanity check */
  2297. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2298. mutex_unlock(&sisusb->lock);
  2299. return -ENODEV;
  2300. }
  2301. if ((*ppos) >= SISUSB_PCI_PSEUDO_IOPORTBASE &&
  2302. (*ppos) < SISUSB_PCI_PSEUDO_IOPORTBASE + 128) {
  2303. address = (*ppos) -
  2304. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2305. SISUSB_PCI_IOPORTBASE;
  2306. /* Write i/o ports
  2307. * Byte, word and long(32) can be written. As this
  2308. * emulates outX instructions, the data is expected
  2309. * in machine-endianness.
  2310. */
  2311. switch (count) {
  2312. case 1:
  2313. if (get_user(buf8, (u8 __user *)buffer))
  2314. errno = -EFAULT;
  2315. else if (sisusb_write_memio_byte(sisusb,
  2316. SISUSB_TYPE_IO,
  2317. address, buf8))
  2318. errno = -EIO;
  2319. else
  2320. bytes_written = 1;
  2321. break;
  2322. case 2:
  2323. if (get_user(buf16, (u16 __user *)buffer))
  2324. errno = -EFAULT;
  2325. else if (sisusb_write_memio_word(sisusb,
  2326. SISUSB_TYPE_IO,
  2327. address, buf16))
  2328. errno = -EIO;
  2329. else
  2330. bytes_written = 2;
  2331. break;
  2332. case 4:
  2333. if (get_user(buf32, (u32 __user *)buffer))
  2334. errno = -EFAULT;
  2335. else if (sisusb_write_memio_long(sisusb,
  2336. SISUSB_TYPE_IO,
  2337. address, buf32))
  2338. errno = -EIO;
  2339. else
  2340. bytes_written = 4;
  2341. break;
  2342. default:
  2343. errno = -EIO;
  2344. }
  2345. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MEMBASE &&
  2346. (*ppos) < SISUSB_PCI_PSEUDO_MEMBASE + sisusb->vramsize) {
  2347. address = (*ppos) -
  2348. SISUSB_PCI_PSEUDO_MEMBASE +
  2349. SISUSB_PCI_MEMBASE;
  2350. /* Write video ram.
  2351. * Buffer is copied 1:1, therefore, on big-endian
  2352. * machines, the data must be swapped by userland
  2353. * in advance (if applicable; no swapping in 8bpp
  2354. * mode or if YUV data is being transferred).
  2355. */
  2356. errno = sisusb_write_mem_bulk(sisusb, address, NULL,
  2357. count, buffer, 0, &bytes_written);
  2358. if (bytes_written)
  2359. errno = bytes_written;
  2360. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MMIOBASE &&
  2361. (*ppos) < SISUSB_PCI_PSEUDO_MMIOBASE + SISUSB_PCI_MMIOSIZE) {
  2362. address = (*ppos) -
  2363. SISUSB_PCI_PSEUDO_MMIOBASE +
  2364. SISUSB_PCI_MMIOBASE;
  2365. /* Write MMIO.
  2366. * Buffer is copied 1:1, therefore, on big-endian
  2367. * machines, the data must be swapped by userland
  2368. * in advance.
  2369. */
  2370. errno = sisusb_write_mem_bulk(sisusb, address, NULL,
  2371. count, buffer, 0, &bytes_written);
  2372. if (bytes_written)
  2373. errno = bytes_written;
  2374. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_PCIBASE &&
  2375. (*ppos) <= SISUSB_PCI_PSEUDO_PCIBASE + SISUSB_PCI_PCONFSIZE) {
  2376. if (count != 4) {
  2377. mutex_unlock(&sisusb->lock);
  2378. return -EINVAL;
  2379. }
  2380. address = (*ppos) - SISUSB_PCI_PSEUDO_PCIBASE;
  2381. /* Write PCI config register.
  2382. * Given value expected in machine endianness.
  2383. */
  2384. if (get_user(buf32, (u32 __user *)buffer))
  2385. errno = -EFAULT;
  2386. else if (sisusb_write_pci_config(sisusb, address, buf32))
  2387. errno = -EIO;
  2388. else
  2389. bytes_written = 4;
  2390. } else {
  2391. /* Error */
  2392. errno = -EBADFD;
  2393. }
  2394. (*ppos) += bytes_written;
  2395. mutex_unlock(&sisusb->lock);
  2396. return errno ? errno : bytes_written;
  2397. }
  2398. static loff_t
  2399. sisusb_lseek(struct file *file, loff_t offset, int orig)
  2400. {
  2401. struct sisusb_usb_data *sisusb;
  2402. loff_t ret;
  2403. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2404. return -ENODEV;
  2405. mutex_lock(&sisusb->lock);
  2406. /* Sanity check */
  2407. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2408. mutex_unlock(&sisusb->lock);
  2409. return -ENODEV;
  2410. }
  2411. switch (orig) {
  2412. case 0:
  2413. file->f_pos = offset;
  2414. ret = file->f_pos;
  2415. /* never negative, no force_successful_syscall needed */
  2416. break;
  2417. case 1:
  2418. file->f_pos += offset;
  2419. ret = file->f_pos;
  2420. /* never negative, no force_successful_syscall needed */
  2421. break;
  2422. default:
  2423. /* seeking relative to "end of file" is not supported */
  2424. ret = -EINVAL;
  2425. }
  2426. mutex_unlock(&sisusb->lock);
  2427. return ret;
  2428. }
  2429. static int
  2430. sisusb_handle_command(struct sisusb_usb_data *sisusb, struct sisusb_command *y,
  2431. unsigned long arg)
  2432. {
  2433. int retval, port, length;
  2434. u32 address;
  2435. /* All our commands require the device
  2436. * to be initialized.
  2437. */
  2438. if (!sisusb->devinit)
  2439. return -ENODEV;
  2440. port = y->data3 -
  2441. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2442. SISUSB_PCI_IOPORTBASE;
  2443. switch (y->operation) {
  2444. case SUCMD_GET:
  2445. retval = sisusb_getidxreg(sisusb, port,
  2446. y->data0, &y->data1);
  2447. if (!retval) {
  2448. if (copy_to_user((void __user *)arg, y,
  2449. sizeof(*y)))
  2450. retval = -EFAULT;
  2451. }
  2452. break;
  2453. case SUCMD_SET:
  2454. retval = sisusb_setidxreg(sisusb, port,
  2455. y->data0, y->data1);
  2456. break;
  2457. case SUCMD_SETOR:
  2458. retval = sisusb_setidxregor(sisusb, port,
  2459. y->data0, y->data1);
  2460. break;
  2461. case SUCMD_SETAND:
  2462. retval = sisusb_setidxregand(sisusb, port,
  2463. y->data0, y->data1);
  2464. break;
  2465. case SUCMD_SETANDOR:
  2466. retval = sisusb_setidxregandor(sisusb, port,
  2467. y->data0, y->data1, y->data2);
  2468. break;
  2469. case SUCMD_SETMASK:
  2470. retval = sisusb_setidxregmask(sisusb, port,
  2471. y->data0, y->data1, y->data2);
  2472. break;
  2473. case SUCMD_CLRSCR:
  2474. /* Gfx core must be initialized */
  2475. if (!sisusb->gfxinit)
  2476. return -ENODEV;
  2477. length = (y->data0 << 16) | (y->data1 << 8) | y->data2;
  2478. address = y->data3 -
  2479. SISUSB_PCI_PSEUDO_MEMBASE +
  2480. SISUSB_PCI_MEMBASE;
  2481. retval = sisusb_clear_vram(sisusb, address, length);
  2482. break;
  2483. case SUCMD_HANDLETEXTMODE:
  2484. retval = 0;
  2485. #ifdef INCL_SISUSB_CON
  2486. /* Gfx core must be initialized, SiS_Pr must exist */
  2487. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2488. return -ENODEV;
  2489. switch (y->data0) {
  2490. case 0:
  2491. retval = sisusb_reset_text_mode(sisusb, 0);
  2492. break;
  2493. case 1:
  2494. sisusb->textmodedestroyed = 1;
  2495. break;
  2496. }
  2497. #endif
  2498. break;
  2499. #ifdef INCL_SISUSB_CON
  2500. case SUCMD_SETMODE:
  2501. /* Gfx core must be initialized, SiS_Pr must exist */
  2502. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2503. return -ENODEV;
  2504. retval = 0;
  2505. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2506. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2507. if (SiSUSBSetMode(sisusb->SiS_Pr, y->data3))
  2508. retval = -EINVAL;
  2509. break;
  2510. case SUCMD_SETVESAMODE:
  2511. /* Gfx core must be initialized, SiS_Pr must exist */
  2512. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2513. return -ENODEV;
  2514. retval = 0;
  2515. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2516. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2517. if (SiSUSBSetVESAMode(sisusb->SiS_Pr, y->data3))
  2518. retval = -EINVAL;
  2519. break;
  2520. #endif
  2521. default:
  2522. retval = -EINVAL;
  2523. }
  2524. if (retval > 0)
  2525. retval = -EIO;
  2526. return retval;
  2527. }
  2528. static int
  2529. sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  2530. unsigned long arg)
  2531. {
  2532. struct sisusb_usb_data *sisusb;
  2533. struct sisusb_info x;
  2534. struct sisusb_command y;
  2535. int retval = 0;
  2536. u32 __user *argp = (u32 __user *)arg;
  2537. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2538. return -ENODEV;
  2539. mutex_lock(&sisusb->lock);
  2540. /* Sanity check */
  2541. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2542. retval = -ENODEV;
  2543. goto err_out;
  2544. }
  2545. switch (cmd) {
  2546. case SISUSB_GET_CONFIG_SIZE:
  2547. if (put_user(sizeof(x), argp))
  2548. retval = -EFAULT;
  2549. break;
  2550. case SISUSB_GET_CONFIG:
  2551. x.sisusb_id = SISUSB_ID;
  2552. x.sisusb_version = SISUSB_VERSION;
  2553. x.sisusb_revision = SISUSB_REVISION;
  2554. x.sisusb_patchlevel = SISUSB_PATCHLEVEL;
  2555. x.sisusb_gfxinit = sisusb->gfxinit;
  2556. x.sisusb_vrambase = SISUSB_PCI_PSEUDO_MEMBASE;
  2557. x.sisusb_mmiobase = SISUSB_PCI_PSEUDO_MMIOBASE;
  2558. x.sisusb_iobase = SISUSB_PCI_PSEUDO_IOPORTBASE;
  2559. x.sisusb_pcibase = SISUSB_PCI_PSEUDO_PCIBASE;
  2560. x.sisusb_vramsize = sisusb->vramsize;
  2561. x.sisusb_minor = sisusb->minor;
  2562. x.sisusb_fbdevactive= 0;
  2563. #ifdef INCL_SISUSB_CON
  2564. x.sisusb_conactive = sisusb->haveconsole ? 1 : 0;
  2565. #else
  2566. x.sisusb_conactive = 0;
  2567. #endif
  2568. if (copy_to_user((void __user *)arg, &x, sizeof(x)))
  2569. retval = -EFAULT;
  2570. break;
  2571. case SISUSB_COMMAND:
  2572. if (copy_from_user(&y, (void __user *)arg, sizeof(y)))
  2573. retval = -EFAULT;
  2574. else
  2575. retval = sisusb_handle_command(sisusb, &y, arg);
  2576. break;
  2577. default:
  2578. retval = -ENOTTY;
  2579. break;
  2580. }
  2581. err_out:
  2582. mutex_unlock(&sisusb->lock);
  2583. return retval;
  2584. }
  2585. #ifdef SISUSB_NEW_CONFIG_COMPAT
  2586. static long
  2587. sisusb_compat_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  2588. {
  2589. long retval;
  2590. switch (cmd) {
  2591. case SISUSB_GET_CONFIG_SIZE:
  2592. case SISUSB_GET_CONFIG:
  2593. case SISUSB_COMMAND:
  2594. lock_kernel();
  2595. retval = sisusb_ioctl(f->f_dentry->d_inode, f, cmd, arg);
  2596. unlock_kernel();
  2597. return retval;
  2598. default:
  2599. return -ENOIOCTLCMD;
  2600. }
  2601. }
  2602. #endif
  2603. static const struct file_operations usb_sisusb_fops = {
  2604. .owner = THIS_MODULE,
  2605. .open = sisusb_open,
  2606. .release = sisusb_release,
  2607. .read = sisusb_read,
  2608. .write = sisusb_write,
  2609. .llseek = sisusb_lseek,
  2610. #ifdef SISUSB_NEW_CONFIG_COMPAT
  2611. .compat_ioctl = sisusb_compat_ioctl,
  2612. #endif
  2613. .ioctl = sisusb_ioctl
  2614. };
  2615. static struct usb_class_driver usb_sisusb_class = {
  2616. .name = "sisusbvga%d",
  2617. .fops = &usb_sisusb_fops,
  2618. .minor_base = SISUSB_MINOR
  2619. };
  2620. static int sisusb_probe(struct usb_interface *intf,
  2621. const struct usb_device_id *id)
  2622. {
  2623. struct usb_device *dev = interface_to_usbdev(intf);
  2624. struct sisusb_usb_data *sisusb;
  2625. int retval = 0, i;
  2626. const char *memfail =
  2627. KERN_ERR
  2628. "sisusbvga[%d]: Failed to allocate memory for %s buffer\n";
  2629. printk(KERN_INFO "sisusb: USB2VGA dongle found at address %d\n",
  2630. dev->devnum);
  2631. /* Allocate memory for our private */
  2632. if (!(sisusb = kzalloc(sizeof(*sisusb), GFP_KERNEL))) {
  2633. printk(KERN_ERR
  2634. "sisusb: Failed to allocate memory for private data\n");
  2635. return -ENOMEM;
  2636. }
  2637. kref_init(&sisusb->kref);
  2638. mutex_init(&(sisusb->lock));
  2639. /* Register device */
  2640. if ((retval = usb_register_dev(intf, &usb_sisusb_class))) {
  2641. printk(KERN_ERR
  2642. "sisusb: Failed to get a minor for device %d\n",
  2643. dev->devnum);
  2644. retval = -ENODEV;
  2645. goto error_1;
  2646. }
  2647. sisusb->sisusb_dev = dev;
  2648. sisusb->minor = intf->minor;
  2649. sisusb->vrambase = SISUSB_PCI_MEMBASE;
  2650. sisusb->mmiobase = SISUSB_PCI_MMIOBASE;
  2651. sisusb->mmiosize = SISUSB_PCI_MMIOSIZE;
  2652. sisusb->ioportbase = SISUSB_PCI_IOPORTBASE;
  2653. /* Everything else is zero */
  2654. /* Allocate buffers */
  2655. sisusb->ibufsize = SISUSB_IBUF_SIZE;
  2656. if (!(sisusb->ibuf = usb_buffer_alloc(dev, SISUSB_IBUF_SIZE,
  2657. GFP_KERNEL, &sisusb->transfer_dma_in))) {
  2658. printk(memfail, "input", sisusb->minor);
  2659. retval = -ENOMEM;
  2660. goto error_2;
  2661. }
  2662. sisusb->numobufs = 0;
  2663. sisusb->obufsize = SISUSB_OBUF_SIZE;
  2664. for (i = 0; i < NUMOBUFS; i++) {
  2665. if (!(sisusb->obuf[i] = usb_buffer_alloc(dev, SISUSB_OBUF_SIZE,
  2666. GFP_KERNEL,
  2667. &sisusb->transfer_dma_out[i]))) {
  2668. if (i == 0) {
  2669. printk(memfail, "output", sisusb->minor);
  2670. retval = -ENOMEM;
  2671. goto error_3;
  2672. }
  2673. break;
  2674. } else
  2675. sisusb->numobufs++;
  2676. }
  2677. /* Allocate URBs */
  2678. if (!(sisusb->sisurbin = usb_alloc_urb(0, GFP_KERNEL))) {
  2679. printk(KERN_ERR
  2680. "sisusbvga[%d]: Failed to allocate URBs\n",
  2681. sisusb->minor);
  2682. retval = -ENOMEM;
  2683. goto error_3;
  2684. }
  2685. sisusb->completein = 1;
  2686. for (i = 0; i < sisusb->numobufs; i++) {
  2687. if (!(sisusb->sisurbout[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  2688. printk(KERN_ERR
  2689. "sisusbvga[%d]: Failed to allocate URBs\n",
  2690. sisusb->minor);
  2691. retval = -ENOMEM;
  2692. goto error_4;
  2693. }
  2694. sisusb->urbout_context[i].sisusb = (void *)sisusb;
  2695. sisusb->urbout_context[i].urbindex = i;
  2696. sisusb->urbstatus[i] = 0;
  2697. }
  2698. printk(KERN_INFO "sisusbvga[%d]: Allocated %d output buffers\n",
  2699. sisusb->minor, sisusb->numobufs);
  2700. #ifdef INCL_SISUSB_CON
  2701. /* Allocate our SiS_Pr */
  2702. if (!(sisusb->SiS_Pr = kmalloc(sizeof(struct SiS_Private), GFP_KERNEL))) {
  2703. printk(KERN_ERR
  2704. "sisusbvga[%d]: Failed to allocate SiS_Pr\n",
  2705. sisusb->minor);
  2706. }
  2707. #endif
  2708. /* Do remaining init stuff */
  2709. init_waitqueue_head(&sisusb->wait_q);
  2710. usb_set_intfdata(intf, sisusb);
  2711. usb_get_dev(sisusb->sisusb_dev);
  2712. sisusb->present = 1;
  2713. #ifdef SISUSB_OLD_CONFIG_COMPAT
  2714. {
  2715. int ret;
  2716. /* Our ioctls are all "32/64bit compatible" */
  2717. ret = register_ioctl32_conversion(SISUSB_GET_CONFIG_SIZE, NULL);
  2718. ret |= register_ioctl32_conversion(SISUSB_GET_CONFIG, NULL);
  2719. ret |= register_ioctl32_conversion(SISUSB_COMMAND, NULL);
  2720. if (ret)
  2721. printk(KERN_ERR
  2722. "sisusbvga[%d]: Error registering ioctl32 "
  2723. "translations\n",
  2724. sisusb->minor);
  2725. else
  2726. sisusb->ioctl32registered = 1;
  2727. }
  2728. #endif
  2729. if (dev->speed == USB_SPEED_HIGH) {
  2730. int initscreen = 1;
  2731. #ifdef INCL_SISUSB_CON
  2732. if (sisusb_first_vc > 0 &&
  2733. sisusb_last_vc > 0 &&
  2734. sisusb_first_vc <= sisusb_last_vc &&
  2735. sisusb_last_vc <= MAX_NR_CONSOLES)
  2736. initscreen = 0;
  2737. #endif
  2738. if (sisusb_init_gfxdevice(sisusb, initscreen))
  2739. printk(KERN_ERR
  2740. "sisusbvga[%d]: Failed to early "
  2741. "initialize device\n",
  2742. sisusb->minor);
  2743. } else
  2744. printk(KERN_INFO
  2745. "sisusbvga[%d]: Not attached to USB 2.0 hub, "
  2746. "deferring init\n",
  2747. sisusb->minor);
  2748. sisusb->ready = 1;
  2749. #ifdef SISUSBENDIANTEST
  2750. printk(KERN_DEBUG "sisusb: *** RWTEST ***\n");
  2751. sisusb_testreadwrite(sisusb);
  2752. printk(KERN_DEBUG "sisusb: *** RWTEST END ***\n");
  2753. #endif
  2754. #ifdef INCL_SISUSB_CON
  2755. sisusb_console_init(sisusb, sisusb_first_vc, sisusb_last_vc);
  2756. #endif
  2757. return 0;
  2758. error_4:
  2759. sisusb_free_urbs(sisusb);
  2760. error_3:
  2761. sisusb_free_buffers(sisusb);
  2762. error_2:
  2763. usb_deregister_dev(intf, &usb_sisusb_class);
  2764. error_1:
  2765. kfree(sisusb);
  2766. return retval;
  2767. }
  2768. static void sisusb_disconnect(struct usb_interface *intf)
  2769. {
  2770. struct sisusb_usb_data *sisusb;
  2771. int minor;
  2772. /* This should *not* happen */
  2773. if (!(sisusb = usb_get_intfdata(intf)))
  2774. return;
  2775. #ifdef INCL_SISUSB_CON
  2776. sisusb_console_exit(sisusb);
  2777. #endif
  2778. /* The above code doesn't need the disconnect
  2779. * semaphore to be down; its meaning is to
  2780. * protect all other routines from the disconnect
  2781. * case, not the other way round.
  2782. */
  2783. mutex_lock(&disconnect_mutex);
  2784. mutex_lock(&sisusb->lock);
  2785. /* Wait for all URBs to complete and kill them in case (MUST do) */
  2786. if (!sisusb_wait_all_out_complete(sisusb))
  2787. sisusb_kill_all_busy(sisusb);
  2788. minor = sisusb->minor;
  2789. usb_set_intfdata(intf, NULL);
  2790. usb_deregister_dev(intf, &usb_sisusb_class);
  2791. #ifdef SISUSB_OLD_CONFIG_COMPAT
  2792. if (sisusb->ioctl32registered) {
  2793. int ret;
  2794. sisusb->ioctl32registered = 0;
  2795. ret = unregister_ioctl32_conversion(SISUSB_GET_CONFIG_SIZE);
  2796. ret |= unregister_ioctl32_conversion(SISUSB_GET_CONFIG);
  2797. ret |= unregister_ioctl32_conversion(SISUSB_COMMAND);
  2798. if (ret) {
  2799. printk(KERN_ERR
  2800. "sisusbvga[%d]: Error unregistering "
  2801. "ioctl32 translations\n",
  2802. minor);
  2803. }
  2804. }
  2805. #endif
  2806. sisusb->present = 0;
  2807. sisusb->ready = 0;
  2808. mutex_unlock(&sisusb->lock);
  2809. /* decrement our usage count */
  2810. kref_put(&sisusb->kref, sisusb_delete);
  2811. mutex_unlock(&disconnect_mutex);
  2812. printk(KERN_INFO "sisusbvga[%d]: Disconnected\n", minor);
  2813. }
  2814. static struct usb_device_id sisusb_table [] = {
  2815. { USB_DEVICE(0x0711, 0x0900) },
  2816. { USB_DEVICE(0x0711, 0x0901) },
  2817. { USB_DEVICE(0x0711, 0x0902) },
  2818. { USB_DEVICE(0x182d, 0x021c) },
  2819. { USB_DEVICE(0x182d, 0x0269) },
  2820. { }
  2821. };
  2822. MODULE_DEVICE_TABLE (usb, sisusb_table);
  2823. static struct usb_driver sisusb_driver = {
  2824. .name = "sisusb",
  2825. .probe = sisusb_probe,
  2826. .disconnect = sisusb_disconnect,
  2827. .id_table = sisusb_table,
  2828. };
  2829. static int __init usb_sisusb_init(void)
  2830. {
  2831. int retval;
  2832. #ifdef INCL_SISUSB_CON
  2833. sisusb_init_concode();
  2834. #endif
  2835. if (!(retval = usb_register(&sisusb_driver))) {
  2836. printk(KERN_INFO "sisusb: Driver version %d.%d.%d\n",
  2837. SISUSB_VERSION, SISUSB_REVISION, SISUSB_PATCHLEVEL);
  2838. printk(KERN_INFO
  2839. "sisusb: Copyright (C) 2005 Thomas Winischhofer\n");
  2840. }
  2841. return retval;
  2842. }
  2843. static void __exit usb_sisusb_exit(void)
  2844. {
  2845. usb_deregister(&sisusb_driver);
  2846. }
  2847. module_init(usb_sisusb_init);
  2848. module_exit(usb_sisusb_exit);
  2849. MODULE_AUTHOR("Thomas Winischhofer <thomas@winischhofer.net>");
  2850. MODULE_DESCRIPTION("sisusbvga - Driver for Net2280/SiS315-based USB2VGA dongles");
  2851. MODULE_LICENSE("GPL");