sisusb.c 75 KB

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