aztcd.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. #define AZT_VERSION "2.60"
  2. /* $Id: aztcd.c,v 2.60 1997/11/29 09:51:19 root Exp root $
  3. linux/drivers/block/aztcd.c - Aztech CD268 CDROM driver
  4. Copyright (C) 1994-98 Werner Zimmermann(Werner.Zimmermann@fht-esslingen.de)
  5. based on Mitsumi CDROM driver by Martin Hariss and preworks by
  6. Eberhard Moenkeberg; contains contributions by Joe Nardone and Robby
  7. Schirmer.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. HISTORY
  20. V0.0 Adaption to Aztech CD268-01A Version 1.3
  21. Version is PRE_ALPHA, unresolved points:
  22. 1. I use busy wait instead of timer wait in STEN_LOW,DTEN_LOW
  23. thus driver causes CPU overhead and is very slow
  24. 2. could not find a way to stop the drive, when it is
  25. in data read mode, therefore I had to set
  26. msf.end.min/sec/frame to 0:0:1 (in azt_poll); so only one
  27. frame can be read in sequence, this is also the reason for
  28. 3. getting 'timeout in state 4' messages, but nevertheless
  29. it works
  30. W.Zimmermann, Oct. 31, 1994
  31. V0.1 Version is ALPHA, problems #2 and #3 resolved.
  32. W.Zimmermann, Nov. 3, 1994
  33. V0.2 Modification to some comments, debugging aids for partial test
  34. with Borland C under DOS eliminated. Timer interrupt wait
  35. STEN_LOW_WAIT additionally to busy wait for STEN_LOW implemented;
  36. use it only for the 'slow' commands (ACMD_GET_Q_CHANNEL, ACMD_
  37. SEEK_TO_LEAD_IN), all other commands are so 'fast', that busy
  38. waiting seems better to me than interrupt rescheduling.
  39. Besides that, when used in the wrong place, STEN_LOW_WAIT causes
  40. kernel panic.
  41. In function aztPlay command ACMD_PLAY_AUDIO added, should make
  42. audio functions work. The Aztech drive needs different commands
  43. to read data tracks and play audio tracks.
  44. W.Zimmermann, Nov. 8, 1994
  45. V0.3 Recognition of missing drive during boot up improved (speeded up).
  46. W.Zimmermann, Nov. 13, 1994
  47. V0.35 Rewrote the control mechanism in azt_poll (formerly mcd_poll)
  48. including removal of all 'goto' commands. :-);
  49. J. Nardone, Nov. 14, 1994
  50. V0.4 Renamed variables and constants to 'azt' instead of 'mcd'; had
  51. to make some "compatibility" defines in azt.h; please note,
  52. that the source file was renamed to azt.c, the include file to
  53. azt.h
  54. Speeded up drive recognition during init (will be a little bit
  55. slower than before if no drive is installed!); suggested by
  56. Robby Schirmer.
  57. read_count declared volatile and set to AZT_BUF_SIZ to make
  58. drive faster (now 300kB/sec, was 60kB/sec before, measured
  59. by 'time dd if=/dev/cdrom of=/dev/null bs=2048 count=4096';
  60. different AZT_BUF_SIZes were test, above 16 no further im-
  61. provement seems to be possible; suggested by E.Moenkeberg.
  62. W.Zimmermann, Nov. 18, 1994
  63. V0.42 Included getAztStatus command in GetQChannelInfo() to allow
  64. reading Q-channel info on audio disks, if drive is stopped,
  65. and some other bug fixes in the audio stuff, suggested by
  66. Robby Schirmer.
  67. Added more ioctls (reading data in mode 1 and mode 2).
  68. Completely removed the old azt_poll() routine.
  69. Detection of ORCHID CDS-3110 in aztcd_init implemented.
  70. Additional debugging aids (see the readme file).
  71. W.Zimmermann, Dec. 9, 1994
  72. V0.50 Autodetection of drives implemented.
  73. W.Zimmermann, Dec. 12, 1994
  74. V0.52 Prepared for including in the standard kernel, renamed most
  75. variables to contain 'azt', included autoconf.h
  76. W.Zimmermann, Dec. 16, 1994
  77. V0.6 Version for being included in the standard Linux kernel.
  78. Renamed source and header file to aztcd.c and aztcd.h
  79. W.Zimmermann, Dec. 24, 1994
  80. V0.7 Changed VERIFY_READ to VERIFY_WRITE in aztcd_ioctl, case
  81. CDROMREADMODE1 and CDROMREADMODE2; bug fix in the ioctl,
  82. which causes kernel crashes when playing audio, changed
  83. include-files (config.h instead of autoconf.h, removed
  84. delay.h)
  85. W.Zimmermann, Jan. 8, 1995
  86. V0.72 Some more modifications for adaption to the standard kernel.
  87. W.Zimmermann, Jan. 16, 1995
  88. V0.80 aztcd is now part of the standard kernel since version 1.1.83.
  89. Modified the SET_TIMER and CLEAR_TIMER macros to comply with
  90. the new timer scheme.
  91. W.Zimmermann, Jan. 21, 1995
  92. V0.90 Included CDROMVOLCTRL, but with my Aztech drive I can only turn
  93. the channels on and off. If it works better with your drive,
  94. please mail me. Also implemented ACMD_CLOSE for CDROMSTART.
  95. W.Zimmermann, Jan. 24, 1995
  96. V1.00 Implemented close and lock tray commands. Patches supplied by
  97. Frank Racis
  98. Added support for loadable MODULEs, so aztcd can now also be
  99. loaded by insmod and removed by rmmod during run time
  100. Werner Zimmermann, Mar. 24, 95
  101. V1.10 Implemented soundcard configuration for Orchid CDS-3110 drives
  102. connected to Soundwave32 cards. Release for LST 2.1.
  103. (still experimental)
  104. Werner Zimmermann, May 8, 95
  105. V1.20 Implemented limited support for DOSEMU0.60's cdrom.c. Now it works, but
  106. sometimes DOSEMU may hang for 30 seconds or so. A fully functional ver-
  107. sion needs an update of Dosemu0.60's cdrom.c, which will come with the
  108. next revision of Dosemu.
  109. Also Soundwave32 support now works.
  110. Werner Zimmermann, May 22, 95
  111. V1.30 Auto-eject feature. Inspired by Franc Racis (racis@psu.edu)
  112. Werner Zimmermann, July 4, 95
  113. V1.40 Started multisession support. Implementation copied from mcdx.c
  114. by Heiko Schlittermann. Not tested yet.
  115. Werner Zimmermann, July 15, 95
  116. V1.50 Implementation of ioctl CDROMRESET, continued multisession, began
  117. XA, but still untested. Heavy modifications to drive status de-
  118. tection.
  119. Werner Zimmermann, July 25, 95
  120. V1.60 XA support now should work. Speeded up drive recognition in cases,
  121. where no drive is installed.
  122. Werner Zimmermann, August 8, 1995
  123. V1.70 Multisession support now is completed, but there is still not
  124. enough testing done. If you can test it, please contact me. For
  125. details please read Documentation/cdrom/aztcd
  126. Werner Zimmermann, August 19, 1995
  127. V1.80 Modification to suit the new kernel boot procedure introduced
  128. with kernel 1.3.33. Will definitely not work with older kernels.
  129. Programming done by Linus himself.
  130. Werner Zimmermann, October 11, 1995
  131. V1.90 Support for Conrad TXC drives, thank's to Jochen Kunz and Olaf Kaluza.
  132. Werner Zimmermann, October 21, 1995
  133. V2.00 Changed #include "blk.h" to <linux/blk.h> as the directory
  134. structure was changed. README.aztcd is now /usr/src/docu-
  135. mentation/cdrom/aztcd
  136. Werner Zimmermann, November 10, 95
  137. V2.10 Started to modify azt_poll to prevent reading beyond end of
  138. tracks.
  139. Werner Zimmermann, December 3, 95
  140. V2.20 Changed some comments
  141. Werner Zimmermann, April 1, 96
  142. V2.30 Implemented support for CyCDROM CR520, CR940, Code for CR520
  143. delivered by H.Berger with preworks by E.Moenkeberg.
  144. Werner Zimmermann, April 29, 96
  145. V2.40 Reorganized the placement of functions in the source code file
  146. to reflect the layered approach; did not actually change code
  147. Werner Zimmermann, May 1, 96
  148. V2.50 Heiko Eissfeldt suggested to remove some VERIFY_READs in
  149. aztcd_ioctl; check_aztcd_media_change modified
  150. Werner Zimmermann, May 16, 96
  151. V2.60 Implemented Auto-Probing; made changes for kernel's 2.1.xx blocksize
  152. Adaption to linux kernel > 2.1.0
  153. Werner Zimmermann, Nov 29, 97
  154. November 1999 -- Make kernel-parameter implementation work with 2.3.x
  155. Removed init_module & cleanup_module in favor of
  156. module_init & module_exit.
  157. Torben Mathiasen <tmm@image.dk>
  158. */
  159. #include <linux/blkdev.h>
  160. #include "aztcd.h"
  161. #include <linux/module.h>
  162. #include <linux/errno.h>
  163. #include <linux/mm.h>
  164. #include <linux/timer.h>
  165. #include <linux/fs.h>
  166. #include <linux/kernel.h>
  167. #include <linux/cdrom.h>
  168. #include <linux/ioport.h>
  169. #include <linux/string.h>
  170. #include <linux/major.h>
  171. #include <linux/init.h>
  172. #include <asm/system.h>
  173. #include <asm/io.h>
  174. #include <asm/uaccess.h>
  175. /*###########################################################################
  176. Defines
  177. ###########################################################################
  178. */
  179. #define MAJOR_NR AZTECH_CDROM_MAJOR
  180. #define QUEUE (azt_queue)
  181. #define CURRENT elv_next_request(azt_queue)
  182. #define SET_TIMER(func, jifs) delay_timer.expires = jiffies + (jifs); \
  183. delay_timer.function = (void *) (func); \
  184. add_timer(&delay_timer);
  185. #define CLEAR_TIMER del_timer(&delay_timer);
  186. #define RETURNM(message,value) {printk("aztcd: Warning: %s failed\n",message);\
  187. return value;}
  188. #define RETURN(message) {printk("aztcd: Warning: %s failed\n",message);\
  189. return;}
  190. /* Macros to switch the IDE-interface to the slave device and back to the master*/
  191. #define SWITCH_IDE_SLAVE outb_p(0xa0,azt_port+6); \
  192. outb_p(0x10,azt_port+6); \
  193. outb_p(0x00,azt_port+7); \
  194. outb_p(0x10,azt_port+6);
  195. #define SWITCH_IDE_MASTER outb_p(0xa0,azt_port+6);
  196. #if 0
  197. #define AZT_TEST
  198. #define AZT_TEST1 /* <int-..> */
  199. #define AZT_TEST2 /* do_aztcd_request */
  200. #define AZT_TEST3 /* AZT_S_state */
  201. #define AZT_TEST4 /* QUICK_LOOP-counter */
  202. #define AZT_TEST5 /* port(1) state */
  203. #define AZT_DEBUG
  204. #define AZT_DEBUG_MULTISESSION
  205. #endif
  206. static struct request_queue *azt_queue;
  207. static int current_valid(void)
  208. {
  209. return CURRENT &&
  210. CURRENT->cmd == READ &&
  211. CURRENT->sector != -1;
  212. }
  213. #define AFL_STATUSorDATA (AFL_STATUS | AFL_DATA)
  214. #define AZT_BUF_SIZ 16
  215. #define READ_TIMEOUT 3000
  216. #define azt_port aztcd /*needed for the modutils */
  217. /*##########################################################################
  218. Type Definitions
  219. ##########################################################################
  220. */
  221. enum azt_state_e { AZT_S_IDLE, /* 0 */
  222. AZT_S_START, /* 1 */
  223. AZT_S_MODE, /* 2 */
  224. AZT_S_READ, /* 3 */
  225. AZT_S_DATA, /* 4 */
  226. AZT_S_STOP, /* 5 */
  227. AZT_S_STOPPING /* 6 */
  228. };
  229. enum azt_read_modes { AZT_MODE_0, /*read mode for audio disks, not supported by Aztech firmware */
  230. AZT_MODE_1, /*read mode for normal CD-ROMs */
  231. AZT_MODE_2 /*read mode for XA CD-ROMs */
  232. };
  233. /*##########################################################################
  234. Global Variables
  235. ##########################################################################
  236. */
  237. static int aztPresent = 0;
  238. static volatile int azt_transfer_is_active = 0;
  239. static char azt_buf[CD_FRAMESIZE_RAW * AZT_BUF_SIZ]; /*buffer for block size conversion */
  240. #if AZT_PRIVATE_IOCTLS
  241. static char buf[CD_FRAMESIZE_RAW]; /*separate buffer for the ioctls */
  242. #endif
  243. static volatile int azt_buf_bn[AZT_BUF_SIZ], azt_next_bn;
  244. static volatile int azt_buf_in, azt_buf_out = -1;
  245. static volatile int azt_error = 0;
  246. static int azt_open_count = 0;
  247. static volatile enum azt_state_e azt_state = AZT_S_IDLE;
  248. #ifdef AZT_TEST3
  249. static volatile enum azt_state_e azt_state_old = AZT_S_STOP;
  250. static volatile int azt_st_old = 0;
  251. #endif
  252. static volatile enum azt_read_modes azt_read_mode = AZT_MODE_1;
  253. static int azt_mode = -1;
  254. static volatile int azt_read_count = 1;
  255. static int azt_port = AZT_BASE_ADDR;
  256. module_param(azt_port, int, 0);
  257. static int azt_port_auto[16] = AZT_BASE_AUTO;
  258. static char azt_cont = 0;
  259. static char azt_init_end = 0;
  260. static char azt_auto_eject = AZT_AUTO_EJECT;
  261. static int AztTimeout, AztTries;
  262. static DECLARE_WAIT_QUEUE_HEAD(azt_waitq);
  263. static DEFINE_TIMER(delay_timer, NULL, 0, 0);
  264. static struct azt_DiskInfo DiskInfo;
  265. static struct azt_Toc Toc[MAX_TRACKS];
  266. static struct azt_Play_msf azt_Play;
  267. static int aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  268. static char aztDiskChanged = 1;
  269. static char aztTocUpToDate = 0;
  270. static unsigned char aztIndatum;
  271. static unsigned long aztTimeOutCount;
  272. static int aztCmd = 0;
  273. static DEFINE_SPINLOCK(aztSpin);
  274. /*###########################################################################
  275. Function Prototypes
  276. ###########################################################################
  277. */
  278. /* CDROM Drive Low Level I/O Functions */
  279. static void aztStatTimer(void);
  280. /* CDROM Drive Command Functions */
  281. static int aztGetDiskInfo(void);
  282. #if AZT_MULTISESSION
  283. static int aztGetMultiDiskInfo(void);
  284. #endif
  285. static int aztGetToc(int multi);
  286. /* Kernel Interface Functions */
  287. static int check_aztcd_media_change(struct gendisk *disk);
  288. static int aztcd_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
  289. unsigned long arg);
  290. static int aztcd_open(struct inode *ip, struct file *fp);
  291. static int aztcd_release(struct inode *inode, struct file *file);
  292. static struct block_device_operations azt_fops = {
  293. .owner = THIS_MODULE,
  294. .open = aztcd_open,
  295. .release = aztcd_release,
  296. .ioctl = aztcd_ioctl,
  297. .media_changed = check_aztcd_media_change,
  298. };
  299. /* Aztcd State Machine: Controls Drive Operating State */
  300. static void azt_poll(void);
  301. /* Miscellaneous support functions */
  302. static void azt_hsg2msf(long hsg, struct msf *msf);
  303. static long azt_msf2hsg(struct msf *mp);
  304. static void azt_bin2bcd(unsigned char *p);
  305. static int azt_bcd2bin(unsigned char bcd);
  306. /*##########################################################################
  307. CDROM Drive Low Level I/O Functions
  308. ##########################################################################
  309. */
  310. /* Macros for the drive hardware interface handshake, these macros use
  311. busy waiting */
  312. /* Wait for OP_OK = drive answers with AFL_OP_OK after receiving a command*/
  313. # define OP_OK op_ok()
  314. static void op_ok(void)
  315. {
  316. aztTimeOutCount = 0;
  317. do {
  318. aztIndatum = inb(DATA_PORT);
  319. aztTimeOutCount++;
  320. if (aztTimeOutCount >= AZT_TIMEOUT) {
  321. printk("aztcd: Error Wait OP_OK\n");
  322. break;
  323. }
  324. } while (aztIndatum != AFL_OP_OK);
  325. }
  326. /* Wait for PA_OK = drive answers with AFL_PA_OK after receiving parameters*/
  327. #if 0
  328. # define PA_OK pa_ok()
  329. static void pa_ok(void)
  330. {
  331. aztTimeOutCount = 0;
  332. do {
  333. aztIndatum = inb(DATA_PORT);
  334. aztTimeOutCount++;
  335. if (aztTimeOutCount >= AZT_TIMEOUT) {
  336. printk("aztcd: Error Wait PA_OK\n");
  337. break;
  338. }
  339. } while (aztIndatum != AFL_PA_OK);
  340. }
  341. #endif
  342. /* Wait for STEN=Low = handshake signal 'AFL_.._OK available or command executed*/
  343. # define STEN_LOW sten_low()
  344. static void sten_low(void)
  345. {
  346. aztTimeOutCount = 0;
  347. do {
  348. aztIndatum = inb(STATUS_PORT);
  349. aztTimeOutCount++;
  350. if (aztTimeOutCount >= AZT_TIMEOUT) {
  351. if (azt_init_end)
  352. printk
  353. ("aztcd: Error Wait STEN_LOW commands:%x\n",
  354. aztCmd);
  355. break;
  356. }
  357. } while (aztIndatum & AFL_STATUS);
  358. }
  359. /* Wait for DTEN=Low = handshake signal 'Data available'*/
  360. # define DTEN_LOW dten_low()
  361. static void dten_low(void)
  362. {
  363. aztTimeOutCount = 0;
  364. do {
  365. aztIndatum = inb(STATUS_PORT);
  366. aztTimeOutCount++;
  367. if (aztTimeOutCount >= AZT_TIMEOUT) {
  368. printk("aztcd: Error Wait DTEN_OK\n");
  369. break;
  370. }
  371. } while (aztIndatum & AFL_DATA);
  372. }
  373. /*
  374. * Macro for timer wait on STEN=Low, should only be used for 'slow' commands;
  375. * may cause kernel panic when used in the wrong place
  376. */
  377. #define STEN_LOW_WAIT statusAzt()
  378. static void statusAzt(void)
  379. {
  380. AztTimeout = AZT_STATUS_DELAY;
  381. SET_TIMER(aztStatTimer, HZ / 100);
  382. sleep_on(&azt_waitq);
  383. if (AztTimeout <= 0)
  384. printk("aztcd: Error Wait STEN_LOW_WAIT command:%x\n",
  385. aztCmd);
  386. return;
  387. }
  388. static void aztStatTimer(void)
  389. {
  390. if (!(inb(STATUS_PORT) & AFL_STATUS)) {
  391. wake_up(&azt_waitq);
  392. return;
  393. }
  394. AztTimeout--;
  395. if (AztTimeout <= 0) {
  396. wake_up(&azt_waitq);
  397. printk("aztcd: Error aztStatTimer: Timeout\n");
  398. return;
  399. }
  400. SET_TIMER(aztStatTimer, HZ / 100);
  401. }
  402. /*##########################################################################
  403. CDROM Drive Command Functions
  404. ##########################################################################
  405. */
  406. /*
  407. * Send a single command, return -1 on error, else 0
  408. */
  409. static int aztSendCmd(int cmd)
  410. {
  411. unsigned char data;
  412. int retry;
  413. #ifdef AZT_DEBUG
  414. printk("aztcd: Executing command %x\n", cmd);
  415. #endif
  416. if ((azt_port == 0x1f0) || (azt_port == 0x170))
  417. SWITCH_IDE_SLAVE; /*switch IDE interface to slave configuration */
  418. aztCmd = cmd;
  419. outb(POLLED, MODE_PORT);
  420. do {
  421. if (inb(STATUS_PORT) & AFL_STATUS)
  422. break;
  423. inb(DATA_PORT); /* if status left from last command, read and */
  424. } while (1); /* discard it */
  425. do {
  426. if (inb(STATUS_PORT) & AFL_DATA)
  427. break;
  428. inb(DATA_PORT); /* if data left from last command, read and */
  429. } while (1); /* discard it */
  430. for (retry = 0; retry < AZT_RETRY_ATTEMPTS; retry++) {
  431. outb((unsigned char) cmd, CMD_PORT);
  432. STEN_LOW;
  433. data = inb(DATA_PORT);
  434. if (data == AFL_OP_OK) {
  435. return 0;
  436. } /*OP_OK? */
  437. if (data == AFL_OP_ERR) {
  438. STEN_LOW;
  439. data = inb(DATA_PORT);
  440. printk
  441. ("### Error 1 aztcd: aztSendCmd %x Error Code %x\n",
  442. cmd, data);
  443. }
  444. }
  445. if (retry >= AZT_RETRY_ATTEMPTS) {
  446. printk("### Error 2 aztcd: aztSendCmd %x \n", cmd);
  447. azt_error = 0xA5;
  448. }
  449. RETURNM("aztSendCmd", -1);
  450. }
  451. /*
  452. * Send a play or read command to the drive, return -1 on error, else 0
  453. */
  454. static int sendAztCmd(int cmd, struct azt_Play_msf *params)
  455. {
  456. unsigned char data;
  457. int retry;
  458. #ifdef AZT_DEBUG
  459. printk("aztcd: play start=%02x:%02x:%02x end=%02x:%02x:%02x\n",
  460. params->start.min, params->start.sec, params->start.frame,
  461. params->end.min, params->end.sec, params->end.frame);
  462. #endif
  463. for (retry = 0; retry < AZT_RETRY_ATTEMPTS; retry++) {
  464. aztSendCmd(cmd);
  465. outb(params->start.min, CMD_PORT);
  466. outb(params->start.sec, CMD_PORT);
  467. outb(params->start.frame, CMD_PORT);
  468. outb(params->end.min, CMD_PORT);
  469. outb(params->end.sec, CMD_PORT);
  470. outb(params->end.frame, CMD_PORT);
  471. STEN_LOW;
  472. data = inb(DATA_PORT);
  473. if (data == AFL_PA_OK) {
  474. return 0;
  475. } /*PA_OK ? */
  476. if (data == AFL_PA_ERR) {
  477. STEN_LOW;
  478. data = inb(DATA_PORT);
  479. printk
  480. ("### Error 1 aztcd: sendAztCmd %x Error Code %x\n",
  481. cmd, data);
  482. }
  483. }
  484. if (retry >= AZT_RETRY_ATTEMPTS) {
  485. printk("### Error 2 aztcd: sendAztCmd %x\n ", cmd);
  486. azt_error = 0xA5;
  487. }
  488. RETURNM("sendAztCmd", -1);
  489. }
  490. /*
  491. * Send a seek command to the drive, return -1 on error, else 0
  492. */
  493. static int aztSeek(struct azt_Play_msf *params)
  494. {
  495. unsigned char data;
  496. int retry;
  497. #ifdef AZT_DEBUG
  498. printk("aztcd: aztSeek %02x:%02x:%02x\n",
  499. params->start.min, params->start.sec, params->start.frame);
  500. #endif
  501. for (retry = 0; retry < AZT_RETRY_ATTEMPTS; retry++) {
  502. aztSendCmd(ACMD_SEEK);
  503. outb(params->start.min, CMD_PORT);
  504. outb(params->start.sec, CMD_PORT);
  505. outb(params->start.frame, CMD_PORT);
  506. STEN_LOW;
  507. data = inb(DATA_PORT);
  508. if (data == AFL_PA_OK) {
  509. return 0;
  510. } /*PA_OK ? */
  511. if (data == AFL_PA_ERR) {
  512. STEN_LOW;
  513. data = inb(DATA_PORT);
  514. printk("### Error 1 aztcd: aztSeek\n");
  515. }
  516. }
  517. if (retry >= AZT_RETRY_ATTEMPTS) {
  518. printk("### Error 2 aztcd: aztSeek\n ");
  519. azt_error = 0xA5;
  520. }
  521. RETURNM("aztSeek", -1);
  522. }
  523. /* Send a Set Disk Type command
  524. does not seem to work with Aztech drives, behavior is completely indepen-
  525. dent on which mode is set ???
  526. */
  527. static int aztSetDiskType(int type)
  528. {
  529. unsigned char data;
  530. int retry;
  531. #ifdef AZT_DEBUG
  532. printk("aztcd: set disk type command: type= %i\n", type);
  533. #endif
  534. for (retry = 0; retry < AZT_RETRY_ATTEMPTS; retry++) {
  535. aztSendCmd(ACMD_SET_DISK_TYPE);
  536. outb(type, CMD_PORT);
  537. STEN_LOW;
  538. data = inb(DATA_PORT);
  539. if (data == AFL_PA_OK) { /*PA_OK ? */
  540. azt_read_mode = type;
  541. return 0;
  542. }
  543. if (data == AFL_PA_ERR) {
  544. STEN_LOW;
  545. data = inb(DATA_PORT);
  546. printk
  547. ("### Error 1 aztcd: aztSetDiskType %x Error Code %x\n",
  548. type, data);
  549. }
  550. }
  551. if (retry >= AZT_RETRY_ATTEMPTS) {
  552. printk("### Error 2 aztcd: aztSetDiskType %x\n ", type);
  553. azt_error = 0xA5;
  554. }
  555. RETURNM("aztSetDiskType", -1);
  556. }
  557. /* used in azt_poll to poll the status, expects another program to issue a
  558. * ACMD_GET_STATUS directly before
  559. */
  560. static int aztStatus(void)
  561. {
  562. int st;
  563. /* int i;
  564. i = inb(STATUS_PORT) & AFL_STATUS; is STEN=0? ???
  565. if (!i)
  566. */ STEN_LOW;
  567. if (aztTimeOutCount < AZT_TIMEOUT) {
  568. st = inb(DATA_PORT) & 0xFF;
  569. return st;
  570. } else
  571. RETURNM("aztStatus", -1);
  572. }
  573. /*
  574. * Get the drive status
  575. */
  576. static int getAztStatus(void)
  577. {
  578. int st;
  579. if (aztSendCmd(ACMD_GET_STATUS))
  580. RETURNM("getAztStatus 1", -1);
  581. STEN_LOW;
  582. st = inb(DATA_PORT) & 0xFF;
  583. #ifdef AZT_DEBUG
  584. printk("aztcd: Status = %x\n", st);
  585. #endif
  586. if ((st == 0xFF) || (st & AST_CMD_CHECK)) {
  587. printk
  588. ("aztcd: AST_CMD_CHECK error or no status available\n");
  589. return -1;
  590. }
  591. if (((st & AST_MODE_BITS) != AST_BUSY)
  592. && (aztAudioStatus == CDROM_AUDIO_PLAY))
  593. /* XXX might be an error? look at q-channel? */
  594. aztAudioStatus = CDROM_AUDIO_COMPLETED;
  595. if ((st & AST_DSK_CHG) || (st & AST_NOT_READY)) {
  596. aztDiskChanged = 1;
  597. aztTocUpToDate = 0;
  598. aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  599. }
  600. return st;
  601. }
  602. /*
  603. * Send a 'Play' command and get the status. Use only from the top half.
  604. */
  605. static int aztPlay(struct azt_Play_msf *arg)
  606. {
  607. if (sendAztCmd(ACMD_PLAY_AUDIO, arg) < 0)
  608. RETURNM("aztPlay", -1);
  609. return 0;
  610. }
  611. /*
  612. * Subroutines to automatically close the door (tray) and
  613. * lock it closed when the cd is mounted. Leave the tray
  614. * locking as an option
  615. */
  616. static void aztCloseDoor(void)
  617. {
  618. aztSendCmd(ACMD_CLOSE);
  619. STEN_LOW;
  620. return;
  621. }
  622. static void aztLockDoor(void)
  623. {
  624. #if AZT_ALLOW_TRAY_LOCK
  625. aztSendCmd(ACMD_LOCK);
  626. STEN_LOW;
  627. #endif
  628. return;
  629. }
  630. static void aztUnlockDoor(void)
  631. {
  632. #if AZT_ALLOW_TRAY_LOCK
  633. aztSendCmd(ACMD_UNLOCK);
  634. STEN_LOW;
  635. #endif
  636. return;
  637. }
  638. /*
  639. * Read a value from the drive. Should return quickly, so a busy wait
  640. * is used to avoid excessive rescheduling. The read command itself must
  641. * be issued with aztSendCmd() directly before
  642. */
  643. static int aztGetValue(unsigned char *result)
  644. {
  645. int s;
  646. STEN_LOW;
  647. if (aztTimeOutCount >= AZT_TIMEOUT) {
  648. printk("aztcd: aztGetValue timeout\n");
  649. return -1;
  650. }
  651. s = inb(DATA_PORT) & 0xFF;
  652. *result = (unsigned char) s;
  653. return 0;
  654. }
  655. /*
  656. * Read the current Q-channel info. Also used for reading the
  657. * table of contents.
  658. */
  659. static int aztGetQChannelInfo(struct azt_Toc *qp)
  660. {
  661. unsigned char notUsed;
  662. int st;
  663. #ifdef AZT_DEBUG
  664. printk("aztcd: starting aztGetQChannelInfo Time:%li\n", jiffies);
  665. #endif
  666. if ((st = getAztStatus()) == -1)
  667. RETURNM("aztGetQChannelInfo 1", -1);
  668. if (aztSendCmd(ACMD_GET_Q_CHANNEL))
  669. RETURNM("aztGetQChannelInfo 2", -1);
  670. /*STEN_LOW_WAIT; ??? Dosemu0.60's cdrom.c does not like STEN_LOW_WAIT here */
  671. if (aztGetValue(&notUsed))
  672. RETURNM("aztGetQChannelInfo 3", -1); /*??? Nullbyte einlesen */
  673. if ((st & AST_MODE_BITS) == AST_INITIAL) {
  674. qp->ctrl_addr = 0; /* when audio stop ACMD_GET_Q_CHANNEL returns */
  675. qp->track = 0; /* only one byte with Aztech drives */
  676. qp->pointIndex = 0;
  677. qp->trackTime.min = 0;
  678. qp->trackTime.sec = 0;
  679. qp->trackTime.frame = 0;
  680. qp->diskTime.min = 0;
  681. qp->diskTime.sec = 0;
  682. qp->diskTime.frame = 0;
  683. return 0;
  684. } else {
  685. if (aztGetValue(&qp->ctrl_addr) < 0)
  686. RETURNM("aztGetQChannelInfo 4", -1);
  687. if (aztGetValue(&qp->track) < 0)
  688. RETURNM("aztGetQChannelInfo 4", -1);
  689. if (aztGetValue(&qp->pointIndex) < 0)
  690. RETURNM("aztGetQChannelInfo 4", -1);
  691. if (aztGetValue(&qp->trackTime.min) < 0)
  692. RETURNM("aztGetQChannelInfo 4", -1);
  693. if (aztGetValue(&qp->trackTime.sec) < 0)
  694. RETURNM("aztGetQChannelInfo 4", -1);
  695. if (aztGetValue(&qp->trackTime.frame) < 0)
  696. RETURNM("aztGetQChannelInfo 4", -1);
  697. if (aztGetValue(&notUsed) < 0)
  698. RETURNM("aztGetQChannelInfo 4", -1);
  699. if (aztGetValue(&qp->diskTime.min) < 0)
  700. RETURNM("aztGetQChannelInfo 4", -1);
  701. if (aztGetValue(&qp->diskTime.sec) < 0)
  702. RETURNM("aztGetQChannelInfo 4", -1);
  703. if (aztGetValue(&qp->diskTime.frame) < 0)
  704. RETURNM("aztGetQChannelInfo 4", -1);
  705. }
  706. #ifdef AZT_DEBUG
  707. printk("aztcd: exiting aztGetQChannelInfo Time:%li\n", jiffies);
  708. #endif
  709. return 0;
  710. }
  711. /*
  712. * Read the table of contents (TOC) and TOC header if necessary
  713. */
  714. static int aztUpdateToc(void)
  715. {
  716. int st;
  717. #ifdef AZT_DEBUG
  718. printk("aztcd: starting aztUpdateToc Time:%li\n", jiffies);
  719. #endif
  720. if (aztTocUpToDate)
  721. return 0;
  722. if (aztGetDiskInfo() < 0)
  723. return -EIO;
  724. if (aztGetToc(0) < 0)
  725. return -EIO;
  726. /*audio disk detection
  727. with my Aztech drive there is no audio status bit, so I use the copy
  728. protection bit of the first track. If this track is copy protected
  729. (copy bit = 0), I assume, it's an audio disk. Strange, but works ??? */
  730. if (!(Toc[DiskInfo.first].ctrl_addr & 0x40))
  731. DiskInfo.audio = 1;
  732. else
  733. DiskInfo.audio = 0;
  734. /* XA detection */
  735. if (!DiskInfo.audio) {
  736. azt_Play.start.min = 0; /*XA detection only seems to work */
  737. azt_Play.start.sec = 2; /*when we play a track */
  738. azt_Play.start.frame = 0;
  739. azt_Play.end.min = 0;
  740. azt_Play.end.sec = 0;
  741. azt_Play.end.frame = 1;
  742. if (sendAztCmd(ACMD_PLAY_READ, &azt_Play))
  743. return -1;
  744. DTEN_LOW;
  745. for (st = 0; st < CD_FRAMESIZE; st++)
  746. inb(DATA_PORT);
  747. }
  748. DiskInfo.xa = getAztStatus() & AST_MODE;
  749. if (DiskInfo.xa) {
  750. printk
  751. ("aztcd: XA support experimental - mail results to Werner.Zimmermann@fht-esslingen.de\n");
  752. }
  753. /*multisession detection
  754. support for multisession CDs is done automatically with Aztech drives,
  755. we don't have to take care about TOC redirection; if we want the isofs
  756. to take care about redirection, we have to set AZT_MULTISESSION to 1 */
  757. DiskInfo.multi = 0;
  758. #if AZT_MULTISESSION
  759. if (DiskInfo.xa) {
  760. aztGetMultiDiskInfo(); /*here Disk.Info.multi is set */
  761. }
  762. #endif
  763. if (DiskInfo.multi) {
  764. DiskInfo.lastSession.min = Toc[DiskInfo.next].diskTime.min;
  765. DiskInfo.lastSession.sec = Toc[DiskInfo.next].diskTime.sec;
  766. DiskInfo.lastSession.frame =
  767. Toc[DiskInfo.next].diskTime.frame;
  768. printk("aztcd: Multisession support experimental\n");
  769. } else {
  770. DiskInfo.lastSession.min =
  771. Toc[DiskInfo.first].diskTime.min;
  772. DiskInfo.lastSession.sec =
  773. Toc[DiskInfo.first].diskTime.sec;
  774. DiskInfo.lastSession.frame =
  775. Toc[DiskInfo.first].diskTime.frame;
  776. }
  777. aztTocUpToDate = 1;
  778. #ifdef AZT_DEBUG
  779. printk("aztcd: exiting aztUpdateToc Time:%li\n", jiffies);
  780. #endif
  781. return 0;
  782. }
  783. /* Read the table of contents header, i.e. no. of tracks and start of first
  784. * track
  785. */
  786. static int aztGetDiskInfo(void)
  787. {
  788. int limit;
  789. unsigned char test;
  790. struct azt_Toc qInfo;
  791. #ifdef AZT_DEBUG
  792. printk("aztcd: starting aztGetDiskInfo Time:%li\n", jiffies);
  793. #endif
  794. if (aztSendCmd(ACMD_SEEK_TO_LEADIN))
  795. RETURNM("aztGetDiskInfo 1", -1);
  796. STEN_LOW_WAIT;
  797. test = 0;
  798. for (limit = 300; limit > 0; limit--) {
  799. if (aztGetQChannelInfo(&qInfo) < 0)
  800. RETURNM("aztGetDiskInfo 2", -1);
  801. if (qInfo.pointIndex == 0xA0) { /*Number of FirstTrack */
  802. DiskInfo.first = qInfo.diskTime.min;
  803. DiskInfo.first = azt_bcd2bin(DiskInfo.first);
  804. test = test | 0x01;
  805. }
  806. if (qInfo.pointIndex == 0xA1) { /*Number of LastTrack */
  807. DiskInfo.last = qInfo.diskTime.min;
  808. DiskInfo.last = azt_bcd2bin(DiskInfo.last);
  809. test = test | 0x02;
  810. }
  811. if (qInfo.pointIndex == 0xA2) { /*DiskLength */
  812. DiskInfo.diskLength.min = qInfo.diskTime.min;
  813. DiskInfo.diskLength.sec = qInfo.diskTime.sec;
  814. DiskInfo.diskLength.frame = qInfo.diskTime.frame;
  815. test = test | 0x04;
  816. }
  817. if ((qInfo.pointIndex == DiskInfo.first) && (test & 0x01)) { /*StartTime of First Track */
  818. DiskInfo.firstTrack.min = qInfo.diskTime.min;
  819. DiskInfo.firstTrack.sec = qInfo.diskTime.sec;
  820. DiskInfo.firstTrack.frame = qInfo.diskTime.frame;
  821. test = test | 0x08;
  822. }
  823. if (test == 0x0F)
  824. break;
  825. }
  826. #ifdef AZT_DEBUG
  827. printk("aztcd: exiting aztGetDiskInfo Time:%li\n", jiffies);
  828. printk
  829. ("Disk Info: first %d last %d length %02X:%02X.%02X dez first %02X:%02X.%02X dez\n",
  830. DiskInfo.first, DiskInfo.last, DiskInfo.diskLength.min,
  831. DiskInfo.diskLength.sec, DiskInfo.diskLength.frame,
  832. DiskInfo.firstTrack.min, DiskInfo.firstTrack.sec,
  833. DiskInfo.firstTrack.frame);
  834. #endif
  835. if (test != 0x0F)
  836. return -1;
  837. return 0;
  838. }
  839. #if AZT_MULTISESSION
  840. /*
  841. * Get Multisession Disk Info
  842. */
  843. static int aztGetMultiDiskInfo(void)
  844. {
  845. int limit, k = 5;
  846. unsigned char test;
  847. struct azt_Toc qInfo;
  848. #ifdef AZT_DEBUG
  849. printk("aztcd: starting aztGetMultiDiskInfo\n");
  850. #endif
  851. do {
  852. azt_Play.start.min = Toc[DiskInfo.last + 1].diskTime.min;
  853. azt_Play.start.sec = Toc[DiskInfo.last + 1].diskTime.sec;
  854. azt_Play.start.frame =
  855. Toc[DiskInfo.last + 1].diskTime.frame;
  856. test = 0;
  857. for (limit = 30; limit > 0; limit--) { /*Seek for LeadIn of next session */
  858. if (aztSeek(&azt_Play))
  859. RETURNM("aztGetMultiDiskInfo 1", -1);
  860. if (aztGetQChannelInfo(&qInfo) < 0)
  861. RETURNM("aztGetMultiDiskInfo 2", -1);
  862. if ((qInfo.track == 0) && (qInfo.pointIndex))
  863. break; /*LeadIn found */
  864. if ((azt_Play.start.sec += 10) > 59) {
  865. azt_Play.start.sec = 0;
  866. azt_Play.start.min++;
  867. }
  868. }
  869. if (!limit)
  870. break; /*Check, if a leadin track was found, if not we're
  871. at the end of the disk */
  872. #ifdef AZT_DEBUG_MULTISESSION
  873. printk("leadin found track %d pointIndex %x limit %d\n",
  874. qInfo.track, qInfo.pointIndex, limit);
  875. #endif
  876. for (limit = 300; limit > 0; limit--) {
  877. if (++azt_Play.start.frame > 74) {
  878. azt_Play.start.frame = 0;
  879. if (azt_Play.start.sec > 59) {
  880. azt_Play.start.sec = 0;
  881. azt_Play.start.min++;
  882. }
  883. }
  884. if (aztSeek(&azt_Play))
  885. RETURNM("aztGetMultiDiskInfo 3", -1);
  886. if (aztGetQChannelInfo(&qInfo) < 0)
  887. RETURNM("aztGetMultiDiskInfo 4", -1);
  888. if (qInfo.pointIndex == 0xA0) { /*Number of NextTrack */
  889. DiskInfo.next = qInfo.diskTime.min;
  890. DiskInfo.next = azt_bcd2bin(DiskInfo.next);
  891. test = test | 0x01;
  892. }
  893. if (qInfo.pointIndex == 0xA1) { /*Number of LastTrack */
  894. DiskInfo.last = qInfo.diskTime.min;
  895. DiskInfo.last = azt_bcd2bin(DiskInfo.last);
  896. test = test | 0x02;
  897. }
  898. if (qInfo.pointIndex == 0xA2) { /*DiskLength */
  899. DiskInfo.diskLength.min =
  900. qInfo.diskTime.min;
  901. DiskInfo.diskLength.sec =
  902. qInfo.diskTime.sec;
  903. DiskInfo.diskLength.frame =
  904. qInfo.diskTime.frame;
  905. test = test | 0x04;
  906. }
  907. if ((qInfo.pointIndex == DiskInfo.next) && (test & 0x01)) { /*StartTime of Next Track */
  908. DiskInfo.nextSession.min =
  909. qInfo.diskTime.min;
  910. DiskInfo.nextSession.sec =
  911. qInfo.diskTime.sec;
  912. DiskInfo.nextSession.frame =
  913. qInfo.diskTime.frame;
  914. test = test | 0x08;
  915. }
  916. if (test == 0x0F)
  917. break;
  918. }
  919. #ifdef AZT_DEBUG_MULTISESSION
  920. printk
  921. ("MultiDisk Info: first %d next %d last %d length %02x:%02x.%02x dez first %02x:%02x.%02x dez next %02x:%02x.%02x dez\n",
  922. DiskInfo.first, DiskInfo.next, DiskInfo.last,
  923. DiskInfo.diskLength.min, DiskInfo.diskLength.sec,
  924. DiskInfo.diskLength.frame, DiskInfo.firstTrack.min,
  925. DiskInfo.firstTrack.sec, DiskInfo.firstTrack.frame,
  926. DiskInfo.nextSession.min, DiskInfo.nextSession.sec,
  927. DiskInfo.nextSession.frame);
  928. #endif
  929. if (test != 0x0F)
  930. break;
  931. else
  932. DiskInfo.multi = 1; /*found TOC of more than one session */
  933. aztGetToc(1);
  934. } while (--k);
  935. #ifdef AZT_DEBUG
  936. printk("aztcd: exiting aztGetMultiDiskInfo Time:%li\n", jiffies);
  937. #endif
  938. return 0;
  939. }
  940. #endif
  941. /*
  942. * Read the table of contents (TOC)
  943. */
  944. static int aztGetToc(int multi)
  945. {
  946. int i, px;
  947. int limit;
  948. struct azt_Toc qInfo;
  949. #ifdef AZT_DEBUG
  950. printk("aztcd: starting aztGetToc Time:%li\n", jiffies);
  951. #endif
  952. if (!multi) {
  953. for (i = 0; i < MAX_TRACKS; i++)
  954. Toc[i].pointIndex = 0;
  955. i = DiskInfo.last + 3;
  956. } else {
  957. for (i = DiskInfo.next; i < MAX_TRACKS; i++)
  958. Toc[i].pointIndex = 0;
  959. i = DiskInfo.last + 4 - DiskInfo.next;
  960. }
  961. /*Is there a good reason to stop motor before TOC read?
  962. if (aztSendCmd(ACMD_STOP)) RETURNM("aztGetToc 1",-1);
  963. STEN_LOW_WAIT;
  964. */
  965. if (!multi) {
  966. azt_mode = 0x05;
  967. if (aztSendCmd(ACMD_SEEK_TO_LEADIN))
  968. RETURNM("aztGetToc 2", -1);
  969. STEN_LOW_WAIT;
  970. }
  971. for (limit = 300; limit > 0; limit--) {
  972. if (multi) {
  973. if (++azt_Play.start.sec > 59) {
  974. azt_Play.start.sec = 0;
  975. azt_Play.start.min++;
  976. }
  977. if (aztSeek(&azt_Play))
  978. RETURNM("aztGetToc 3", -1);
  979. }
  980. if (aztGetQChannelInfo(&qInfo) < 0)
  981. break;
  982. px = azt_bcd2bin(qInfo.pointIndex);
  983. if (px > 0 && px < MAX_TRACKS && qInfo.track == 0)
  984. if (Toc[px].pointIndex == 0) {
  985. Toc[px] = qInfo;
  986. i--;
  987. }
  988. if (i <= 0)
  989. break;
  990. }
  991. Toc[DiskInfo.last + 1].diskTime = DiskInfo.diskLength;
  992. Toc[DiskInfo.last].trackTime = DiskInfo.diskLength;
  993. #ifdef AZT_DEBUG_MULTISESSION
  994. printk("aztcd: exiting aztGetToc\n");
  995. for (i = 1; i <= DiskInfo.last + 1; i++)
  996. printk
  997. ("i = %2d ctl-adr = %02X track %2d px %02X %02X:%02X.%02X dez %02X:%02X.%02X dez\n",
  998. i, Toc[i].ctrl_addr, Toc[i].track, Toc[i].pointIndex,
  999. Toc[i].trackTime.min, Toc[i].trackTime.sec,
  1000. Toc[i].trackTime.frame, Toc[i].diskTime.min,
  1001. Toc[i].diskTime.sec, Toc[i].diskTime.frame);
  1002. for (i = 100; i < 103; i++)
  1003. printk
  1004. ("i = %2d ctl-adr = %02X track %2d px %02X %02X:%02X.%02X dez %02X:%02X.%02X dez\n",
  1005. i, Toc[i].ctrl_addr, Toc[i].track, Toc[i].pointIndex,
  1006. Toc[i].trackTime.min, Toc[i].trackTime.sec,
  1007. Toc[i].trackTime.frame, Toc[i].diskTime.min,
  1008. Toc[i].diskTime.sec, Toc[i].diskTime.frame);
  1009. #endif
  1010. return limit > 0 ? 0 : -1;
  1011. }
  1012. /*##########################################################################
  1013. Kernel Interface Functions
  1014. ##########################################################################
  1015. */
  1016. #ifndef MODULE
  1017. static int __init aztcd_setup(char *str)
  1018. {
  1019. int ints[4];
  1020. (void) get_options(str, ARRAY_SIZE(ints), ints);
  1021. if (ints[0] > 0)
  1022. azt_port = ints[1];
  1023. if (ints[1] > 1)
  1024. azt_cont = ints[2];
  1025. return 1;
  1026. }
  1027. __setup("aztcd=", aztcd_setup);
  1028. #endif /* !MODULE */
  1029. /*
  1030. * Checking if the media has been changed
  1031. */
  1032. static int check_aztcd_media_change(struct gendisk *disk)
  1033. {
  1034. if (aztDiskChanged) { /* disk changed */
  1035. aztDiskChanged = 0;
  1036. return 1;
  1037. } else
  1038. return 0; /* no change */
  1039. }
  1040. /*
  1041. * Kernel IO-controls
  1042. */
  1043. static int aztcd_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
  1044. unsigned long arg)
  1045. {
  1046. int i;
  1047. struct azt_Toc qInfo;
  1048. struct cdrom_ti ti;
  1049. struct cdrom_tochdr tocHdr;
  1050. struct cdrom_msf msf;
  1051. struct cdrom_tocentry entry;
  1052. struct azt_Toc *tocPtr;
  1053. struct cdrom_subchnl subchnl;
  1054. struct cdrom_volctrl volctrl;
  1055. void __user *argp = (void __user *)arg;
  1056. #ifdef AZT_DEBUG
  1057. printk("aztcd: starting aztcd_ioctl - Command:%x Time: %li\n",
  1058. cmd, jiffies);
  1059. printk("aztcd Status %x\n", getAztStatus());
  1060. #endif
  1061. if (!ip)
  1062. RETURNM("aztcd_ioctl 1", -EINVAL);
  1063. if (getAztStatus() < 0)
  1064. RETURNM("aztcd_ioctl 2", -EIO);
  1065. if ((!aztTocUpToDate) || (aztDiskChanged)) {
  1066. if ((i = aztUpdateToc()) < 0)
  1067. RETURNM("aztcd_ioctl 3", i); /* error reading TOC */
  1068. }
  1069. switch (cmd) {
  1070. case CDROMSTART: /* Spin up the drive. Don't know, what to do,
  1071. at least close the tray */
  1072. #if AZT_PRIVATE_IOCTLS
  1073. if (aztSendCmd(ACMD_CLOSE))
  1074. RETURNM("aztcd_ioctl 4", -1);
  1075. STEN_LOW_WAIT;
  1076. #endif
  1077. break;
  1078. case CDROMSTOP: /* Spin down the drive */
  1079. if (aztSendCmd(ACMD_STOP))
  1080. RETURNM("aztcd_ioctl 5", -1);
  1081. STEN_LOW_WAIT;
  1082. /* should we do anything if it fails? */
  1083. aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  1084. break;
  1085. case CDROMPAUSE: /* Pause the drive */
  1086. if (aztAudioStatus != CDROM_AUDIO_PLAY)
  1087. return -EINVAL;
  1088. if (aztGetQChannelInfo(&qInfo) < 0) { /* didn't get q channel info */
  1089. aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  1090. RETURNM("aztcd_ioctl 7", 0);
  1091. }
  1092. azt_Play.start = qInfo.diskTime; /* remember restart point */
  1093. if (aztSendCmd(ACMD_PAUSE))
  1094. RETURNM("aztcd_ioctl 8", -1);
  1095. STEN_LOW_WAIT;
  1096. aztAudioStatus = CDROM_AUDIO_PAUSED;
  1097. break;
  1098. case CDROMRESUME: /* Play it again, Sam */
  1099. if (aztAudioStatus != CDROM_AUDIO_PAUSED)
  1100. return -EINVAL;
  1101. /* restart the drive at the saved position. */
  1102. i = aztPlay(&azt_Play);
  1103. if (i < 0) {
  1104. aztAudioStatus = CDROM_AUDIO_ERROR;
  1105. return -EIO;
  1106. }
  1107. aztAudioStatus = CDROM_AUDIO_PLAY;
  1108. break;
  1109. case CDROMMULTISESSION: /*multisession support -- experimental */
  1110. {
  1111. struct cdrom_multisession ms;
  1112. #ifdef AZT_DEBUG
  1113. printk("aztcd ioctl MULTISESSION\n");
  1114. #endif
  1115. if (copy_from_user(&ms, argp,
  1116. sizeof(struct cdrom_multisession)))
  1117. return -EFAULT;
  1118. if (ms.addr_format == CDROM_MSF) {
  1119. ms.addr.msf.minute =
  1120. azt_bcd2bin(DiskInfo.lastSession.min);
  1121. ms.addr.msf.second =
  1122. azt_bcd2bin(DiskInfo.lastSession.sec);
  1123. ms.addr.msf.frame =
  1124. azt_bcd2bin(DiskInfo.lastSession.
  1125. frame);
  1126. } else if (ms.addr_format == CDROM_LBA)
  1127. ms.addr.lba =
  1128. azt_msf2hsg(&DiskInfo.lastSession);
  1129. else
  1130. return -EINVAL;
  1131. ms.xa_flag = DiskInfo.xa;
  1132. if (copy_to_user(argp, &ms,
  1133. sizeof(struct cdrom_multisession)))
  1134. return -EFAULT;
  1135. #ifdef AZT_DEBUG
  1136. if (ms.addr_format == CDROM_MSF)
  1137. printk
  1138. ("aztcd multisession xa:%d, msf:%02x:%02x.%02x [%02x:%02x.%02x])\n",
  1139. ms.xa_flag, ms.addr.msf.minute,
  1140. ms.addr.msf.second, ms.addr.msf.frame,
  1141. DiskInfo.lastSession.min,
  1142. DiskInfo.lastSession.sec,
  1143. DiskInfo.lastSession.frame);
  1144. else
  1145. printk
  1146. ("aztcd multisession %d, lba:0x%08x [%02x:%02x.%02x])\n",
  1147. ms.xa_flag, ms.addr.lba,
  1148. DiskInfo.lastSession.min,
  1149. DiskInfo.lastSession.sec,
  1150. DiskInfo.lastSession.frame);
  1151. #endif
  1152. return 0;
  1153. }
  1154. case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
  1155. if (copy_from_user(&ti, argp, sizeof ti))
  1156. return -EFAULT;
  1157. if (ti.cdti_trk0 < DiskInfo.first
  1158. || ti.cdti_trk0 > DiskInfo.last
  1159. || ti.cdti_trk1 < ti.cdti_trk0) {
  1160. return -EINVAL;
  1161. }
  1162. if (ti.cdti_trk1 > DiskInfo.last)
  1163. ti.cdti_trk1 = DiskInfo.last;
  1164. azt_Play.start = Toc[ti.cdti_trk0].diskTime;
  1165. azt_Play.end = Toc[ti.cdti_trk1 + 1].diskTime;
  1166. #ifdef AZT_DEBUG
  1167. printk("aztcd play: %02x:%02x.%02x to %02x:%02x.%02x\n",
  1168. azt_Play.start.min, azt_Play.start.sec,
  1169. azt_Play.start.frame, azt_Play.end.min,
  1170. azt_Play.end.sec, azt_Play.end.frame);
  1171. #endif
  1172. i = aztPlay(&azt_Play);
  1173. if (i < 0) {
  1174. aztAudioStatus = CDROM_AUDIO_ERROR;
  1175. return -EIO;
  1176. }
  1177. aztAudioStatus = CDROM_AUDIO_PLAY;
  1178. break;
  1179. case CDROMPLAYMSF: /* Play starting at the given MSF address. */
  1180. /* if (aztAudioStatus == CDROM_AUDIO_PLAY)
  1181. { if (aztSendCmd(ACMD_STOP)) RETURNM("aztcd_ioctl 9",-1);
  1182. STEN_LOW;
  1183. aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  1184. }
  1185. */
  1186. if (copy_from_user(&msf, argp, sizeof msf))
  1187. return -EFAULT;
  1188. /* convert to bcd */
  1189. azt_bin2bcd(&msf.cdmsf_min0);
  1190. azt_bin2bcd(&msf.cdmsf_sec0);
  1191. azt_bin2bcd(&msf.cdmsf_frame0);
  1192. azt_bin2bcd(&msf.cdmsf_min1);
  1193. azt_bin2bcd(&msf.cdmsf_sec1);
  1194. azt_bin2bcd(&msf.cdmsf_frame1);
  1195. azt_Play.start.min = msf.cdmsf_min0;
  1196. azt_Play.start.sec = msf.cdmsf_sec0;
  1197. azt_Play.start.frame = msf.cdmsf_frame0;
  1198. azt_Play.end.min = msf.cdmsf_min1;
  1199. azt_Play.end.sec = msf.cdmsf_sec1;
  1200. azt_Play.end.frame = msf.cdmsf_frame1;
  1201. #ifdef AZT_DEBUG
  1202. printk("aztcd play: %02x:%02x.%02x to %02x:%02x.%02x\n",
  1203. azt_Play.start.min, azt_Play.start.sec,
  1204. azt_Play.start.frame, azt_Play.end.min,
  1205. azt_Play.end.sec, azt_Play.end.frame);
  1206. #endif
  1207. i = aztPlay(&azt_Play);
  1208. if (i < 0) {
  1209. aztAudioStatus = CDROM_AUDIO_ERROR;
  1210. return -EIO;
  1211. }
  1212. aztAudioStatus = CDROM_AUDIO_PLAY;
  1213. break;
  1214. case CDROMREADTOCHDR: /* Read the table of contents header */
  1215. tocHdr.cdth_trk0 = DiskInfo.first;
  1216. tocHdr.cdth_trk1 = DiskInfo.last;
  1217. if (copy_to_user(argp, &tocHdr, sizeof tocHdr))
  1218. return -EFAULT;
  1219. break;
  1220. case CDROMREADTOCENTRY: /* Read an entry in the table of contents */
  1221. if (copy_from_user(&entry, argp, sizeof entry))
  1222. return -EFAULT;
  1223. if ((!aztTocUpToDate) || aztDiskChanged)
  1224. aztUpdateToc();
  1225. if (entry.cdte_track == CDROM_LEADOUT)
  1226. tocPtr = &Toc[DiskInfo.last + 1];
  1227. else if (entry.cdte_track > DiskInfo.last
  1228. || entry.cdte_track < DiskInfo.first) {
  1229. return -EINVAL;
  1230. } else
  1231. tocPtr = &Toc[entry.cdte_track];
  1232. entry.cdte_adr = tocPtr->ctrl_addr;
  1233. entry.cdte_ctrl = tocPtr->ctrl_addr >> 4;
  1234. if (entry.cdte_format == CDROM_LBA)
  1235. entry.cdte_addr.lba =
  1236. azt_msf2hsg(&tocPtr->diskTime);
  1237. else if (entry.cdte_format == CDROM_MSF) {
  1238. entry.cdte_addr.msf.minute =
  1239. azt_bcd2bin(tocPtr->diskTime.min);
  1240. entry.cdte_addr.msf.second =
  1241. azt_bcd2bin(tocPtr->diskTime.sec);
  1242. entry.cdte_addr.msf.frame =
  1243. azt_bcd2bin(tocPtr->diskTime.frame);
  1244. } else {
  1245. return -EINVAL;
  1246. }
  1247. if (copy_to_user(argp, &entry, sizeof entry))
  1248. return -EFAULT;
  1249. break;
  1250. case CDROMSUBCHNL: /* Get subchannel info */
  1251. if (copy_from_user
  1252. (&subchnl, argp, sizeof(struct cdrom_subchnl)))
  1253. return -EFAULT;
  1254. if (aztGetQChannelInfo(&qInfo) < 0) {
  1255. #ifdef AZT_DEBUG
  1256. printk
  1257. ("aztcd: exiting aztcd_ioctl - Error 3 - Command:%x\n",
  1258. cmd);
  1259. #endif
  1260. return -EIO;
  1261. }
  1262. subchnl.cdsc_audiostatus = aztAudioStatus;
  1263. subchnl.cdsc_adr = qInfo.ctrl_addr;
  1264. subchnl.cdsc_ctrl = qInfo.ctrl_addr >> 4;
  1265. subchnl.cdsc_trk = azt_bcd2bin(qInfo.track);
  1266. subchnl.cdsc_ind = azt_bcd2bin(qInfo.pointIndex);
  1267. if (subchnl.cdsc_format == CDROM_LBA) {
  1268. subchnl.cdsc_absaddr.lba =
  1269. azt_msf2hsg(&qInfo.diskTime);
  1270. subchnl.cdsc_reladdr.lba =
  1271. azt_msf2hsg(&qInfo.trackTime);
  1272. } else { /*default */
  1273. subchnl.cdsc_format = CDROM_MSF;
  1274. subchnl.cdsc_absaddr.msf.minute =
  1275. azt_bcd2bin(qInfo.diskTime.min);
  1276. subchnl.cdsc_absaddr.msf.second =
  1277. azt_bcd2bin(qInfo.diskTime.sec);
  1278. subchnl.cdsc_absaddr.msf.frame =
  1279. azt_bcd2bin(qInfo.diskTime.frame);
  1280. subchnl.cdsc_reladdr.msf.minute =
  1281. azt_bcd2bin(qInfo.trackTime.min);
  1282. subchnl.cdsc_reladdr.msf.second =
  1283. azt_bcd2bin(qInfo.trackTime.sec);
  1284. subchnl.cdsc_reladdr.msf.frame =
  1285. azt_bcd2bin(qInfo.trackTime.frame);
  1286. }
  1287. if (copy_to_user(argp, &subchnl, sizeof(struct cdrom_subchnl)))
  1288. return -EFAULT;
  1289. break;
  1290. case CDROMVOLCTRL: /* Volume control
  1291. * With my Aztech CD268-01A volume control does not work, I can only
  1292. turn the channels on (any value !=0) or off (value==0). Maybe it
  1293. works better with your drive */
  1294. if (copy_from_user(&volctrl, argp, sizeof(volctrl)))
  1295. return -EFAULT;
  1296. azt_Play.start.min = 0x21;
  1297. azt_Play.start.sec = 0x84;
  1298. azt_Play.start.frame = volctrl.channel0;
  1299. azt_Play.end.min = volctrl.channel1;
  1300. azt_Play.end.sec = volctrl.channel2;
  1301. azt_Play.end.frame = volctrl.channel3;
  1302. sendAztCmd(ACMD_SET_VOLUME, &azt_Play);
  1303. STEN_LOW_WAIT;
  1304. break;
  1305. case CDROMEJECT:
  1306. aztUnlockDoor(); /* Assume user knows what they're doing */
  1307. /* all drives can at least stop! */
  1308. if (aztAudioStatus == CDROM_AUDIO_PLAY) {
  1309. if (aztSendCmd(ACMD_STOP))
  1310. RETURNM("azt_ioctl 10", -1);
  1311. STEN_LOW_WAIT;
  1312. }
  1313. if (aztSendCmd(ACMD_EJECT))
  1314. RETURNM("azt_ioctl 11", -1);
  1315. STEN_LOW_WAIT;
  1316. aztAudioStatus = CDROM_AUDIO_NO_STATUS;
  1317. break;
  1318. case CDROMEJECT_SW:
  1319. azt_auto_eject = (char) arg;
  1320. break;
  1321. case CDROMRESET:
  1322. outb(ACMD_SOFT_RESET, CMD_PORT); /*send reset */
  1323. STEN_LOW;
  1324. if (inb(DATA_PORT) != AFL_OP_OK) { /*OP_OK? */
  1325. printk
  1326. ("aztcd: AZTECH CD-ROM drive does not respond\n");
  1327. }
  1328. break;
  1329. /*Take care, the following code is not compatible with other CD-ROM drivers,
  1330. use it at your own risk with cdplay.c. Set AZT_PRIVATE_IOCTLS to 0 in aztcd.h,
  1331. if you do not want to use it!
  1332. */
  1333. #if AZT_PRIVATE_IOCTLS
  1334. case CDROMREADCOOKED: /*read data in mode 1 (2048 Bytes) */
  1335. case CDROMREADRAW: /*read data in mode 2 (2336 Bytes) */
  1336. {
  1337. if (copy_from_user(&msf, argp, sizeof msf))
  1338. return -EFAULT;
  1339. /* convert to bcd */
  1340. azt_bin2bcd(&msf.cdmsf_min0);
  1341. azt_bin2bcd(&msf.cdmsf_sec0);
  1342. azt_bin2bcd(&msf.cdmsf_frame0);
  1343. msf.cdmsf_min1 = 0;
  1344. msf.cdmsf_sec1 = 0;
  1345. msf.cdmsf_frame1 = 1; /*read only one frame */
  1346. azt_Play.start.min = msf.cdmsf_min0;
  1347. azt_Play.start.sec = msf.cdmsf_sec0;
  1348. azt_Play.start.frame = msf.cdmsf_frame0;
  1349. azt_Play.end.min = msf.cdmsf_min1;
  1350. azt_Play.end.sec = msf.cdmsf_sec1;
  1351. azt_Play.end.frame = msf.cdmsf_frame1;
  1352. if (cmd == CDROMREADRAW) {
  1353. if (DiskInfo.xa) {
  1354. return -1; /*XA Disks can't be read raw */
  1355. } else {
  1356. if (sendAztCmd(ACMD_PLAY_READ_RAW, &azt_Play))
  1357. return -1;
  1358. DTEN_LOW;
  1359. insb(DATA_PORT, buf, CD_FRAMESIZE_RAW);
  1360. if (copy_to_user(argp, &buf, CD_FRAMESIZE_RAW))
  1361. return -EFAULT;
  1362. }
  1363. } else
  1364. /*CDROMREADCOOKED*/ {
  1365. if (sendAztCmd(ACMD_PLAY_READ, &azt_Play))
  1366. return -1;
  1367. DTEN_LOW;
  1368. insb(DATA_PORT, buf, CD_FRAMESIZE);
  1369. if (copy_to_user(argp, &buf, CD_FRAMESIZE))
  1370. return -EFAULT;
  1371. }
  1372. }
  1373. break;
  1374. case CDROMSEEK: /*seek msf address */
  1375. if (copy_from_user(&msf, argp, sizeof msf))
  1376. return -EFAULT;
  1377. /* convert to bcd */
  1378. azt_bin2bcd(&msf.cdmsf_min0);
  1379. azt_bin2bcd(&msf.cdmsf_sec0);
  1380. azt_bin2bcd(&msf.cdmsf_frame0);
  1381. azt_Play.start.min = msf.cdmsf_min0;
  1382. azt_Play.start.sec = msf.cdmsf_sec0;
  1383. azt_Play.start.frame = msf.cdmsf_frame0;
  1384. if (aztSeek(&azt_Play))
  1385. return -1;
  1386. break;
  1387. #endif /*end of incompatible code */
  1388. case CDROMREADMODE1: /*set read data in mode 1 */
  1389. return aztSetDiskType(AZT_MODE_1);
  1390. case CDROMREADMODE2: /*set read data in mode 2 */
  1391. return aztSetDiskType(AZT_MODE_2);
  1392. default:
  1393. return -EINVAL;
  1394. }
  1395. #ifdef AZT_DEBUG
  1396. printk("aztcd: exiting aztcd_ioctl Command:%x Time:%li\n", cmd,
  1397. jiffies);
  1398. #endif
  1399. return 0;
  1400. }
  1401. /*
  1402. * Take care of the different block sizes between cdrom and Linux.
  1403. * When Linux gets variable block sizes this will probably go away.
  1404. */
  1405. static void azt_transfer(void)
  1406. {
  1407. #ifdef AZT_TEST
  1408. printk("aztcd: executing azt_transfer Time:%li\n", jiffies);
  1409. #endif
  1410. if (!current_valid())
  1411. return;
  1412. while (CURRENT->nr_sectors) {
  1413. int bn = CURRENT->sector / 4;
  1414. int i;
  1415. for (i = 0; i < AZT_BUF_SIZ && azt_buf_bn[i] != bn; ++i);
  1416. if (i < AZT_BUF_SIZ) {
  1417. int offs = (i * 4 + (CURRENT->sector & 3)) * 512;
  1418. int nr_sectors = 4 - (CURRENT->sector & 3);
  1419. if (azt_buf_out != i) {
  1420. azt_buf_out = i;
  1421. if (azt_buf_bn[i] != bn) {
  1422. azt_buf_out = -1;
  1423. continue;
  1424. }
  1425. }
  1426. if (nr_sectors > CURRENT->nr_sectors)
  1427. nr_sectors = CURRENT->nr_sectors;
  1428. memcpy(CURRENT->buffer, azt_buf + offs,
  1429. nr_sectors * 512);
  1430. CURRENT->nr_sectors -= nr_sectors;
  1431. CURRENT->sector += nr_sectors;
  1432. CURRENT->buffer += nr_sectors * 512;
  1433. } else {
  1434. azt_buf_out = -1;
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. static void do_aztcd_request(request_queue_t * q)
  1440. {
  1441. #ifdef AZT_TEST
  1442. printk(" do_aztcd_request(%ld+%ld) Time:%li\n", CURRENT->sector,
  1443. CURRENT->nr_sectors, jiffies);
  1444. #endif
  1445. if (DiskInfo.audio) {
  1446. printk("aztcd: Error, tried to mount an Audio CD\n");
  1447. end_request(CURRENT, 0);
  1448. return;
  1449. }
  1450. azt_transfer_is_active = 1;
  1451. while (current_valid()) {
  1452. azt_transfer();
  1453. if (CURRENT->nr_sectors == 0) {
  1454. end_request(CURRENT, 1);
  1455. } else {
  1456. azt_buf_out = -1; /* Want to read a block not in buffer */
  1457. if (azt_state == AZT_S_IDLE) {
  1458. if ((!aztTocUpToDate) || aztDiskChanged) {
  1459. if (aztUpdateToc() < 0) {
  1460. while (current_valid())
  1461. end_request(CURRENT, 0);
  1462. break;
  1463. }
  1464. }
  1465. azt_state = AZT_S_START;
  1466. AztTries = 5;
  1467. SET_TIMER(azt_poll, HZ / 100);
  1468. }
  1469. break;
  1470. }
  1471. }
  1472. azt_transfer_is_active = 0;
  1473. #ifdef AZT_TEST2
  1474. printk
  1475. ("azt_next_bn:%x azt_buf_in:%x azt_buf_out:%x azt_buf_bn:%x\n",
  1476. azt_next_bn, azt_buf_in, azt_buf_out, azt_buf_bn[azt_buf_in]);
  1477. printk(" do_aztcd_request ends Time:%li\n", jiffies);
  1478. #endif
  1479. }
  1480. static void azt_invalidate_buffers(void)
  1481. {
  1482. int i;
  1483. #ifdef AZT_DEBUG
  1484. printk("aztcd: executing azt_invalidate_buffers\n");
  1485. #endif
  1486. for (i = 0; i < AZT_BUF_SIZ; ++i)
  1487. azt_buf_bn[i] = -1;
  1488. azt_buf_out = -1;
  1489. }
  1490. /*
  1491. * Open the device special file. Check that a disk is in.
  1492. */
  1493. static int aztcd_open(struct inode *ip, struct file *fp)
  1494. {
  1495. int st;
  1496. #ifdef AZT_DEBUG
  1497. printk("aztcd: starting aztcd_open\n");
  1498. #endif
  1499. if (aztPresent == 0)
  1500. return -ENXIO; /* no hardware */
  1501. if (!azt_open_count && azt_state == AZT_S_IDLE) {
  1502. azt_invalidate_buffers();
  1503. st = getAztStatus(); /* check drive status */
  1504. if (st == -1)
  1505. goto err_out; /* drive doesn't respond */
  1506. if (st & AST_DOOR_OPEN) { /* close door, then get the status again. */
  1507. printk("aztcd: Door Open?\n");
  1508. aztCloseDoor();
  1509. st = getAztStatus();
  1510. }
  1511. if ((st & AST_NOT_READY) || (st & AST_DSK_CHG)) { /*no disk in drive or changed */
  1512. printk
  1513. ("aztcd: Disk Changed or No Disk in Drive?\n");
  1514. aztTocUpToDate = 0;
  1515. }
  1516. if (aztUpdateToc())
  1517. goto err_out;
  1518. }
  1519. ++azt_open_count;
  1520. aztLockDoor();
  1521. #ifdef AZT_DEBUG
  1522. printk("aztcd: exiting aztcd_open\n");
  1523. #endif
  1524. return 0;
  1525. err_out:
  1526. return -EIO;
  1527. }
  1528. /*
  1529. * On close, we flush all azt blocks from the buffer cache.
  1530. */
  1531. static int aztcd_release(struct inode *inode, struct file *file)
  1532. {
  1533. #ifdef AZT_DEBUG
  1534. printk("aztcd: executing aztcd_release\n");
  1535. printk("inode: %p, device: %s file: %p\n", inode,
  1536. inode->i_bdev->bd_disk->disk_name, file);
  1537. #endif
  1538. if (!--azt_open_count) {
  1539. azt_invalidate_buffers();
  1540. aztUnlockDoor();
  1541. if (azt_auto_eject)
  1542. aztSendCmd(ACMD_EJECT);
  1543. CLEAR_TIMER;
  1544. }
  1545. return 0;
  1546. }
  1547. static struct gendisk *azt_disk;
  1548. /*
  1549. * Test for presence of drive and initialize it. Called at boot time.
  1550. */
  1551. static int __init aztcd_init(void)
  1552. {
  1553. long int count, max_count;
  1554. unsigned char result[50];
  1555. int st;
  1556. void* status = NULL;
  1557. int i = 0;
  1558. int ret = 0;
  1559. if (azt_port == 0) {
  1560. printk(KERN_INFO "aztcd: no Aztech CD-ROM Initialization");
  1561. return -EIO;
  1562. }
  1563. printk(KERN_INFO "aztcd: AZTECH, ORCHID, OKANO, WEARNES, TXC, CyDROM "
  1564. "CD-ROM Driver\n");
  1565. printk(KERN_INFO "aztcd: (C) 1994-98 W.Zimmermann\n");
  1566. if (azt_port == -1) {
  1567. printk
  1568. ("aztcd: DriverVersion=%s For IDE/ATAPI-drives use ide-cd.c\n",
  1569. AZT_VERSION);
  1570. } else
  1571. printk
  1572. ("aztcd: DriverVersion=%s BaseAddress=0x%x For IDE/ATAPI-drives use ide-cd.c\n",
  1573. AZT_VERSION, azt_port);
  1574. printk(KERN_INFO "aztcd: If you have problems, read /usr/src/linux/"
  1575. "Documentation/cdrom/aztcd\n");
  1576. #ifdef AZT_SW32 /*CDROM connected to Soundwave32 card */
  1577. if ((0xFF00 & inw(AZT_SW32_ID_REG)) != 0x4500) {
  1578. printk
  1579. ("aztcd: no Soundwave32 card detected at base:%x init:%x config:%x id:%x\n",
  1580. AZT_SW32_BASE_ADDR, AZT_SW32_INIT,
  1581. AZT_SW32_CONFIG_REG, AZT_SW32_ID_REG);
  1582. return -EIO;
  1583. } else {
  1584. printk(KERN_INFO
  1585. "aztcd: Soundwave32 card detected at %x Version %x\n",
  1586. AZT_SW32_BASE_ADDR, inw(AZT_SW32_ID_REG));
  1587. outw(AZT_SW32_INIT, AZT_SW32_CONFIG_REG);
  1588. for (count = 0; count < 10000; count++); /*delay a bit */
  1589. }
  1590. #endif
  1591. /* check for presence of drive */
  1592. if (azt_port == -1) { /* autoprobing for proprietary interface */
  1593. for (i = 0; (azt_port_auto[i] != 0) && (i < 16); i++) {
  1594. azt_port = azt_port_auto[i];
  1595. printk(KERN_INFO "aztcd: Autoprobing BaseAddress=0x%x"
  1596. "\n", azt_port);
  1597. /*proprietary interfaces need 4 bytes */
  1598. if (!request_region(azt_port, 4, "aztcd")) {
  1599. continue;
  1600. }
  1601. outb(POLLED, MODE_PORT);
  1602. inb(CMD_PORT);
  1603. inb(CMD_PORT);
  1604. outb(ACMD_GET_VERSION, CMD_PORT); /*Try to get version info */
  1605. aztTimeOutCount = 0;
  1606. do {
  1607. aztIndatum = inb(STATUS_PORT);
  1608. aztTimeOutCount++;
  1609. if (aztTimeOutCount >= AZT_FAST_TIMEOUT)
  1610. break;
  1611. } while (aztIndatum & AFL_STATUS);
  1612. if (inb(DATA_PORT) == AFL_OP_OK) { /* OK drive found */
  1613. break;
  1614. }
  1615. else { /* Drive not found on this port - try next one */
  1616. release_region(azt_port, 4);
  1617. }
  1618. }
  1619. if ((i == 16) || (azt_port_auto[i] == 0)) {
  1620. printk(KERN_INFO "aztcd: no AZTECH CD-ROM drive found\n");
  1621. return -EIO;
  1622. }
  1623. } else { /* no autoprobing */
  1624. if ((azt_port == 0x1f0) || (azt_port == 0x170))
  1625. status = request_region(azt_port, 8, "aztcd"); /*IDE-interfaces need 8 bytes */
  1626. else
  1627. status = request_region(azt_port, 4, "aztcd"); /*proprietary interfaces need 4 bytes */
  1628. if (!status) {
  1629. printk(KERN_WARNING "aztcd: conflict, I/O port (%X) "
  1630. "already used\n", azt_port);
  1631. return -EIO;
  1632. }
  1633. if ((azt_port == 0x1f0) || (azt_port == 0x170))
  1634. SWITCH_IDE_SLAVE; /*switch IDE interface to slave configuration */
  1635. outb(POLLED, MODE_PORT);
  1636. inb(CMD_PORT);
  1637. inb(CMD_PORT);
  1638. outb(ACMD_GET_VERSION, CMD_PORT); /*Try to get version info */
  1639. aztTimeOutCount = 0;
  1640. do {
  1641. aztIndatum = inb(STATUS_PORT);
  1642. aztTimeOutCount++;
  1643. if (aztTimeOutCount >= AZT_FAST_TIMEOUT)
  1644. break;
  1645. } while (aztIndatum & AFL_STATUS);
  1646. if (inb(DATA_PORT) != AFL_OP_OK) { /*OP_OK? If not, reset and try again */
  1647. #ifndef MODULE
  1648. if (azt_cont != 0x79) {
  1649. printk(KERN_WARNING "aztcd: no AZTECH CD-ROM "
  1650. "drive found-Try boot parameter aztcd="
  1651. "<BaseAddress>,0x79\n");
  1652. ret = -EIO;
  1653. goto err_out;
  1654. }
  1655. #else
  1656. if (0) {
  1657. }
  1658. #endif
  1659. else {
  1660. printk(KERN_INFO "aztcd: drive reset - "
  1661. "please wait\n");
  1662. for (count = 0; count < 50; count++) {
  1663. inb(STATUS_PORT); /*removing all data from earlier tries */
  1664. inb(DATA_PORT);
  1665. }
  1666. outb(POLLED, MODE_PORT);
  1667. inb(CMD_PORT);
  1668. inb(CMD_PORT);
  1669. getAztStatus(); /*trap errors */
  1670. outb(ACMD_SOFT_RESET, CMD_PORT); /*send reset */
  1671. STEN_LOW;
  1672. if (inb(DATA_PORT) != AFL_OP_OK) { /*OP_OK? */
  1673. printk(KERN_WARNING "aztcd: no AZTECH "
  1674. "CD-ROM drive found\n");
  1675. ret = -EIO;
  1676. goto err_out;
  1677. }
  1678. for (count = 0; count < AZT_TIMEOUT;
  1679. count++)
  1680. barrier(); /* Stop gcc 2.96 being smart */
  1681. /* use udelay(), damnit -- AV */
  1682. if ((st = getAztStatus()) == -1) {
  1683. printk(KERN_WARNING "aztcd: Drive Status"
  1684. " Error Status=%x\n", st);
  1685. ret = -EIO;
  1686. goto err_out;
  1687. }
  1688. #ifdef AZT_DEBUG
  1689. printk(KERN_DEBUG "aztcd: Status = %x\n", st);
  1690. #endif
  1691. outb(POLLED, MODE_PORT);
  1692. inb(CMD_PORT);
  1693. inb(CMD_PORT);
  1694. outb(ACMD_GET_VERSION, CMD_PORT); /*GetVersion */
  1695. STEN_LOW;
  1696. OP_OK;
  1697. }
  1698. }
  1699. }
  1700. azt_init_end = 1;
  1701. STEN_LOW;
  1702. result[0] = inb(DATA_PORT); /*reading in a null byte??? */
  1703. for (count = 1; count < 50; count++) { /*Reading version string */
  1704. aztTimeOutCount = 0; /*here we must implement STEN_LOW differently */
  1705. do {
  1706. aztIndatum = inb(STATUS_PORT); /*because we want to exit by timeout */
  1707. aztTimeOutCount++;
  1708. if (aztTimeOutCount >= AZT_FAST_TIMEOUT)
  1709. break;
  1710. } while (aztIndatum & AFL_STATUS);
  1711. if (aztTimeOutCount >= AZT_FAST_TIMEOUT)
  1712. break; /*all chars read? */
  1713. result[count] = inb(DATA_PORT);
  1714. }
  1715. if (count > 30)
  1716. max_count = 30; /*print max.30 chars of the version string */
  1717. else
  1718. max_count = count;
  1719. printk(KERN_INFO "aztcd: FirmwareVersion=");
  1720. for (count = 1; count < max_count; count++)
  1721. printk("%c", result[count]);
  1722. printk("<<>> ");
  1723. if ((result[1] == 'A') && (result[2] == 'Z') && (result[3] == 'T')) {
  1724. printk("AZTECH drive detected\n");
  1725. /*AZTECH*/}
  1726. else if ((result[2] == 'C') && (result[3] == 'D')
  1727. && (result[4] == 'D')) {
  1728. printk("ORCHID or WEARNES drive detected\n"); /*ORCHID or WEARNES */
  1729. } else if ((result[1] == 0x03) && (result[2] == '5')) {
  1730. printk("TXC or CyCDROM drive detected\n"); /*Conrad TXC, CyCDROM */
  1731. } else { /*OTHERS or none */
  1732. printk("\nunknown drive or firmware version detected\n");
  1733. printk
  1734. ("aztcd may not run stable, if you want to try anyhow,\n");
  1735. printk("boot with: aztcd=<BaseAddress>,0x79\n");
  1736. if ((azt_cont != 0x79)) {
  1737. printk("aztcd: FirmwareVersion=");
  1738. for (count = 1; count < 5; count++)
  1739. printk("%c", result[count]);
  1740. printk("<<>> ");
  1741. printk("Aborted\n");
  1742. ret = -EIO;
  1743. goto err_out;
  1744. }
  1745. }
  1746. azt_disk = alloc_disk(1);
  1747. if (!azt_disk)
  1748. goto err_out;
  1749. if (register_blkdev(MAJOR_NR, "aztcd")) {
  1750. ret = -EIO;
  1751. goto err_out2;
  1752. }
  1753. azt_queue = blk_init_queue(do_aztcd_request, &aztSpin);
  1754. if (!azt_queue) {
  1755. ret = -ENOMEM;
  1756. goto err_out3;
  1757. }
  1758. blk_queue_hardsect_size(azt_queue, 2048);
  1759. azt_disk->major = MAJOR_NR;
  1760. azt_disk->first_minor = 0;
  1761. azt_disk->fops = &azt_fops;
  1762. sprintf(azt_disk->disk_name, "aztcd");
  1763. azt_disk->queue = azt_queue;
  1764. add_disk(azt_disk);
  1765. azt_invalidate_buffers();
  1766. aztPresent = 1;
  1767. aztCloseDoor();
  1768. return 0;
  1769. err_out3:
  1770. unregister_blkdev(MAJOR_NR, "aztcd");
  1771. err_out2:
  1772. put_disk(azt_disk);
  1773. err_out:
  1774. if ((azt_port == 0x1f0) || (azt_port == 0x170)) {
  1775. SWITCH_IDE_MASTER;
  1776. release_region(azt_port, 8); /*IDE-interface */
  1777. } else
  1778. release_region(azt_port, 4); /*proprietary interface */
  1779. return ret;
  1780. }
  1781. static void __exit aztcd_exit(void)
  1782. {
  1783. del_gendisk(azt_disk);
  1784. put_disk(azt_disk);
  1785. if ((unregister_blkdev(MAJOR_NR, "aztcd") == -EINVAL)) {
  1786. printk("What's that: can't unregister aztcd\n");
  1787. return;
  1788. }
  1789. blk_cleanup_queue(azt_queue);
  1790. if ((azt_port == 0x1f0) || (azt_port == 0x170)) {
  1791. SWITCH_IDE_MASTER;
  1792. release_region(azt_port, 8); /*IDE-interface */
  1793. } else
  1794. release_region(azt_port, 4); /*proprietary interface */
  1795. printk(KERN_INFO "aztcd module released.\n");
  1796. }
  1797. module_init(aztcd_init);
  1798. module_exit(aztcd_exit);
  1799. /*##########################################################################
  1800. Aztcd State Machine: Controls Drive Operating State
  1801. ##########################################################################
  1802. */
  1803. static void azt_poll(void)
  1804. {
  1805. int st = 0;
  1806. int loop_ctl = 1;
  1807. int skip = 0;
  1808. if (azt_error) {
  1809. if (aztSendCmd(ACMD_GET_ERROR))
  1810. RETURN("azt_poll 1");
  1811. STEN_LOW;
  1812. azt_error = inb(DATA_PORT) & 0xFF;
  1813. printk("aztcd: I/O error 0x%02x\n", azt_error);
  1814. azt_invalidate_buffers();
  1815. #ifdef WARN_IF_READ_FAILURE
  1816. if (AztTries == 5)
  1817. printk
  1818. ("aztcd: Read of Block %d Failed - Maybe Audio Disk?\n",
  1819. azt_next_bn);
  1820. #endif
  1821. if (!AztTries--) {
  1822. printk
  1823. ("aztcd: Read of Block %d Failed, Maybe Audio Disk? Giving up\n",
  1824. azt_next_bn);
  1825. if (azt_transfer_is_active) {
  1826. AztTries = 0;
  1827. loop_ctl = 0;
  1828. }
  1829. if (current_valid())
  1830. end_request(CURRENT, 0);
  1831. AztTries = 5;
  1832. }
  1833. azt_error = 0;
  1834. azt_state = AZT_S_STOP;
  1835. }
  1836. while (loop_ctl) {
  1837. loop_ctl = 0; /* each case must flip this back to 1 if we want
  1838. to come back up here */
  1839. switch (azt_state) {
  1840. case AZT_S_IDLE:
  1841. #ifdef AZT_TEST3
  1842. if (azt_state != azt_state_old) {
  1843. azt_state_old = azt_state;
  1844. printk("AZT_S_IDLE\n");
  1845. }
  1846. #endif
  1847. return;
  1848. case AZT_S_START:
  1849. #ifdef AZT_TEST3
  1850. if (azt_state != azt_state_old) {
  1851. azt_state_old = azt_state;
  1852. printk("AZT_S_START\n");
  1853. }
  1854. #endif
  1855. if (aztSendCmd(ACMD_GET_STATUS))
  1856. RETURN("azt_poll 2"); /*result will be checked by aztStatus() */
  1857. azt_state =
  1858. azt_mode == 1 ? AZT_S_READ : AZT_S_MODE;
  1859. AztTimeout = 3000;
  1860. break;
  1861. case AZT_S_MODE:
  1862. #ifdef AZT_TEST3
  1863. if (azt_state != azt_state_old) {
  1864. azt_state_old = azt_state;
  1865. printk("AZT_S_MODE\n");
  1866. }
  1867. #endif
  1868. if (!skip) {
  1869. if ((st = aztStatus()) != -1) {
  1870. if ((st & AST_DSK_CHG)
  1871. || (st & AST_NOT_READY)) {
  1872. aztDiskChanged = 1;
  1873. aztTocUpToDate = 0;
  1874. azt_invalidate_buffers();
  1875. end_request(CURRENT, 0);
  1876. printk
  1877. ("aztcd: Disk Changed or Not Ready 1 - Unmount Disk!\n");
  1878. }
  1879. } else
  1880. break;
  1881. }
  1882. skip = 0;
  1883. if ((st & AST_DOOR_OPEN) || (st & AST_NOT_READY)) {
  1884. aztDiskChanged = 1;
  1885. aztTocUpToDate = 0;
  1886. printk
  1887. ("aztcd: Disk Changed or Not Ready 2 - Unmount Disk!\n");
  1888. end_request(CURRENT, 0);
  1889. printk((st & AST_DOOR_OPEN) ?
  1890. "aztcd: door open\n" :
  1891. "aztcd: disk removed\n");
  1892. if (azt_transfer_is_active) {
  1893. azt_state = AZT_S_START;
  1894. loop_ctl = 1; /* goto immediately */
  1895. break;
  1896. }
  1897. azt_state = AZT_S_IDLE;
  1898. while (current_valid())
  1899. end_request(CURRENT, 0);
  1900. return;
  1901. }
  1902. /* if (aztSendCmd(ACMD_SET_MODE)) RETURN("azt_poll 3");
  1903. outb(0x01, DATA_PORT);
  1904. PA_OK;
  1905. STEN_LOW;
  1906. */
  1907. if (aztSendCmd(ACMD_GET_STATUS))
  1908. RETURN("azt_poll 4");
  1909. STEN_LOW;
  1910. azt_mode = 1;
  1911. azt_state = AZT_S_READ;
  1912. AztTimeout = 3000;
  1913. break;
  1914. case AZT_S_READ:
  1915. #ifdef AZT_TEST3
  1916. if (azt_state != azt_state_old) {
  1917. azt_state_old = azt_state;
  1918. printk("AZT_S_READ\n");
  1919. }
  1920. #endif
  1921. if (!skip) {
  1922. if ((st = aztStatus()) != -1) {
  1923. if ((st & AST_DSK_CHG)
  1924. || (st & AST_NOT_READY)) {
  1925. aztDiskChanged = 1;
  1926. aztTocUpToDate = 0;
  1927. azt_invalidate_buffers();
  1928. printk
  1929. ("aztcd: Disk Changed or Not Ready 3 - Unmount Disk!\n");
  1930. end_request(CURRENT, 0);
  1931. }
  1932. } else
  1933. break;
  1934. }
  1935. skip = 0;
  1936. if ((st & AST_DOOR_OPEN) || (st & AST_NOT_READY)) {
  1937. aztDiskChanged = 1;
  1938. aztTocUpToDate = 0;
  1939. printk((st & AST_DOOR_OPEN) ?
  1940. "aztcd: door open\n" :
  1941. "aztcd: disk removed\n");
  1942. if (azt_transfer_is_active) {
  1943. azt_state = AZT_S_START;
  1944. loop_ctl = 1;
  1945. break;
  1946. }
  1947. azt_state = AZT_S_IDLE;
  1948. while (current_valid())
  1949. end_request(CURRENT, 0);
  1950. return;
  1951. }
  1952. if (current_valid()) {
  1953. struct azt_Play_msf msf;
  1954. int i;
  1955. azt_next_bn = CURRENT->sector / 4;
  1956. azt_hsg2msf(azt_next_bn, &msf.start);
  1957. i = 0;
  1958. /* find out in which track we are */
  1959. while (azt_msf2hsg(&msf.start) >
  1960. azt_msf2hsg(&Toc[++i].trackTime)) {
  1961. };
  1962. if (azt_msf2hsg(&msf.start) <
  1963. azt_msf2hsg(&Toc[i].trackTime) -
  1964. AZT_BUF_SIZ) {
  1965. azt_read_count = AZT_BUF_SIZ; /*fast, because we read ahead */
  1966. /*azt_read_count=CURRENT->nr_sectors; slow, no read ahead */
  1967. } else /* don't read beyond end of track */
  1968. #if AZT_MULTISESSION
  1969. {
  1970. azt_read_count =
  1971. (azt_msf2hsg(&Toc[i].trackTime)
  1972. / 4) * 4 -
  1973. azt_msf2hsg(&msf.start);
  1974. if (azt_read_count < 0)
  1975. azt_read_count = 0;
  1976. if (azt_read_count > AZT_BUF_SIZ)
  1977. azt_read_count =
  1978. AZT_BUF_SIZ;
  1979. printk
  1980. ("aztcd: warning - trying to read beyond end of track\n");
  1981. /* printk("%i %i %li %li\n",i,azt_read_count,azt_msf2hsg(&msf.start),azt_msf2hsg(&Toc[i].trackTime));
  1982. */ }
  1983. #else
  1984. {
  1985. azt_read_count = AZT_BUF_SIZ;
  1986. }
  1987. #endif
  1988. msf.end.min = 0;
  1989. msf.end.sec = 0;
  1990. msf.end.frame = azt_read_count; /*Mitsumi here reads 0xffffff sectors */
  1991. #ifdef AZT_TEST3
  1992. printk
  1993. ("---reading msf-address %x:%x:%x %x:%x:%x\n",
  1994. msf.start.min, msf.start.sec,
  1995. msf.start.frame, msf.end.min,
  1996. msf.end.sec, msf.end.frame);
  1997. printk
  1998. ("azt_next_bn:%x azt_buf_in:%x azt_buf_out:%x azt_buf_bn:%x\n",
  1999. azt_next_bn, azt_buf_in, azt_buf_out,
  2000. azt_buf_bn[azt_buf_in]);
  2001. #endif
  2002. if (azt_read_mode == AZT_MODE_2) {
  2003. sendAztCmd(ACMD_PLAY_READ_RAW, &msf); /*XA disks in raw mode */
  2004. } else {
  2005. sendAztCmd(ACMD_PLAY_READ, &msf); /*others in cooked mode */
  2006. }
  2007. azt_state = AZT_S_DATA;
  2008. AztTimeout = READ_TIMEOUT;
  2009. } else {
  2010. azt_state = AZT_S_STOP;
  2011. loop_ctl = 1;
  2012. break;
  2013. }
  2014. break;
  2015. case AZT_S_DATA:
  2016. #ifdef AZT_TEST3
  2017. if (azt_state != azt_state_old) {
  2018. azt_state_old = azt_state;
  2019. printk("AZT_S_DATA\n");
  2020. }
  2021. #endif
  2022. st = inb(STATUS_PORT) & AFL_STATUSorDATA;
  2023. switch (st) {
  2024. case AFL_DATA:
  2025. #ifdef AZT_TEST3
  2026. if (st != azt_st_old) {
  2027. azt_st_old = st;
  2028. printk("---AFL_DATA st:%x\n", st);
  2029. }
  2030. #endif
  2031. if (!AztTries--) {
  2032. printk
  2033. ("aztcd: Read of Block %d Failed, Maybe Audio Disk ? Giving up\n",
  2034. azt_next_bn);
  2035. if (azt_transfer_is_active) {
  2036. AztTries = 0;
  2037. break;
  2038. }
  2039. if (current_valid())
  2040. end_request(CURRENT, 0);
  2041. AztTries = 5;
  2042. }
  2043. azt_state = AZT_S_START;
  2044. AztTimeout = READ_TIMEOUT;
  2045. loop_ctl = 1;
  2046. break;
  2047. case AFL_STATUSorDATA:
  2048. #ifdef AZT_TEST3
  2049. if (st != azt_st_old) {
  2050. azt_st_old = st;
  2051. printk
  2052. ("---AFL_STATUSorDATA st:%x\n",
  2053. st);
  2054. }
  2055. #endif
  2056. break;
  2057. default:
  2058. #ifdef AZT_TEST3
  2059. if (st != azt_st_old) {
  2060. azt_st_old = st;
  2061. printk("---default: st:%x\n", st);
  2062. }
  2063. #endif
  2064. AztTries = 5;
  2065. if (!current_valid() && azt_buf_in == azt_buf_out) {
  2066. azt_state = AZT_S_STOP;
  2067. loop_ctl = 1;
  2068. break;
  2069. }
  2070. if (azt_read_count <= 0)
  2071. printk
  2072. ("aztcd: warning - try to read 0 frames\n");
  2073. while (azt_read_count) { /*??? fast read ahead loop */
  2074. azt_buf_bn[azt_buf_in] = -1;
  2075. DTEN_LOW; /*??? unsolved problem, very
  2076. seldom we get timeouts
  2077. here, don't now the real
  2078. reason. With my drive this
  2079. sometimes also happens with
  2080. Aztech's original driver under
  2081. DOS. Is it a hardware bug?
  2082. I tried to recover from such
  2083. situations here. Zimmermann */
  2084. if (aztTimeOutCount >= AZT_TIMEOUT) {
  2085. printk
  2086. ("read_count:%d CURRENT->nr_sectors:%ld azt_buf_in:%d\n",
  2087. azt_read_count,
  2088. CURRENT->nr_sectors,
  2089. azt_buf_in);
  2090. printk
  2091. ("azt_transfer_is_active:%x\n",
  2092. azt_transfer_is_active);
  2093. azt_read_count = 0;
  2094. azt_state = AZT_S_STOP;
  2095. loop_ctl = 1;
  2096. end_request(CURRENT, 1); /*should we have here (1) or (0)? */
  2097. } else {
  2098. if (azt_read_mode ==
  2099. AZT_MODE_2) {
  2100. insb(DATA_PORT,
  2101. azt_buf +
  2102. CD_FRAMESIZE_RAW
  2103. * azt_buf_in,
  2104. CD_FRAMESIZE_RAW);
  2105. } else {
  2106. insb(DATA_PORT,
  2107. azt_buf +
  2108. CD_FRAMESIZE *
  2109. azt_buf_in,
  2110. CD_FRAMESIZE);
  2111. }
  2112. azt_read_count--;
  2113. #ifdef AZT_TEST3
  2114. printk
  2115. ("AZT_S_DATA; ---I've read data- read_count: %d\n",
  2116. azt_read_count);
  2117. printk
  2118. ("azt_next_bn:%d azt_buf_in:%d azt_buf_out:%d azt_buf_bn:%d\n",
  2119. azt_next_bn,
  2120. azt_buf_in,
  2121. azt_buf_out,
  2122. azt_buf_bn
  2123. [azt_buf_in]);
  2124. #endif
  2125. azt_buf_bn[azt_buf_in] =
  2126. azt_next_bn++;
  2127. if (azt_buf_out == -1)
  2128. azt_buf_out =
  2129. azt_buf_in;
  2130. azt_buf_in =
  2131. azt_buf_in + 1 ==
  2132. AZT_BUF_SIZ ? 0 :
  2133. azt_buf_in + 1;
  2134. }
  2135. }
  2136. if (!azt_transfer_is_active) {
  2137. while (current_valid()) {
  2138. azt_transfer();
  2139. if (CURRENT->nr_sectors ==
  2140. 0)
  2141. end_request(CURRENT, 1);
  2142. else
  2143. break;
  2144. }
  2145. }
  2146. if (current_valid()
  2147. && (CURRENT->sector / 4 < azt_next_bn
  2148. || CURRENT->sector / 4 >
  2149. azt_next_bn + AZT_BUF_SIZ)) {
  2150. azt_state = AZT_S_STOP;
  2151. loop_ctl = 1;
  2152. break;
  2153. }
  2154. AztTimeout = READ_TIMEOUT;
  2155. if (azt_read_count == 0) {
  2156. azt_state = AZT_S_STOP;
  2157. loop_ctl = 1;
  2158. break;
  2159. }
  2160. break;
  2161. }
  2162. break;
  2163. case AZT_S_STOP:
  2164. #ifdef AZT_TEST3
  2165. if (azt_state != azt_state_old) {
  2166. azt_state_old = azt_state;
  2167. printk("AZT_S_STOP\n");
  2168. }
  2169. #endif
  2170. if (azt_read_count != 0)
  2171. printk("aztcd: discard data=%x frames\n",
  2172. azt_read_count);
  2173. while (azt_read_count != 0) {
  2174. int i;
  2175. if (!(inb(STATUS_PORT) & AFL_DATA)) {
  2176. if (azt_read_mode == AZT_MODE_2)
  2177. for (i = 0;
  2178. i < CD_FRAMESIZE_RAW;
  2179. i++)
  2180. inb(DATA_PORT);
  2181. else
  2182. for (i = 0;
  2183. i < CD_FRAMESIZE; i++)
  2184. inb(DATA_PORT);
  2185. }
  2186. azt_read_count--;
  2187. }
  2188. if (aztSendCmd(ACMD_GET_STATUS))
  2189. RETURN("azt_poll 5");
  2190. azt_state = AZT_S_STOPPING;
  2191. AztTimeout = 1000;
  2192. break;
  2193. case AZT_S_STOPPING:
  2194. #ifdef AZT_TEST3
  2195. if (azt_state != azt_state_old) {
  2196. azt_state_old = azt_state;
  2197. printk("AZT_S_STOPPING\n");
  2198. }
  2199. #endif
  2200. if ((st = aztStatus()) == -1 && AztTimeout)
  2201. break;
  2202. if ((st != -1)
  2203. && ((st & AST_DSK_CHG)
  2204. || (st & AST_NOT_READY))) {
  2205. aztDiskChanged = 1;
  2206. aztTocUpToDate = 0;
  2207. azt_invalidate_buffers();
  2208. printk
  2209. ("aztcd: Disk Changed or Not Ready 4 - Unmount Disk!\n");
  2210. end_request(CURRENT, 0);
  2211. }
  2212. #ifdef AZT_TEST3
  2213. printk("CURRENT_VALID %d azt_mode %d\n",
  2214. current_valid(), azt_mode);
  2215. #endif
  2216. if (current_valid()) {
  2217. if (st != -1) {
  2218. if (azt_mode == 1) {
  2219. azt_state = AZT_S_READ;
  2220. loop_ctl = 1;
  2221. skip = 1;
  2222. break;
  2223. } else {
  2224. azt_state = AZT_S_MODE;
  2225. loop_ctl = 1;
  2226. skip = 1;
  2227. break;
  2228. }
  2229. } else {
  2230. azt_state = AZT_S_START;
  2231. AztTimeout = 1;
  2232. }
  2233. } else {
  2234. azt_state = AZT_S_IDLE;
  2235. return;
  2236. }
  2237. break;
  2238. default:
  2239. printk("aztcd: invalid state %d\n", azt_state);
  2240. return;
  2241. } /* case */
  2242. } /* while */
  2243. if (!AztTimeout--) {
  2244. printk("aztcd: timeout in state %d\n", azt_state);
  2245. azt_state = AZT_S_STOP;
  2246. if (aztSendCmd(ACMD_STOP))
  2247. RETURN("azt_poll 6");
  2248. STEN_LOW_WAIT;
  2249. };
  2250. SET_TIMER(azt_poll, HZ / 100);
  2251. }
  2252. /*###########################################################################
  2253. * Miscellaneous support functions
  2254. ###########################################################################
  2255. */
  2256. static void azt_hsg2msf(long hsg, struct msf *msf)
  2257. {
  2258. hsg += 150;
  2259. msf->min = hsg / 4500;
  2260. hsg %= 4500;
  2261. msf->sec = hsg / 75;
  2262. msf->frame = hsg % 75;
  2263. #ifdef AZT_DEBUG
  2264. if (msf->min >= 70)
  2265. printk("aztcd: Error hsg2msf address Minutes\n");
  2266. if (msf->sec >= 60)
  2267. printk("aztcd: Error hsg2msf address Seconds\n");
  2268. if (msf->frame >= 75)
  2269. printk("aztcd: Error hsg2msf address Frames\n");
  2270. #endif
  2271. azt_bin2bcd(&msf->min); /* convert to BCD */
  2272. azt_bin2bcd(&msf->sec);
  2273. azt_bin2bcd(&msf->frame);
  2274. }
  2275. static long azt_msf2hsg(struct msf *mp)
  2276. {
  2277. return azt_bcd2bin(mp->frame) + azt_bcd2bin(mp->sec) * 75
  2278. + azt_bcd2bin(mp->min) * 4500 - CD_MSF_OFFSET;
  2279. }
  2280. static void azt_bin2bcd(unsigned char *p)
  2281. {
  2282. int u, t;
  2283. u = *p % 10;
  2284. t = *p / 10;
  2285. *p = u | (t << 4);
  2286. }
  2287. static int azt_bcd2bin(unsigned char bcd)
  2288. {
  2289. return (bcd >> 4) * 10 + (bcd & 0xF);
  2290. }
  2291. MODULE_LICENSE("GPL");
  2292. MODULE_ALIAS_BLOCKDEV_MAJOR(AZTECH_CDROM_MAJOR);