sisusbvga.c 69 KB

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