sonycd535.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /*
  2. * Sony CDU-535 interface device driver
  3. *
  4. * This is a modified version of the CDU-31A device driver (see below).
  5. * Changes were made using documentation for the CDU-531 (which Sony
  6. * assures me is very similar to the 535) and partial disassembly of the
  7. * DOS driver. I used Minyard's driver and replaced the CDU-31A
  8. * commands with the CDU-531 commands. This was complicated by a different
  9. * interface protocol with the drive. The driver is still polled.
  10. *
  11. * Data transfer rate is about 110 Kb/sec, theoretical maximum is 150 Kb/sec.
  12. * I tried polling without the sony_sleep during the data transfers but
  13. * it did not speed things up any.
  14. *
  15. * 1993-05-23 (rgj) changed the major number to 21 to get rid of conflict
  16. * with CDU-31A driver. This is the also the number from the Linux
  17. * Device Driver Registry for the Sony Drive. Hope nobody else is using it.
  18. *
  19. * 1993-08-29 (rgj) remove the configuring of the interface board address
  20. * from the top level configuration, you have to modify it in this file.
  21. *
  22. * 1995-01-26 Made module-capable (Joel Katz <Stimpson@Panix.COM>)
  23. *
  24. * 1995-05-20
  25. * Modified to support CDU-510/515 series
  26. * (Claudio Porfiri<C.Porfiri@nisms.tei.ericsson.se>)
  27. * Fixed to report verify_area() failures
  28. * (Heiko Eissfeldt <heiko@colossus.escape.de>)
  29. *
  30. * 1995-06-01
  31. * More changes to support CDU-510/515 series
  32. * (Claudio Porfiri<C.Porfiri@nisms.tei.ericsson.se>)
  33. *
  34. * November 1999 -- Make kernel-parameter implementation work with 2.3.x
  35. * Removed init_module & cleanup_module in favor of
  36. * module_init & module_exit.
  37. * Torben Mathiasen <tmm@image.dk>
  38. *
  39. * September 2003 - Fix SMP support by removing cli/sti calls.
  40. * Using spinlocks with a wait_queue instead.
  41. * Felipe Damasio <felipewd@terra.com.br>
  42. *
  43. * Things to do:
  44. * - handle errors and status better, put everything into a single word
  45. * - use interrupts (code mostly there, but a big hole still missing)
  46. * - handle multi-session CDs?
  47. * - use DMA?
  48. *
  49. * Known Bugs:
  50. * -
  51. *
  52. * Ken Pizzini (ken@halcyon.com)
  53. *
  54. * Original by:
  55. * Ron Jeppesen (ronj.an@site007.saic.com)
  56. *
  57. *
  58. *------------------------------------------------------------------------
  59. * Sony CDROM interface device driver.
  60. *
  61. * Corey Minyard (minyard@wf-rch.cirr.com) (CDU-535 complaints to Ken above)
  62. *
  63. * Colossians 3:17
  64. *
  65. * The Sony interface device driver handles Sony interface CDROM
  66. * drives and provides a complete block-level interface as well as an
  67. * ioctl() interface compatible with the Sun (as specified in
  68. * include/linux/cdrom.h). With this interface, CDROMs can be
  69. * accessed and standard audio CDs can be played back normally.
  70. *
  71. * This interface is (unfortunately) a polled interface. This is
  72. * because most Sony interfaces are set up with DMA and interrupts
  73. * disables. Some (like mine) do not even have the capability to
  74. * handle interrupts or DMA. For this reason you will see a bit of
  75. * the following:
  76. *
  77. * snap = jiffies;
  78. * while (jiffies-snap < SONY_JIFFIES_TIMEOUT)
  79. * {
  80. * if (some_condition())
  81. * break;
  82. * sony_sleep();
  83. * }
  84. * if (some_condition not met)
  85. * {
  86. * return an_error;
  87. * }
  88. *
  89. * This ugly hack waits for something to happen, sleeping a little
  90. * between every try. (The conditional is written so that jiffies
  91. * wrap-around is handled properly.)
  92. *
  93. * One thing about these drives: They talk in MSF (Minute Second Frame) format.
  94. * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
  95. * disk. The funny thing is that these are sent to the drive in BCD, but the
  96. * interface wants to see them in decimal. A lot of conversion goes on.
  97. *
  98. * Copyright (C) 1993 Corey Minyard
  99. *
  100. * This program is free software; you can redistribute it and/or modify
  101. * it under the terms of the GNU General Public License as published by
  102. * the Free Software Foundation; either version 2 of the License, or
  103. * (at your option) any later version.
  104. *
  105. * This program is distributed in the hope that it will be useful,
  106. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  107. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  108. * GNU General Public License for more details.
  109. *
  110. * You should have received a copy of the GNU General Public License
  111. * along with this program; if not, write to the Free Software
  112. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  113. *
  114. */
  115. # include <linux/module.h>
  116. #include <linux/errno.h>
  117. #include <linux/signal.h>
  118. #include <linux/sched.h>
  119. #include <linux/timer.h>
  120. #include <linux/fs.h>
  121. #include <linux/kernel.h>
  122. #include <linux/interrupt.h>
  123. #include <linux/ioport.h>
  124. #include <linux/hdreg.h>
  125. #include <linux/genhd.h>
  126. #include <linux/mm.h>
  127. #include <linux/slab.h>
  128. #include <linux/init.h>
  129. #define REALLY_SLOW_IO
  130. #include <asm/system.h>
  131. #include <asm/io.h>
  132. #include <asm/uaccess.h>
  133. #include <linux/cdrom.h>
  134. #define MAJOR_NR CDU535_CDROM_MAJOR
  135. #include <linux/blkdev.h>
  136. #define sony535_cd_base_io sonycd535 /* for compatible parameter passing with "insmod" */
  137. #include "sonycd535.h"
  138. /*
  139. * this is the base address of the interface card for the Sony CDU-535
  140. * CDROM drive. If your jumpers are set for an address other than
  141. * this one (the default), change the following line to the
  142. * proper address.
  143. */
  144. #ifndef CDU535_ADDRESS
  145. # define CDU535_ADDRESS 0x340
  146. #endif
  147. #ifndef CDU535_INTERRUPT
  148. # define CDU535_INTERRUPT 0
  149. #endif
  150. #ifndef CDU535_HANDLE
  151. # define CDU535_HANDLE "cdu535"
  152. #endif
  153. #ifndef CDU535_MESSAGE_NAME
  154. # define CDU535_MESSAGE_NAME "Sony CDU-535"
  155. #endif
  156. #define CDU535_BLOCK_SIZE 2048
  157. #ifndef MAX_SPINUP_RETRY
  158. # define MAX_SPINUP_RETRY 3 /* 1 is sufficient for most drives... */
  159. #endif
  160. #ifndef RETRY_FOR_BAD_STATUS
  161. # define RETRY_FOR_BAD_STATUS 100 /* in 10th of second */
  162. #endif
  163. #ifndef DEBUG
  164. # define DEBUG 1
  165. #endif
  166. /*
  167. * SONY535_BUFFER_SIZE determines the size of internal buffer used
  168. * by the drive. It must be at least 2K and the larger the buffer
  169. * the better the transfer rate. It does however take system memory.
  170. * On my system I get the following transfer rates using dd to read
  171. * 10 Mb off /dev/cdrom.
  172. *
  173. * 8K buffer 43 Kb/sec
  174. * 16K buffer 66 Kb/sec
  175. * 32K buffer 91 Kb/sec
  176. * 64K buffer 111 Kb/sec
  177. * 128K buffer 123 Kb/sec
  178. * 512K buffer 123 Kb/sec
  179. */
  180. #define SONY535_BUFFER_SIZE (64*1024)
  181. /*
  182. * if LOCK_DOORS is defined then the eject button is disabled while
  183. * the device is open.
  184. */
  185. #ifndef NO_LOCK_DOORS
  186. # define LOCK_DOORS
  187. #endif
  188. static int read_subcode(void);
  189. static void sony_get_toc(void);
  190. static int cdu_open(struct inode *inode, struct file *filp);
  191. static inline unsigned int int_to_bcd(unsigned int val);
  192. static unsigned int bcd_to_int(unsigned int bcd);
  193. static int do_sony_cmd(Byte * cmd, int nCmd, Byte status[2],
  194. Byte * response, int n_response, int ignoreStatusBit7);
  195. /* The base I/O address of the Sony Interface. This is a variable (not a
  196. #define) so it can be easily changed via some future ioctl() */
  197. static unsigned int sony535_cd_base_io = CDU535_ADDRESS;
  198. module_param(sony535_cd_base_io, int, 0);
  199. /*
  200. * The following are I/O addresses of the various registers for the drive. The
  201. * comment for the base address also applies here.
  202. */
  203. static unsigned short select_unit_reg;
  204. static unsigned short result_reg;
  205. static unsigned short command_reg;
  206. static unsigned short read_status_reg;
  207. static unsigned short data_reg;
  208. static DEFINE_SPINLOCK(sonycd535_lock); /* queue lock */
  209. static struct request_queue *sonycd535_queue;
  210. static int initialized; /* Has the drive been initialized? */
  211. static int sony_disc_changed = 1; /* Has the disk been changed
  212. since the last check? */
  213. static int sony_toc_read; /* Has the table of contents been
  214. read? */
  215. static unsigned int sony_buffer_size; /* Size in bytes of the read-ahead
  216. buffer. */
  217. static unsigned int sony_buffer_sectors; /* Size (in 2048 byte records) of
  218. the read-ahead buffer. */
  219. static unsigned int sony_usage; /* How many processes have the
  220. drive open. */
  221. static int sony_first_block = -1; /* First OS block (512 byte) in
  222. the read-ahead buffer */
  223. static int sony_last_block = -1; /* Last OS block (512 byte) in
  224. the read-ahead buffer */
  225. static struct s535_sony_toc *sony_toc; /* Points to the table of
  226. contents. */
  227. static struct s535_sony_subcode *last_sony_subcode; /* Points to the last
  228. subcode address read */
  229. static Byte **sony_buffer; /* Points to the pointers
  230. to the sector buffers */
  231. static int sony_inuse; /* is the drive in use? Only one
  232. open at a time allowed */
  233. /*
  234. * The audio status uses the values from read subchannel data as specified
  235. * in include/linux/cdrom.h.
  236. */
  237. static int sony_audio_status = CDROM_AUDIO_NO_STATUS;
  238. /*
  239. * The following are a hack for pausing and resuming audio play. The drive
  240. * does not work as I would expect it, if you stop it then start it again,
  241. * the drive seeks back to the beginning and starts over. This holds the
  242. * position during a pause so a resume can restart it. It uses the
  243. * audio status variable above to tell if it is paused.
  244. * I just kept the CDU-31A driver behavior rather than using the PAUSE
  245. * command on the CDU-535.
  246. */
  247. static Byte cur_pos_msf[3];
  248. static Byte final_pos_msf[3];
  249. /* What IRQ is the drive using? 0 if none. */
  250. static int sony535_irq_used = CDU535_INTERRUPT;
  251. /* The interrupt handler will wake this queue up when it gets an interrupt. */
  252. static DECLARE_WAIT_QUEUE_HEAD(cdu535_irq_wait);
  253. /*
  254. * This routine returns 1 if the disk has been changed since the last
  255. * check or 0 if it hasn't. Setting flag to 0 resets the changed flag.
  256. */
  257. static int
  258. cdu535_check_media_change(struct gendisk *disk)
  259. {
  260. /* if driver is not initialized, always return 0 */
  261. int retval = initialized ? sony_disc_changed : 0;
  262. sony_disc_changed = 0;
  263. return retval;
  264. }
  265. static inline void
  266. enable_interrupts(void)
  267. {
  268. #ifdef USE_IRQ
  269. /*
  270. * This code was taken from cdu31a.c; it will not
  271. * directly work for the cdu535 as written...
  272. */
  273. curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
  274. | SONY_RES_RDY_INT_EN_BIT
  275. | SONY_DATA_RDY_INT_EN_BIT);
  276. outb(curr_control_reg, sony_cd_control_reg);
  277. #endif
  278. }
  279. static inline void
  280. disable_interrupts(void)
  281. {
  282. #ifdef USE_IRQ
  283. /*
  284. * This code was taken from cdu31a.c; it will not
  285. * directly work for the cdu535 as written...
  286. */
  287. curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
  288. | SONY_RES_RDY_INT_EN_BIT
  289. | SONY_DATA_RDY_INT_EN_BIT);
  290. outb(curr_control_reg, sony_cd_control_reg);
  291. #endif
  292. }
  293. static irqreturn_t
  294. cdu535_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  295. {
  296. disable_interrupts();
  297. if (waitqueue_active(&cdu535_irq_wait)) {
  298. wake_up(&cdu535_irq_wait);
  299. return IRQ_HANDLED;
  300. }
  301. printk(CDU535_MESSAGE_NAME
  302. ": Got an interrupt but nothing was waiting\n");
  303. return IRQ_NONE;
  304. }
  305. /*
  306. * Wait a little while.
  307. */
  308. static inline void
  309. sony_sleep(void)
  310. {
  311. if (sony535_irq_used <= 0) { /* poll */
  312. yield();
  313. } else { /* Interrupt driven */
  314. DEFINE_WAIT(wait);
  315. spin_lock_irq(&sonycd535_lock);
  316. enable_interrupts();
  317. prepare_to_wait(&cdu535_irq_wait, &wait, TASK_INTERRUPTIBLE);
  318. spin_unlock_irq(&sonycd535_lock);
  319. schedule();
  320. finish_wait(&cdu535_irq_wait, &wait);
  321. }
  322. }
  323. /*------------------start of SONY CDU535 very specific ---------------------*/
  324. /****************************************************************************
  325. * void select_unit( int unit_no )
  326. *
  327. * Select the specified unit (0-3) so that subsequent commands reference it
  328. ****************************************************************************/
  329. static void
  330. select_unit(int unit_no)
  331. {
  332. unsigned int select_mask = ~(1 << unit_no);
  333. outb(select_mask, select_unit_reg);
  334. }
  335. /***************************************************************************
  336. * int read_result_reg( Byte *data_ptr )
  337. *
  338. * Read a result byte from the Sony CDU controller, store in location pointed
  339. * to by data_ptr. Return zero on success, TIME_OUT if we did not receive
  340. * data.
  341. ***************************************************************************/
  342. static int
  343. read_result_reg(Byte *data_ptr)
  344. {
  345. unsigned long snap;
  346. int read_status;
  347. snap = jiffies;
  348. while (jiffies-snap < SONY_JIFFIES_TIMEOUT) {
  349. read_status = inb(read_status_reg);
  350. if ((read_status & SONY535_RESULT_NOT_READY_BIT) == 0) {
  351. #if DEBUG > 1
  352. printk(CDU535_MESSAGE_NAME
  353. ": read_result_reg(): readStatReg = 0x%x\n", read_status);
  354. #endif
  355. *data_ptr = inb(result_reg);
  356. return 0;
  357. } else {
  358. sony_sleep();
  359. }
  360. }
  361. printk(CDU535_MESSAGE_NAME " read_result_reg: TIME OUT!\n");
  362. return TIME_OUT;
  363. }
  364. /****************************************************************************
  365. * int read_exec_status( Byte status[2] )
  366. *
  367. * Read the execution status of the last command and put into status.
  368. * Handles reading second status word if available. Returns 0 on success,
  369. * TIME_OUT on failure.
  370. ****************************************************************************/
  371. static int
  372. read_exec_status(Byte status[2])
  373. {
  374. status[1] = 0;
  375. if (read_result_reg(&(status[0])) != 0)
  376. return TIME_OUT;
  377. if ((status[0] & 0x80) != 0) { /* byte two follows */
  378. if (read_result_reg(&(status[1])) != 0)
  379. return TIME_OUT;
  380. }
  381. #if DEBUG > 1
  382. printk(CDU535_MESSAGE_NAME ": read_exec_status: read 0x%x 0x%x\n",
  383. status[0], status[1]);
  384. #endif
  385. return 0;
  386. }
  387. /****************************************************************************
  388. * int check_drive_status( void )
  389. *
  390. * Check the current drive status. Using this before executing a command
  391. * takes care of the problem of unsolicited drive status-2 messages.
  392. * Add a check of the audio status if we think the disk is playing.
  393. ****************************************************************************/
  394. static int
  395. check_drive_status(void)
  396. {
  397. Byte status, e_status[2];
  398. int CDD, ATN;
  399. Byte cmd;
  400. select_unit(0);
  401. if (sony_audio_status == CDROM_AUDIO_PLAY) { /* check status */
  402. outb(SONY535_REQUEST_AUDIO_STATUS, command_reg);
  403. if (read_result_reg(&status) == 0) {
  404. switch (status) {
  405. case 0x0:
  406. break; /* play in progress */
  407. case 0x1:
  408. break; /* paused */
  409. case 0x3: /* audio play completed */
  410. case 0x5: /* play not requested */
  411. sony_audio_status = CDROM_AUDIO_COMPLETED;
  412. read_subcode();
  413. break;
  414. case 0x4: /* error during play */
  415. sony_audio_status = CDROM_AUDIO_ERROR;
  416. break;
  417. }
  418. }
  419. }
  420. /* now check drive status */
  421. outb(SONY535_REQUEST_DRIVE_STATUS_2, command_reg);
  422. if (read_result_reg(&status) != 0)
  423. return TIME_OUT;
  424. #if DEBUG > 1
  425. printk(CDU535_MESSAGE_NAME ": check_drive_status() got 0x%x\n", status);
  426. #endif
  427. if (status == 0)
  428. return 0;
  429. ATN = status & 0xf;
  430. CDD = (status >> 4) & 0xf;
  431. switch (ATN) {
  432. case 0x0:
  433. break; /* go on to CDD stuff */
  434. case SONY535_ATN_BUSY:
  435. if (initialized)
  436. printk(CDU535_MESSAGE_NAME " error: drive busy\n");
  437. return CD_BUSY;
  438. case SONY535_ATN_EJECT_IN_PROGRESS:
  439. printk(CDU535_MESSAGE_NAME " error: eject in progress\n");
  440. sony_audio_status = CDROM_AUDIO_INVALID;
  441. return CD_BUSY;
  442. case SONY535_ATN_RESET_OCCURRED:
  443. case SONY535_ATN_DISC_CHANGED:
  444. case SONY535_ATN_RESET_AND_DISC_CHANGED:
  445. #if DEBUG > 0
  446. printk(CDU535_MESSAGE_NAME " notice: reset occurred or disc changed\n");
  447. #endif
  448. sony_disc_changed = 1;
  449. sony_toc_read = 0;
  450. sony_audio_status = CDROM_AUDIO_NO_STATUS;
  451. sony_first_block = -1;
  452. sony_last_block = -1;
  453. if (initialized) {
  454. cmd = SONY535_SPIN_UP;
  455. do_sony_cmd(&cmd, 1, e_status, NULL, 0, 0);
  456. sony_get_toc();
  457. }
  458. return 0;
  459. default:
  460. printk(CDU535_MESSAGE_NAME " error: drive busy (ATN=0x%x)\n", ATN);
  461. return CD_BUSY;
  462. }
  463. switch (CDD) { /* the 531 docs are not helpful in decoding this */
  464. case 0x0: /* just use the values from the DOS driver */
  465. case 0x2:
  466. case 0xa:
  467. break; /* no error */
  468. case 0xc:
  469. printk(CDU535_MESSAGE_NAME
  470. ": check_drive_status(): CDD = 0xc! Not properly handled!\n");
  471. return CD_BUSY; /* ? */
  472. default:
  473. return CD_BUSY;
  474. }
  475. return 0;
  476. } /* check_drive_status() */
  477. /*****************************************************************************
  478. * int do_sony_cmd( Byte *cmd, int n_cmd, Byte status[2],
  479. * Byte *response, int n_response, int ignore_status_bit7 )
  480. *
  481. * Generic routine for executing commands. The command and its parameters
  482. * should be placed in the cmd[] array, number of bytes in the command is
  483. * stored in nCmd. The response from the command will be stored in the
  484. * response array. The number of bytes you expect back (excluding status)
  485. * should be passed in n_response. Finally, some
  486. * commands set bit 7 of the return status even when there is no second
  487. * status byte, on these commands set ignoreStatusBit7 TRUE.
  488. * If the command was sent and data received back, then we return 0,
  489. * else we return TIME_OUT. You still have to check the status yourself.
  490. * You should call check_drive_status() before calling this routine
  491. * so that you do not lose notifications of disk changes, etc.
  492. ****************************************************************************/
  493. static int
  494. do_sony_cmd(Byte * cmd, int n_cmd, Byte status[2],
  495. Byte * response, int n_response, int ignore_status_bit7)
  496. {
  497. int i;
  498. /* write out the command */
  499. for (i = 0; i < n_cmd; i++)
  500. outb(cmd[i], command_reg);
  501. /* read back the status */
  502. if (read_result_reg(status) != 0)
  503. return TIME_OUT;
  504. if (!ignore_status_bit7 && ((status[0] & 0x80) != 0)) {
  505. /* get second status byte */
  506. if (read_result_reg(status + 1) != 0)
  507. return TIME_OUT;
  508. } else {
  509. status[1] = 0;
  510. }
  511. #if DEBUG > 2
  512. printk(CDU535_MESSAGE_NAME ": do_sony_cmd %x: %x %x\n",
  513. *cmd, status[0], status[1]);
  514. #endif
  515. /* do not know about when I should read set of data and when not to */
  516. if ((status[0] & ((ignore_status_bit7 ? 0x7f : 0xff) & 0x8f)) != 0)
  517. return 0;
  518. /* else, read in rest of data */
  519. for (i = 0; 0 < n_response; n_response--, i++)
  520. if (read_result_reg(response + i) != 0)
  521. return TIME_OUT;
  522. return 0;
  523. } /* do_sony_cmd() */
  524. /**************************************************************************
  525. * int set_drive_mode( int mode, Byte status[2] )
  526. *
  527. * Set the drive mode to the specified value (mode=0 is audio, mode=e0
  528. * is mode-1 CDROM
  529. **************************************************************************/
  530. static int
  531. set_drive_mode(int mode, Byte status[2])
  532. {
  533. Byte cmd_buff[2];
  534. Byte ret_buff[1];
  535. cmd_buff[0] = SONY535_SET_DRIVE_MODE;
  536. cmd_buff[1] = mode;
  537. return do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1);
  538. }
  539. /***************************************************************************
  540. * int seek_and_read_N_blocks( Byte params[], int n_blocks, Byte status[2],
  541. * Byte *data_buff, int buff_size )
  542. *
  543. * Read n_blocks of data from the CDROM starting at position params[0:2],
  544. * number of blocks in stored in params[3:5] -- both these are already
  545. * int bcd format.
  546. * Transfer the data into the buffer pointed at by data_buff. buff_size
  547. * gives the number of bytes available in the buffer.
  548. * The routine returns number of bytes read in if successful, otherwise
  549. * it returns one of the standard error returns.
  550. ***************************************************************************/
  551. static int
  552. seek_and_read_N_blocks(Byte params[], int n_blocks, Byte status[2],
  553. Byte **buff, int buf_size)
  554. {
  555. Byte cmd_buff[7];
  556. int i;
  557. int read_status;
  558. unsigned long snap;
  559. Byte *data_buff;
  560. int sector_count = 0;
  561. if (buf_size < CDU535_BLOCK_SIZE * n_blocks)
  562. return NO_ROOM;
  563. set_drive_mode(SONY535_CDROM_DRIVE_MODE, status);
  564. /* send command to read the data */
  565. cmd_buff[0] = SONY535_SEEK_AND_READ_N_BLOCKS_1;
  566. for (i = 0; i < 6; i++)
  567. cmd_buff[i + 1] = params[i];
  568. for (i = 0; i < 7; i++)
  569. outb(cmd_buff[i], command_reg);
  570. /* read back the data one block at a time */
  571. while (0 < n_blocks--) {
  572. /* wait for data to be ready */
  573. int data_valid = 0;
  574. snap = jiffies;
  575. while (jiffies-snap < SONY_JIFFIES_TIMEOUT) {
  576. read_status = inb(read_status_reg);
  577. if ((read_status & SONY535_RESULT_NOT_READY_BIT) == 0) {
  578. read_exec_status(status);
  579. return BAD_STATUS;
  580. }
  581. if ((read_status & SONY535_DATA_NOT_READY_BIT) == 0) {
  582. /* data is ready, read it */
  583. data_buff = buff[sector_count++];
  584. for (i = 0; i < CDU535_BLOCK_SIZE; i++)
  585. *data_buff++ = inb(data_reg); /* unrolling this loop does not seem to help */
  586. data_valid = 1;
  587. break; /* exit the timeout loop */
  588. }
  589. sony_sleep(); /* data not ready, sleep a while */
  590. }
  591. if (!data_valid)
  592. return TIME_OUT; /* if we reach this stage */
  593. }
  594. /* read all the data, now read the status */
  595. if ((i = read_exec_status(status)) != 0)
  596. return i;
  597. return CDU535_BLOCK_SIZE * sector_count;
  598. } /* seek_and_read_N_blocks() */
  599. /****************************************************************************
  600. * int request_toc_data( Byte status[2], struct s535_sony_toc *toc )
  601. *
  602. * Read in the table of contents data. Converts all the bcd data
  603. * into integers in the toc structure.
  604. ****************************************************************************/
  605. static int
  606. request_toc_data(Byte status[2], struct s535_sony_toc *toc)
  607. {
  608. int to_status;
  609. int i, j, n_tracks, track_no;
  610. int first_track_num, last_track_num;
  611. Byte cmd_no = 0xb2;
  612. Byte track_address_buffer[5];
  613. /* read the fixed portion of the table of contents */
  614. if ((to_status = do_sony_cmd(&cmd_no, 1, status, (Byte *) toc, 15, 1)) != 0)
  615. return to_status;
  616. /* convert the data into integers so we can use them */
  617. first_track_num = bcd_to_int(toc->first_track_num);
  618. last_track_num = bcd_to_int(toc->last_track_num);
  619. n_tracks = last_track_num - first_track_num + 1;
  620. /* read each of the track address descriptors */
  621. for (i = 0; i < n_tracks; i++) {
  622. /* read the descriptor into a temporary buffer */
  623. for (j = 0; j < 5; j++) {
  624. if (read_result_reg(track_address_buffer + j) != 0)
  625. return TIME_OUT;
  626. if (j == 1) /* need to convert from bcd */
  627. track_no = bcd_to_int(track_address_buffer[j]);
  628. }
  629. /* copy the descriptor to proper location - sonycd.c just fills */
  630. memcpy(toc->tracks + i, track_address_buffer, 5);
  631. }
  632. return 0;
  633. } /* request_toc_data() */
  634. /***************************************************************************
  635. * int spin_up_drive( Byte status[2] )
  636. *
  637. * Spin up the drive (unless it is already spinning).
  638. ***************************************************************************/
  639. static int
  640. spin_up_drive(Byte status[2])
  641. {
  642. Byte cmd;
  643. /* first see if the drive is already spinning */
  644. cmd = SONY535_REQUEST_DRIVE_STATUS_1;
  645. if (do_sony_cmd(&cmd, 1, status, NULL, 0, 0) != 0)
  646. return TIME_OUT;
  647. if ((status[0] & SONY535_STATUS1_NOT_SPINNING) == 0)
  648. return 0; /* it's already spinning */
  649. /* otherwise, give the spin-up command */
  650. cmd = SONY535_SPIN_UP;
  651. return do_sony_cmd(&cmd, 1, status, NULL, 0, 0);
  652. }
  653. /*--------------------end of SONY CDU535 very specific ---------------------*/
  654. /* Convert from an integer 0-99 to BCD */
  655. static inline unsigned int
  656. int_to_bcd(unsigned int val)
  657. {
  658. int retval;
  659. retval = (val / 10) << 4;
  660. retval = retval | val % 10;
  661. return retval;
  662. }
  663. /* Convert from BCD to an integer from 0-99 */
  664. static unsigned int
  665. bcd_to_int(unsigned int bcd)
  666. {
  667. return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
  668. }
  669. /*
  670. * Convert a logical sector value (like the OS would want to use for
  671. * a block device) to an MSF format.
  672. */
  673. static void
  674. log_to_msf(unsigned int log, Byte *msf)
  675. {
  676. log = log + LOG_START_OFFSET;
  677. msf[0] = int_to_bcd(log / 4500);
  678. log = log % 4500;
  679. msf[1] = int_to_bcd(log / 75);
  680. msf[2] = int_to_bcd(log % 75);
  681. }
  682. /*
  683. * Convert an MSF format to a logical sector.
  684. */
  685. static unsigned int
  686. msf_to_log(Byte *msf)
  687. {
  688. unsigned int log;
  689. log = bcd_to_int(msf[2]);
  690. log += bcd_to_int(msf[1]) * 75;
  691. log += bcd_to_int(msf[0]) * 4500;
  692. log = log - LOG_START_OFFSET;
  693. return log;
  694. }
  695. /*
  696. * Take in integer size value and put it into a buffer like
  697. * the drive would want to see a number-of-sector value.
  698. */
  699. static void
  700. size_to_buf(unsigned int size, Byte *buf)
  701. {
  702. buf[0] = size / 65536;
  703. size = size % 65536;
  704. buf[1] = size / 256;
  705. buf[2] = size % 256;
  706. }
  707. /*
  708. * The OS calls this to perform a read or write operation to the drive.
  709. * Write obviously fail. Reads to a read ahead of sony_buffer_size
  710. * bytes to help speed operations. This especially helps since the OS
  711. * may use 1024 byte blocks and the drive uses 2048 byte blocks. Since most
  712. * data access on a CD is done sequentially, this saves a lot of operations.
  713. */
  714. static void
  715. do_cdu535_request(request_queue_t * q)
  716. {
  717. struct request *req;
  718. unsigned int read_size;
  719. int block;
  720. int nsect;
  721. int copyoff;
  722. int spin_up_retry;
  723. Byte params[10];
  724. Byte status[2];
  725. Byte cmd[2];
  726. while (1) {
  727. req = elv_next_request(q);
  728. if (!req)
  729. return;
  730. block = req->sector;
  731. nsect = req->nr_sectors;
  732. if (!blk_fs_request(req)) {
  733. end_request(req, 0);
  734. continue;
  735. }
  736. if (rq_data_dir(req) == WRITE) {
  737. end_request(req, 0);
  738. continue;
  739. }
  740. /*
  741. * If the block address is invalid or the request goes beyond
  742. * the end of the media, return an error.
  743. */
  744. if (sony_toc->lead_out_start_lba <= (block/4)) {
  745. end_request(req, 0);
  746. return;
  747. }
  748. if (sony_toc->lead_out_start_lba <= ((block + nsect) / 4)) {
  749. end_request(req, 0);
  750. return;
  751. }
  752. while (0 < nsect) {
  753. /*
  754. * If the requested sector is not currently in
  755. * the read-ahead buffer, it must be read in.
  756. */
  757. if ((block < sony_first_block) || (sony_last_block < block)) {
  758. sony_first_block = (block / 4) * 4;
  759. log_to_msf(block / 4, params);
  760. /*
  761. * If the full read-ahead would go beyond the end of the media, trim
  762. * it back to read just till the end of the media.
  763. */
  764. if (sony_toc->lead_out_start_lba <= ((block / 4) + sony_buffer_sectors)) {
  765. sony_last_block = (sony_toc->lead_out_start_lba * 4) - 1;
  766. read_size = sony_toc->lead_out_start_lba - (block / 4);
  767. } else {
  768. sony_last_block = sony_first_block + (sony_buffer_sectors * 4) - 1;
  769. read_size = sony_buffer_sectors;
  770. }
  771. size_to_buf(read_size, &params[3]);
  772. /*
  773. * Read the data. If the drive was not spinning,
  774. * spin it up and try some more.
  775. */
  776. for (spin_up_retry=0 ;; ++spin_up_retry) {
  777. /* This loop has been modified to support the Sony
  778. * CDU-510/515 series, thanks to Claudio Porfiri
  779. * <C.Porfiri@nisms.tei.ericsson.se>.
  780. */
  781. /*
  782. * This part is to deal with very slow hardware. We
  783. * try at most MAX_SPINUP_RETRY times to read the same
  784. * block. A check for seek_and_read_N_blocks' result is
  785. * performed; if the result is wrong, the CDROM's engine
  786. * is restarted and the operation is tried again.
  787. */
  788. /*
  789. * 1995-06-01: The system got problems when downloading
  790. * from Slackware CDROM, the problem seems to be:
  791. * seek_and_read_N_blocks returns BAD_STATUS and we
  792. * should wait for a while before retrying, so a new
  793. * part was added to discriminate the return value from
  794. * seek_and_read_N_blocks for the various cases.
  795. */
  796. int readStatus = seek_and_read_N_blocks(params, read_size,
  797. status, sony_buffer, (read_size * CDU535_BLOCK_SIZE));
  798. if (0 <= readStatus) /* Good data; common case, placed first */
  799. break;
  800. if (readStatus == NO_ROOM || spin_up_retry == MAX_SPINUP_RETRY) {
  801. /* give up */
  802. if (readStatus == NO_ROOM)
  803. printk(CDU535_MESSAGE_NAME " No room to read from CD\n");
  804. else
  805. printk(CDU535_MESSAGE_NAME " Read error: 0x%.2x\n",
  806. status[0]);
  807. sony_first_block = -1;
  808. sony_last_block = -1;
  809. end_request(req, 0);
  810. return;
  811. }
  812. if (readStatus == BAD_STATUS) {
  813. /* Sleep for a while, then retry */
  814. set_current_state(TASK_INTERRUPTIBLE);
  815. spin_unlock_irq(&sonycd535_lock);
  816. schedule_timeout(RETRY_FOR_BAD_STATUS*HZ/10);
  817. spin_lock_irq(&sonycd535_lock);
  818. }
  819. #if DEBUG > 0
  820. printk(CDU535_MESSAGE_NAME
  821. " debug: calling spin up when reading data!\n");
  822. #endif
  823. cmd[0] = SONY535_SPIN_UP;
  824. do_sony_cmd(cmd, 1, status, NULL, 0, 0);
  825. }
  826. }
  827. /*
  828. * The data is in memory now, copy it to the buffer and advance to the
  829. * next block to read.
  830. */
  831. copyoff = block - sony_first_block;
  832. memcpy(req->buffer,
  833. sony_buffer[copyoff / 4] + 512 * (copyoff % 4), 512);
  834. block += 1;
  835. nsect -= 1;
  836. req->buffer += 512;
  837. }
  838. end_request(req, 1);
  839. }
  840. }
  841. /*
  842. * Read the table of contents from the drive and set sony_toc_read if
  843. * successful.
  844. */
  845. static void
  846. sony_get_toc(void)
  847. {
  848. Byte status[2];
  849. if (!sony_toc_read) {
  850. /* do not call check_drive_status() from here since it can call this routine */
  851. if (request_toc_data(status, sony_toc) < 0)
  852. return;
  853. sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
  854. sony_toc_read = 1;
  855. }
  856. }
  857. /*
  858. * Search for a specific track in the table of contents. track is
  859. * passed in bcd format
  860. */
  861. static int
  862. find_track(int track)
  863. {
  864. int i;
  865. int num_tracks;
  866. num_tracks = bcd_to_int(sony_toc->last_track_num) -
  867. bcd_to_int(sony_toc->first_track_num) + 1;
  868. for (i = 0; i < num_tracks; i++) {
  869. if (sony_toc->tracks[i].track == track) {
  870. return i;
  871. }
  872. }
  873. return -1;
  874. }
  875. /*
  876. * Read the subcode and put it int last_sony_subcode for future use.
  877. */
  878. static int
  879. read_subcode(void)
  880. {
  881. Byte cmd = SONY535_REQUEST_SUB_Q_DATA;
  882. Byte status[2];
  883. int dsc_status;
  884. if (check_drive_status() != 0)
  885. return -EIO;
  886. if ((dsc_status = do_sony_cmd(&cmd, 1, status, (Byte *) last_sony_subcode,
  887. sizeof(struct s535_sony_subcode), 1)) != 0) {
  888. printk(CDU535_MESSAGE_NAME " error 0x%.2x, %d (read_subcode)\n",
  889. status[0], dsc_status);
  890. return -EIO;
  891. }
  892. return 0;
  893. }
  894. /*
  895. * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
  896. * the drive is playing, the subchannel needs to be read (since it would be
  897. * changing). If the drive is paused or completed, the subcode information has
  898. * already been stored, just use that. The ioctl call wants things in decimal
  899. * (not BCD), so all the conversions are done.
  900. */
  901. static int
  902. sony_get_subchnl_info(void __user *arg)
  903. {
  904. struct cdrom_subchnl schi;
  905. /* Get attention stuff */
  906. if (check_drive_status() != 0)
  907. return -EIO;
  908. sony_get_toc();
  909. if (!sony_toc_read) {
  910. return -EIO;
  911. }
  912. if (copy_from_user(&schi, arg, sizeof schi))
  913. return -EFAULT;
  914. switch (sony_audio_status) {
  915. case CDROM_AUDIO_PLAY:
  916. if (read_subcode() < 0) {
  917. return -EIO;
  918. }
  919. break;
  920. case CDROM_AUDIO_PAUSED:
  921. case CDROM_AUDIO_COMPLETED:
  922. break;
  923. case CDROM_AUDIO_NO_STATUS:
  924. schi.cdsc_audiostatus = sony_audio_status;
  925. if (copy_to_user(arg, &schi, sizeof schi))
  926. return -EFAULT;
  927. return 0;
  928. break;
  929. case CDROM_AUDIO_INVALID:
  930. case CDROM_AUDIO_ERROR:
  931. default:
  932. return -EIO;
  933. }
  934. schi.cdsc_audiostatus = sony_audio_status;
  935. schi.cdsc_adr = last_sony_subcode->address;
  936. schi.cdsc_ctrl = last_sony_subcode->control;
  937. schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
  938. schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
  939. if (schi.cdsc_format == CDROM_MSF) {
  940. schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
  941. schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
  942. schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
  943. schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
  944. schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
  945. schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
  946. } else if (schi.cdsc_format == CDROM_LBA) {
  947. schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
  948. schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
  949. }
  950. return copy_to_user(arg, &schi, sizeof schi) ? -EFAULT : 0;
  951. }
  952. /*
  953. * The big ugly ioctl handler.
  954. */
  955. static int
  956. cdu_ioctl(struct inode *inode,
  957. struct file *file,
  958. unsigned int cmd,
  959. unsigned long arg)
  960. {
  961. Byte status[2];
  962. Byte cmd_buff[10], params[10];
  963. int i;
  964. int dsc_status;
  965. void __user *argp = (void __user *)arg;
  966. if (check_drive_status() != 0)
  967. return -EIO;
  968. switch (cmd) {
  969. case CDROMSTART: /* Spin up the drive */
  970. if (spin_up_drive(status) < 0) {
  971. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTART)\n",
  972. status[0]);
  973. return -EIO;
  974. }
  975. return 0;
  976. break;
  977. case CDROMSTOP: /* Spin down the drive */
  978. cmd_buff[0] = SONY535_HOLD;
  979. do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  980. /*
  981. * Spin the drive down, ignoring the error if the disk was
  982. * already not spinning.
  983. */
  984. sony_audio_status = CDROM_AUDIO_NO_STATUS;
  985. cmd_buff[0] = SONY535_SPIN_DOWN;
  986. dsc_status = do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  987. if (((dsc_status < 0) && (dsc_status != BAD_STATUS)) ||
  988. ((status[0] & ~(SONY535_STATUS1_NOT_SPINNING)) != 0)) {
  989. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTOP)\n",
  990. status[0]);
  991. return -EIO;
  992. }
  993. return 0;
  994. break;
  995. case CDROMPAUSE: /* Pause the drive */
  996. cmd_buff[0] = SONY535_HOLD; /* CDU-31 driver uses AUDIO_STOP, not pause */
  997. if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
  998. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPAUSE)\n",
  999. status[0]);
  1000. return -EIO;
  1001. }
  1002. /* Get the current position and save it for resuming */
  1003. if (read_subcode() < 0) {
  1004. return -EIO;
  1005. }
  1006. cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
  1007. cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
  1008. cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
  1009. sony_audio_status = CDROM_AUDIO_PAUSED;
  1010. return 0;
  1011. break;
  1012. case CDROMRESUME: /* Start the drive after being paused */
  1013. set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
  1014. if (sony_audio_status != CDROM_AUDIO_PAUSED) {
  1015. return -EINVAL;
  1016. }
  1017. spin_up_drive(status);
  1018. /* Start the drive at the saved position. */
  1019. cmd_buff[0] = SONY535_PLAY_AUDIO;
  1020. cmd_buff[1] = 0; /* play back starting at this address */
  1021. cmd_buff[2] = cur_pos_msf[0];
  1022. cmd_buff[3] = cur_pos_msf[1];
  1023. cmd_buff[4] = cur_pos_msf[2];
  1024. cmd_buff[5] = SONY535_PLAY_AUDIO;
  1025. cmd_buff[6] = 2; /* set ending address */
  1026. cmd_buff[7] = final_pos_msf[0];
  1027. cmd_buff[8] = final_pos_msf[1];
  1028. cmd_buff[9] = final_pos_msf[2];
  1029. if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
  1030. (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
  1031. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMRESUME)\n",
  1032. status[0]);
  1033. return -EIO;
  1034. }
  1035. sony_audio_status = CDROM_AUDIO_PLAY;
  1036. return 0;
  1037. break;
  1038. case CDROMPLAYMSF: /* Play starting at the given MSF address. */
  1039. if (copy_from_user(params, argp, 6))
  1040. return -EFAULT;
  1041. spin_up_drive(status);
  1042. set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
  1043. /* The parameters are given in int, must be converted */
  1044. for (i = 0; i < 3; i++) {
  1045. cmd_buff[2 + i] = int_to_bcd(params[i]);
  1046. cmd_buff[7 + i] = int_to_bcd(params[i + 3]);
  1047. }
  1048. cmd_buff[0] = SONY535_PLAY_AUDIO;
  1049. cmd_buff[1] = 0; /* play back starting at this address */
  1050. /* cmd_buff[2-4] are filled in for loop above */
  1051. cmd_buff[5] = SONY535_PLAY_AUDIO;
  1052. cmd_buff[6] = 2; /* set ending address */
  1053. /* cmd_buff[7-9] are filled in for loop above */
  1054. if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
  1055. (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
  1056. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYMSF)\n",
  1057. status[0]);
  1058. return -EIO;
  1059. }
  1060. /* Save the final position for pauses and resumes */
  1061. final_pos_msf[0] = cmd_buff[7];
  1062. final_pos_msf[1] = cmd_buff[8];
  1063. final_pos_msf[2] = cmd_buff[9];
  1064. sony_audio_status = CDROM_AUDIO_PLAY;
  1065. return 0;
  1066. break;
  1067. case CDROMREADTOCHDR: /* Read the table of contents header */
  1068. {
  1069. struct cdrom_tochdr __user *hdr = argp;
  1070. struct cdrom_tochdr loc_hdr;
  1071. sony_get_toc();
  1072. if (!sony_toc_read)
  1073. return -EIO;
  1074. loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
  1075. loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
  1076. if (copy_to_user(hdr, &loc_hdr, sizeof *hdr))
  1077. return -EFAULT;
  1078. }
  1079. return 0;
  1080. break;
  1081. case CDROMREADTOCENTRY: /* Read a given table of contents entry */
  1082. {
  1083. struct cdrom_tocentry __user *entry = argp;
  1084. struct cdrom_tocentry loc_entry;
  1085. int track_idx;
  1086. Byte *msf_val = NULL;
  1087. sony_get_toc();
  1088. if (!sony_toc_read) {
  1089. return -EIO;
  1090. }
  1091. if (copy_from_user(&loc_entry, entry, sizeof loc_entry))
  1092. return -EFAULT;
  1093. /* Lead out is handled separately since it is special. */
  1094. if (loc_entry.cdte_track == CDROM_LEADOUT) {
  1095. loc_entry.cdte_adr = 0 /*sony_toc->address2 */ ;
  1096. loc_entry.cdte_ctrl = sony_toc->control2;
  1097. msf_val = sony_toc->lead_out_start_msf;
  1098. } else {
  1099. track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
  1100. if (track_idx < 0)
  1101. return -EINVAL;
  1102. loc_entry.cdte_adr = 0 /*sony_toc->tracks[track_idx].address */ ;
  1103. loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
  1104. msf_val = sony_toc->tracks[track_idx].track_start_msf;
  1105. }
  1106. /* Logical buffer address or MSF format requested? */
  1107. if (loc_entry.cdte_format == CDROM_LBA) {
  1108. loc_entry.cdte_addr.lba = msf_to_log(msf_val);
  1109. } else if (loc_entry.cdte_format == CDROM_MSF) {
  1110. loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
  1111. loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val + 1));
  1112. loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val + 2));
  1113. }
  1114. if (copy_to_user(entry, &loc_entry, sizeof *entry))
  1115. return -EFAULT;
  1116. }
  1117. return 0;
  1118. break;
  1119. case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
  1120. {
  1121. struct cdrom_ti ti;
  1122. int track_idx;
  1123. sony_get_toc();
  1124. if (!sony_toc_read)
  1125. return -EIO;
  1126. if (copy_from_user(&ti, argp, sizeof ti))
  1127. return -EFAULT;
  1128. if ((ti.cdti_trk0 < sony_toc->first_track_num)
  1129. || (sony_toc->last_track_num < ti.cdti_trk0)
  1130. || (ti.cdti_trk1 < ti.cdti_trk0)) {
  1131. return -EINVAL;
  1132. }
  1133. track_idx = find_track(int_to_bcd(ti.cdti_trk0));
  1134. if (track_idx < 0)
  1135. return -EINVAL;
  1136. params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
  1137. params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
  1138. params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
  1139. /*
  1140. * If we want to stop after the last track, use the lead-out
  1141. * MSF to do that.
  1142. */
  1143. if (bcd_to_int(sony_toc->last_track_num) <= ti.cdti_trk1) {
  1144. log_to_msf(msf_to_log(sony_toc->lead_out_start_msf) - 1,
  1145. &(params[4]));
  1146. } else {
  1147. track_idx = find_track(int_to_bcd(ti.cdti_trk1 + 1));
  1148. if (track_idx < 0)
  1149. return -EINVAL;
  1150. log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf) - 1,
  1151. &(params[4]));
  1152. }
  1153. params[0] = 0x03;
  1154. spin_up_drive(status);
  1155. set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
  1156. /* Start the drive at the saved position. */
  1157. cmd_buff[0] = SONY535_PLAY_AUDIO;
  1158. cmd_buff[1] = 0; /* play back starting at this address */
  1159. cmd_buff[2] = params[1];
  1160. cmd_buff[3] = params[2];
  1161. cmd_buff[4] = params[3];
  1162. cmd_buff[5] = SONY535_PLAY_AUDIO;
  1163. cmd_buff[6] = 2; /* set ending address */
  1164. cmd_buff[7] = params[4];
  1165. cmd_buff[8] = params[5];
  1166. cmd_buff[9] = params[6];
  1167. if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
  1168. (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
  1169. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYTRKIND)\n",
  1170. status[0]);
  1171. printk("... Params: %x %x %x %x %x %x %x\n",
  1172. params[0], params[1], params[2],
  1173. params[3], params[4], params[5], params[6]);
  1174. return -EIO;
  1175. }
  1176. /* Save the final position for pauses and resumes */
  1177. final_pos_msf[0] = params[4];
  1178. final_pos_msf[1] = params[5];
  1179. final_pos_msf[2] = params[6];
  1180. sony_audio_status = CDROM_AUDIO_PLAY;
  1181. return 0;
  1182. }
  1183. case CDROMSUBCHNL: /* Get subchannel info */
  1184. return sony_get_subchnl_info(argp);
  1185. case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
  1186. {
  1187. struct cdrom_volctrl volctrl;
  1188. if (copy_from_user(&volctrl, argp, sizeof volctrl))
  1189. return -EFAULT;
  1190. cmd_buff[0] = SONY535_SET_VOLUME;
  1191. cmd_buff[1] = volctrl.channel0;
  1192. cmd_buff[2] = volctrl.channel1;
  1193. if (do_sony_cmd(cmd_buff, 3, status, NULL, 0, 0) != 0) {
  1194. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMVOLCTRL)\n",
  1195. status[0]);
  1196. return -EIO;
  1197. }
  1198. }
  1199. return 0;
  1200. case CDROMEJECT: /* Eject the drive */
  1201. cmd_buff[0] = SONY535_STOP;
  1202. do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  1203. cmd_buff[0] = SONY535_SPIN_DOWN;
  1204. do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  1205. sony_audio_status = CDROM_AUDIO_INVALID;
  1206. cmd_buff[0] = SONY535_EJECT_CADDY;
  1207. if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
  1208. printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMEJECT)\n",
  1209. status[0]);
  1210. return -EIO;
  1211. }
  1212. return 0;
  1213. break;
  1214. default:
  1215. return -EINVAL;
  1216. }
  1217. }
  1218. /*
  1219. * Open the drive for operations. Spin the drive up and read the table of
  1220. * contents if these have not already been done.
  1221. */
  1222. static int
  1223. cdu_open(struct inode *inode,
  1224. struct file *filp)
  1225. {
  1226. Byte status[2], cmd_buff[2];
  1227. if (sony_inuse)
  1228. return -EBUSY;
  1229. if (check_drive_status() != 0)
  1230. return -EIO;
  1231. sony_inuse = 1;
  1232. if (spin_up_drive(status) != 0) {
  1233. printk(CDU535_MESSAGE_NAME " error 0x%.2x (cdu_open, spin up)\n",
  1234. status[0]);
  1235. sony_inuse = 0;
  1236. return -EIO;
  1237. }
  1238. sony_get_toc();
  1239. if (!sony_toc_read) {
  1240. cmd_buff[0] = SONY535_SPIN_DOWN;
  1241. do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  1242. sony_inuse = 0;
  1243. return -EIO;
  1244. }
  1245. check_disk_change(inode->i_bdev);
  1246. sony_usage++;
  1247. #ifdef LOCK_DOORS
  1248. /* disable the eject button while mounted */
  1249. cmd_buff[0] = SONY535_DISABLE_EJECT_BUTTON;
  1250. do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
  1251. #endif
  1252. return 0;
  1253. }
  1254. /*
  1255. * Close the drive. Spin it down if no task is using it. The spin
  1256. * down will fail if playing audio, so audio play is OK.
  1257. */
  1258. static int
  1259. cdu_release(struct inode *inode,
  1260. struct file *filp)
  1261. {
  1262. Byte status[2], cmd_no;
  1263. sony_inuse = 0;
  1264. if (0 < sony_usage) {
  1265. sony_usage--;
  1266. }
  1267. if (sony_usage == 0) {
  1268. check_drive_status();
  1269. if (sony_audio_status != CDROM_AUDIO_PLAY) {
  1270. cmd_no = SONY535_SPIN_DOWN;
  1271. do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
  1272. }
  1273. #ifdef LOCK_DOORS
  1274. /* enable the eject button after umount */
  1275. cmd_no = SONY535_ENABLE_EJECT_BUTTON;
  1276. do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
  1277. #endif
  1278. }
  1279. return 0;
  1280. }
  1281. static struct block_device_operations cdu_fops =
  1282. {
  1283. .owner = THIS_MODULE,
  1284. .open = cdu_open,
  1285. .release = cdu_release,
  1286. .ioctl = cdu_ioctl,
  1287. .media_changed = cdu535_check_media_change,
  1288. };
  1289. static struct gendisk *cdu_disk;
  1290. /*
  1291. * Initialize the driver.
  1292. */
  1293. static int __init sony535_init(void)
  1294. {
  1295. struct s535_sony_drive_config drive_config;
  1296. Byte cmd_buff[3];
  1297. Byte ret_buff[2];
  1298. Byte status[2];
  1299. unsigned long snap;
  1300. int got_result = 0;
  1301. int tmp_irq;
  1302. int i;
  1303. int err;
  1304. /* Setting the base I/O address to 0 will disable it. */
  1305. if ((sony535_cd_base_io == 0xffff)||(sony535_cd_base_io == 0))
  1306. return 0;
  1307. /* Set up all the register locations */
  1308. result_reg = sony535_cd_base_io;
  1309. command_reg = sony535_cd_base_io;
  1310. data_reg = sony535_cd_base_io + 1;
  1311. read_status_reg = sony535_cd_base_io + 2;
  1312. select_unit_reg = sony535_cd_base_io + 3;
  1313. #ifndef USE_IRQ
  1314. sony535_irq_used = 0; /* polling only until this is ready... */
  1315. #endif
  1316. /* we need to poll until things get initialized */
  1317. tmp_irq = sony535_irq_used;
  1318. sony535_irq_used = 0;
  1319. #if DEBUG > 0
  1320. printk(KERN_INFO CDU535_MESSAGE_NAME ": probing base address %03X\n",
  1321. sony535_cd_base_io);
  1322. #endif
  1323. /* look for the CD-ROM, follows the procedure in the DOS driver */
  1324. inb(select_unit_reg);
  1325. /* wait for 40 18 Hz ticks (reverse-engineered from DOS driver) */
  1326. schedule_timeout_interruptible((HZ+17)*40/18);
  1327. inb(result_reg);
  1328. outb(0, read_status_reg); /* does a reset? */
  1329. snap = jiffies;
  1330. while (jiffies-snap < SONY_JIFFIES_TIMEOUT) {
  1331. select_unit(0);
  1332. if (inb(result_reg) != 0xff) {
  1333. got_result = 1;
  1334. break;
  1335. }
  1336. sony_sleep();
  1337. }
  1338. if (!got_result || check_drive_status() == TIME_OUT)
  1339. goto Enodev;
  1340. /* CD-ROM drive responded -- get the drive configuration */
  1341. cmd_buff[0] = SONY535_INQUIRY;
  1342. if (do_sony_cmd(cmd_buff, 1, status, (Byte *)&drive_config, 28, 1) != 0)
  1343. goto Enodev;
  1344. /* was able to get the configuration,
  1345. * set drive mode as rest of init
  1346. */
  1347. #if DEBUG > 0
  1348. /* 0x50 == CADDY_NOT_INSERTED | NOT_SPINNING */
  1349. if ( (status[0] & 0x7f) != 0 && (status[0] & 0x7f) != 0x50 )
  1350. printk(CDU535_MESSAGE_NAME
  1351. "Inquiry command returned status = 0x%x\n", status[0]);
  1352. #endif
  1353. /* now ready to use interrupts, if available */
  1354. sony535_irq_used = tmp_irq;
  1355. /* A negative sony535_irq_used will attempt an autoirq. */
  1356. if (sony535_irq_used < 0) {
  1357. unsigned long irq_mask, delay;
  1358. irq_mask = probe_irq_on();
  1359. enable_interrupts();
  1360. outb(0, read_status_reg); /* does a reset? */
  1361. delay = jiffies + HZ/10;
  1362. while (time_before(jiffies, delay)) ;
  1363. sony535_irq_used = probe_irq_off(irq_mask);
  1364. disable_interrupts();
  1365. }
  1366. if (sony535_irq_used > 0) {
  1367. if (request_irq(sony535_irq_used, cdu535_interrupt,
  1368. SA_INTERRUPT, CDU535_HANDLE, NULL)) {
  1369. printk("Unable to grab IRQ%d for the " CDU535_MESSAGE_NAME
  1370. " driver; polling instead.\n", sony535_irq_used);
  1371. sony535_irq_used = 0;
  1372. }
  1373. }
  1374. cmd_buff[0] = SONY535_SET_DRIVE_MODE;
  1375. cmd_buff[1] = 0x0; /* default audio */
  1376. if (do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1) != 0)
  1377. goto Enodev_irq;
  1378. /* set the drive mode successful, we are set! */
  1379. sony_buffer_size = SONY535_BUFFER_SIZE;
  1380. sony_buffer_sectors = sony_buffer_size / CDU535_BLOCK_SIZE;
  1381. printk(KERN_INFO CDU535_MESSAGE_NAME " I/F CDROM : %8.8s %16.16s %4.4s",
  1382. drive_config.vendor_id,
  1383. drive_config.product_id,
  1384. drive_config.product_rev_level);
  1385. printk(" base address %03X, ", sony535_cd_base_io);
  1386. if (tmp_irq > 0)
  1387. printk("IRQ%d, ", tmp_irq);
  1388. printk("using %d byte buffer\n", sony_buffer_size);
  1389. if (register_blkdev(MAJOR_NR, CDU535_HANDLE)) {
  1390. err = -EIO;
  1391. goto out1;
  1392. }
  1393. sonycd535_queue = blk_init_queue(do_cdu535_request, &sonycd535_lock);
  1394. if (!sonycd535_queue) {
  1395. err = -ENOMEM;
  1396. goto out1a;
  1397. }
  1398. blk_queue_hardsect_size(sonycd535_queue, CDU535_BLOCK_SIZE);
  1399. sony_toc = kmalloc(sizeof(struct s535_sony_toc), GFP_KERNEL);
  1400. err = -ENOMEM;
  1401. if (!sony_toc)
  1402. goto out2;
  1403. last_sony_subcode = kmalloc(sizeof(struct s535_sony_subcode), GFP_KERNEL);
  1404. if (!last_sony_subcode)
  1405. goto out3;
  1406. sony_buffer = kmalloc(sizeof(Byte *) * sony_buffer_sectors, GFP_KERNEL);
  1407. if (!sony_buffer)
  1408. goto out4;
  1409. for (i = 0; i < sony_buffer_sectors; i++) {
  1410. sony_buffer[i] = kmalloc(CDU535_BLOCK_SIZE, GFP_KERNEL);
  1411. if (!sony_buffer[i]) {
  1412. while (--i>=0)
  1413. kfree(sony_buffer[i]);
  1414. goto out5;
  1415. }
  1416. }
  1417. initialized = 1;
  1418. cdu_disk = alloc_disk(1);
  1419. if (!cdu_disk)
  1420. goto out6;
  1421. cdu_disk->major = MAJOR_NR;
  1422. cdu_disk->first_minor = 0;
  1423. cdu_disk->fops = &cdu_fops;
  1424. sprintf(cdu_disk->disk_name, "cdu");
  1425. sprintf(cdu_disk->devfs_name, "cdu535");
  1426. if (!request_region(sony535_cd_base_io, 4, CDU535_HANDLE)) {
  1427. printk(KERN_WARNING"sonycd535: Unable to request region 0x%x\n",
  1428. sony535_cd_base_io);
  1429. goto out7;
  1430. }
  1431. cdu_disk->queue = sonycd535_queue;
  1432. add_disk(cdu_disk);
  1433. return 0;
  1434. out7:
  1435. put_disk(cdu_disk);
  1436. out6:
  1437. for (i = 0; i < sony_buffer_sectors; i++)
  1438. kfree(sony_buffer[i]);
  1439. out5:
  1440. kfree(sony_buffer);
  1441. out4:
  1442. kfree(last_sony_subcode);
  1443. out3:
  1444. kfree(sony_toc);
  1445. out2:
  1446. blk_cleanup_queue(sonycd535_queue);
  1447. out1a:
  1448. unregister_blkdev(MAJOR_NR, CDU535_HANDLE);
  1449. out1:
  1450. if (sony535_irq_used)
  1451. free_irq(sony535_irq_used, NULL);
  1452. return err;
  1453. Enodev_irq:
  1454. if (sony535_irq_used)
  1455. free_irq(sony535_irq_used, NULL);
  1456. Enodev:
  1457. printk("Did not find a " CDU535_MESSAGE_NAME " drive\n");
  1458. return -EIO;
  1459. }
  1460. #ifndef MODULE
  1461. /*
  1462. * accept "kernel command line" parameters
  1463. * (added by emoenke@gwdg.de)
  1464. *
  1465. * use: tell LILO:
  1466. * sonycd535=0x320
  1467. *
  1468. * the address value has to be the existing CDROM port address.
  1469. */
  1470. static int __init
  1471. sonycd535_setup(char *strings)
  1472. {
  1473. int ints[3];
  1474. (void)get_options(strings, ARRAY_SIZE(ints), ints);
  1475. /* if IRQ change and default io base desired,
  1476. * then call with io base of 0
  1477. */
  1478. if (ints[0] > 0)
  1479. if (ints[1] != 0)
  1480. sony535_cd_base_io = ints[1];
  1481. if (ints[0] > 1)
  1482. sony535_irq_used = ints[2];
  1483. if ((strings != NULL) && (*strings != '\0'))
  1484. printk(CDU535_MESSAGE_NAME
  1485. ": Warning: Unknown interface type: %s\n", strings);
  1486. return 1;
  1487. }
  1488. __setup("sonycd535=", sonycd535_setup);
  1489. #endif /* MODULE */
  1490. static void __exit
  1491. sony535_exit(void)
  1492. {
  1493. int i;
  1494. release_region(sony535_cd_base_io, 4);
  1495. for (i = 0; i < sony_buffer_sectors; i++)
  1496. kfree(sony_buffer[i]);
  1497. kfree(sony_buffer);
  1498. kfree(last_sony_subcode);
  1499. kfree(sony_toc);
  1500. del_gendisk(cdu_disk);
  1501. put_disk(cdu_disk);
  1502. blk_cleanup_queue(sonycd535_queue);
  1503. if (unregister_blkdev(MAJOR_NR, CDU535_HANDLE) == -EINVAL)
  1504. printk("Uh oh, couldn't unregister " CDU535_HANDLE "\n");
  1505. else
  1506. printk(KERN_INFO CDU535_HANDLE " module released\n");
  1507. }
  1508. module_init(sony535_init);
  1509. module_exit(sony535_exit);
  1510. MODULE_LICENSE("GPL");
  1511. MODULE_ALIAS_BLOCKDEV_MAJOR(CDU535_CDROM_MAJOR);