cdu31a.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250
  1. /*
  2. * Sony CDU-31A CDROM interface device driver.
  3. *
  4. * Corey Minyard (minyard@wf-rch.cirr.com)
  5. *
  6. * Colossians 3:17
  7. *
  8. * See Documentation/cdrom/cdu31a for additional details about this driver.
  9. *
  10. * The Sony interface device driver handles Sony interface CDROM
  11. * drives and provides a complete block-level interface as well as an
  12. * ioctl() interface compatible with the Sun (as specified in
  13. * include/linux/cdrom.h). With this interface, CDROMs can be
  14. * accessed and standard audio CDs can be played back normally.
  15. *
  16. * WARNING - All autoprobes have been removed from the driver.
  17. * You MUST configure the CDU31A via a LILO config
  18. * at boot time or in lilo.conf. I have the
  19. * following in my lilo.conf:
  20. *
  21. * append="cdu31a=0x1f88,0,PAS"
  22. *
  23. * The first number is the I/O base address of the
  24. * card. The second is the interrupt (0 means none).
  25. * The third should be "PAS" if on a Pro-Audio
  26. * spectrum, or nothing if on something else.
  27. *
  28. * This interface is (unfortunately) a polled interface. This is
  29. * because most Sony interfaces are set up with DMA and interrupts
  30. * disables. Some (like mine) do not even have the capability to
  31. * handle interrupts or DMA. For this reason you will see a lot of
  32. * the following:
  33. *
  34. * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
  35. * while (time_before(jiffies, retry_count) && (! <some condition to wait for))
  36. * {
  37. * while (handle_sony_cd_attention())
  38. * ;
  39. *
  40. * sony_sleep();
  41. * }
  42. * if (the condition not met)
  43. * {
  44. * return an error;
  45. * }
  46. *
  47. * This ugly hack waits for something to happen, sleeping a little
  48. * between every try. it also handles attentions, which are
  49. * asynchronous events from the drive informing the driver that a disk
  50. * has been inserted, removed, etc.
  51. *
  52. * NEWS FLASH - The driver now supports interrupts but they are
  53. * turned off by default. Use of interrupts is highly encouraged, it
  54. * cuts CPU usage down to a reasonable level. I had DMA in for a while
  55. * but PC DMA is just too slow. Better to just insb() it.
  56. *
  57. * One thing about these drives: They talk in MSF (Minute Second Frame) format.
  58. * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
  59. * disk. The funny thing is that these are sent to the drive in BCD, but the
  60. * interface wants to see them in decimal. A lot of conversion goes on.
  61. *
  62. * DRIVER SPECIAL FEATURES
  63. * -----------------------
  64. *
  65. * This section describes features beyond the normal audio and CD-ROM
  66. * functions of the drive.
  67. *
  68. * XA compatibility
  69. *
  70. * The driver should support XA disks for both the CDU31A and CDU33A.
  71. * It does this transparently, the using program doesn't need to set it.
  72. *
  73. * Multi-Session
  74. *
  75. * A multi-session disk looks just like a normal disk to the user.
  76. * Just mount one normally, and all the data should be there.
  77. * A special thanks to Koen for help with this!
  78. *
  79. * Raw sector I/O
  80. *
  81. * Using the CDROMREADAUDIO it is possible to read raw audio and data
  82. * tracks. Both operations return 2352 bytes per sector. On the data
  83. * tracks, the first 12 bytes is not returned by the drive and the value
  84. * of that data is indeterminate.
  85. *
  86. *
  87. * Copyright (C) 1993 Corey Minyard
  88. *
  89. * This program is free software; you can redistribute it and/or modify
  90. * it under the terms of the GNU General Public License as published by
  91. * the Free Software Foundation; either version 2 of the License, or
  92. * (at your option) any later version.
  93. *
  94. * This program is distributed in the hope that it will be useful,
  95. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  96. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  97. * GNU General Public License for more details.
  98. *
  99. * You should have received a copy of the GNU General Public License
  100. * along with this program; if not, write to the Free Software
  101. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  102. *
  103. * TODO:
  104. * CDs with form1 and form2 sectors cause problems
  105. * with current read-ahead strategy.
  106. *
  107. * Credits:
  108. * Heiko Eissfeldt <heiko@colossus.escape.de>
  109. * For finding abug in the return of the track numbers.
  110. * TOC processing redone for proper multisession support.
  111. *
  112. *
  113. * It probably a little late to be adding a history, but I guess I
  114. * will start.
  115. *
  116. * 10/24/95 - Added support for disabling the eject button when the
  117. * drive is open. Note that there is a small problem
  118. * still here, if the eject button is pushed while the
  119. * drive light is flashing, the drive will return a bad
  120. * status and be reset. It recovers, though.
  121. *
  122. * 03/07/97 - Fixed a problem with timers.
  123. *
  124. *
  125. * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
  126. * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
  127. * changes by Erik Andersen <andersee@debian.org>
  128. *
  129. * 24 January 1998 -- Removed the scd_disc_status() function, which was now
  130. * just dead code left over from the port.
  131. * Erik Andersen <andersee@debian.org>
  132. *
  133. * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
  134. * <kodis@jagunet.com>. Work begun on fixing driver to
  135. * work under 2.1.X. Added temporary extra printks
  136. * which seem to slow it down enough to work.
  137. *
  138. * 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
  139. * Removed init_module & cleanup_module in favor of
  140. * module_init & module_exit.
  141. * Torben Mathiasen <tmm@image.dk>
  142. *
  143. * 22 October 2004 -- Make the driver work in 2.6.X
  144. * Added workaround to fix hard lockups on eject
  145. * Fixed door locking problem after mounting empty drive
  146. * Set double-speed drives to double speed by default
  147. * Removed all readahead things - not needed anymore
  148. * Ondrej Zary <rainbow@rainbow-software.org>
  149. */
  150. #define DEBUG 1
  151. #include <linux/major.h>
  152. #include <linux/module.h>
  153. #include <linux/errno.h>
  154. #include <linux/signal.h>
  155. #include <linux/sched.h>
  156. #include <linux/timer.h>
  157. #include <linux/fs.h>
  158. #include <linux/kernel.h>
  159. #include <linux/hdreg.h>
  160. #include <linux/genhd.h>
  161. #include <linux/ioport.h>
  162. #include <linux/devfs_fs_kernel.h>
  163. #include <linux/string.h>
  164. #include <linux/slab.h>
  165. #include <linux/init.h>
  166. #include <linux/interrupt.h>
  167. #include <linux/cdrom.h>
  168. #include <asm/system.h>
  169. #include <asm/io.h>
  170. #include <asm/uaccess.h>
  171. #include <asm/dma.h>
  172. #include "cdu31a.h"
  173. #define MAJOR_NR CDU31A_CDROM_MAJOR
  174. #include <linux/blkdev.h>
  175. #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
  176. #define PFX "CDU31A: "
  177. /*
  178. ** Edit the following data to change interrupts, DMA channels, etc.
  179. ** Default is polled and no DMA. DMA is not recommended for double-speed
  180. ** drives.
  181. */
  182. static struct {
  183. unsigned short base; /* I/O Base Address */
  184. short int_num; /* Interrupt Number (-1 means scan for it,
  185. 0 means don't use) */
  186. } cdu31a_addresses[] __initdata = {
  187. {0}
  188. };
  189. static int handle_sony_cd_attention(void);
  190. static int read_subcode(void);
  191. static void sony_get_toc(void);
  192. static int scd_spinup(void);
  193. /*static int scd_open(struct inode *inode, struct file *filp);*/
  194. static int scd_open(struct cdrom_device_info *, int);
  195. static void do_sony_cd_cmd(unsigned char cmd,
  196. unsigned char *params,
  197. unsigned int num_params,
  198. unsigned char *result_buffer,
  199. unsigned int *result_size);
  200. static void size_to_buf(unsigned int size, unsigned char *buf);
  201. /* Parameters for the read-ahead. */
  202. static unsigned int sony_next_block; /* Next 512 byte block offset */
  203. static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
  204. in the current read command. */
  205. /* The base I/O address of the Sony Interface. This is a variable (not a
  206. #define) so it can be easily changed via some future ioctl() */
  207. static unsigned int cdu31a_port = 0;
  208. module_param(cdu31a_port, uint, 0);
  209. /*
  210. * The following are I/O addresses of the various registers for the drive. The
  211. * comment for the base address also applies here.
  212. */
  213. static volatile unsigned short sony_cd_cmd_reg;
  214. static volatile unsigned short sony_cd_param_reg;
  215. static volatile unsigned short sony_cd_write_reg;
  216. static volatile unsigned short sony_cd_control_reg;
  217. static volatile unsigned short sony_cd_status_reg;
  218. static volatile unsigned short sony_cd_result_reg;
  219. static volatile unsigned short sony_cd_read_reg;
  220. static volatile unsigned short sony_cd_fifost_reg;
  221. static struct request_queue *cdu31a_queue;
  222. static DEFINE_SPINLOCK(cdu31a_lock); /* queue lock */
  223. static int sony_spun_up = 0; /* Has the drive been spun up? */
  224. static int sony_speed = 0; /* Last wanted speed */
  225. static int sony_xa_mode = 0; /* Is an XA disk in the drive
  226. and the drive a CDU31A? */
  227. static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
  228. For raw data reads. */
  229. static unsigned int sony_usage = 0; /* How many processes have the
  230. drive open. */
  231. static int sony_pas_init = 0; /* Initialize the Pro-Audio
  232. Spectrum card? */
  233. static struct s_sony_session_toc single_toc; /* Holds the
  234. table of
  235. contents. */
  236. static struct s_all_sessions_toc sony_toc; /* entries gathered from all
  237. sessions */
  238. static int sony_toc_read = 0; /* Has the TOC been read for
  239. the drive? */
  240. static struct s_sony_subcode last_sony_subcode; /* Points to the last
  241. subcode address read */
  242. static DECLARE_MUTEX(sony_sem); /* Semaphore for drive hardware access */
  243. static int is_double_speed = 0; /* does the drive support double speed ? */
  244. static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
  245. /*
  246. * The audio status uses the values from read subchannel data as specified
  247. * in include/linux/cdrom.h.
  248. */
  249. static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
  250. /*
  251. * The following are a hack for pausing and resuming audio play. The drive
  252. * does not work as I would expect it, if you stop it then start it again,
  253. * the drive seeks back to the beginning and starts over. This holds the
  254. * position during a pause so a resume can restart it. It uses the
  255. * audio status variable above to tell if it is paused.
  256. */
  257. static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
  258. static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
  259. /* What IRQ is the drive using? 0 if none. */
  260. static int cdu31a_irq = 0;
  261. module_param(cdu31a_irq, int, 0);
  262. /* The interrupt handler will wake this queue up when it gets an
  263. interrupts. */
  264. static DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
  265. static int irq_flag = 0;
  266. static int curr_control_reg = 0; /* Current value of the control register */
  267. /* A disk changed variable. When a disk change is detected, it will
  268. all be set to TRUE. As the upper layers ask for disk_changed status
  269. it will be cleared. */
  270. static char disk_changed;
  271. /* This was readahead_buffer once... Now it's used only for audio reads */
  272. static char audio_buffer[CD_FRAMESIZE_RAW];
  273. /* Used to time a short period to abort an operation after the
  274. drive has been idle for a while. This keeps the light on
  275. the drive from flashing for very long. */
  276. static struct timer_list cdu31a_abort_timer;
  277. /* Marks if the timeout has started an abort read. This is used
  278. on entry to the drive to tell the code to read out the status
  279. from the abort read. */
  280. static int abort_read_started = 0;
  281. /*
  282. * Uniform cdrom interface function
  283. * report back, if disc has changed from time of last request.
  284. */
  285. static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
  286. {
  287. int retval;
  288. retval = disk_changed;
  289. disk_changed = 0;
  290. return retval;
  291. }
  292. /*
  293. * Uniform cdrom interface function
  294. * report back, if drive is ready
  295. */
  296. static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
  297. {
  298. if (CDSL_CURRENT != slot_nr)
  299. /* we have no changer support */
  300. return -EINVAL;
  301. if (sony_spun_up)
  302. return CDS_DISC_OK;
  303. if (down_interruptible(&sony_sem))
  304. return -ERESTARTSYS;
  305. if (scd_spinup() == 0)
  306. sony_spun_up = 1;
  307. up(&sony_sem);
  308. return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
  309. }
  310. static inline void enable_interrupts(void)
  311. {
  312. curr_control_reg |= (SONY_ATTN_INT_EN_BIT
  313. | SONY_RES_RDY_INT_EN_BIT
  314. | SONY_DATA_RDY_INT_EN_BIT);
  315. outb(curr_control_reg, sony_cd_control_reg);
  316. }
  317. static inline void disable_interrupts(void)
  318. {
  319. curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
  320. | SONY_RES_RDY_INT_EN_BIT
  321. | SONY_DATA_RDY_INT_EN_BIT);
  322. outb(curr_control_reg, sony_cd_control_reg);
  323. }
  324. /*
  325. * Wait a little while (used for polling the drive). If in initialization,
  326. * setting a timeout doesn't work, so just loop for a while.
  327. */
  328. static inline void sony_sleep(void)
  329. {
  330. if (cdu31a_irq <= 0) {
  331. yield();
  332. } else { /* Interrupt driven */
  333. DEFINE_WAIT(w);
  334. int first = 1;
  335. while (1) {
  336. prepare_to_wait(&cdu31a_irq_wait, &w,
  337. TASK_INTERRUPTIBLE);
  338. if (first) {
  339. enable_interrupts();
  340. first = 0;
  341. }
  342. if (irq_flag != 0)
  343. break;
  344. if (!signal_pending(current)) {
  345. schedule();
  346. continue;
  347. } else
  348. disable_interrupts();
  349. break;
  350. }
  351. finish_wait(&cdu31a_irq_wait, &w);
  352. irq_flag = 0;
  353. }
  354. }
  355. /*
  356. * The following are convenience routine to read various status and set
  357. * various conditions in the drive.
  358. */
  359. static inline int is_attention(void)
  360. {
  361. return (inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0;
  362. }
  363. static inline int is_busy(void)
  364. {
  365. return (inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0;
  366. }
  367. static inline int is_data_ready(void)
  368. {
  369. return (inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0;
  370. }
  371. static inline int is_data_requested(void)
  372. {
  373. return (inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0;
  374. }
  375. static inline int is_result_ready(void)
  376. {
  377. return (inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0;
  378. }
  379. static inline int is_param_write_rdy(void)
  380. {
  381. return (inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0;
  382. }
  383. static inline int is_result_reg_not_empty(void)
  384. {
  385. return (inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0;
  386. }
  387. static inline void reset_drive(void)
  388. {
  389. curr_control_reg = 0;
  390. sony_toc_read = 0;
  391. outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
  392. }
  393. /*
  394. * Uniform cdrom interface function
  395. * reset drive and return when it is ready
  396. */
  397. static int scd_reset(struct cdrom_device_info *cdi)
  398. {
  399. unsigned long retry_count;
  400. if (down_interruptible(&sony_sem))
  401. return -ERESTARTSYS;
  402. reset_drive();
  403. retry_count = jiffies + SONY_RESET_TIMEOUT;
  404. while (time_before(jiffies, retry_count) && (!is_attention())) {
  405. sony_sleep();
  406. }
  407. up(&sony_sem);
  408. return 0;
  409. }
  410. static inline void clear_attention(void)
  411. {
  412. outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
  413. }
  414. static inline void clear_result_ready(void)
  415. {
  416. outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
  417. }
  418. static inline void clear_data_ready(void)
  419. {
  420. outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
  421. sony_cd_control_reg);
  422. }
  423. static inline void clear_param_reg(void)
  424. {
  425. outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
  426. }
  427. static inline unsigned char read_status_register(void)
  428. {
  429. return inb(sony_cd_status_reg);
  430. }
  431. static inline unsigned char read_result_register(void)
  432. {
  433. return inb(sony_cd_result_reg);
  434. }
  435. static inline unsigned char read_data_register(void)
  436. {
  437. return inb(sony_cd_read_reg);
  438. }
  439. static inline void write_param(unsigned char param)
  440. {
  441. outb(param, sony_cd_param_reg);
  442. }
  443. static inline void write_cmd(unsigned char cmd)
  444. {
  445. outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
  446. sony_cd_control_reg);
  447. outb(cmd, sony_cd_cmd_reg);
  448. }
  449. static irqreturn_t cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  450. {
  451. unsigned char val;
  452. if (abort_read_started) {
  453. /* We might be waiting for an abort to finish. Don't
  454. disable interrupts yet, though, because we handle
  455. this one here. */
  456. /* Clear out the result registers. */
  457. while (is_result_reg_not_empty()) {
  458. val = read_result_register();
  459. }
  460. clear_data_ready();
  461. clear_result_ready();
  462. /* Clear out the data */
  463. while (is_data_requested()) {
  464. val = read_data_register();
  465. }
  466. abort_read_started = 0;
  467. /* If something was waiting, wake it up now. */
  468. if (waitqueue_active(&cdu31a_irq_wait)) {
  469. disable_interrupts();
  470. irq_flag = 1;
  471. wake_up_interruptible(&cdu31a_irq_wait);
  472. }
  473. } else if (waitqueue_active(&cdu31a_irq_wait)) {
  474. disable_interrupts();
  475. irq_flag = 1;
  476. wake_up_interruptible(&cdu31a_irq_wait);
  477. } else {
  478. disable_interrupts();
  479. printk(KERN_NOTICE PFX
  480. "Got an interrupt but nothing was waiting\n");
  481. }
  482. return IRQ_HANDLED;
  483. }
  484. /*
  485. * give more verbose error messages
  486. */
  487. static unsigned char *translate_error(unsigned char err_code)
  488. {
  489. static unsigned char errbuf[80];
  490. switch (err_code) {
  491. case 0x10: return "illegal command ";
  492. case 0x11: return "illegal parameter ";
  493. case 0x20: return "not loaded ";
  494. case 0x21: return "no disc ";
  495. case 0x22: return "not spinning ";
  496. case 0x23: return "spinning ";
  497. case 0x25: return "spindle servo ";
  498. case 0x26: return "focus servo ";
  499. case 0x29: return "eject mechanism ";
  500. case 0x2a: return "audio playing ";
  501. case 0x2c: return "emergency eject ";
  502. case 0x30: return "focus ";
  503. case 0x31: return "frame sync ";
  504. case 0x32: return "subcode address ";
  505. case 0x33: return "block sync ";
  506. case 0x34: return "header address ";
  507. case 0x40: return "illegal track read ";
  508. case 0x41: return "mode 0 read ";
  509. case 0x42: return "illegal mode read ";
  510. case 0x43: return "illegal block size read ";
  511. case 0x44: return "mode read ";
  512. case 0x45: return "form read ";
  513. case 0x46: return "leadout read ";
  514. case 0x47: return "buffer overrun ";
  515. case 0x53: return "unrecoverable CIRC ";
  516. case 0x57: return "unrecoverable LECC ";
  517. case 0x60: return "no TOC ";
  518. case 0x61: return "invalid subcode data ";
  519. case 0x63: return "focus on TOC read ";
  520. case 0x64: return "frame sync on TOC read ";
  521. case 0x65: return "TOC data ";
  522. case 0x70: return "hardware failure ";
  523. case 0x91: return "leadin ";
  524. case 0x92: return "leadout ";
  525. case 0x93: return "data track ";
  526. }
  527. sprintf(errbuf, "unknown 0x%02x ", err_code);
  528. return errbuf;
  529. }
  530. /*
  531. * Set the drive parameters so the drive will auto-spin-up when a
  532. * disk is inserted.
  533. */
  534. static void set_drive_params(int want_doublespeed)
  535. {
  536. unsigned char res_reg[12];
  537. unsigned int res_size;
  538. unsigned char params[3];
  539. params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
  540. params[1] = 0x00; /* Never spin down the drive. */
  541. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  542. params, 2, res_reg, &res_size);
  543. if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
  544. printk(KERN_NOTICE PFX
  545. "Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
  546. }
  547. params[0] = SONY_SD_MECH_CONTROL;
  548. params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
  549. if (is_auto_eject)
  550. params[1] |= SONY_AUTO_EJECT_BIT;
  551. if (is_double_speed && want_doublespeed) {
  552. params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
  553. possible */
  554. }
  555. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  556. params, 2, res_reg, &res_size);
  557. if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
  558. printk(KERN_NOTICE PFX "Unable to set mechanical "
  559. "parameters: 0x%2.2x\n", res_reg[1]);
  560. }
  561. }
  562. /*
  563. * Uniform cdrom interface function
  564. * select reading speed for data access
  565. */
  566. static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
  567. {
  568. if (speed == 0)
  569. sony_speed = 1;
  570. else
  571. sony_speed = speed - 1;
  572. if (down_interruptible(&sony_sem))
  573. return -ERESTARTSYS;
  574. set_drive_params(sony_speed);
  575. up(&sony_sem);
  576. return 0;
  577. }
  578. /*
  579. * Uniform cdrom interface function
  580. * lock or unlock eject button
  581. */
  582. static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
  583. {
  584. if (lock == 0) {
  585. is_auto_eject = 1;
  586. } else {
  587. is_auto_eject = 0;
  588. }
  589. if (down_interruptible(&sony_sem))
  590. return -ERESTARTSYS;
  591. set_drive_params(sony_speed);
  592. up(&sony_sem);
  593. return 0;
  594. }
  595. /*
  596. * This code will reset the drive and attempt to restore sane parameters.
  597. */
  598. static void restart_on_error(void)
  599. {
  600. unsigned char res_reg[12];
  601. unsigned int res_size;
  602. unsigned long retry_count;
  603. printk(KERN_NOTICE PFX "Resetting drive on error\n");
  604. reset_drive();
  605. retry_count = jiffies + SONY_RESET_TIMEOUT;
  606. while (time_before(jiffies, retry_count) && (!is_attention())) {
  607. sony_sleep();
  608. }
  609. set_drive_params(sony_speed);
  610. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
  611. if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
  612. printk(KERN_NOTICE PFX "Unable to spin up drive: 0x%2.2x\n",
  613. res_reg[1]);
  614. }
  615. msleep(2000);
  616. sony_get_toc();
  617. }
  618. /*
  619. * This routine writes data to the parameter register. Since this should
  620. * happen fairly fast, it is polled with no OS waits between.
  621. */
  622. static int write_params(unsigned char *params, int num_params)
  623. {
  624. unsigned int retry_count;
  625. retry_count = SONY_READY_RETRIES;
  626. while ((retry_count > 0) && (!is_param_write_rdy())) {
  627. retry_count--;
  628. }
  629. if (!is_param_write_rdy()) {
  630. return -EIO;
  631. }
  632. while (num_params > 0) {
  633. write_param(*params);
  634. params++;
  635. num_params--;
  636. }
  637. return 0;
  638. }
  639. /*
  640. * The following reads data from the command result register. It is a
  641. * fairly complex routine, all status info flows back through this
  642. * interface. The algorithm is stolen directly from the flowcharts in
  643. * the drive manual.
  644. */
  645. static void
  646. get_result(unsigned char *result_buffer, unsigned int *result_size)
  647. {
  648. unsigned char a, b;
  649. int i;
  650. unsigned long retry_count;
  651. while (handle_sony_cd_attention());
  652. /* Wait for the result data to be ready */
  653. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  654. while (time_before(jiffies, retry_count)
  655. && (is_busy() || (!(is_result_ready())))) {
  656. sony_sleep();
  657. while (handle_sony_cd_attention());
  658. }
  659. if (is_busy() || (!(is_result_ready()))) {
  660. pr_debug(PFX "timeout out %d\n", __LINE__);
  661. result_buffer[0] = 0x20;
  662. result_buffer[1] = SONY_TIMEOUT_OP_ERR;
  663. *result_size = 2;
  664. return;
  665. }
  666. /*
  667. * Get the first two bytes. This determines what else needs
  668. * to be done.
  669. */
  670. clear_result_ready();
  671. a = read_result_register();
  672. *result_buffer = a;
  673. result_buffer++;
  674. /* Check for block error status result. */
  675. if ((a & 0xf0) == 0x50) {
  676. *result_size = 1;
  677. return;
  678. }
  679. b = read_result_register();
  680. *result_buffer = b;
  681. result_buffer++;
  682. *result_size = 2;
  683. /*
  684. * 0x20 means an error occurred. Byte 2 will have the error code.
  685. * Otherwise, the command succeeded, byte 2 will have the count of
  686. * how many more status bytes are coming.
  687. *
  688. * The result register can be read 10 bytes at a time, a wait for
  689. * result ready to be asserted must be done between every 10 bytes.
  690. */
  691. if ((a & 0xf0) != 0x20) {
  692. if (b > 8) {
  693. for (i = 0; i < 8; i++) {
  694. *result_buffer = read_result_register();
  695. result_buffer++;
  696. (*result_size)++;
  697. }
  698. b = b - 8;
  699. while (b > 10) {
  700. retry_count = SONY_READY_RETRIES;
  701. while ((retry_count > 0)
  702. && (!is_result_ready())) {
  703. retry_count--;
  704. }
  705. if (!is_result_ready()) {
  706. pr_debug(PFX "timeout out %d\n",
  707. __LINE__);
  708. result_buffer[0] = 0x20;
  709. result_buffer[1] =
  710. SONY_TIMEOUT_OP_ERR;
  711. *result_size = 2;
  712. return;
  713. }
  714. clear_result_ready();
  715. for (i = 0; i < 10; i++) {
  716. *result_buffer =
  717. read_result_register();
  718. result_buffer++;
  719. (*result_size)++;
  720. }
  721. b = b - 10;
  722. }
  723. if (b > 0) {
  724. retry_count = SONY_READY_RETRIES;
  725. while ((retry_count > 0)
  726. && (!is_result_ready())) {
  727. retry_count--;
  728. }
  729. if (!is_result_ready()) {
  730. pr_debug(PFX "timeout out %d\n",
  731. __LINE__);
  732. result_buffer[0] = 0x20;
  733. result_buffer[1] =
  734. SONY_TIMEOUT_OP_ERR;
  735. *result_size = 2;
  736. return;
  737. }
  738. }
  739. }
  740. while (b > 0) {
  741. *result_buffer = read_result_register();
  742. result_buffer++;
  743. (*result_size)++;
  744. b--;
  745. }
  746. }
  747. }
  748. /*
  749. * Do a command that does not involve data transfer. This routine must
  750. * be re-entrant from the same task to support being called from the
  751. * data operation code when an error occurs.
  752. */
  753. static void
  754. do_sony_cd_cmd(unsigned char cmd,
  755. unsigned char *params,
  756. unsigned int num_params,
  757. unsigned char *result_buffer, unsigned int *result_size)
  758. {
  759. unsigned long retry_count;
  760. int num_retries = 0;
  761. retry_cd_operation:
  762. while (handle_sony_cd_attention());
  763. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  764. while (time_before(jiffies, retry_count) && (is_busy())) {
  765. sony_sleep();
  766. while (handle_sony_cd_attention());
  767. }
  768. if (is_busy()) {
  769. pr_debug(PFX "timeout out %d\n", __LINE__);
  770. result_buffer[0] = 0x20;
  771. result_buffer[1] = SONY_TIMEOUT_OP_ERR;
  772. *result_size = 2;
  773. } else {
  774. clear_result_ready();
  775. clear_param_reg();
  776. write_params(params, num_params);
  777. write_cmd(cmd);
  778. get_result(result_buffer, result_size);
  779. }
  780. if (((result_buffer[0] & 0xf0) == 0x20)
  781. && (num_retries < MAX_CDU31A_RETRIES)) {
  782. num_retries++;
  783. msleep(100);
  784. goto retry_cd_operation;
  785. }
  786. }
  787. /*
  788. * Handle an attention from the drive. This will return 1 if it found one
  789. * or 0 if not (if one is found, the caller might want to call again).
  790. *
  791. * This routine counts the number of consecutive times it is called
  792. * (since this is always called from a while loop until it returns
  793. * a 0), and returns a 0 if it happens too many times. This will help
  794. * prevent a lockup.
  795. */
  796. static int handle_sony_cd_attention(void)
  797. {
  798. unsigned char atten_code;
  799. static int num_consecutive_attentions = 0;
  800. volatile int val;
  801. #if 0
  802. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  803. #endif
  804. if (is_attention()) {
  805. if (num_consecutive_attentions >
  806. CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
  807. printk(KERN_NOTICE PFX "Too many consecutive "
  808. "attentions: %d\n", num_consecutive_attentions);
  809. num_consecutive_attentions = 0;
  810. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__,
  811. __LINE__);
  812. return 0;
  813. }
  814. clear_attention();
  815. atten_code = read_result_register();
  816. switch (atten_code) {
  817. /* Someone changed the CD. Mark it as changed */
  818. case SONY_MECH_LOADED_ATTN:
  819. disk_changed = 1;
  820. sony_toc_read = 0;
  821. sony_audio_status = CDROM_AUDIO_NO_STATUS;
  822. sony_blocks_left = 0;
  823. break;
  824. case SONY_SPIN_DOWN_COMPLETE_ATTN:
  825. /* Mark the disk as spun down. */
  826. sony_spun_up = 0;
  827. break;
  828. case SONY_AUDIO_PLAY_DONE_ATTN:
  829. sony_audio_status = CDROM_AUDIO_COMPLETED;
  830. read_subcode();
  831. break;
  832. case SONY_EJECT_PUSHED_ATTN:
  833. if (is_auto_eject) {
  834. sony_audio_status = CDROM_AUDIO_INVALID;
  835. }
  836. break;
  837. case SONY_LEAD_IN_ERR_ATTN:
  838. case SONY_LEAD_OUT_ERR_ATTN:
  839. case SONY_DATA_TRACK_ERR_ATTN:
  840. case SONY_AUDIO_PLAYBACK_ERR_ATTN:
  841. sony_audio_status = CDROM_AUDIO_ERROR;
  842. break;
  843. }
  844. num_consecutive_attentions++;
  845. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  846. return 1;
  847. } else if (abort_read_started) {
  848. while (is_result_reg_not_empty()) {
  849. val = read_result_register();
  850. }
  851. clear_data_ready();
  852. clear_result_ready();
  853. /* Clear out the data */
  854. while (is_data_requested()) {
  855. val = read_data_register();
  856. }
  857. abort_read_started = 0;
  858. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  859. return 1;
  860. }
  861. num_consecutive_attentions = 0;
  862. #if 0
  863. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  864. #endif
  865. return 0;
  866. }
  867. /* Convert from an integer 0-99 to BCD */
  868. static inline unsigned int int_to_bcd(unsigned int val)
  869. {
  870. int retval;
  871. retval = (val / 10) << 4;
  872. retval = retval | val % 10;
  873. return retval;
  874. }
  875. /* Convert from BCD to an integer from 0-99 */
  876. static unsigned int bcd_to_int(unsigned int bcd)
  877. {
  878. return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
  879. }
  880. /*
  881. * Convert a logical sector value (like the OS would want to use for
  882. * a block device) to an MSF format.
  883. */
  884. static void log_to_msf(unsigned int log, unsigned char *msf)
  885. {
  886. log = log + LOG_START_OFFSET;
  887. msf[0] = int_to_bcd(log / 4500);
  888. log = log % 4500;
  889. msf[1] = int_to_bcd(log / 75);
  890. msf[2] = int_to_bcd(log % 75);
  891. }
  892. /*
  893. * Convert an MSF format to a logical sector.
  894. */
  895. static unsigned int msf_to_log(unsigned char *msf)
  896. {
  897. unsigned int log;
  898. log = msf[2];
  899. log += msf[1] * 75;
  900. log += msf[0] * 4500;
  901. log = log - LOG_START_OFFSET;
  902. return log;
  903. }
  904. /*
  905. * Take in integer size value and put it into a buffer like
  906. * the drive would want to see a number-of-sector value.
  907. */
  908. static void size_to_buf(unsigned int size, unsigned char *buf)
  909. {
  910. buf[0] = size / 65536;
  911. size = size % 65536;
  912. buf[1] = size / 256;
  913. buf[2] = size % 256;
  914. }
  915. /* Starts a read operation. Returns 0 on success and 1 on failure.
  916. The read operation used here allows multiple sequential sectors
  917. to be read and status returned for each sector. The driver will
  918. read the output one at a time as the requests come and abort the
  919. operation if the requested sector is not the next one from the
  920. drive. */
  921. static int
  922. start_request(unsigned int sector, unsigned int nsect)
  923. {
  924. unsigned char params[6];
  925. unsigned long retry_count;
  926. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  927. log_to_msf(sector, params);
  928. size_to_buf(nsect, &params[3]);
  929. /*
  930. * Clear any outstanding attentions and wait for the drive to
  931. * complete any pending operations.
  932. */
  933. while (handle_sony_cd_attention());
  934. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  935. while (time_before(jiffies, retry_count) && (is_busy())) {
  936. sony_sleep();
  937. while (handle_sony_cd_attention());
  938. }
  939. if (is_busy()) {
  940. printk(KERN_NOTICE PFX "Timeout while waiting "
  941. "to issue command\n");
  942. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  943. return 1;
  944. } else {
  945. /* Issue the command */
  946. clear_result_ready();
  947. clear_param_reg();
  948. write_params(params, 6);
  949. write_cmd(SONY_READ_BLKERR_STAT_CMD);
  950. sony_blocks_left = nsect * 4;
  951. sony_next_block = sector * 4;
  952. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  953. return 0;
  954. }
  955. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  956. }
  957. /* Abort a pending read operation. Clear all the drive status variables. */
  958. static void abort_read(void)
  959. {
  960. unsigned char result_reg[2];
  961. int result_size;
  962. volatile int val;
  963. do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
  964. if ((result_reg[0] & 0xf0) == 0x20) {
  965. printk(KERN_ERR PFX "Aborting read, %s error\n",
  966. translate_error(result_reg[1]));
  967. }
  968. while (is_result_reg_not_empty()) {
  969. val = read_result_register();
  970. }
  971. clear_data_ready();
  972. clear_result_ready();
  973. /* Clear out the data */
  974. while (is_data_requested()) {
  975. val = read_data_register();
  976. }
  977. sony_blocks_left = 0;
  978. }
  979. /* Called when the timer times out. This will abort the
  980. pending read operation. */
  981. static void handle_abort_timeout(unsigned long data)
  982. {
  983. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  984. /* If it is in use, ignore it. */
  985. if (down_trylock(&sony_sem) == 0) {
  986. /* We can't use abort_read(), because it will sleep
  987. or schedule in the timer interrupt. Just start
  988. the operation, finish it on the next access to
  989. the drive. */
  990. clear_result_ready();
  991. clear_param_reg();
  992. write_cmd(SONY_ABORT_CMD);
  993. sony_blocks_left = 0;
  994. abort_read_started = 1;
  995. up(&sony_sem);
  996. }
  997. pr_debug(PFX "Leaving %s\n", __FUNCTION__);
  998. }
  999. /* Actually get one sector of data from the drive. */
  1000. static void
  1001. input_data_sector(char *buffer)
  1002. {
  1003. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  1004. /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
  1005. the disk. The real data is after that. We can use audio_buffer. */
  1006. if (sony_xa_mode)
  1007. insb(sony_cd_read_reg, audio_buffer, CD_XA_HEAD);
  1008. clear_data_ready();
  1009. insb(sony_cd_read_reg, buffer, 2048);
  1010. /* If an XA disk, we have to clear out the rest of the unused
  1011. error correction data. We can use audio_buffer for that. */
  1012. if (sony_xa_mode)
  1013. insb(sony_cd_read_reg, audio_buffer, CD_XA_TAIL);
  1014. pr_debug(PFX "Leaving %s\n", __FUNCTION__);
  1015. }
  1016. /* read data from the drive. Note the nsect must be <= 4. */
  1017. static void
  1018. read_data_block(char *buffer,
  1019. unsigned int block,
  1020. unsigned int nblocks,
  1021. unsigned char res_reg[], int *res_size)
  1022. {
  1023. unsigned long retry_count;
  1024. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  1025. res_reg[0] = 0;
  1026. res_reg[1] = 0;
  1027. *res_size = 0;
  1028. /* Wait for the drive to tell us we have something */
  1029. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  1030. while (time_before(jiffies, retry_count) && !(is_data_ready())) {
  1031. while (handle_sony_cd_attention());
  1032. sony_sleep();
  1033. }
  1034. if (!(is_data_ready())) {
  1035. if (is_result_ready()) {
  1036. get_result(res_reg, res_size);
  1037. if ((res_reg[0] & 0xf0) != 0x20) {
  1038. printk(KERN_NOTICE PFX "Got result that should"
  1039. " have been error: %d\n", res_reg[0]);
  1040. res_reg[0] = 0x20;
  1041. res_reg[1] = SONY_BAD_DATA_ERR;
  1042. *res_size = 2;
  1043. }
  1044. abort_read();
  1045. } else {
  1046. pr_debug(PFX "timeout out %d\n", __LINE__);
  1047. res_reg[0] = 0x20;
  1048. res_reg[1] = SONY_TIMEOUT_OP_ERR;
  1049. *res_size = 2;
  1050. abort_read();
  1051. }
  1052. } else {
  1053. input_data_sector(buffer);
  1054. sony_blocks_left -= nblocks;
  1055. sony_next_block += nblocks;
  1056. /* Wait for the status from the drive. */
  1057. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  1058. while (time_before(jiffies, retry_count)
  1059. && !(is_result_ready())) {
  1060. while (handle_sony_cd_attention());
  1061. sony_sleep();
  1062. }
  1063. if (!is_result_ready()) {
  1064. pr_debug(PFX "timeout out %d\n", __LINE__);
  1065. res_reg[0] = 0x20;
  1066. res_reg[1] = SONY_TIMEOUT_OP_ERR;
  1067. *res_size = 2;
  1068. abort_read();
  1069. } else {
  1070. get_result(res_reg, res_size);
  1071. /* If we got a buffer status, handle that. */
  1072. if ((res_reg[0] & 0xf0) == 0x50) {
  1073. if ((res_reg[0] ==
  1074. SONY_NO_CIRC_ERR_BLK_STAT)
  1075. || (res_reg[0] ==
  1076. SONY_NO_LECC_ERR_BLK_STAT)
  1077. || (res_reg[0] ==
  1078. SONY_RECOV_LECC_ERR_BLK_STAT)) {
  1079. /* nothing here */
  1080. } else {
  1081. printk(KERN_ERR PFX "Data block "
  1082. "error: 0x%x\n", res_reg[0]);
  1083. res_reg[0] = 0x20;
  1084. res_reg[1] = SONY_BAD_DATA_ERR;
  1085. *res_size = 2;
  1086. }
  1087. /* Final transfer is done for read command, get final result. */
  1088. if (sony_blocks_left == 0) {
  1089. get_result(res_reg, res_size);
  1090. }
  1091. } else if ((res_reg[0] & 0xf0) != 0x20) {
  1092. /* The drive gave me bad status, I don't know what to do.
  1093. Reset the driver and return an error. */
  1094. printk(KERN_ERR PFX "Invalid block "
  1095. "status: 0x%x\n", res_reg[0]);
  1096. restart_on_error();
  1097. res_reg[0] = 0x20;
  1098. res_reg[1] = SONY_BAD_DATA_ERR;
  1099. *res_size = 2;
  1100. }
  1101. }
  1102. }
  1103. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  1104. }
  1105. /*
  1106. * The OS calls this to perform a read or write operation to the drive.
  1107. * Write obviously fail. Reads to a read ahead of sony_buffer_size
  1108. * bytes to help speed operations. This especially helps since the OS
  1109. * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
  1110. * data access on a CD is done sequentially, this saves a lot of operations.
  1111. */
  1112. static void do_cdu31a_request(request_queue_t * q)
  1113. {
  1114. struct request *req;
  1115. int block, nblock, num_retries;
  1116. unsigned char res_reg[12];
  1117. unsigned int res_size;
  1118. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  1119. spin_unlock_irq(q->queue_lock);
  1120. if (down_interruptible(&sony_sem)) {
  1121. spin_lock_irq(q->queue_lock);
  1122. return;
  1123. }
  1124. /* Get drive status before doing anything. */
  1125. while (handle_sony_cd_attention());
  1126. /* Make sure we have a valid TOC. */
  1127. sony_get_toc();
  1128. /* Make sure the timer is cancelled. */
  1129. del_timer(&cdu31a_abort_timer);
  1130. while (1) {
  1131. /*
  1132. * The beginning here is stolen from the hard disk driver. I hope
  1133. * it's right.
  1134. */
  1135. req = elv_next_request(q);
  1136. if (!req)
  1137. goto end_do_cdu31a_request;
  1138. if (!sony_spun_up)
  1139. scd_spinup();
  1140. block = req->sector;
  1141. nblock = req->nr_sectors;
  1142. pr_debug(PFX "request at block %d, length %d blocks\n",
  1143. block, nblock);
  1144. if (!sony_toc_read) {
  1145. printk(KERN_NOTICE PFX "TOC not read\n");
  1146. end_request(req, 0);
  1147. continue;
  1148. }
  1149. /* WTF??? */
  1150. if (!(req->flags & REQ_CMD))
  1151. continue;
  1152. if (rq_data_dir(req) == WRITE) {
  1153. end_request(req, 0);
  1154. continue;
  1155. }
  1156. /*
  1157. * If the block address is invalid or the request goes beyond the end of
  1158. * the media, return an error.
  1159. */
  1160. if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba) {
  1161. printk(KERN_NOTICE PFX "Request past end of media\n");
  1162. end_request(req, 0);
  1163. continue;
  1164. }
  1165. if (nblock > 4)
  1166. nblock = 4;
  1167. num_retries = 0;
  1168. try_read_again:
  1169. while (handle_sony_cd_attention());
  1170. if (!sony_toc_read) {
  1171. printk(KERN_NOTICE PFX "TOC not read\n");
  1172. end_request(req, 0);
  1173. continue;
  1174. }
  1175. /* If no data is left to be read from the drive, start the
  1176. next request. */
  1177. if (sony_blocks_left == 0) {
  1178. if (start_request(block / 4, nblock / 4)) {
  1179. end_request(req, 0);
  1180. continue;
  1181. }
  1182. }
  1183. /* If the requested block is not the next one waiting in
  1184. the driver, abort the current operation and start a
  1185. new one. */
  1186. else if (block != sony_next_block) {
  1187. pr_debug(PFX "Read for block %d, expected %d\n",
  1188. block, sony_next_block);
  1189. abort_read();
  1190. if (!sony_toc_read) {
  1191. printk(KERN_NOTICE PFX "TOC not read\n");
  1192. end_request(req, 0);
  1193. continue;
  1194. }
  1195. if (start_request(block / 4, nblock / 4)) {
  1196. printk(KERN_NOTICE PFX "start request failed\n");
  1197. end_request(req, 0);
  1198. continue;
  1199. }
  1200. }
  1201. read_data_block(req->buffer, block, nblock, res_reg, &res_size);
  1202. if (res_reg[0] != 0x20) {
  1203. if (!end_that_request_first(req, 1, nblock)) {
  1204. spin_lock_irq(q->queue_lock);
  1205. blkdev_dequeue_request(req);
  1206. end_that_request_last(req, 1);
  1207. spin_unlock_irq(q->queue_lock);
  1208. }
  1209. continue;
  1210. }
  1211. if (num_retries > MAX_CDU31A_RETRIES) {
  1212. end_request(req, 0);
  1213. continue;
  1214. }
  1215. num_retries++;
  1216. if (res_reg[1] == SONY_NOT_SPIN_ERR) {
  1217. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
  1218. &res_size);
  1219. } else {
  1220. printk(KERN_NOTICE PFX "%s error for block %d, nblock %d\n",
  1221. translate_error(res_reg[1]), block, nblock);
  1222. }
  1223. goto try_read_again;
  1224. }
  1225. end_do_cdu31a_request:
  1226. #if 0
  1227. /* After finished, cancel any pending operations. */
  1228. abort_read();
  1229. #else
  1230. /* Start a timer to time out after a while to disable
  1231. the read. */
  1232. cdu31a_abort_timer.expires = jiffies + 2 * HZ; /* Wait 2 seconds */
  1233. add_timer(&cdu31a_abort_timer);
  1234. #endif
  1235. up(&sony_sem);
  1236. spin_lock_irq(q->queue_lock);
  1237. pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
  1238. }
  1239. /*
  1240. * Read the table of contents from the drive and set up TOC if
  1241. * successful.
  1242. */
  1243. static void sony_get_toc(void)
  1244. {
  1245. unsigned char res_reg[2];
  1246. unsigned int res_size;
  1247. unsigned char parms[1];
  1248. int session;
  1249. int num_spin_ups;
  1250. int totaltracks = 0;
  1251. int mint = 99;
  1252. int maxt = 0;
  1253. pr_debug(PFX "Entering %s\n", __FUNCTION__);
  1254. num_spin_ups = 0;
  1255. if (!sony_toc_read) {
  1256. respinup_on_gettoc:
  1257. /* Ignore the result, since it might error if spinning already. */
  1258. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
  1259. &res_size);
  1260. do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
  1261. &res_size);
  1262. /* The drive sometimes returns error 0. I don't know why, but ignore
  1263. it. It seems to mean the drive has already done the operation. */
  1264. if ((res_size < 2)
  1265. || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
  1266. /* If the drive is already playing, it's ok. */
  1267. if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
  1268. || (res_reg[1] == 0)) {
  1269. goto gettoc_drive_spinning;
  1270. }
  1271. /* If the drive says it is not spun up (even though we just did it!)
  1272. then retry the operation at least a few times. */
  1273. if ((res_reg[1] == SONY_NOT_SPIN_ERR)
  1274. && (num_spin_ups < MAX_CDU31A_RETRIES)) {
  1275. num_spin_ups++;
  1276. goto respinup_on_gettoc;
  1277. }
  1278. printk("cdu31a: Error reading TOC: %x %s\n",
  1279. res_reg[0], translate_error(res_reg[1]));
  1280. return;
  1281. }
  1282. gettoc_drive_spinning:
  1283. /* The idea here is we keep asking for sessions until the command
  1284. fails. Then we know what the last valid session on the disk is.
  1285. No need to check session 0, since session 0 is the same as session
  1286. 1; the command returns different information if you give it 0.
  1287. */
  1288. #if DEBUG
  1289. memset(&sony_toc, 0x0e, sizeof(sony_toc));
  1290. memset(&single_toc, 0x0f, sizeof(single_toc));
  1291. #endif
  1292. session = 1;
  1293. while (1) {
  1294. /* This seems to slow things down enough to make it work. This
  1295. * appears to be a problem in do_sony_cd_cmd. This printk seems
  1296. * to address the symptoms... -Erik */
  1297. pr_debug(PFX "Trying session %d\n", session);
  1298. parms[0] = session;
  1299. do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
  1300. parms, 1, res_reg, &res_size);
  1301. pr_debug(PFX "%2.2x %2.2x\n", res_reg[0], res_reg[1]);
  1302. if ((res_size < 2)
  1303. || ((res_reg[0] & 0xf0) == 0x20)) {
  1304. /* An error reading the TOC, this must be past the last session. */
  1305. if (session == 1)
  1306. printk
  1307. ("Yikes! Couldn't read any sessions!");
  1308. break;
  1309. }
  1310. pr_debug(PFX "Reading session %d\n", session);
  1311. parms[0] = session;
  1312. do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
  1313. parms,
  1314. 1,
  1315. (unsigned char *) &single_toc,
  1316. &res_size);
  1317. if ((res_size < 2)
  1318. || ((single_toc.exec_status[0] & 0xf0) ==
  1319. 0x20)) {
  1320. printk(KERN_ERR PFX "Error reading "
  1321. "session %d: %x %s\n",
  1322. session, single_toc.exec_status[0],
  1323. translate_error(single_toc.
  1324. exec_status[1]));
  1325. /* An error reading the TOC. Return without sony_toc_read
  1326. set. */
  1327. return;
  1328. }
  1329. pr_debug(PFX "add0 %01x, con0 %01x, poi0 %02x, "
  1330. "1st trk %d, dsktyp %x, dum0 %x\n",
  1331. single_toc.address0, single_toc.control0,
  1332. single_toc.point0,
  1333. bcd_to_int(single_toc.first_track_num),
  1334. single_toc.disk_type, single_toc.dummy0);
  1335. pr_debug(PFX "add1 %01x, con1 %01x, poi1 %02x, "
  1336. "lst trk %d, dummy1 %x, dum2 %x\n",
  1337. single_toc.address1, single_toc.control1,
  1338. single_toc.point1,
  1339. bcd_to_int(single_toc.last_track_num),
  1340. single_toc.dummy1, single_toc.dummy2);
  1341. pr_debug(PFX "add2 %01x, con2 %01x, poi2 %02x "
  1342. "leadout start min %d, sec %d, frame %d\n",
  1343. single_toc.address2, single_toc.control2,
  1344. single_toc.point2,
  1345. bcd_to_int(single_toc.lead_out_start_msf[0]),
  1346. bcd_to_int(single_toc.lead_out_start_msf[1]),
  1347. bcd_to_int(single_toc.lead_out_start_msf[2]));
  1348. if (res_size > 18 && single_toc.pointb0 > 0xaf)
  1349. pr_debug(PFX "addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
  1350. "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
  1351. single_toc.addressb0,
  1352. single_toc.controlb0,
  1353. single_toc.pointb0,
  1354. bcd_to_int(single_toc.
  1355. next_poss_prog_area_msf
  1356. [0]),
  1357. bcd_to_int(single_toc.
  1358. next_poss_prog_area_msf
  1359. [1]),
  1360. bcd_to_int(single_toc.
  1361. next_poss_prog_area_msf
  1362. [2]),
  1363. single_toc.num_mode_5_pointers,
  1364. bcd_to_int(single_toc.
  1365. max_start_outer_leadout_msf
  1366. [0]),
  1367. bcd_to_int(single_toc.
  1368. max_start_outer_leadout_msf
  1369. [1]),
  1370. bcd_to_int(single_toc.
  1371. max_start_outer_leadout_msf
  1372. [2]));
  1373. if (res_size > 27 && single_toc.pointb1 > 0xaf)
  1374. pr_debug(PFX "addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
  1375. single_toc.addressb1,
  1376. single_toc.controlb1,
  1377. single_toc.pointb1,
  1378. single_toc.dummyb0_1[0],
  1379. single_toc.dummyb0_1[1],
  1380. single_toc.dummyb0_1[2],
  1381. single_toc.dummyb0_1[3],
  1382. single_toc.num_skip_interval_pointers,
  1383. single_toc.num_skip_track_assignments,
  1384. single_toc.dummyb0_2);
  1385. if (res_size > 36 && single_toc.pointb2 > 0xaf)
  1386. pr_debug(PFX "addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
  1387. single_toc.addressb2,
  1388. single_toc.controlb2,
  1389. single_toc.pointb2,
  1390. single_toc.tracksb2[0],
  1391. single_toc.tracksb2[1],
  1392. single_toc.tracksb2[2],
  1393. single_toc.tracksb2[3],
  1394. single_toc.tracksb2[4],
  1395. single_toc.tracksb2[5],
  1396. single_toc.tracksb2[6]);
  1397. if (res_size > 45 && single_toc.pointb3 > 0xaf)
  1398. pr_debug(PFX "addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
  1399. single_toc.addressb3,
  1400. single_toc.controlb3,
  1401. single_toc.pointb3,
  1402. single_toc.tracksb3[0],
  1403. single_toc.tracksb3[1],
  1404. single_toc.tracksb3[2],
  1405. single_toc.tracksb3[3],
  1406. single_toc.tracksb3[4],
  1407. single_toc.tracksb3[5],
  1408. single_toc.tracksb3[6]);
  1409. if (res_size > 54 && single_toc.pointb4 > 0xaf)
  1410. pr_debug(PFX "addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
  1411. single_toc.addressb4,
  1412. single_toc.controlb4,
  1413. single_toc.pointb4,
  1414. single_toc.tracksb4[0],
  1415. single_toc.tracksb4[1],
  1416. single_toc.tracksb4[2],
  1417. single_toc.tracksb4[3],
  1418. single_toc.tracksb4[4],
  1419. single_toc.tracksb4[5],
  1420. single_toc.tracksb4[6]);
  1421. if (res_size > 63 && single_toc.pointc0 > 0xaf)
  1422. pr_debug(PFX "addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
  1423. single_toc.addressc0,
  1424. single_toc.controlc0,
  1425. single_toc.pointc0,
  1426. single_toc.dummyc0[0],
  1427. single_toc.dummyc0[1],
  1428. single_toc.dummyc0[2],
  1429. single_toc.dummyc0[3],
  1430. single_toc.dummyc0[4],
  1431. single_toc.dummyc0[5],
  1432. single_toc.dummyc0[6]);
  1433. #undef DEBUG
  1434. #define DEBUG 0
  1435. sony_toc.lead_out_start_msf[0] =
  1436. bcd_to_int(single_toc.lead_out_start_msf[0]);
  1437. sony_toc.lead_out_start_msf[1] =
  1438. bcd_to_int(single_toc.lead_out_start_msf[1]);
  1439. sony_toc.lead_out_start_msf[2] =
  1440. bcd_to_int(single_toc.lead_out_start_msf[2]);
  1441. sony_toc.lead_out_start_lba =
  1442. single_toc.lead_out_start_lba =
  1443. msf_to_log(sony_toc.lead_out_start_msf);
  1444. /* For points that do not exist, move the data over them
  1445. to the right location. */
  1446. if (single_toc.pointb0 != 0xb0) {
  1447. memmove(((char *) &single_toc) + 27,
  1448. ((char *) &single_toc) + 18,
  1449. res_size - 18);
  1450. res_size += 9;
  1451. } else if (res_size > 18) {
  1452. sony_toc.lead_out_start_msf[0] =
  1453. bcd_to_int(single_toc.
  1454. max_start_outer_leadout_msf
  1455. [0]);
  1456. sony_toc.lead_out_start_msf[1] =
  1457. bcd_to_int(single_toc.
  1458. max_start_outer_leadout_msf
  1459. [1]);
  1460. sony_toc.lead_out_start_msf[2] =
  1461. bcd_to_int(single_toc.
  1462. max_start_outer_leadout_msf
  1463. [2]);
  1464. sony_toc.lead_out_start_lba =
  1465. msf_to_log(sony_toc.
  1466. lead_out_start_msf);
  1467. }
  1468. if (single_toc.pointb1 != 0xb1) {
  1469. memmove(((char *) &single_toc) + 36,
  1470. ((char *) &single_toc) + 27,
  1471. res_size - 27);
  1472. res_size += 9;
  1473. }
  1474. if (single_toc.pointb2 != 0xb2) {
  1475. memmove(((char *) &single_toc) + 45,
  1476. ((char *) &single_toc) + 36,
  1477. res_size - 36);
  1478. res_size += 9;
  1479. }
  1480. if (single_toc.pointb3 != 0xb3) {
  1481. memmove(((char *) &single_toc) + 54,
  1482. ((char *) &single_toc) + 45,
  1483. res_size - 45);
  1484. res_size += 9;
  1485. }
  1486. if (single_toc.pointb4 != 0xb4) {
  1487. memmove(((char *) &single_toc) + 63,
  1488. ((char *) &single_toc) + 54,
  1489. res_size - 54);
  1490. res_size += 9;
  1491. }
  1492. if (single_toc.pointc0 != 0xc0) {
  1493. memmove(((char *) &single_toc) + 72,
  1494. ((char *) &single_toc) + 63,
  1495. res_size - 63);
  1496. res_size += 9;
  1497. }
  1498. #if DEBUG
  1499. printk(PRINT_INFO PFX "start track lba %u, "
  1500. "leadout start lba %u\n",
  1501. single_toc.start_track_lba,
  1502. single_toc.lead_out_start_lba);
  1503. {
  1504. int i;
  1505. for (i = 0;
  1506. i <
  1507. 1 +
  1508. bcd_to_int(single_toc.last_track_num)
  1509. -
  1510. bcd_to_int(single_toc.
  1511. first_track_num); i++) {
  1512. printk(KERN_INFO PFX "trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
  1513. i,
  1514. single_toc.tracks[i].address,
  1515. single_toc.tracks[i].control,
  1516. bcd_to_int(single_toc.
  1517. tracks[i].track),
  1518. bcd_to_int(single_toc.
  1519. tracks[i].
  1520. track_start_msf
  1521. [0]),
  1522. bcd_to_int(single_toc.
  1523. tracks[i].
  1524. track_start_msf
  1525. [1]),
  1526. bcd_to_int(single_toc.
  1527. tracks[i].
  1528. track_start_msf
  1529. [2]));
  1530. if (mint >
  1531. bcd_to_int(single_toc.
  1532. tracks[i].track))
  1533. mint =
  1534. bcd_to_int(single_toc.
  1535. tracks[i].
  1536. track);
  1537. if (maxt <
  1538. bcd_to_int(single_toc.
  1539. tracks[i].track))
  1540. maxt =
  1541. bcd_to_int(single_toc.
  1542. tracks[i].
  1543. track);
  1544. }
  1545. printk(KERN_INFO PFX "min track number %d, "
  1546. "max track number %d\n",
  1547. mint, maxt);
  1548. }
  1549. #endif
  1550. /* prepare a special table of contents for a CD-I disc. They don't have one. */
  1551. if (single_toc.disk_type == 0x10 &&
  1552. single_toc.first_track_num == 2 &&
  1553. single_toc.last_track_num == 2 /* CD-I */ ) {
  1554. sony_toc.tracks[totaltracks].address = 1;
  1555. sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
  1556. sony_toc.tracks[totaltracks].track = 1;
  1557. sony_toc.tracks[totaltracks].
  1558. track_start_msf[0] = 0;
  1559. sony_toc.tracks[totaltracks].
  1560. track_start_msf[1] = 2;
  1561. sony_toc.tracks[totaltracks].
  1562. track_start_msf[2] = 0;
  1563. mint = maxt = 1;
  1564. totaltracks++;
  1565. } else
  1566. /* gather track entries from this session */
  1567. {
  1568. int i;
  1569. for (i = 0;
  1570. i <
  1571. 1 +
  1572. bcd_to_int(single_toc.last_track_num)
  1573. -
  1574. bcd_to_int(single_toc.
  1575. first_track_num);
  1576. i++, totaltracks++) {
  1577. sony_toc.tracks[totaltracks].
  1578. address =
  1579. single_toc.tracks[i].address;
  1580. sony_toc.tracks[totaltracks].
  1581. control =
  1582. single_toc.tracks[i].control;
  1583. sony_toc.tracks[totaltracks].
  1584. track =
  1585. bcd_to_int(single_toc.
  1586. tracks[i].track);
  1587. sony_toc.tracks[totaltracks].
  1588. track_start_msf[0] =
  1589. bcd_to_int(single_toc.
  1590. tracks[i].
  1591. track_start_msf[0]);
  1592. sony_toc.tracks[totaltracks].
  1593. track_start_msf[1] =
  1594. bcd_to_int(single_toc.
  1595. tracks[i].
  1596. track_start_msf[1]);
  1597. sony_toc.tracks[totaltracks].
  1598. track_start_msf[2] =
  1599. bcd_to_int(single_toc.
  1600. tracks[i].
  1601. track_start_msf[2]);
  1602. if (i == 0)
  1603. single_toc.
  1604. start_track_lba =
  1605. msf_to_log(sony_toc.
  1606. tracks
  1607. [totaltracks].
  1608. track_start_msf);
  1609. if (mint >
  1610. sony_toc.tracks[totaltracks].
  1611. track)
  1612. mint =
  1613. sony_toc.
  1614. tracks[totaltracks].
  1615. track;
  1616. if (maxt <
  1617. sony_toc.tracks[totaltracks].
  1618. track)
  1619. maxt =
  1620. sony_toc.
  1621. tracks[totaltracks].
  1622. track;
  1623. }
  1624. }
  1625. sony_toc.first_track_num = mint;
  1626. sony_toc.last_track_num = maxt;
  1627. /* Disk type of last session wins. For example:
  1628. CD-Extra has disk type 0 for the first session, so
  1629. a dumb HiFi CD player thinks it is a plain audio CD.
  1630. We are interested in the disk type of the last session,
  1631. which is 0x20 (XA) for CD-Extra, so we can access the
  1632. data track ... */
  1633. sony_toc.disk_type = single_toc.disk_type;
  1634. sony_toc.sessions = session;
  1635. /* don't believe everything :-) */
  1636. if (session == 1)
  1637. single_toc.start_track_lba = 0;
  1638. sony_toc.start_track_lba =
  1639. single_toc.start_track_lba;
  1640. if (session > 1 && single_toc.pointb0 == 0xb0 &&
  1641. sony_toc.lead_out_start_lba ==
  1642. single_toc.lead_out_start_lba) {
  1643. break;
  1644. }
  1645. /* Let's not get carried away... */
  1646. if (session > 40) {
  1647. printk(KERN_NOTICE PFX "too many sessions: "
  1648. "%d\n", session);
  1649. break;
  1650. }
  1651. session++;
  1652. }
  1653. sony_toc.track_entries = totaltracks;
  1654. /* add one entry for the LAST track with track number CDROM_LEADOUT */
  1655. sony_toc.tracks[totaltracks].address = single_toc.address2;
  1656. sony_toc.tracks[totaltracks].control = single_toc.control2;
  1657. sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
  1658. sony_toc.tracks[totaltracks].track_start_msf[0] =
  1659. sony_toc.lead_out_start_msf[0];
  1660. sony_toc.tracks[totaltracks].track_start_msf[1] =
  1661. sony_toc.lead_out_start_msf[1];
  1662. sony_toc.tracks[totaltracks].track_start_msf[2] =
  1663. sony_toc.lead_out_start_msf[2];
  1664. sony_toc_read = 1;
  1665. pr_debug(PFX "Disk session %d, start track: %d, "
  1666. "stop track: %d\n",
  1667. session, single_toc.start_track_lba,
  1668. single_toc.lead_out_start_lba);
  1669. }
  1670. pr_debug(PFX "Leaving %s\n", __FUNCTION__);
  1671. }
  1672. /*
  1673. * Uniform cdrom interface function
  1674. * return multisession offset and sector information
  1675. */
  1676. static int scd_get_last_session(struct cdrom_device_info *cdi,
  1677. struct cdrom_multisession *ms_info)
  1678. {
  1679. if (ms_info == NULL)
  1680. return 1;
  1681. if (!sony_toc_read) {
  1682. if (down_interruptible(&sony_sem))
  1683. return -ERESTARTSYS;
  1684. sony_get_toc();
  1685. up(&sony_sem);
  1686. }
  1687. ms_info->addr_format = CDROM_LBA;
  1688. ms_info->addr.lba = sony_toc.start_track_lba;
  1689. ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
  1690. sony_toc.disk_type == 0x10 /* CDI */ ;
  1691. return 0;
  1692. }
  1693. /*
  1694. * Search for a specific track in the table of contents.
  1695. */
  1696. static int find_track(int track)
  1697. {
  1698. int i;
  1699. for (i = 0; i <= sony_toc.track_entries; i++) {
  1700. if (sony_toc.tracks[i].track == track) {
  1701. return i;
  1702. }
  1703. }
  1704. return -1;
  1705. }
  1706. /*
  1707. * Read the subcode and put it in last_sony_subcode for future use.
  1708. */
  1709. static int read_subcode(void)
  1710. {
  1711. unsigned int res_size;
  1712. do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
  1713. NULL,
  1714. 0, (unsigned char *) &last_sony_subcode, &res_size);
  1715. if ((res_size < 2)
  1716. || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
  1717. printk(KERN_ERR PFX "Sony CDROM error %s (read_subcode)\n",
  1718. translate_error(last_sony_subcode.exec_status[1]));
  1719. return -EIO;
  1720. }
  1721. last_sony_subcode.track_num =
  1722. bcd_to_int(last_sony_subcode.track_num);
  1723. last_sony_subcode.index_num =
  1724. bcd_to_int(last_sony_subcode.index_num);
  1725. last_sony_subcode.abs_msf[0] =
  1726. bcd_to_int(last_sony_subcode.abs_msf[0]);
  1727. last_sony_subcode.abs_msf[1] =
  1728. bcd_to_int(last_sony_subcode.abs_msf[1]);
  1729. last_sony_subcode.abs_msf[2] =
  1730. bcd_to_int(last_sony_subcode.abs_msf[2]);
  1731. last_sony_subcode.rel_msf[0] =
  1732. bcd_to_int(last_sony_subcode.rel_msf[0]);
  1733. last_sony_subcode.rel_msf[1] =
  1734. bcd_to_int(last_sony_subcode.rel_msf[1]);
  1735. last_sony_subcode.rel_msf[2] =
  1736. bcd_to_int(last_sony_subcode.rel_msf[2]);
  1737. return 0;
  1738. }
  1739. /*
  1740. * Uniform cdrom interface function
  1741. * return the media catalog number found on some older audio cds
  1742. */
  1743. static int
  1744. scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
  1745. {
  1746. unsigned char resbuffer[2 + 14];
  1747. unsigned char *mcnp = mcn->medium_catalog_number;
  1748. unsigned char *resp = resbuffer + 3;
  1749. unsigned int res_size;
  1750. memset(mcn->medium_catalog_number, 0, 14);
  1751. if (down_interruptible(&sony_sem))
  1752. return -ERESTARTSYS;
  1753. do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
  1754. NULL, 0, resbuffer, &res_size);
  1755. up(&sony_sem);
  1756. if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
  1757. else {
  1758. /* packed bcd to single ASCII digits */
  1759. *mcnp++ = (*resp >> 4) + '0';
  1760. *mcnp++ = (*resp++ & 0x0f) + '0';
  1761. *mcnp++ = (*resp >> 4) + '0';
  1762. *mcnp++ = (*resp++ & 0x0f) + '0';
  1763. *mcnp++ = (*resp >> 4) + '0';
  1764. *mcnp++ = (*resp++ & 0x0f) + '0';
  1765. *mcnp++ = (*resp >> 4) + '0';
  1766. *mcnp++ = (*resp++ & 0x0f) + '0';
  1767. *mcnp++ = (*resp >> 4) + '0';
  1768. *mcnp++ = (*resp++ & 0x0f) + '0';
  1769. *mcnp++ = (*resp >> 4) + '0';
  1770. *mcnp++ = (*resp++ & 0x0f) + '0';
  1771. *mcnp++ = (*resp >> 4) + '0';
  1772. }
  1773. *mcnp = '\0';
  1774. return 0;
  1775. }
  1776. /*
  1777. * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
  1778. * the drive is playing, the subchannel needs to be read (since it would be
  1779. * changing). If the drive is paused or completed, the subcode information has
  1780. * already been stored, just use that. The ioctl call wants things in decimal
  1781. * (not BCD), so all the conversions are done.
  1782. */
  1783. static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
  1784. {
  1785. /* Get attention stuff */
  1786. while (handle_sony_cd_attention());
  1787. sony_get_toc();
  1788. if (!sony_toc_read) {
  1789. return -EIO;
  1790. }
  1791. switch (sony_audio_status) {
  1792. case CDROM_AUDIO_NO_STATUS:
  1793. case CDROM_AUDIO_PLAY:
  1794. if (read_subcode() < 0) {
  1795. return -EIO;
  1796. }
  1797. break;
  1798. case CDROM_AUDIO_PAUSED:
  1799. case CDROM_AUDIO_COMPLETED:
  1800. break;
  1801. #if 0
  1802. case CDROM_AUDIO_NO_STATUS:
  1803. schi->cdsc_audiostatus = sony_audio_status;
  1804. return 0;
  1805. break;
  1806. #endif
  1807. case CDROM_AUDIO_INVALID:
  1808. case CDROM_AUDIO_ERROR:
  1809. default:
  1810. return -EIO;
  1811. }
  1812. schi->cdsc_audiostatus = sony_audio_status;
  1813. schi->cdsc_adr = last_sony_subcode.address;
  1814. schi->cdsc_ctrl = last_sony_subcode.control;
  1815. schi->cdsc_trk = last_sony_subcode.track_num;
  1816. schi->cdsc_ind = last_sony_subcode.index_num;
  1817. if (schi->cdsc_format == CDROM_MSF) {
  1818. schi->cdsc_absaddr.msf.minute =
  1819. last_sony_subcode.abs_msf[0];
  1820. schi->cdsc_absaddr.msf.second =
  1821. last_sony_subcode.abs_msf[1];
  1822. schi->cdsc_absaddr.msf.frame =
  1823. last_sony_subcode.abs_msf[2];
  1824. schi->cdsc_reladdr.msf.minute =
  1825. last_sony_subcode.rel_msf[0];
  1826. schi->cdsc_reladdr.msf.second =
  1827. last_sony_subcode.rel_msf[1];
  1828. schi->cdsc_reladdr.msf.frame =
  1829. last_sony_subcode.rel_msf[2];
  1830. } else if (schi->cdsc_format == CDROM_LBA) {
  1831. schi->cdsc_absaddr.lba =
  1832. msf_to_log(last_sony_subcode.abs_msf);
  1833. schi->cdsc_reladdr.lba =
  1834. msf_to_log(last_sony_subcode.rel_msf);
  1835. }
  1836. return 0;
  1837. }
  1838. /* Get audio data from the drive. This is fairly complex because I
  1839. am looking for status and data at the same time, but if I get status
  1840. then I just look for data. I need to get the status immediately so
  1841. the switch from audio to data tracks will happen quickly. */
  1842. static void
  1843. read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
  1844. {
  1845. unsigned long retry_count;
  1846. int result_read;
  1847. res_reg[0] = 0;
  1848. res_reg[1] = 0;
  1849. *res_size = 0;
  1850. result_read = 0;
  1851. /* Wait for the drive to tell us we have something */
  1852. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  1853. continue_read_audio_wait:
  1854. while (time_before(jiffies, retry_count) && !(is_data_ready())
  1855. && !(is_result_ready() || result_read)) {
  1856. while (handle_sony_cd_attention());
  1857. sony_sleep();
  1858. }
  1859. if (!(is_data_ready())) {
  1860. if (is_result_ready() && !result_read) {
  1861. get_result(res_reg, res_size);
  1862. /* Read block status and continue waiting for data. */
  1863. if ((res_reg[0] & 0xf0) == 0x50) {
  1864. result_read = 1;
  1865. goto continue_read_audio_wait;
  1866. }
  1867. /* Invalid data from the drive. Shut down the operation. */
  1868. else if ((res_reg[0] & 0xf0) != 0x20) {
  1869. printk(KERN_WARNING PFX "Got result that "
  1870. "should have been error: %d\n",
  1871. res_reg[0]);
  1872. res_reg[0] = 0x20;
  1873. res_reg[1] = SONY_BAD_DATA_ERR;
  1874. *res_size = 2;
  1875. }
  1876. abort_read();
  1877. } else {
  1878. pr_debug(PFX "timeout out %d\n", __LINE__);
  1879. res_reg[0] = 0x20;
  1880. res_reg[1] = SONY_TIMEOUT_OP_ERR;
  1881. *res_size = 2;
  1882. abort_read();
  1883. }
  1884. } else {
  1885. clear_data_ready();
  1886. /* If data block, then get 2340 bytes offset by 12. */
  1887. if (sony_raw_data_mode) {
  1888. insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
  1889. CD_FRAMESIZE_RAW1);
  1890. } else {
  1891. /* Audio gets the whole 2352 bytes. */
  1892. insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
  1893. }
  1894. /* If I haven't already gotten the result, get it now. */
  1895. if (!result_read) {
  1896. /* Wait for the drive to tell us we have something */
  1897. retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
  1898. while (time_before(jiffies, retry_count)
  1899. && !(is_result_ready())) {
  1900. while (handle_sony_cd_attention());
  1901. sony_sleep();
  1902. }
  1903. if (!is_result_ready()) {
  1904. pr_debug(PFX "timeout out %d\n", __LINE__);
  1905. res_reg[0] = 0x20;
  1906. res_reg[1] = SONY_TIMEOUT_OP_ERR;
  1907. *res_size = 2;
  1908. abort_read();
  1909. return;
  1910. } else {
  1911. get_result(res_reg, res_size);
  1912. }
  1913. }
  1914. if ((res_reg[0] & 0xf0) == 0x50) {
  1915. if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
  1916. || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
  1917. || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
  1918. || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
  1919. /* Ok, nothing to do. */
  1920. } else {
  1921. printk(KERN_ERR PFX "Data block error: 0x%x\n",
  1922. res_reg[0]);
  1923. res_reg[0] = 0x20;
  1924. res_reg[1] = SONY_BAD_DATA_ERR;
  1925. *res_size = 2;
  1926. }
  1927. } else if ((res_reg[0] & 0xf0) != 0x20) {
  1928. /* The drive gave me bad status, I don't know what to do.
  1929. Reset the driver and return an error. */
  1930. printk(KERN_NOTICE PFX "Invalid block status: 0x%x\n",
  1931. res_reg[0]);
  1932. restart_on_error();
  1933. res_reg[0] = 0x20;
  1934. res_reg[1] = SONY_BAD_DATA_ERR;
  1935. *res_size = 2;
  1936. }
  1937. }
  1938. }
  1939. /* Perform a raw data read. This will automatically detect the
  1940. track type and read the proper data (audio or data). */
  1941. static int read_audio(struct cdrom_read_audio *ra)
  1942. {
  1943. int retval;
  1944. unsigned char params[2];
  1945. unsigned char res_reg[12];
  1946. unsigned int res_size;
  1947. unsigned int cframe;
  1948. if (down_interruptible(&sony_sem))
  1949. return -ERESTARTSYS;
  1950. if (!sony_spun_up)
  1951. scd_spinup();
  1952. /* Set the drive to do raw operations. */
  1953. params[0] = SONY_SD_DECODE_PARAM;
  1954. params[1] = 0x06 | sony_raw_data_mode;
  1955. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  1956. params, 2, res_reg, &res_size);
  1957. if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
  1958. printk(KERN_ERR PFX "Unable to set decode params: 0x%2.2x\n",
  1959. res_reg[1]);
  1960. retval = -EIO;
  1961. goto out_up;
  1962. }
  1963. /* From here down, we have to goto exit_read_audio instead of returning
  1964. because the drive parameters have to be set back to data before
  1965. return. */
  1966. retval = 0;
  1967. if (start_request(ra->addr.lba, ra->nframes)) {
  1968. retval = -EIO;
  1969. goto exit_read_audio;
  1970. }
  1971. /* For every requested frame. */
  1972. cframe = 0;
  1973. while (cframe < ra->nframes) {
  1974. read_audio_data(audio_buffer, res_reg, &res_size);
  1975. if ((res_reg[0] & 0xf0) == 0x20) {
  1976. if (res_reg[1] == SONY_BAD_DATA_ERR) {
  1977. printk(KERN_ERR PFX "Data error on audio "
  1978. "sector %d\n",
  1979. ra->addr.lba + cframe);
  1980. } else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
  1981. /* Illegal track type, change track types and start over. */
  1982. sony_raw_data_mode =
  1983. (sony_raw_data_mode) ? 0 : 1;
  1984. /* Set the drive mode. */
  1985. params[0] = SONY_SD_DECODE_PARAM;
  1986. params[1] = 0x06 | sony_raw_data_mode;
  1987. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  1988. params,
  1989. 2, res_reg, &res_size);
  1990. if ((res_size < 2)
  1991. || ((res_reg[0] & 0xf0) == 0x20)) {
  1992. printk(KERN_ERR PFX "Unable to set "
  1993. "decode params: 0x%2.2x\n",
  1994. res_reg[1]);
  1995. retval = -EIO;
  1996. goto exit_read_audio;
  1997. }
  1998. /* Restart the request on the current frame. */
  1999. if (start_request
  2000. (ra->addr.lba + cframe,
  2001. ra->nframes - cframe)) {
  2002. retval = -EIO;
  2003. goto exit_read_audio;
  2004. }
  2005. /* Don't go back to the top because don't want to get into
  2006. and infinite loop. A lot of code gets duplicated, but
  2007. that's no big deal, I don't guess. */
  2008. read_audio_data(audio_buffer, res_reg,
  2009. &res_size);
  2010. if ((res_reg[0] & 0xf0) == 0x20) {
  2011. if (res_reg[1] ==
  2012. SONY_BAD_DATA_ERR) {
  2013. printk(KERN_ERR PFX "Data error"
  2014. " on audio sector %d\n",
  2015. ra->addr.lba +
  2016. cframe);
  2017. } else {
  2018. printk(KERN_ERR PFX "Error reading audio data on sector %d: %s\n",
  2019. ra->addr.lba + cframe,
  2020. translate_error
  2021. (res_reg[1]));
  2022. retval = -EIO;
  2023. goto exit_read_audio;
  2024. }
  2025. } else if (copy_to_user(ra->buf +
  2026. (CD_FRAMESIZE_RAW
  2027. * cframe),
  2028. audio_buffer,
  2029. CD_FRAMESIZE_RAW)) {
  2030. retval = -EFAULT;
  2031. goto exit_read_audio;
  2032. }
  2033. } else {
  2034. printk(KERN_ERR PFX "Error reading audio "
  2035. "data on sector %d: %s\n",
  2036. ra->addr.lba + cframe,
  2037. translate_error(res_reg[1]));
  2038. retval = -EIO;
  2039. goto exit_read_audio;
  2040. }
  2041. } else if (copy_to_user(ra->buf + (CD_FRAMESIZE_RAW * cframe),
  2042. (char *)audio_buffer,
  2043. CD_FRAMESIZE_RAW)) {
  2044. retval = -EFAULT;
  2045. goto exit_read_audio;
  2046. }
  2047. cframe++;
  2048. }
  2049. get_result(res_reg, &res_size);
  2050. if ((res_reg[0] & 0xf0) == 0x20) {
  2051. printk(KERN_ERR PFX "Error return from audio read: %s\n",
  2052. translate_error(res_reg[1]));
  2053. retval = -EIO;
  2054. goto exit_read_audio;
  2055. }
  2056. exit_read_audio:
  2057. /* Set the drive mode back to the proper one for the disk. */
  2058. params[0] = SONY_SD_DECODE_PARAM;
  2059. if (!sony_xa_mode) {
  2060. params[1] = 0x0f;
  2061. } else {
  2062. params[1] = 0x07;
  2063. }
  2064. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  2065. params, 2, res_reg, &res_size);
  2066. if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
  2067. printk(KERN_ERR PFX "Unable to reset decode params: 0x%2.2x\n",
  2068. res_reg[1]);
  2069. retval = -EIO;
  2070. }
  2071. out_up:
  2072. up(&sony_sem);
  2073. return retval;
  2074. }
  2075. static int
  2076. do_sony_cd_cmd_chk(const char *name,
  2077. unsigned char cmd,
  2078. unsigned char *params,
  2079. unsigned int num_params,
  2080. unsigned char *result_buffer, unsigned int *result_size)
  2081. {
  2082. do_sony_cd_cmd(cmd, params, num_params, result_buffer,
  2083. result_size);
  2084. if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
  2085. printk(KERN_ERR PFX "Error %s (CDROM%s)\n",
  2086. translate_error(result_buffer[1]), name);
  2087. return -EIO;
  2088. }
  2089. return 0;
  2090. }
  2091. /*
  2092. * Uniform cdrom interface function
  2093. * open the tray
  2094. */
  2095. static int scd_tray_move(struct cdrom_device_info *cdi, int position)
  2096. {
  2097. int retval;
  2098. if (down_interruptible(&sony_sem))
  2099. return -ERESTARTSYS;
  2100. if (position == 1 /* open tray */ ) {
  2101. unsigned char res_reg[12];
  2102. unsigned int res_size;
  2103. do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
  2104. &res_size);
  2105. do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
  2106. &res_size);
  2107. sony_audio_status = CDROM_AUDIO_INVALID;
  2108. retval = do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
  2109. res_reg, &res_size);
  2110. } else {
  2111. if (0 == scd_spinup())
  2112. sony_spun_up = 1;
  2113. retval = 0;
  2114. }
  2115. up(&sony_sem);
  2116. return retval;
  2117. }
  2118. /*
  2119. * The big ugly ioctl handler.
  2120. */
  2121. static int scd_audio_ioctl(struct cdrom_device_info *cdi,
  2122. unsigned int cmd, void *arg)
  2123. {
  2124. unsigned char res_reg[12];
  2125. unsigned int res_size;
  2126. unsigned char params[7];
  2127. int i, retval;
  2128. if (down_interruptible(&sony_sem))
  2129. return -ERESTARTSYS;
  2130. switch (cmd) {
  2131. case CDROMSTART: /* Spin up the drive */
  2132. retval = do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
  2133. 0, res_reg, &res_size);
  2134. break;
  2135. case CDROMSTOP: /* Spin down the drive */
  2136. do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
  2137. &res_size);
  2138. /*
  2139. * Spin the drive down, ignoring the error if the disk was
  2140. * already not spinning.
  2141. */
  2142. sony_audio_status = CDROM_AUDIO_NO_STATUS;
  2143. retval = do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
  2144. 0, res_reg, &res_size);
  2145. break;
  2146. case CDROMPAUSE: /* Pause the drive */
  2147. if (do_sony_cd_cmd_chk
  2148. ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
  2149. &res_size)) {
  2150. retval = -EIO;
  2151. break;
  2152. }
  2153. /* Get the current position and save it for resuming */
  2154. if (read_subcode() < 0) {
  2155. retval = -EIO;
  2156. break;
  2157. }
  2158. cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
  2159. cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
  2160. cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
  2161. sony_audio_status = CDROM_AUDIO_PAUSED;
  2162. retval = 0;
  2163. break;
  2164. case CDROMRESUME: /* Start the drive after being paused */
  2165. if (sony_audio_status != CDROM_AUDIO_PAUSED) {
  2166. retval = -EINVAL;
  2167. break;
  2168. }
  2169. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
  2170. &res_size);
  2171. /* Start the drive at the saved position. */
  2172. params[1] = int_to_bcd(cur_pos_msf[0]);
  2173. params[2] = int_to_bcd(cur_pos_msf[1]);
  2174. params[3] = int_to_bcd(cur_pos_msf[2]);
  2175. params[4] = int_to_bcd(final_pos_msf[0]);
  2176. params[5] = int_to_bcd(final_pos_msf[1]);
  2177. params[6] = int_to_bcd(final_pos_msf[2]);
  2178. params[0] = 0x03;
  2179. if (do_sony_cd_cmd_chk
  2180. ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
  2181. &res_size) < 0) {
  2182. retval = -EIO;
  2183. break;
  2184. }
  2185. sony_audio_status = CDROM_AUDIO_PLAY;
  2186. retval = 0;
  2187. break;
  2188. case CDROMPLAYMSF: /* Play starting at the given MSF address. */
  2189. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
  2190. &res_size);
  2191. /* The parameters are given in int, must be converted */
  2192. for (i = 1; i < 7; i++) {
  2193. params[i] =
  2194. int_to_bcd(((unsigned char *) arg)[i - 1]);
  2195. }
  2196. params[0] = 0x03;
  2197. if (do_sony_cd_cmd_chk
  2198. ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
  2199. res_reg, &res_size) < 0) {
  2200. retval = -EIO;
  2201. break;
  2202. }
  2203. /* Save the final position for pauses and resumes */
  2204. final_pos_msf[0] = bcd_to_int(params[4]);
  2205. final_pos_msf[1] = bcd_to_int(params[5]);
  2206. final_pos_msf[2] = bcd_to_int(params[6]);
  2207. sony_audio_status = CDROM_AUDIO_PLAY;
  2208. retval = 0;
  2209. break;
  2210. case CDROMREADTOCHDR: /* Read the table of contents header */
  2211. {
  2212. struct cdrom_tochdr *hdr;
  2213. sony_get_toc();
  2214. if (!sony_toc_read) {
  2215. retval = -EIO;
  2216. break;
  2217. }
  2218. hdr = (struct cdrom_tochdr *) arg;
  2219. hdr->cdth_trk0 = sony_toc.first_track_num;
  2220. hdr->cdth_trk1 = sony_toc.last_track_num;
  2221. }
  2222. retval = 0;
  2223. break;
  2224. case CDROMREADTOCENTRY: /* Read a given table of contents entry */
  2225. {
  2226. struct cdrom_tocentry *entry;
  2227. int track_idx;
  2228. unsigned char *msf_val = NULL;
  2229. sony_get_toc();
  2230. if (!sony_toc_read) {
  2231. retval = -EIO;
  2232. break;
  2233. }
  2234. entry = (struct cdrom_tocentry *) arg;
  2235. track_idx = find_track(entry->cdte_track);
  2236. if (track_idx < 0) {
  2237. retval = -EINVAL;
  2238. break;
  2239. }
  2240. entry->cdte_adr =
  2241. sony_toc.tracks[track_idx].address;
  2242. entry->cdte_ctrl =
  2243. sony_toc.tracks[track_idx].control;
  2244. msf_val =
  2245. sony_toc.tracks[track_idx].track_start_msf;
  2246. /* Logical buffer address or MSF format requested? */
  2247. if (entry->cdte_format == CDROM_LBA) {
  2248. entry->cdte_addr.lba = msf_to_log(msf_val);
  2249. } else if (entry->cdte_format == CDROM_MSF) {
  2250. entry->cdte_addr.msf.minute = *msf_val;
  2251. entry->cdte_addr.msf.second =
  2252. *(msf_val + 1);
  2253. entry->cdte_addr.msf.frame =
  2254. *(msf_val + 2);
  2255. }
  2256. }
  2257. retval = 0;
  2258. break;
  2259. case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
  2260. {
  2261. struct cdrom_ti *ti = (struct cdrom_ti *) arg;
  2262. int track_idx;
  2263. sony_get_toc();
  2264. if (!sony_toc_read) {
  2265. retval = -EIO;
  2266. break;
  2267. }
  2268. if ((ti->cdti_trk0 < sony_toc.first_track_num)
  2269. || (ti->cdti_trk0 > sony_toc.last_track_num)
  2270. || (ti->cdti_trk1 < ti->cdti_trk0)) {
  2271. retval = -EINVAL;
  2272. break;
  2273. }
  2274. track_idx = find_track(ti->cdti_trk0);
  2275. if (track_idx < 0) {
  2276. retval = -EINVAL;
  2277. break;
  2278. }
  2279. params[1] =
  2280. int_to_bcd(sony_toc.tracks[track_idx].
  2281. track_start_msf[0]);
  2282. params[2] =
  2283. int_to_bcd(sony_toc.tracks[track_idx].
  2284. track_start_msf[1]);
  2285. params[3] =
  2286. int_to_bcd(sony_toc.tracks[track_idx].
  2287. track_start_msf[2]);
  2288. /*
  2289. * If we want to stop after the last track, use the lead-out
  2290. * MSF to do that.
  2291. */
  2292. if (ti->cdti_trk1 >= sony_toc.last_track_num) {
  2293. track_idx = find_track(CDROM_LEADOUT);
  2294. } else {
  2295. track_idx = find_track(ti->cdti_trk1 + 1);
  2296. }
  2297. if (track_idx < 0) {
  2298. retval = -EINVAL;
  2299. break;
  2300. }
  2301. params[4] =
  2302. int_to_bcd(sony_toc.tracks[track_idx].
  2303. track_start_msf[0]);
  2304. params[5] =
  2305. int_to_bcd(sony_toc.tracks[track_idx].
  2306. track_start_msf[1]);
  2307. params[6] =
  2308. int_to_bcd(sony_toc.tracks[track_idx].
  2309. track_start_msf[2]);
  2310. params[0] = 0x03;
  2311. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
  2312. &res_size);
  2313. do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
  2314. res_reg, &res_size);
  2315. if ((res_size < 2)
  2316. || ((res_reg[0] & 0xf0) == 0x20)) {
  2317. printk(KERN_ERR PFX
  2318. "Params: %x %x %x %x %x %x %x\n",
  2319. params[0], params[1], params[2],
  2320. params[3], params[4], params[5],
  2321. params[6]);
  2322. printk(KERN_ERR PFX
  2323. "Error %s (CDROMPLAYTRKIND)\n",
  2324. translate_error(res_reg[1]));
  2325. retval = -EIO;
  2326. break;
  2327. }
  2328. /* Save the final position for pauses and resumes */
  2329. final_pos_msf[0] = bcd_to_int(params[4]);
  2330. final_pos_msf[1] = bcd_to_int(params[5]);
  2331. final_pos_msf[2] = bcd_to_int(params[6]);
  2332. sony_audio_status = CDROM_AUDIO_PLAY;
  2333. retval = 0;
  2334. break;
  2335. }
  2336. case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
  2337. {
  2338. struct cdrom_volctrl *volctrl =
  2339. (struct cdrom_volctrl *) arg;
  2340. params[0] = SONY_SD_AUDIO_VOLUME;
  2341. params[1] = volctrl->channel0;
  2342. params[2] = volctrl->channel1;
  2343. retval = do_sony_cd_cmd_chk("VOLCTRL",
  2344. SONY_SET_DRIVE_PARAM_CMD,
  2345. params, 3, res_reg,
  2346. &res_size);
  2347. break;
  2348. }
  2349. case CDROMSUBCHNL: /* Get subchannel info */
  2350. retval = sony_get_subchnl_info((struct cdrom_subchnl *) arg);
  2351. break;
  2352. default:
  2353. retval = -EINVAL;
  2354. break;
  2355. }
  2356. up(&sony_sem);
  2357. return retval;
  2358. }
  2359. static int scd_read_audio(struct cdrom_device_info *cdi,
  2360. unsigned int cmd, unsigned long arg)
  2361. {
  2362. void __user *argp = (void __user *)arg;
  2363. int retval;
  2364. if (down_interruptible(&sony_sem))
  2365. return -ERESTARTSYS;
  2366. switch (cmd) {
  2367. case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
  2368. raw data tracks. */
  2369. {
  2370. struct cdrom_read_audio ra;
  2371. sony_get_toc();
  2372. if (!sony_toc_read) {
  2373. retval = -EIO;
  2374. break;
  2375. }
  2376. if (copy_from_user(&ra, argp, sizeof(ra))) {
  2377. retval = -EFAULT;
  2378. break;
  2379. }
  2380. if (ra.nframes == 0) {
  2381. retval = 0;
  2382. break;
  2383. }
  2384. if (!access_ok(VERIFY_WRITE, ra.buf,
  2385. CD_FRAMESIZE_RAW * ra.nframes))
  2386. return -EFAULT;
  2387. if (ra.addr_format == CDROM_LBA) {
  2388. if ((ra.addr.lba >=
  2389. sony_toc.lead_out_start_lba)
  2390. || (ra.addr.lba + ra.nframes >=
  2391. sony_toc.lead_out_start_lba)) {
  2392. retval = -EINVAL;
  2393. break;
  2394. }
  2395. } else if (ra.addr_format == CDROM_MSF) {
  2396. if ((ra.addr.msf.minute >= 75)
  2397. || (ra.addr.msf.second >= 60)
  2398. || (ra.addr.msf.frame >= 75)) {
  2399. retval = -EINVAL;
  2400. break;
  2401. }
  2402. ra.addr.lba = ((ra.addr.msf.minute * 4500)
  2403. + (ra.addr.msf.second * 75)
  2404. + ra.addr.msf.frame);
  2405. if ((ra.addr.lba >=
  2406. sony_toc.lead_out_start_lba)
  2407. || (ra.addr.lba + ra.nframes >=
  2408. sony_toc.lead_out_start_lba)) {
  2409. retval = -EINVAL;
  2410. break;
  2411. }
  2412. /* I know, this can go negative on an unsigned. However,
  2413. the first thing done to the data is to add this value,
  2414. so this should compensate and allow direct msf access. */
  2415. ra.addr.lba -= LOG_START_OFFSET;
  2416. } else {
  2417. retval = -EINVAL;
  2418. break;
  2419. }
  2420. retval = read_audio(&ra);
  2421. break;
  2422. }
  2423. retval = 0;
  2424. break;
  2425. default:
  2426. retval = -EINVAL;
  2427. }
  2428. up(&sony_sem);
  2429. return retval;
  2430. }
  2431. static int scd_spinup(void)
  2432. {
  2433. unsigned char res_reg[12];
  2434. unsigned int res_size;
  2435. int num_spin_ups;
  2436. num_spin_ups = 0;
  2437. respinup_on_open:
  2438. do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
  2439. /* The drive sometimes returns error 0. I don't know why, but ignore
  2440. it. It seems to mean the drive has already done the operation. */
  2441. if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
  2442. printk(KERN_ERR PFX "%s error (scd_open, spin up)\n",
  2443. translate_error(res_reg[1]));
  2444. return 1;
  2445. }
  2446. do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
  2447. /* The drive sometimes returns error 0. I don't know why, but ignore
  2448. it. It seems to mean the drive has already done the operation. */
  2449. if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
  2450. /* If the drive is already playing, it's ok. */
  2451. if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
  2452. || (res_reg[1] == 0)) {
  2453. return 0;
  2454. }
  2455. /* If the drive says it is not spun up (even though we just did it!)
  2456. then retry the operation at least a few times. */
  2457. if ((res_reg[1] == SONY_NOT_SPIN_ERR)
  2458. && (num_spin_ups < MAX_CDU31A_RETRIES)) {
  2459. num_spin_ups++;
  2460. goto respinup_on_open;
  2461. }
  2462. printk(KERN_ERR PFX "Error %s (scd_open, read toc)\n",
  2463. translate_error(res_reg[1]));
  2464. do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
  2465. &res_size);
  2466. return 1;
  2467. }
  2468. return 0;
  2469. }
  2470. /*
  2471. * Open the drive for operations. Spin the drive up and read the table of
  2472. * contents if these have not already been done.
  2473. */
  2474. static int scd_open(struct cdrom_device_info *cdi, int purpose)
  2475. {
  2476. unsigned char res_reg[12];
  2477. unsigned int res_size;
  2478. unsigned char params[2];
  2479. if (purpose == 1) {
  2480. /* Open for IOCTLs only - no media check */
  2481. sony_usage++;
  2482. return 0;
  2483. }
  2484. if (sony_usage == 0) {
  2485. if (scd_spinup() != 0)
  2486. return -EIO;
  2487. sony_get_toc();
  2488. if (!sony_toc_read) {
  2489. do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
  2490. res_reg, &res_size);
  2491. return -EIO;
  2492. }
  2493. /* For XA on the CDU31A only, we have to do special reads.
  2494. The CDU33A handles XA automagically. */
  2495. /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
  2496. if ((sony_toc.disk_type != 0x00)
  2497. && (!is_double_speed)) {
  2498. params[0] = SONY_SD_DECODE_PARAM;
  2499. params[1] = 0x07;
  2500. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  2501. params, 2, res_reg, &res_size);
  2502. if ((res_size < 2)
  2503. || ((res_reg[0] & 0xf0) == 0x20)) {
  2504. printk(KERN_WARNING PFX "Unable to set "
  2505. "XA params: 0x%2.2x\n", res_reg[1]);
  2506. }
  2507. sony_xa_mode = 1;
  2508. }
  2509. /* A non-XA disk. Set the parms back if necessary. */
  2510. else if (sony_xa_mode) {
  2511. params[0] = SONY_SD_DECODE_PARAM;
  2512. params[1] = 0x0f;
  2513. do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
  2514. params, 2, res_reg, &res_size);
  2515. if ((res_size < 2)
  2516. || ((res_reg[0] & 0xf0) == 0x20)) {
  2517. printk(KERN_WARNING PFX "Unable to reset "
  2518. "XA params: 0x%2.2x\n", res_reg[1]);
  2519. }
  2520. sony_xa_mode = 0;
  2521. }
  2522. sony_spun_up = 1;
  2523. }
  2524. sony_usage++;
  2525. return 0;
  2526. }
  2527. /*
  2528. * Close the drive. Spin it down if no task is using it. The spin
  2529. * down will fail if playing audio, so audio play is OK.
  2530. */
  2531. static void scd_release(struct cdrom_device_info *cdi)
  2532. {
  2533. if (sony_usage == 1) {
  2534. unsigned char res_reg[12];
  2535. unsigned int res_size;
  2536. do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
  2537. &res_size);
  2538. sony_spun_up = 0;
  2539. }
  2540. sony_usage--;
  2541. }
  2542. static struct cdrom_device_ops scd_dops = {
  2543. .open = scd_open,
  2544. .release = scd_release,
  2545. .drive_status = scd_drive_status,
  2546. .media_changed = scd_media_changed,
  2547. .tray_move = scd_tray_move,
  2548. .lock_door = scd_lock_door,
  2549. .select_speed = scd_select_speed,
  2550. .get_last_session = scd_get_last_session,
  2551. .get_mcn = scd_get_mcn,
  2552. .reset = scd_reset,
  2553. .audio_ioctl = scd_audio_ioctl,
  2554. .capability = CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
  2555. CDC_SELECT_SPEED | CDC_MULTI_SESSION |
  2556. CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
  2557. CDC_RESET | CDC_DRIVE_STATUS,
  2558. .n_minors = 1,
  2559. };
  2560. static struct cdrom_device_info scd_info = {
  2561. .ops = &scd_dops,
  2562. .speed = 2,
  2563. .capacity = 1,
  2564. .name = "cdu31a"
  2565. };
  2566. static int scd_block_open(struct inode *inode, struct file *file)
  2567. {
  2568. return cdrom_open(&scd_info, inode, file);
  2569. }
  2570. static int scd_block_release(struct inode *inode, struct file *file)
  2571. {
  2572. return cdrom_release(&scd_info, file);
  2573. }
  2574. static int scd_block_ioctl(struct inode *inode, struct file *file,
  2575. unsigned cmd, unsigned long arg)
  2576. {
  2577. int retval;
  2578. /* The eject and close commands should be handled by Uniform CD-ROM
  2579. * driver - but I always got hard lockup instead of eject
  2580. * until I put this here.
  2581. */
  2582. switch (cmd) {
  2583. case CDROMEJECT:
  2584. scd_lock_door(&scd_info, 0);
  2585. retval = scd_tray_move(&scd_info, 1);
  2586. break;
  2587. case CDROMCLOSETRAY:
  2588. retval = scd_tray_move(&scd_info, 0);
  2589. break;
  2590. case CDROMREADAUDIO:
  2591. retval = scd_read_audio(&scd_info, CDROMREADAUDIO, arg);
  2592. break;
  2593. default:
  2594. retval = cdrom_ioctl(file, &scd_info, inode, cmd, arg);
  2595. }
  2596. return retval;
  2597. }
  2598. static int scd_block_media_changed(struct gendisk *disk)
  2599. {
  2600. return cdrom_media_changed(&scd_info);
  2601. }
  2602. static struct block_device_operations scd_bdops =
  2603. {
  2604. .owner = THIS_MODULE,
  2605. .open = scd_block_open,
  2606. .release = scd_block_release,
  2607. .ioctl = scd_block_ioctl,
  2608. .media_changed = scd_block_media_changed,
  2609. };
  2610. static struct gendisk *scd_gendisk;
  2611. /* The different types of disc loading mechanisms supported */
  2612. static char *load_mech[] __initdata =
  2613. { "caddy", "tray", "pop-up", "unknown" };
  2614. static int __init
  2615. get_drive_configuration(unsigned short base_io,
  2616. unsigned char res_reg[], unsigned int *res_size)
  2617. {
  2618. unsigned long retry_count;
  2619. if (!request_region(base_io, 4, "cdu31a"))
  2620. return 0;
  2621. /* Set the base address */
  2622. cdu31a_port = base_io;
  2623. /* Set up all the register locations */
  2624. sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
  2625. sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
  2626. sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
  2627. sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
  2628. sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
  2629. sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
  2630. sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
  2631. sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
  2632. /*
  2633. * Check to see if anything exists at the status register location.
  2634. * I don't know if this is a good way to check, but it seems to work
  2635. * ok for me.
  2636. */
  2637. if (read_status_register() != 0xff) {
  2638. /*
  2639. * Reset the drive and wait for attention from it (to say it's reset).
  2640. * If you don't wait, the next operation will probably fail.
  2641. */
  2642. reset_drive();
  2643. retry_count = jiffies + SONY_RESET_TIMEOUT;
  2644. while (time_before(jiffies, retry_count)
  2645. && (!is_attention())) {
  2646. sony_sleep();
  2647. }
  2648. #if 0
  2649. /* If attention is never seen probably not a CDU31a present */
  2650. if (!is_attention()) {
  2651. res_reg[0] = 0x20;
  2652. goto out_err;
  2653. }
  2654. #endif
  2655. /*
  2656. * Get the drive configuration.
  2657. */
  2658. do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
  2659. NULL,
  2660. 0, (unsigned char *) res_reg, res_size);
  2661. if (*res_size <= 2 || (res_reg[0] & 0xf0) != 0)
  2662. goto out_err;
  2663. return 1;
  2664. }
  2665. /* Return an error */
  2666. res_reg[0] = 0x20;
  2667. out_err:
  2668. release_region(cdu31a_port, 4);
  2669. cdu31a_port = 0;
  2670. return 0;
  2671. }
  2672. #ifndef MODULE
  2673. /*
  2674. * Set up base I/O and interrupts, called from main.c.
  2675. */
  2676. static int __init cdu31a_setup(char *strings)
  2677. {
  2678. int ints[4];
  2679. (void) get_options(strings, ARRAY_SIZE(ints), ints);
  2680. if (ints[0] > 0) {
  2681. cdu31a_port = ints[1];
  2682. }
  2683. if (ints[0] > 1) {
  2684. cdu31a_irq = ints[2];
  2685. }
  2686. if ((strings != NULL) && (*strings != '\0')) {
  2687. if (strcmp(strings, "PAS") == 0) {
  2688. sony_pas_init = 1;
  2689. } else {
  2690. printk(KERN_NOTICE PFX "Unknown interface type: %s\n",
  2691. strings);
  2692. }
  2693. }
  2694. return 1;
  2695. }
  2696. __setup("cdu31a=", cdu31a_setup);
  2697. #endif
  2698. /*
  2699. * Initialize the driver.
  2700. */
  2701. int __init cdu31a_init(void)
  2702. {
  2703. struct s_sony_drive_config drive_config;
  2704. struct gendisk *disk;
  2705. int deficiency = 0;
  2706. unsigned int res_size;
  2707. char msg[255];
  2708. char buf[40];
  2709. int i;
  2710. int tmp_irq;
  2711. /*
  2712. * According to Alex Freed (freed@europa.orion.adobe.com), this is
  2713. * required for the Fusion CD-16 package. If the sound driver is
  2714. * loaded, it should work fine, but just in case...
  2715. *
  2716. * The following turn on the CD-ROM interface for a Fusion CD-16.
  2717. */
  2718. if (sony_pas_init) {
  2719. outb(0xbc, 0x9a01);
  2720. outb(0xe2, 0x9a01);
  2721. }
  2722. /* Setting the base I/O address to 0xffff will disable it. */
  2723. if (cdu31a_port == 0xffff)
  2724. goto errout3;
  2725. if (cdu31a_port != 0) {
  2726. /* Need IRQ 0 because we can't sleep here. */
  2727. tmp_irq = cdu31a_irq;
  2728. cdu31a_irq = 0;
  2729. if (!get_drive_configuration(cdu31a_port,
  2730. drive_config.exec_status,
  2731. &res_size))
  2732. goto errout3;
  2733. cdu31a_irq = tmp_irq;
  2734. } else {
  2735. cdu31a_irq = 0;
  2736. for (i = 0; cdu31a_addresses[i].base; i++) {
  2737. if (get_drive_configuration(cdu31a_addresses[i].base,
  2738. drive_config.exec_status,
  2739. &res_size)) {
  2740. cdu31a_irq = cdu31a_addresses[i].int_num;
  2741. break;
  2742. }
  2743. }
  2744. if (!cdu31a_port)
  2745. goto errout3;
  2746. }
  2747. if (register_blkdev(MAJOR_NR, "cdu31a"))
  2748. goto errout2;
  2749. disk = alloc_disk(1);
  2750. if (!disk)
  2751. goto errout1;
  2752. disk->major = MAJOR_NR;
  2753. disk->first_minor = 0;
  2754. sprintf(disk->disk_name, "cdu31a");
  2755. disk->fops = &scd_bdops;
  2756. disk->flags = GENHD_FL_CD;
  2757. if (SONY_HWC_DOUBLE_SPEED(drive_config))
  2758. is_double_speed = 1;
  2759. tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
  2760. cdu31a_irq = 0;
  2761. sony_speed = is_double_speed; /* Set 2X drives to 2X by default */
  2762. set_drive_params(sony_speed);
  2763. cdu31a_irq = tmp_irq;
  2764. if (cdu31a_irq > 0) {
  2765. if (request_irq
  2766. (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
  2767. "cdu31a", NULL)) {
  2768. printk(KERN_WARNING PFX "Unable to grab IRQ%d for "
  2769. "the CDU31A driver\n", cdu31a_irq);
  2770. cdu31a_irq = 0;
  2771. }
  2772. }
  2773. sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
  2774. drive_config.vendor_id,
  2775. drive_config.product_id,
  2776. drive_config.product_rev_level);
  2777. sprintf(buf, " Capabilities: %s",
  2778. load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
  2779. strcat(msg, buf);
  2780. if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
  2781. strcat(msg, ", audio");
  2782. else
  2783. deficiency |= CDC_PLAY_AUDIO;
  2784. if (SONY_HWC_EJECT(drive_config))
  2785. strcat(msg, ", eject");
  2786. else
  2787. deficiency |= CDC_OPEN_TRAY;
  2788. if (SONY_HWC_LED_SUPPORT(drive_config))
  2789. strcat(msg, ", LED");
  2790. if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
  2791. strcat(msg, ", elec. Vol");
  2792. if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
  2793. strcat(msg, ", sep. Vol");
  2794. if (is_double_speed)
  2795. strcat(msg, ", double speed");
  2796. else
  2797. deficiency |= CDC_SELECT_SPEED;
  2798. if (cdu31a_irq > 0) {
  2799. sprintf(buf, ", irq %d", cdu31a_irq);
  2800. strcat(msg, buf);
  2801. }
  2802. strcat(msg, "\n");
  2803. printk(KERN_INFO PFX "%s",msg);
  2804. cdu31a_queue = blk_init_queue(do_cdu31a_request, &cdu31a_lock);
  2805. if (!cdu31a_queue)
  2806. goto errout0;
  2807. blk_queue_hardsect_size(cdu31a_queue, 2048);
  2808. init_timer(&cdu31a_abort_timer);
  2809. cdu31a_abort_timer.function = handle_abort_timeout;
  2810. scd_info.mask = deficiency;
  2811. scd_gendisk = disk;
  2812. if (register_cdrom(&scd_info))
  2813. goto err;
  2814. disk->queue = cdu31a_queue;
  2815. add_disk(disk);
  2816. disk_changed = 1;
  2817. return 0;
  2818. err:
  2819. blk_cleanup_queue(cdu31a_queue);
  2820. errout0:
  2821. if (cdu31a_irq)
  2822. free_irq(cdu31a_irq, NULL);
  2823. printk(KERN_ERR PFX "Unable to register with Uniform cdrom driver\n");
  2824. put_disk(disk);
  2825. errout1:
  2826. if (unregister_blkdev(MAJOR_NR, "cdu31a")) {
  2827. printk(KERN_WARNING PFX "Can't unregister block device\n");
  2828. }
  2829. errout2:
  2830. release_region(cdu31a_port, 4);
  2831. errout3:
  2832. return -EIO;
  2833. }
  2834. static void __exit cdu31a_exit(void)
  2835. {
  2836. del_gendisk(scd_gendisk);
  2837. put_disk(scd_gendisk);
  2838. if (unregister_cdrom(&scd_info)) {
  2839. printk(KERN_WARNING PFX "Can't unregister from Uniform "
  2840. "cdrom driver\n");
  2841. return;
  2842. }
  2843. if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
  2844. printk(KERN_WARNING PFX "Can't unregister\n");
  2845. return;
  2846. }
  2847. blk_cleanup_queue(cdu31a_queue);
  2848. if (cdu31a_irq > 0)
  2849. free_irq(cdu31a_irq, NULL);
  2850. release_region(cdu31a_port, 4);
  2851. printk(KERN_INFO PFX "module released.\n");
  2852. }
  2853. #ifdef MODULE
  2854. module_init(cdu31a_init);
  2855. #endif
  2856. module_exit(cdu31a_exit);
  2857. MODULE_LICENSE("GPL");
  2858. MODULE_ALIAS_BLOCKDEV_MAJOR(CDU31A_CDROM_MAJOR);