sisusb.c 75 KB

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