sisusb.c 75 KB

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