sjcd.c 42 KB

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