sisusb.c 83 KB

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