sjcd.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /* -- sjcd.c
  2. *
  3. * Sanyo CD-ROM device driver implementation, Version 1.6
  4. * Copyright (C) 1995 Vadim V. Model
  5. *
  6. * model@cecmow.enet.dec.com
  7. * vadim@rbrf.ru
  8. * vadim@ipsun.ras.ru
  9. *
  10. *
  11. * This driver is based on pre-works by Eberhard Moenkeberg (emoenke@gwdg.de);
  12. * it was developed under use of mcd.c from Martin Harriss, with help of
  13. * Eric van der Maarel (H.T.M.v.d.Maarel@marin.nl).
  14. *
  15. * It is planned to include these routines into sbpcd.c later - to make
  16. * a "mixed use" on one cable possible for all kinds of drives which use
  17. * the SoundBlaster/Panasonic style CDROM interface. But today, the
  18. * ability to install directly from CDROM is more important than flexibility.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. * History:
  35. * 1.1 First public release with kernel version 1.3.7.
  36. * Written by Vadim Model.
  37. * 1.2 Added detection and configuration of cdrom interface
  38. * on ISP16 soundcard.
  39. * Allow for command line options: sjcd=<io_base>,<irq>,<dma>
  40. * 1.3 Some minor changes to README.sjcd.
  41. * 1.4 MSS Sound support!! Listen to a CD through the speakers.
  42. * 1.5 Module support and bugfixes.
  43. * Tray locking.
  44. * 1.6 Removed ISP16 code from this driver.
  45. * Allow only to set io base address on command line: sjcd=<io_base>
  46. * Changes to Documentation/cdrom/sjcd
  47. * Added cleanup after any error in the initialisation.
  48. * 1.7 Added code to set the sector size tables to prevent the bug present in
  49. * the previous version of this driver. Coded added by Anthony Barbachan
  50. * from bugfix tip originally suggested by Alan Cox.
  51. *
  52. * November 1999 -- Make kernel-parameter implementation work with 2.3.x
  53. * Removed init_module & cleanup_module in favor of
  54. * module_init & module_exit.
  55. * Torben Mathiasen <tmm@image.dk>
  56. */
  57. #define SJCD_VERSION_MAJOR 1
  58. #define SJCD_VERSION_MINOR 7
  59. #include <linux/module.h>
  60. #include <linux/errno.h>
  61. #include <linux/sched.h>
  62. #include <linux/mm.h>
  63. #include <linux/timer.h>
  64. #include <linux/fs.h>
  65. #include <linux/kernel.h>
  66. #include <linux/cdrom.h>
  67. #include <linux/ioport.h>
  68. #include <linux/string.h>
  69. #include <linux/major.h>
  70. #include <linux/init.h>
  71. #include <asm/system.h>
  72. #include <asm/io.h>
  73. #include <asm/uaccess.h>
  74. #include <linux/blkdev.h>
  75. #include "sjcd.h"
  76. static int sjcd_present = 0;
  77. static struct request_queue *sjcd_queue;
  78. #define MAJOR_NR SANYO_CDROM_MAJOR
  79. #define QUEUE (sjcd_queue)
  80. #define CURRENT elv_next_request(sjcd_queue)
  81. #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
  82. /*
  83. * buffer for block size conversion
  84. */
  85. static char sjcd_buf[2048 * SJCD_BUF_SIZ];
  86. static volatile int sjcd_buf_bn[SJCD_BUF_SIZ], sjcd_next_bn;
  87. static volatile int sjcd_buf_in, sjcd_buf_out = -1;
  88. /*
  89. * Status.
  90. */
  91. static unsigned short sjcd_status_valid = 0;
  92. static unsigned short sjcd_door_closed;
  93. static unsigned short sjcd_door_was_open;
  94. static unsigned short sjcd_media_is_available;
  95. static unsigned short sjcd_media_is_changed;
  96. static unsigned short sjcd_toc_uptodate = 0;
  97. static unsigned short sjcd_command_failed;
  98. static volatile unsigned char sjcd_completion_status = 0;
  99. static volatile unsigned char sjcd_completion_error = 0;
  100. static unsigned short sjcd_command_is_in_progress = 0;
  101. static unsigned short sjcd_error_reported = 0;
  102. static DEFINE_SPINLOCK(sjcd_lock);
  103. static int sjcd_open_count;
  104. static int sjcd_audio_status;
  105. static struct sjcd_play_msf sjcd_playing;
  106. static int sjcd_base = SJCD_BASE_ADDR;
  107. module_param(sjcd_base, int, 0);
  108. static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
  109. /*
  110. * Data transfer.
  111. */
  112. static volatile unsigned short sjcd_transfer_is_active = 0;
  113. enum sjcd_transfer_state {
  114. SJCD_S_IDLE = 0,
  115. SJCD_S_START = 1,
  116. SJCD_S_MODE = 2,
  117. SJCD_S_READ = 3,
  118. SJCD_S_DATA = 4,
  119. SJCD_S_STOP = 5,
  120. SJCD_S_STOPPING = 6
  121. };
  122. static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
  123. static long sjcd_transfer_timeout = 0;
  124. static int sjcd_read_count = 0;
  125. static unsigned char sjcd_mode = 0;
  126. #define SJCD_READ_TIMEOUT 5000
  127. #if defined( SJCD_GATHER_STAT )
  128. /*
  129. * Statistic.
  130. */
  131. static struct sjcd_stat statistic;
  132. #endif
  133. /*
  134. * Timer.
  135. */
  136. static DEFINE_TIMER(sjcd_delay_timer, NULL, 0, 0);
  137. #define SJCD_SET_TIMER( func, tmout ) \
  138. ( sjcd_delay_timer.expires = jiffies+tmout, \
  139. sjcd_delay_timer.function = ( void * )func, \
  140. add_timer( &sjcd_delay_timer ) )
  141. #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
  142. /*
  143. * Set up device, i.e., use command line data to set
  144. * base address.
  145. */
  146. #ifndef MODULE
  147. static int __init sjcd_setup(char *str)
  148. {
  149. int ints[2];
  150. (void) get_options(str, ARRAY_SIZE(ints), ints);
  151. if (ints[0] > 0)
  152. sjcd_base = ints[1];
  153. return 1;
  154. }
  155. __setup("sjcd=", sjcd_setup);
  156. #endif
  157. /*
  158. * Special converters.
  159. */
  160. static unsigned char bin2bcd(int bin)
  161. {
  162. int u, v;
  163. u = bin % 10;
  164. v = bin / 10;
  165. return (u | (v << 4));
  166. }
  167. static int bcd2bin(unsigned char bcd)
  168. {
  169. return ((bcd >> 4) * 10 + (bcd & 0x0F));
  170. }
  171. static long msf2hsg(struct msf *mp)
  172. {
  173. return (bcd2bin(mp->frame) + bcd2bin(mp->sec) * 75
  174. + bcd2bin(mp->min) * 4500 - 150);
  175. }
  176. static void hsg2msf(long hsg, struct msf *msf)
  177. {
  178. hsg += 150;
  179. msf->min = hsg / 4500;
  180. hsg %= 4500;
  181. msf->sec = hsg / 75;
  182. msf->frame = hsg % 75;
  183. msf->min = bin2bcd(msf->min); /* convert to BCD */
  184. msf->sec = bin2bcd(msf->sec);
  185. msf->frame = bin2bcd(msf->frame);
  186. }
  187. /*
  188. * Send a command to cdrom. Invalidate status.
  189. */
  190. static void sjcd_send_cmd(unsigned char cmd)
  191. {
  192. #if defined( SJCD_TRACE )
  193. printk("SJCD: send_cmd( 0x%x )\n", cmd);
  194. #endif
  195. outb(cmd, SJCDPORT(0));
  196. sjcd_command_is_in_progress = 1;
  197. sjcd_status_valid = 0;
  198. sjcd_command_failed = 0;
  199. }
  200. /*
  201. * Send a command with one arg to cdrom. Invalidate status.
  202. */
  203. static void sjcd_send_1_cmd(unsigned char cmd, unsigned char a)
  204. {
  205. #if defined( SJCD_TRACE )
  206. printk("SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a);
  207. #endif
  208. outb(cmd, SJCDPORT(0));
  209. outb(a, SJCDPORT(0));
  210. sjcd_command_is_in_progress = 1;
  211. sjcd_status_valid = 0;
  212. sjcd_command_failed = 0;
  213. }
  214. /*
  215. * Send a command with four args to cdrom. Invalidate status.
  216. */
  217. static void sjcd_send_4_cmd(unsigned char cmd, unsigned char a,
  218. unsigned char b, unsigned char c,
  219. unsigned char d)
  220. {
  221. #if defined( SJCD_TRACE )
  222. printk("SJCD: send_4_cmd( 0x%x )\n", cmd);
  223. #endif
  224. outb(cmd, SJCDPORT(0));
  225. outb(a, SJCDPORT(0));
  226. outb(b, SJCDPORT(0));
  227. outb(c, SJCDPORT(0));
  228. outb(d, SJCDPORT(0));
  229. sjcd_command_is_in_progress = 1;
  230. sjcd_status_valid = 0;
  231. sjcd_command_failed = 0;
  232. }
  233. /*
  234. * Send a play or read command to cdrom. Invalidate Status.
  235. */
  236. static void sjcd_send_6_cmd(unsigned char cmd, struct sjcd_play_msf *pms)
  237. {
  238. #if defined( SJCD_TRACE )
  239. printk("SJCD: send_long_cmd( 0x%x )\n", cmd);
  240. #endif
  241. outb(cmd, SJCDPORT(0));
  242. outb(pms->start.min, SJCDPORT(0));
  243. outb(pms->start.sec, SJCDPORT(0));
  244. outb(pms->start.frame, SJCDPORT(0));
  245. outb(pms->end.min, SJCDPORT(0));
  246. outb(pms->end.sec, SJCDPORT(0));
  247. outb(pms->end.frame, SJCDPORT(0));
  248. sjcd_command_is_in_progress = 1;
  249. sjcd_status_valid = 0;
  250. sjcd_command_failed = 0;
  251. }
  252. /*
  253. * Get a value from the data port. Should not block, so we use a little
  254. * wait for a while. Returns 0 if OK.
  255. */
  256. static int sjcd_load_response(void *buf, int len)
  257. {
  258. unsigned char *resp = (unsigned char *) buf;
  259. for (; len; --len) {
  260. int i;
  261. for (i = 200;
  262. i-- && !SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))););
  263. if (i > 0)
  264. *resp++ = (unsigned char) inb(SJCDPORT(0));
  265. else
  266. break;
  267. }
  268. return (len);
  269. }
  270. /*
  271. * Load and parse command completion status (drive info byte and maybe error).
  272. * Sorry, no error classification yet.
  273. */
  274. static void sjcd_load_status(void)
  275. {
  276. sjcd_media_is_changed = 0;
  277. sjcd_completion_error = 0;
  278. sjcd_completion_status = inb(SJCDPORT(0));
  279. if (sjcd_completion_status & SST_DOOR_OPENED) {
  280. sjcd_door_closed = sjcd_media_is_available = 0;
  281. } else {
  282. sjcd_door_closed = 1;
  283. if (sjcd_completion_status & SST_MEDIA_CHANGED)
  284. sjcd_media_is_available = sjcd_media_is_changed =
  285. 1;
  286. else if (sjcd_completion_status & 0x0F) {
  287. /*
  288. * OK, we seem to catch an error ...
  289. */
  290. while (!SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))));
  291. sjcd_completion_error = inb(SJCDPORT(0));
  292. if ((sjcd_completion_status & 0x08) &&
  293. (sjcd_completion_error & 0x40))
  294. sjcd_media_is_available = 0;
  295. else
  296. sjcd_command_failed = 1;
  297. } else
  298. sjcd_media_is_available = 1;
  299. }
  300. /*
  301. * Ok, status loaded successfully.
  302. */
  303. sjcd_status_valid = 1, sjcd_error_reported = 0;
  304. sjcd_command_is_in_progress = 0;
  305. /*
  306. * If the disk is changed, the TOC is not valid.
  307. */
  308. if (sjcd_media_is_changed)
  309. sjcd_toc_uptodate = 0;
  310. #if defined( SJCD_TRACE )
  311. printk("SJCD: status %02x.%02x loaded.\n",
  312. (int) sjcd_completion_status, (int) sjcd_completion_error);
  313. #endif
  314. }
  315. /*
  316. * Read status from cdrom. Check to see if the status is available.
  317. */
  318. static int sjcd_check_status(void)
  319. {
  320. /*
  321. * Try to load the response from cdrom into buffer.
  322. */
  323. if (SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1)))) {
  324. sjcd_load_status();
  325. return (1);
  326. } else {
  327. /*
  328. * No status is available.
  329. */
  330. return (0);
  331. }
  332. }
  333. /*
  334. * This is just timeout counter, and nothing more. Surprised ? :-)
  335. */
  336. static volatile long sjcd_status_timeout;
  337. /*
  338. * We need about 10 seconds to wait. The longest command takes about 5 seconds
  339. * to probe the disk (usually after tray closed or drive reset). Other values
  340. * should be thought of for other commands.
  341. */
  342. #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
  343. static void sjcd_status_timer(void)
  344. {
  345. if (sjcd_check_status()) {
  346. /*
  347. * The command completed and status is loaded, stop waiting.
  348. */
  349. wake_up(&sjcd_waitq);
  350. } else if (--sjcd_status_timeout <= 0) {
  351. /*
  352. * We are timed out.
  353. */
  354. wake_up(&sjcd_waitq);
  355. } else {
  356. /*
  357. * We have still some time to wait. Try again.
  358. */
  359. SJCD_SET_TIMER(sjcd_status_timer, 1);
  360. }
  361. }
  362. /*
  363. * Wait for status for 10 sec approx. Returns non-positive when timed out.
  364. * Should not be used while reading data CDs.
  365. */
  366. static int sjcd_wait_for_status(void)
  367. {
  368. sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
  369. SJCD_SET_TIMER(sjcd_status_timer, 1);
  370. sleep_on(&sjcd_waitq);
  371. #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
  372. if (sjcd_status_timeout <= 0)
  373. printk("SJCD: Error Wait For Status.\n");
  374. #endif
  375. return (sjcd_status_timeout);
  376. }
  377. static int sjcd_receive_status(void)
  378. {
  379. int i;
  380. #if defined( SJCD_TRACE )
  381. printk("SJCD: receive_status\n");
  382. #endif
  383. /*
  384. * Wait a bit for status available.
  385. */
  386. for (i = 200; i-- && (sjcd_check_status() == 0););
  387. if (i < 0) {
  388. #if defined( SJCD_TRACE )
  389. printk("SJCD: long wait for status\n");
  390. #endif
  391. if (sjcd_wait_for_status() <= 0)
  392. printk("SJCD: Timeout when read status.\n");
  393. else
  394. i = 0;
  395. }
  396. return (i);
  397. }
  398. /*
  399. * Load the status. Issue get status command and wait for status available.
  400. */
  401. static void sjcd_get_status(void)
  402. {
  403. #if defined( SJCD_TRACE )
  404. printk("SJCD: get_status\n");
  405. #endif
  406. sjcd_send_cmd(SCMD_GET_STATUS);
  407. sjcd_receive_status();
  408. }
  409. /*
  410. * Check the drive if the disk is changed. Should be revised.
  411. */
  412. static int sjcd_disk_change(struct gendisk *disk)
  413. {
  414. #if 0
  415. printk("SJCD: sjcd_disk_change(%s)\n", disk->disk_name);
  416. #endif
  417. if (!sjcd_command_is_in_progress)
  418. sjcd_get_status();
  419. return (sjcd_status_valid ? sjcd_media_is_changed : 0);
  420. }
  421. /*
  422. * Read the table of contents (TOC) and TOC header if necessary.
  423. * We assume that the drive contains no more than 99 toc entries.
  424. */
  425. static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
  426. static unsigned char sjcd_first_track_no, sjcd_last_track_no;
  427. #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
  428. static int sjcd_update_toc(void)
  429. {
  430. struct sjcd_hw_disk_info info;
  431. int i;
  432. #if defined( SJCD_TRACE )
  433. printk("SJCD: update toc:\n");
  434. #endif
  435. /*
  436. * check to see if we need to do anything
  437. */
  438. if (sjcd_toc_uptodate)
  439. return (0);
  440. /*
  441. * Get the TOC start information.
  442. */
  443. sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
  444. sjcd_receive_status();
  445. if (!sjcd_status_valid) {
  446. printk("SJCD: cannot load status.\n");
  447. return (-1);
  448. }
  449. if (!sjcd_media_is_available) {
  450. printk("SJCD: no disk in drive\n");
  451. return (-1);
  452. }
  453. if (!sjcd_command_failed) {
  454. if (sjcd_load_response(&info, sizeof(info)) != 0) {
  455. printk
  456. ("SJCD: cannot load response about TOC start.\n");
  457. return (-1);
  458. }
  459. sjcd_first_track_no = bcd2bin(info.un.track_no);
  460. } else {
  461. printk("SJCD: get first failed\n");
  462. return (-1);
  463. }
  464. #if defined( SJCD_TRACE )
  465. printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
  466. #endif
  467. /*
  468. * Get the TOC finish information.
  469. */
  470. sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
  471. sjcd_receive_status();
  472. if (!sjcd_status_valid) {
  473. printk("SJCD: cannot load status.\n");
  474. return (-1);
  475. }
  476. if (!sjcd_media_is_available) {
  477. printk("SJCD: no disk in drive\n");
  478. return (-1);
  479. }
  480. if (!sjcd_command_failed) {
  481. if (sjcd_load_response(&info, sizeof(info)) != 0) {
  482. printk
  483. ("SJCD: cannot load response about TOC finish.\n");
  484. return (-1);
  485. }
  486. sjcd_last_track_no = bcd2bin(info.un.track_no);
  487. } else {
  488. printk("SJCD: get last failed\n");
  489. return (-1);
  490. }
  491. #if defined( SJCD_TRACE )
  492. printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
  493. #endif
  494. for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
  495. /*
  496. * Get the first track information.
  497. */
  498. sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
  499. sjcd_receive_status();
  500. if (!sjcd_status_valid) {
  501. printk("SJCD: cannot load status.\n");
  502. return (-1);
  503. }
  504. if (!sjcd_media_is_available) {
  505. printk("SJCD: no disk in drive\n");
  506. return (-1);
  507. }
  508. if (!sjcd_command_failed) {
  509. if (sjcd_load_response(&sjcd_table_of_contents[i],
  510. sizeof(struct
  511. sjcd_hw_disk_info))
  512. != 0) {
  513. printk
  514. ("SJCD: cannot load info for %d track\n",
  515. i);
  516. return (-1);
  517. }
  518. } else {
  519. printk("SJCD: get info %d failed\n", i);
  520. return (-1);
  521. }
  522. }
  523. /*
  524. * Get the disk length info.
  525. */
  526. sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
  527. sjcd_receive_status();
  528. if (!sjcd_status_valid) {
  529. printk("SJCD: cannot load status.\n");
  530. return (-1);
  531. }
  532. if (!sjcd_media_is_available) {
  533. printk("SJCD: no disk in drive\n");
  534. return (-1);
  535. }
  536. if (!sjcd_command_failed) {
  537. if (sjcd_load_response(&info, sizeof(info)) != 0) {
  538. printk
  539. ("SJCD: cannot load response about disk size.\n");
  540. return (-1);
  541. }
  542. sjcd_disk_length.min = info.un.track_msf.min;
  543. sjcd_disk_length.sec = info.un.track_msf.sec;
  544. sjcd_disk_length.frame = info.un.track_msf.frame;
  545. } else {
  546. printk("SJCD: get size failed\n");
  547. return (1);
  548. }
  549. #if defined( SJCD_TRACE )
  550. printk("SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
  551. sjcd_disk_length.sec, sjcd_disk_length.frame);
  552. #endif
  553. return (0);
  554. }
  555. /*
  556. * Load subchannel information.
  557. */
  558. static int sjcd_get_q_info(struct sjcd_hw_qinfo *qp)
  559. {
  560. int s;
  561. #if defined( SJCD_TRACE )
  562. printk("SJCD: load sub q\n");
  563. #endif
  564. sjcd_send_cmd(SCMD_GET_QINFO);
  565. s = sjcd_receive_status();
  566. if (s < 0 || sjcd_command_failed || !sjcd_status_valid) {
  567. sjcd_send_cmd(0xF2);
  568. s = sjcd_receive_status();
  569. if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
  570. return (-1);
  571. sjcd_send_cmd(SCMD_GET_QINFO);
  572. s = sjcd_receive_status();
  573. if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
  574. return (-1);
  575. }
  576. if (sjcd_media_is_available)
  577. if (sjcd_load_response(qp, sizeof(*qp)) == 0)
  578. return (0);
  579. return (-1);
  580. }
  581. /*
  582. * Start playing from the specified position.
  583. */
  584. static int sjcd_play(struct sjcd_play_msf *mp)
  585. {
  586. struct sjcd_play_msf msf;
  587. /*
  588. * Turn the device to play mode.
  589. */
  590. sjcd_send_1_cmd(SCMD_SET_MODE, SCMD_MODE_PLAY);
  591. if (sjcd_receive_status() < 0)
  592. return (-1);
  593. /*
  594. * Seek to the starting point.
  595. */
  596. msf.start = mp->start;
  597. msf.end.min = msf.end.sec = msf.end.frame = 0x00;
  598. sjcd_send_6_cmd(SCMD_SEEK, &msf);
  599. if (sjcd_receive_status() < 0)
  600. return (-1);
  601. /*
  602. * Start playing.
  603. */
  604. sjcd_send_6_cmd(SCMD_PLAY, mp);
  605. return (sjcd_receive_status());
  606. }
  607. /*
  608. * Tray control functions.
  609. */
  610. static int sjcd_tray_close(void)
  611. {
  612. #if defined( SJCD_TRACE )
  613. printk("SJCD: tray_close\n");
  614. #endif
  615. sjcd_send_cmd(SCMD_CLOSE_TRAY);
  616. return (sjcd_receive_status());
  617. }
  618. static int sjcd_tray_lock(void)
  619. {
  620. #if defined( SJCD_TRACE )
  621. printk("SJCD: tray_lock\n");
  622. #endif
  623. sjcd_send_cmd(SCMD_LOCK_TRAY);
  624. return (sjcd_receive_status());
  625. }
  626. static int sjcd_tray_unlock(void)
  627. {
  628. #if defined( SJCD_TRACE )
  629. printk("SJCD: tray_unlock\n");
  630. #endif
  631. sjcd_send_cmd(SCMD_UNLOCK_TRAY);
  632. return (sjcd_receive_status());
  633. }
  634. static int sjcd_tray_open(void)
  635. {
  636. #if defined( SJCD_TRACE )
  637. printk("SJCD: tray_open\n");
  638. #endif
  639. sjcd_send_cmd(SCMD_EJECT_TRAY);
  640. return (sjcd_receive_status());
  641. }
  642. /*
  643. * Do some user commands.
  644. */
  645. static int sjcd_ioctl(struct inode *ip, struct file *fp,
  646. unsigned int cmd, unsigned long arg)
  647. {
  648. void __user *argp = (void __user *)arg;
  649. #if defined( SJCD_TRACE )
  650. printk("SJCD:ioctl\n");
  651. #endif
  652. sjcd_get_status();
  653. if (!sjcd_status_valid)
  654. return (-EIO);
  655. if (sjcd_update_toc() < 0)
  656. return (-EIO);
  657. switch (cmd) {
  658. case CDROMSTART:{
  659. #if defined( SJCD_TRACE )
  660. printk("SJCD: ioctl: start\n");
  661. #endif
  662. return (0);
  663. }
  664. case CDROMSTOP:{
  665. #if defined( SJCD_TRACE )
  666. printk("SJCD: ioctl: stop\n");
  667. #endif
  668. sjcd_send_cmd(SCMD_PAUSE);
  669. (void) sjcd_receive_status();
  670. sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
  671. return (0);
  672. }
  673. case CDROMPAUSE:{
  674. struct sjcd_hw_qinfo q_info;
  675. #if defined( SJCD_TRACE )
  676. printk("SJCD: ioctl: pause\n");
  677. #endif
  678. if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
  679. sjcd_send_cmd(SCMD_PAUSE);
  680. (void) sjcd_receive_status();
  681. if (sjcd_get_q_info(&q_info) < 0) {
  682. sjcd_audio_status =
  683. CDROM_AUDIO_NO_STATUS;
  684. } else {
  685. sjcd_audio_status =
  686. CDROM_AUDIO_PAUSED;
  687. sjcd_playing.start = q_info.abs;
  688. }
  689. return (0);
  690. } else
  691. return (-EINVAL);
  692. }
  693. case CDROMRESUME:{
  694. #if defined( SJCD_TRACE )
  695. printk("SJCD: ioctl: resume\n");
  696. #endif
  697. if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
  698. /*
  699. * continue play starting at saved location
  700. */
  701. if (sjcd_play(&sjcd_playing) < 0) {
  702. sjcd_audio_status =
  703. CDROM_AUDIO_ERROR;
  704. return (-EIO);
  705. } else {
  706. sjcd_audio_status =
  707. CDROM_AUDIO_PLAY;
  708. return (0);
  709. }
  710. } else
  711. return (-EINVAL);
  712. }
  713. case CDROMPLAYTRKIND:{
  714. struct cdrom_ti ti;
  715. int s = -EFAULT;
  716. #if defined( SJCD_TRACE )
  717. printk("SJCD: ioctl: playtrkind\n");
  718. #endif
  719. if (!copy_from_user(&ti, argp, sizeof(ti))) {
  720. s = 0;
  721. if (ti.cdti_trk0 < sjcd_first_track_no)
  722. return (-EINVAL);
  723. if (ti.cdti_trk1 > sjcd_last_track_no)
  724. ti.cdti_trk1 = sjcd_last_track_no;
  725. if (ti.cdti_trk0 > ti.cdti_trk1)
  726. return (-EINVAL);
  727. sjcd_playing.start =
  728. sjcd_table_of_contents[ti.cdti_trk0].
  729. un.track_msf;
  730. sjcd_playing.end =
  731. (ti.cdti_trk1 <
  732. sjcd_last_track_no) ?
  733. sjcd_table_of_contents[ti.cdti_trk1 +
  734. 1].un.
  735. track_msf : sjcd_table_of_contents[0].
  736. un.track_msf;
  737. if (sjcd_play(&sjcd_playing) < 0) {
  738. sjcd_audio_status =
  739. CDROM_AUDIO_ERROR;
  740. return (-EIO);
  741. } else
  742. sjcd_audio_status =
  743. CDROM_AUDIO_PLAY;
  744. }
  745. return (s);
  746. }
  747. case CDROMPLAYMSF:{
  748. struct cdrom_msf sjcd_msf;
  749. int s;
  750. #if defined( SJCD_TRACE )
  751. printk("SJCD: ioctl: playmsf\n");
  752. #endif
  753. if ((s =
  754. access_ok(VERIFY_READ, argp, sizeof(sjcd_msf))
  755. ? 0 : -EFAULT) == 0) {
  756. if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
  757. sjcd_send_cmd(SCMD_PAUSE);
  758. (void) sjcd_receive_status();
  759. sjcd_audio_status =
  760. CDROM_AUDIO_NO_STATUS;
  761. }
  762. if (copy_from_user(&sjcd_msf, argp,
  763. sizeof(sjcd_msf)))
  764. return (-EFAULT);
  765. sjcd_playing.start.min =
  766. bin2bcd(sjcd_msf.cdmsf_min0);
  767. sjcd_playing.start.sec =
  768. bin2bcd(sjcd_msf.cdmsf_sec0);
  769. sjcd_playing.start.frame =
  770. bin2bcd(sjcd_msf.cdmsf_frame0);
  771. sjcd_playing.end.min =
  772. bin2bcd(sjcd_msf.cdmsf_min1);
  773. sjcd_playing.end.sec =
  774. bin2bcd(sjcd_msf.cdmsf_sec1);
  775. sjcd_playing.end.frame =
  776. bin2bcd(sjcd_msf.cdmsf_frame1);
  777. if (sjcd_play(&sjcd_playing) < 0) {
  778. sjcd_audio_status =
  779. CDROM_AUDIO_ERROR;
  780. return (-EIO);
  781. } else
  782. sjcd_audio_status =
  783. CDROM_AUDIO_PLAY;
  784. }
  785. return (s);
  786. }
  787. case CDROMREADTOCHDR:{
  788. struct cdrom_tochdr toc_header;
  789. #if defined (SJCD_TRACE )
  790. printk("SJCD: ioctl: readtocheader\n");
  791. #endif
  792. toc_header.cdth_trk0 = sjcd_first_track_no;
  793. toc_header.cdth_trk1 = sjcd_last_track_no;
  794. if (copy_to_user(argp, &toc_header,
  795. sizeof(toc_header)))
  796. return -EFAULT;
  797. return 0;
  798. }
  799. case CDROMREADTOCENTRY:{
  800. struct cdrom_tocentry toc_entry;
  801. int s;
  802. #if defined( SJCD_TRACE )
  803. printk("SJCD: ioctl: readtocentry\n");
  804. #endif
  805. if ((s =
  806. access_ok(VERIFY_WRITE, argp, sizeof(toc_entry))
  807. ? 0 : -EFAULT) == 0) {
  808. struct sjcd_hw_disk_info *tp;
  809. if (copy_from_user(&toc_entry, argp,
  810. sizeof(toc_entry)))
  811. return (-EFAULT);
  812. if (toc_entry.cdte_track == CDROM_LEADOUT)
  813. tp = &sjcd_table_of_contents[0];
  814. else if (toc_entry.cdte_track <
  815. sjcd_first_track_no)
  816. return (-EINVAL);
  817. else if (toc_entry.cdte_track >
  818. sjcd_last_track_no)
  819. return (-EINVAL);
  820. else
  821. tp = &sjcd_table_of_contents
  822. [toc_entry.cdte_track];
  823. toc_entry.cdte_adr =
  824. tp->track_control & 0x0F;
  825. toc_entry.cdte_ctrl =
  826. tp->track_control >> 4;
  827. switch (toc_entry.cdte_format) {
  828. case CDROM_LBA:
  829. toc_entry.cdte_addr.lba =
  830. msf2hsg(&(tp->un.track_msf));
  831. break;
  832. case CDROM_MSF:
  833. toc_entry.cdte_addr.msf.minute =
  834. bcd2bin(tp->un.track_msf.min);
  835. toc_entry.cdte_addr.msf.second =
  836. bcd2bin(tp->un.track_msf.sec);
  837. toc_entry.cdte_addr.msf.frame =
  838. bcd2bin(tp->un.track_msf.
  839. frame);
  840. break;
  841. default:
  842. return (-EINVAL);
  843. }
  844. if (copy_to_user(argp, &toc_entry,
  845. sizeof(toc_entry)))
  846. s = -EFAULT;
  847. }
  848. return (s);
  849. }
  850. case CDROMSUBCHNL:{
  851. struct cdrom_subchnl subchnl;
  852. int s;
  853. #if defined( SJCD_TRACE )
  854. printk("SJCD: ioctl: subchnl\n");
  855. #endif
  856. if ((s =
  857. access_ok(VERIFY_WRITE, argp, sizeof(subchnl))
  858. ? 0 : -EFAULT) == 0) {
  859. struct sjcd_hw_qinfo q_info;
  860. if (copy_from_user(&subchnl, argp,
  861. sizeof(subchnl)))
  862. return (-EFAULT);
  863. if (sjcd_get_q_info(&q_info) < 0)
  864. return (-EIO);
  865. subchnl.cdsc_audiostatus =
  866. sjcd_audio_status;
  867. subchnl.cdsc_adr =
  868. q_info.track_control & 0x0F;
  869. subchnl.cdsc_ctrl =
  870. q_info.track_control >> 4;
  871. subchnl.cdsc_trk =
  872. bcd2bin(q_info.track_no);
  873. subchnl.cdsc_ind = bcd2bin(q_info.x);
  874. switch (subchnl.cdsc_format) {
  875. case CDROM_LBA:
  876. subchnl.cdsc_absaddr.lba =
  877. msf2hsg(&(q_info.abs));
  878. subchnl.cdsc_reladdr.lba =
  879. msf2hsg(&(q_info.rel));
  880. break;
  881. case CDROM_MSF:
  882. subchnl.cdsc_absaddr.msf.minute =
  883. bcd2bin(q_info.abs.min);
  884. subchnl.cdsc_absaddr.msf.second =
  885. bcd2bin(q_info.abs.sec);
  886. subchnl.cdsc_absaddr.msf.frame =
  887. bcd2bin(q_info.abs.frame);
  888. subchnl.cdsc_reladdr.msf.minute =
  889. bcd2bin(q_info.rel.min);
  890. subchnl.cdsc_reladdr.msf.second =
  891. bcd2bin(q_info.rel.sec);
  892. subchnl.cdsc_reladdr.msf.frame =
  893. bcd2bin(q_info.rel.frame);
  894. break;
  895. default:
  896. return (-EINVAL);
  897. }
  898. if (copy_to_user(argp, &subchnl,
  899. sizeof(subchnl)))
  900. s = -EFAULT;
  901. }
  902. return (s);
  903. }
  904. case CDROMVOLCTRL:{
  905. struct cdrom_volctrl vol_ctrl;
  906. int s;
  907. #if defined( SJCD_TRACE )
  908. printk("SJCD: ioctl: volctrl\n");
  909. #endif
  910. if ((s =
  911. access_ok(VERIFY_READ, argp, sizeof(vol_ctrl))
  912. ? 0 : -EFAULT) == 0) {
  913. unsigned char dummy[4];
  914. if (copy_from_user(&vol_ctrl, argp,
  915. sizeof(vol_ctrl)))
  916. return (-EFAULT);
  917. sjcd_send_4_cmd(SCMD_SET_VOLUME,
  918. vol_ctrl.channel0, 0xFF,
  919. vol_ctrl.channel1, 0xFF);
  920. if (sjcd_receive_status() < 0)
  921. return (-EIO);
  922. (void) sjcd_load_response(dummy, 4);
  923. }
  924. return (s);
  925. }
  926. case CDROMEJECT:{
  927. #if defined( SJCD_TRACE )
  928. printk("SJCD: ioctl: eject\n");
  929. #endif
  930. if (!sjcd_command_is_in_progress) {
  931. sjcd_tray_unlock();
  932. sjcd_send_cmd(SCMD_EJECT_TRAY);
  933. (void) sjcd_receive_status();
  934. }
  935. return (0);
  936. }
  937. #if defined( SJCD_GATHER_STAT )
  938. case 0xABCD:{
  939. #if defined( SJCD_TRACE )
  940. printk("SJCD: ioctl: statistic\n");
  941. #endif
  942. if (copy_to_user(argp, &statistic, sizeof(statistic)))
  943. return -EFAULT;
  944. return 0;
  945. }
  946. #endif
  947. default:
  948. return (-EINVAL);
  949. }
  950. }
  951. /*
  952. * Invalidate internal buffers of the driver.
  953. */
  954. static void sjcd_invalidate_buffers(void)
  955. {
  956. int i;
  957. for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
  958. sjcd_buf_out = -1;
  959. }
  960. /*
  961. * Take care of the different block sizes between cdrom and Linux.
  962. * When Linux gets variable block sizes this will probably go away.
  963. */
  964. static int current_valid(void)
  965. {
  966. return CURRENT &&
  967. CURRENT->cmd == READ &&
  968. CURRENT->sector != -1;
  969. }
  970. static void sjcd_transfer(void)
  971. {
  972. #if defined( SJCD_TRACE )
  973. printk("SJCD: transfer:\n");
  974. #endif
  975. if (current_valid()) {
  976. while (CURRENT->nr_sectors) {
  977. int i, bn = CURRENT->sector / 4;
  978. for (i = 0;
  979. i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
  980. i++);
  981. if (i < SJCD_BUF_SIZ) {
  982. int offs =
  983. (i * 4 + (CURRENT->sector & 3)) * 512;
  984. int nr_sectors = 4 - (CURRENT->sector & 3);
  985. if (sjcd_buf_out != i) {
  986. sjcd_buf_out = i;
  987. if (sjcd_buf_bn[i] != bn) {
  988. sjcd_buf_out = -1;
  989. continue;
  990. }
  991. }
  992. if (nr_sectors > CURRENT->nr_sectors)
  993. nr_sectors = CURRENT->nr_sectors;
  994. #if defined( SJCD_TRACE )
  995. printk("SJCD: copy out\n");
  996. #endif
  997. memcpy(CURRENT->buffer, sjcd_buf + offs,
  998. nr_sectors * 512);
  999. CURRENT->nr_sectors -= nr_sectors;
  1000. CURRENT->sector += nr_sectors;
  1001. CURRENT->buffer += nr_sectors * 512;
  1002. } else {
  1003. sjcd_buf_out = -1;
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. #if defined( SJCD_TRACE )
  1009. printk("SJCD: transfer: done\n");
  1010. #endif
  1011. }
  1012. static void sjcd_poll(void)
  1013. {
  1014. #if defined( SJCD_GATHER_STAT )
  1015. /*
  1016. * Update total number of ticks.
  1017. */
  1018. statistic.ticks++;
  1019. statistic.tticks[sjcd_transfer_state]++;
  1020. #endif
  1021. ReSwitch:switch (sjcd_transfer_state) {
  1022. case SJCD_S_IDLE:{
  1023. #if defined( SJCD_GATHER_STAT )
  1024. statistic.idle_ticks++;
  1025. #endif
  1026. #if defined( SJCD_TRACE )
  1027. printk("SJCD_S_IDLE\n");
  1028. #endif
  1029. return;
  1030. }
  1031. case SJCD_S_START:{
  1032. #if defined( SJCD_GATHER_STAT )
  1033. statistic.start_ticks++;
  1034. #endif
  1035. sjcd_send_cmd(SCMD_GET_STATUS);
  1036. sjcd_transfer_state =
  1037. sjcd_mode ==
  1038. SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
  1039. sjcd_transfer_timeout = 500;
  1040. #if defined( SJCD_TRACE )
  1041. printk("SJCD_S_START: goto SJCD_S_%s mode\n",
  1042. sjcd_transfer_state ==
  1043. SJCD_S_READ ? "READ" : "MODE");
  1044. #endif
  1045. break;
  1046. }
  1047. case SJCD_S_MODE:{
  1048. if (sjcd_check_status()) {
  1049. /*
  1050. * Previous command is completed.
  1051. */
  1052. if (!sjcd_status_valid
  1053. || sjcd_command_failed) {
  1054. #if defined( SJCD_TRACE )
  1055. printk
  1056. ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
  1057. #endif
  1058. sjcd_transfer_state = SJCD_S_STOP;
  1059. goto ReSwitch;
  1060. }
  1061. sjcd_mode = 0; /* unknown mode; should not be valid when failed */
  1062. sjcd_send_1_cmd(SCMD_SET_MODE,
  1063. SCMD_MODE_COOKED);
  1064. sjcd_transfer_state = SJCD_S_READ;
  1065. sjcd_transfer_timeout = 1000;
  1066. #if defined( SJCD_TRACE )
  1067. printk
  1068. ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
  1069. #endif
  1070. }
  1071. #if defined( SJCD_GATHER_STAT )
  1072. else
  1073. statistic.mode_ticks++;
  1074. #endif
  1075. break;
  1076. }
  1077. case SJCD_S_READ:{
  1078. if (sjcd_status_valid ? 1 : sjcd_check_status()) {
  1079. /*
  1080. * Previous command is completed.
  1081. */
  1082. if (!sjcd_status_valid
  1083. || sjcd_command_failed) {
  1084. #if defined( SJCD_TRACE )
  1085. printk
  1086. ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
  1087. #endif
  1088. sjcd_transfer_state = SJCD_S_STOP;
  1089. goto ReSwitch;
  1090. }
  1091. if (!sjcd_media_is_available) {
  1092. #if defined( SJCD_TRACE )
  1093. printk
  1094. ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
  1095. #endif
  1096. sjcd_transfer_state = SJCD_S_STOP;
  1097. goto ReSwitch;
  1098. }
  1099. if (sjcd_mode != SCMD_MODE_COOKED) {
  1100. /*
  1101. * We seem to come from set mode. So discard one byte of result.
  1102. */
  1103. if (sjcd_load_response
  1104. (&sjcd_mode, 1) != 0) {
  1105. #if defined( SJCD_TRACE )
  1106. printk
  1107. ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
  1108. #endif
  1109. sjcd_transfer_state =
  1110. SJCD_S_STOP;
  1111. goto ReSwitch;
  1112. }
  1113. if (sjcd_mode != SCMD_MODE_COOKED) {
  1114. #if defined( SJCD_TRACE )
  1115. printk
  1116. ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
  1117. #endif
  1118. sjcd_transfer_state =
  1119. SJCD_S_STOP;
  1120. goto ReSwitch;
  1121. }
  1122. }
  1123. if (current_valid()) {
  1124. struct sjcd_play_msf msf;
  1125. sjcd_next_bn = CURRENT->sector / 4;
  1126. hsg2msf(sjcd_next_bn, &msf.start);
  1127. msf.end.min = 0;
  1128. msf.end.sec = 0;
  1129. msf.end.frame = sjcd_read_count =
  1130. SJCD_BUF_SIZ;
  1131. #if defined( SJCD_TRACE )
  1132. printk
  1133. ("SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
  1134. msf.start.min, msf.start.sec,
  1135. msf.start.frame, msf.end.min,
  1136. msf.end.sec, msf.end.frame);
  1137. printk
  1138. ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
  1139. sjcd_next_bn, sjcd_buf_in,
  1140. sjcd_buf_out,
  1141. sjcd_buf_bn[sjcd_buf_in]);
  1142. #endif
  1143. sjcd_send_6_cmd(SCMD_DATA_READ,
  1144. &msf);
  1145. sjcd_transfer_state = SJCD_S_DATA;
  1146. sjcd_transfer_timeout = 500;
  1147. #if defined( SJCD_TRACE )
  1148. printk
  1149. ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
  1150. #endif
  1151. } else {
  1152. #if defined( SJCD_TRACE )
  1153. printk
  1154. ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
  1155. #endif
  1156. sjcd_transfer_state = SJCD_S_STOP;
  1157. goto ReSwitch;
  1158. }
  1159. }
  1160. #if defined( SJCD_GATHER_STAT )
  1161. else
  1162. statistic.read_ticks++;
  1163. #endif
  1164. break;
  1165. }
  1166. case SJCD_S_DATA:{
  1167. unsigned char stat;
  1168. sjcd_s_data:stat =
  1169. inb(SJCDPORT
  1170. (1));
  1171. #if defined( SJCD_TRACE )
  1172. printk("SJCD_S_DATA: status = 0x%02x\n", stat);
  1173. #endif
  1174. if (SJCD_STATUS_AVAILABLE(stat)) {
  1175. /*
  1176. * No data is waiting for us in the drive buffer. Status of operation
  1177. * completion is available. Read and parse it.
  1178. */
  1179. sjcd_load_status();
  1180. if (!sjcd_status_valid
  1181. || sjcd_command_failed) {
  1182. #if defined( SJCD_TRACE )
  1183. printk
  1184. ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
  1185. sjcd_next_bn);
  1186. #endif
  1187. if (current_valid())
  1188. end_request(CURRENT, 0);
  1189. #if defined( SJCD_TRACE )
  1190. printk
  1191. ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
  1192. #endif
  1193. sjcd_transfer_state = SJCD_S_STOP;
  1194. goto ReSwitch;
  1195. }
  1196. if (!sjcd_media_is_available) {
  1197. printk
  1198. ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
  1199. sjcd_transfer_state = SJCD_S_STOP;
  1200. goto ReSwitch;
  1201. }
  1202. sjcd_transfer_state = SJCD_S_READ;
  1203. goto ReSwitch;
  1204. } else if (SJCD_DATA_AVAILABLE(stat)) {
  1205. /*
  1206. * One frame is read into device buffer. We must copy it to our memory.
  1207. * Otherwise cdrom hangs up. Check to see if we have something to copy
  1208. * to.
  1209. */
  1210. if (!current_valid()
  1211. && sjcd_buf_in == sjcd_buf_out) {
  1212. #if defined( SJCD_TRACE )
  1213. printk
  1214. ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
  1215. printk
  1216. (" ... all the date would be discarded\n");
  1217. #endif
  1218. sjcd_transfer_state = SJCD_S_STOP;
  1219. goto ReSwitch;
  1220. }
  1221. /*
  1222. * Everything seems to be OK. Just read the frame and recalculate
  1223. * indices.
  1224. */
  1225. sjcd_buf_bn[sjcd_buf_in] = -1; /* ??? */
  1226. insb(SJCDPORT(2),
  1227. sjcd_buf + 2048 * sjcd_buf_in, 2048);
  1228. #if defined( SJCD_TRACE )
  1229. printk
  1230. ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
  1231. sjcd_next_bn, sjcd_buf_in,
  1232. sjcd_buf_out,
  1233. sjcd_buf_bn[sjcd_buf_in]);
  1234. #endif
  1235. sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
  1236. if (sjcd_buf_out == -1)
  1237. sjcd_buf_out = sjcd_buf_in;
  1238. if (++sjcd_buf_in == SJCD_BUF_SIZ)
  1239. sjcd_buf_in = 0;
  1240. /*
  1241. * Only one frame is ready at time. So we should turn over to wait for
  1242. * another frame. If we need that, of course.
  1243. */
  1244. if (--sjcd_read_count == 0) {
  1245. /*
  1246. * OK, request seems to be precessed. Continue transferring...
  1247. */
  1248. if (!sjcd_transfer_is_active) {
  1249. while (current_valid()) {
  1250. /*
  1251. * Continue transferring.
  1252. */
  1253. sjcd_transfer();
  1254. if (CURRENT->
  1255. nr_sectors ==
  1256. 0)
  1257. end_request
  1258. (CURRENT, 1);
  1259. else
  1260. break;
  1261. }
  1262. }
  1263. if (current_valid() &&
  1264. (CURRENT->sector / 4 <
  1265. sjcd_next_bn
  1266. || CURRENT->sector / 4 >
  1267. sjcd_next_bn +
  1268. SJCD_BUF_SIZ)) {
  1269. #if defined( SJCD_TRACE )
  1270. printk
  1271. ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
  1272. #endif
  1273. sjcd_transfer_state =
  1274. SJCD_S_STOP;
  1275. goto ReSwitch;
  1276. }
  1277. }
  1278. /*
  1279. * Now we should turn around rather than wait for while.
  1280. */
  1281. goto sjcd_s_data;
  1282. }
  1283. #if defined( SJCD_GATHER_STAT )
  1284. else
  1285. statistic.data_ticks++;
  1286. #endif
  1287. break;
  1288. }
  1289. case SJCD_S_STOP:{
  1290. sjcd_read_count = 0;
  1291. sjcd_send_cmd(SCMD_STOP);
  1292. sjcd_transfer_state = SJCD_S_STOPPING;
  1293. sjcd_transfer_timeout = 500;
  1294. #if defined( SJCD_GATHER_STAT )
  1295. statistic.stop_ticks++;
  1296. #endif
  1297. break;
  1298. }
  1299. case SJCD_S_STOPPING:{
  1300. unsigned char stat;
  1301. stat = inb(SJCDPORT(1));
  1302. #if defined( SJCD_TRACE )
  1303. printk("SJCD_S_STOP: status = 0x%02x\n", stat);
  1304. #endif
  1305. if (SJCD_DATA_AVAILABLE(stat)) {
  1306. int i;
  1307. #if defined( SJCD_TRACE )
  1308. printk("SJCD_S_STOP: discard data\n");
  1309. #endif
  1310. /*
  1311. * Discard all the data from the pipe. Foolish method.
  1312. */
  1313. for (i = 2048; i--;
  1314. (void) inb(SJCDPORT(2)));
  1315. sjcd_transfer_timeout = 500;
  1316. } else if (SJCD_STATUS_AVAILABLE(stat)) {
  1317. sjcd_load_status();
  1318. if (sjcd_status_valid
  1319. && sjcd_media_is_changed) {
  1320. sjcd_toc_uptodate = 0;
  1321. sjcd_invalidate_buffers();
  1322. }
  1323. if (current_valid()) {
  1324. if (sjcd_status_valid)
  1325. sjcd_transfer_state =
  1326. SJCD_S_READ;
  1327. else
  1328. sjcd_transfer_state =
  1329. SJCD_S_START;
  1330. } else
  1331. sjcd_transfer_state = SJCD_S_IDLE;
  1332. goto ReSwitch;
  1333. }
  1334. #if defined( SJCD_GATHER_STAT )
  1335. else
  1336. statistic.stopping_ticks++;
  1337. #endif
  1338. break;
  1339. }
  1340. default:
  1341. printk("SJCD: poll: invalid state %d\n",
  1342. sjcd_transfer_state);
  1343. return;
  1344. }
  1345. if (--sjcd_transfer_timeout == 0) {
  1346. printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
  1347. while (current_valid())
  1348. end_request(CURRENT, 0);
  1349. sjcd_send_cmd(SCMD_STOP);
  1350. sjcd_transfer_state = SJCD_S_IDLE;
  1351. goto ReSwitch;
  1352. }
  1353. /*
  1354. * Get back in some time. 1 should be replaced with count variable to
  1355. * avoid unnecessary testings.
  1356. */
  1357. SJCD_SET_TIMER(sjcd_poll, 1);
  1358. }
  1359. static void do_sjcd_request(request_queue_t * q)
  1360. {
  1361. #if defined( SJCD_TRACE )
  1362. printk("SJCD: do_sjcd_request(%ld+%ld)\n",
  1363. CURRENT->sector, CURRENT->nr_sectors);
  1364. #endif
  1365. sjcd_transfer_is_active = 1;
  1366. while (current_valid()) {
  1367. sjcd_transfer();
  1368. if (CURRENT->nr_sectors == 0)
  1369. end_request(CURRENT, 1);
  1370. else {
  1371. sjcd_buf_out = -1; /* Want to read a block not in buffer */
  1372. if (sjcd_transfer_state == SJCD_S_IDLE) {
  1373. if (!sjcd_toc_uptodate) {
  1374. if (sjcd_update_toc() < 0) {
  1375. printk
  1376. ("SJCD: transfer: discard\n");
  1377. while (current_valid())
  1378. end_request(CURRENT, 0);
  1379. break;
  1380. }
  1381. }
  1382. sjcd_transfer_state = SJCD_S_START;
  1383. SJCD_SET_TIMER(sjcd_poll, HZ / 100);
  1384. }
  1385. break;
  1386. }
  1387. }
  1388. sjcd_transfer_is_active = 0;
  1389. #if defined( SJCD_TRACE )
  1390. printk
  1391. ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
  1392. sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
  1393. sjcd_buf_bn[sjcd_buf_in]);
  1394. printk("do_sjcd_request ends\n");
  1395. #endif
  1396. }
  1397. /*
  1398. * Open the device special file. Check disk is in.
  1399. */
  1400. static int sjcd_open(struct inode *ip, struct file *fp)
  1401. {
  1402. /*
  1403. * Check the presence of device.
  1404. */
  1405. if (!sjcd_present)
  1406. return (-ENXIO);
  1407. /*
  1408. * Only read operations are allowed. Really? (:-)
  1409. */
  1410. if (fp->f_mode & 2)
  1411. return (-EROFS);
  1412. if (sjcd_open_count == 0) {
  1413. int s, sjcd_open_tries;
  1414. /* We don't know that, do we? */
  1415. /*
  1416. sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
  1417. */
  1418. sjcd_mode = 0;
  1419. sjcd_door_was_open = 0;
  1420. sjcd_transfer_state = SJCD_S_IDLE;
  1421. sjcd_invalidate_buffers();
  1422. sjcd_status_valid = 0;
  1423. /*
  1424. * Strict status checking.
  1425. */
  1426. for (sjcd_open_tries = 4; --sjcd_open_tries;) {
  1427. if (!sjcd_status_valid)
  1428. sjcd_get_status();
  1429. if (!sjcd_status_valid) {
  1430. #if defined( SJCD_DIAGNOSTIC )
  1431. printk
  1432. ("SJCD: open: timed out when check status.\n");
  1433. #endif
  1434. goto err_out;
  1435. } else if (!sjcd_media_is_available) {
  1436. #if defined( SJCD_DIAGNOSTIC )
  1437. printk("SJCD: open: no disk in drive\n");
  1438. #endif
  1439. if (!sjcd_door_closed) {
  1440. sjcd_door_was_open = 1;
  1441. #if defined( SJCD_TRACE )
  1442. printk
  1443. ("SJCD: open: close the tray\n");
  1444. #endif
  1445. s = sjcd_tray_close();
  1446. if (s < 0 || !sjcd_status_valid
  1447. || sjcd_command_failed) {
  1448. #if defined( SJCD_DIAGNOSTIC )
  1449. printk
  1450. ("SJCD: open: tray close attempt failed\n");
  1451. #endif
  1452. goto err_out;
  1453. }
  1454. continue;
  1455. } else
  1456. goto err_out;
  1457. }
  1458. break;
  1459. }
  1460. s = sjcd_tray_lock();
  1461. if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
  1462. #if defined( SJCD_DIAGNOSTIC )
  1463. printk("SJCD: open: tray lock attempt failed\n");
  1464. #endif
  1465. goto err_out;
  1466. }
  1467. #if defined( SJCD_TRACE )
  1468. printk("SJCD: open: done\n");
  1469. #endif
  1470. }
  1471. ++sjcd_open_count;
  1472. return (0);
  1473. err_out:
  1474. return (-EIO);
  1475. }
  1476. /*
  1477. * On close, we flush all sjcd blocks from the buffer cache.
  1478. */
  1479. static int sjcd_release(struct inode *inode, struct file *file)
  1480. {
  1481. int s;
  1482. #if defined( SJCD_TRACE )
  1483. printk("SJCD: release\n");
  1484. #endif
  1485. if (--sjcd_open_count == 0) {
  1486. sjcd_invalidate_buffers();
  1487. s = sjcd_tray_unlock();
  1488. if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
  1489. #if defined( SJCD_DIAGNOSTIC )
  1490. printk
  1491. ("SJCD: release: tray unlock attempt failed.\n");
  1492. #endif
  1493. }
  1494. if (sjcd_door_was_open) {
  1495. s = sjcd_tray_open();
  1496. if (s < 0 || !sjcd_status_valid
  1497. || sjcd_command_failed) {
  1498. #if defined( SJCD_DIAGNOSTIC )
  1499. printk
  1500. ("SJCD: release: tray unload attempt failed.\n");
  1501. #endif
  1502. }
  1503. }
  1504. }
  1505. return 0;
  1506. }
  1507. /*
  1508. * A list of file operations allowed for this cdrom.
  1509. */
  1510. static struct block_device_operations sjcd_fops = {
  1511. .owner = THIS_MODULE,
  1512. .open = sjcd_open,
  1513. .release = sjcd_release,
  1514. .ioctl = sjcd_ioctl,
  1515. .media_changed = sjcd_disk_change,
  1516. };
  1517. /*
  1518. * Following stuff is intended for initialization of the cdrom. It
  1519. * first looks for presence of device. If the device is present, it
  1520. * will be reset. Then read the version of the drive and load status.
  1521. * The version is two BCD-coded bytes.
  1522. */
  1523. static struct {
  1524. unsigned char major, minor;
  1525. } sjcd_version;
  1526. static struct gendisk *sjcd_disk;
  1527. /*
  1528. * Test for presence of drive and initialize it. Called at boot time.
  1529. * Probe cdrom, find out version and status.
  1530. */
  1531. static int __init sjcd_init(void)
  1532. {
  1533. int i;
  1534. printk(KERN_INFO
  1535. "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
  1536. SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
  1537. #if defined( SJCD_TRACE )
  1538. printk("SJCD: sjcd=0x%x: ", sjcd_base);
  1539. #endif
  1540. if (register_blkdev(MAJOR_NR, "sjcd"))
  1541. return -EIO;
  1542. sjcd_queue = blk_init_queue(do_sjcd_request, &sjcd_lock);
  1543. if (!sjcd_queue)
  1544. goto out0;
  1545. blk_queue_hardsect_size(sjcd_queue, 2048);
  1546. sjcd_disk = alloc_disk(1);
  1547. if (!sjcd_disk) {
  1548. printk(KERN_ERR "SJCD: can't allocate disk");
  1549. goto out1;
  1550. }
  1551. sjcd_disk->major = MAJOR_NR,
  1552. sjcd_disk->first_minor = 0,
  1553. sjcd_disk->fops = &sjcd_fops,
  1554. sprintf(sjcd_disk->disk_name, "sjcd");
  1555. sprintf(sjcd_disk->devfs_name, "sjcd");
  1556. if (!request_region(sjcd_base, 4,"sjcd")) {
  1557. printk
  1558. ("SJCD: Init failed, I/O port (%X) is already in use\n",
  1559. sjcd_base);
  1560. goto out2;
  1561. }
  1562. /*
  1563. * Check for card. Since we are booting now, we can't use standard
  1564. * wait algorithm.
  1565. */
  1566. printk(KERN_INFO "SJCD: Resetting: ");
  1567. sjcd_send_cmd(SCMD_RESET);
  1568. for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
  1569. unsigned long timer;
  1570. /*
  1571. * Wait 10ms approx.
  1572. */
  1573. for (timer = jiffies; time_before_eq(jiffies, timer););
  1574. if ((i % 100) == 0)
  1575. printk(".");
  1576. (void) sjcd_check_status();
  1577. }
  1578. if (i == 0 || sjcd_command_failed) {
  1579. printk(" reset failed, no drive found.\n");
  1580. goto out3;
  1581. } else
  1582. printk("\n");
  1583. /*
  1584. * Get and print out cdrom version.
  1585. */
  1586. printk(KERN_INFO "SJCD: Getting version: ");
  1587. sjcd_send_cmd(SCMD_GET_VERSION);
  1588. for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
  1589. unsigned long timer;
  1590. /*
  1591. * Wait 10ms approx.
  1592. */
  1593. for (timer = jiffies; time_before_eq(jiffies, timer););
  1594. if ((i % 100) == 0)
  1595. printk(".");
  1596. (void) sjcd_check_status();
  1597. }
  1598. if (i == 0 || sjcd_command_failed) {
  1599. printk(" get version failed, no drive found.\n");
  1600. goto out3;
  1601. }
  1602. if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
  1603. printk(" %1x.%02x\n", (int) sjcd_version.major,
  1604. (int) sjcd_version.minor);
  1605. } else {
  1606. printk(" read version failed, no drive found.\n");
  1607. goto out3;
  1608. }
  1609. /*
  1610. * Check and print out the tray state. (if it is needed?).
  1611. */
  1612. if (!sjcd_status_valid) {
  1613. printk(KERN_INFO "SJCD: Getting status: ");
  1614. sjcd_send_cmd(SCMD_GET_STATUS);
  1615. for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
  1616. unsigned long timer;
  1617. /*
  1618. * Wait 10ms approx.
  1619. */
  1620. for (timer = jiffies;
  1621. time_before_eq(jiffies, timer););
  1622. if ((i % 100) == 0)
  1623. printk(".");
  1624. (void) sjcd_check_status();
  1625. }
  1626. if (i == 0 || sjcd_command_failed) {
  1627. printk(" get status failed, no drive found.\n");
  1628. goto out3;
  1629. } else
  1630. printk("\n");
  1631. }
  1632. printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
  1633. sjcd_disk->queue = sjcd_queue;
  1634. add_disk(sjcd_disk);
  1635. sjcd_present++;
  1636. return (0);
  1637. out3:
  1638. release_region(sjcd_base, 4);
  1639. out2:
  1640. put_disk(sjcd_disk);
  1641. out1:
  1642. blk_cleanup_queue(sjcd_queue);
  1643. out0:
  1644. if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
  1645. printk("SJCD: cannot unregister device.\n");
  1646. return (-EIO);
  1647. }
  1648. static void __exit sjcd_exit(void)
  1649. {
  1650. del_gendisk(sjcd_disk);
  1651. put_disk(sjcd_disk);
  1652. release_region(sjcd_base, 4);
  1653. blk_cleanup_queue(sjcd_queue);
  1654. if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
  1655. printk("SJCD: cannot unregister device.\n");
  1656. printk(KERN_INFO "SJCD: module: removed.\n");
  1657. }
  1658. module_init(sjcd_init);
  1659. module_exit(sjcd_exit);
  1660. MODULE_LICENSE("GPL");
  1661. MODULE_ALIAS_BLOCKDEV_MAJOR(SANYO_CDROM_MAJOR);