optcd.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /* linux/drivers/cdrom/optcd.c - Optics Storage 8000 AT CDROM driver
  2. $Id: optcd.c,v 1.11 1997/01/26 07:13:00 davem Exp $
  3. Copyright (C) 1995 Leo Spiekman (spiekman@dutette.et.tudelft.nl)
  4. Based on Aztech CD268 CDROM driver by Werner Zimmermann and preworks
  5. by Eberhard Moenkeberg (emoenke@gwdg.de).
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Revision history
  19. 14-5-95 v0.0 Plays sound tracks. No reading of data CDs yet.
  20. Detection of disk change doesn't work.
  21. 21-5-95 v0.1 First ALPHA version. CD can be mounted. The
  22. device major nr is borrowed from the Aztech
  23. driver. Speed is around 240 kb/s, as measured
  24. with "time dd if=/dev/cdrom of=/dev/null \
  25. bs=2048 count=4096".
  26. 24-6-95 v0.2 Reworked the #defines for the command codes
  27. and the like, as well as the structure of
  28. the hardware communication protocol, to
  29. reflect the "official" documentation, kindly
  30. supplied by C.K. Tan, Optics Storage Pte. Ltd.
  31. Also tidied up the state machine somewhat.
  32. 28-6-95 v0.3 Removed the ISP-16 interface code, as this
  33. should go into its own driver. The driver now
  34. has its own major nr.
  35. Disk change detection now seems to work, too.
  36. This version became part of the standard
  37. kernel as of version 1.3.7
  38. 24-9-95 v0.4 Re-inserted ISP-16 interface code which I
  39. copied from sjcd.c, with a few changes.
  40. Updated README.optcd. Submitted for
  41. inclusion in 1.3.21
  42. 29-9-95 v0.4a Fixed bug that prevented compilation as module
  43. 25-10-95 v0.5 Started multisession code. Implementation
  44. copied from Werner Zimmermann, who copied it
  45. from Heiko Schlittermann's mcdx.
  46. 17-1-96 v0.6 Multisession works; some cleanup too.
  47. 18-4-96 v0.7 Increased some timing constants;
  48. thanks to Luke McFarlane. Also tidied up some
  49. printk behaviour. ISP16 initialization
  50. is now handled by a separate driver.
  51. 09-11-99 Make kernel-parameter implementation work with 2.3.x
  52. Removed init_module & cleanup_module in favor of
  53. module_init & module_exit.
  54. Torben Mathiasen <tmm@image.dk>
  55. */
  56. /* Includes */
  57. #include <linux/module.h>
  58. #include <linux/mm.h>
  59. #include <linux/ioport.h>
  60. #include <linux/init.h>
  61. #include <asm/io.h>
  62. #include <linux/blkdev.h>
  63. #include <linux/cdrom.h>
  64. #include "optcd.h"
  65. #include <asm/uaccess.h>
  66. #define MAJOR_NR OPTICS_CDROM_MAJOR
  67. #define QUEUE (opt_queue)
  68. #define CURRENT elv_next_request(opt_queue)
  69. /* Debug support */
  70. /* Don't forget to add new debug flags here. */
  71. #if DEBUG_DRIVE_IF | DEBUG_VFS | DEBUG_CONV | DEBUG_TOC | \
  72. DEBUG_BUFFERS | DEBUG_REQUEST | DEBUG_STATE | DEBUG_MULTIS
  73. #define DEBUG(x) debug x
  74. static void debug(int debug_this, const char* fmt, ...)
  75. {
  76. char s[1024];
  77. va_list args;
  78. if (!debug_this)
  79. return;
  80. va_start(args, fmt);
  81. vsprintf(s, fmt, args);
  82. printk(KERN_DEBUG "optcd: %s\n", s);
  83. va_end(args);
  84. }
  85. #else
  86. #define DEBUG(x)
  87. #endif
  88. /* Drive hardware/firmware characteristics
  89. Identifiers in accordance with Optics Storage documentation */
  90. #define optcd_port optcd /* Needed for the modutils. */
  91. static short optcd_port = OPTCD_PORTBASE; /* I/O base of drive. */
  92. module_param(optcd_port, short, 0);
  93. /* Drive registers, read */
  94. #define DATA_PORT optcd_port /* Read data/status */
  95. #define STATUS_PORT optcd_port+1 /* Indicate data/status availability */
  96. /* Drive registers, write */
  97. #define COMIN_PORT optcd_port /* For passing command/parameter */
  98. #define RESET_PORT optcd_port+1 /* Write anything and wait 0.5 sec */
  99. #define HCON_PORT optcd_port+2 /* Host Xfer Configuration */
  100. /* Command completion/status read from DATA register */
  101. #define ST_DRVERR 0x80
  102. #define ST_DOOR_OPEN 0x40
  103. #define ST_MIXEDMODE_DISK 0x20
  104. #define ST_MODE_BITS 0x1c
  105. #define ST_M_STOP 0x00
  106. #define ST_M_READ 0x04
  107. #define ST_M_AUDIO 0x04
  108. #define ST_M_PAUSE 0x08
  109. #define ST_M_INITIAL 0x0c
  110. #define ST_M_ERROR 0x10
  111. #define ST_M_OTHERS 0x14
  112. #define ST_MODE2TRACK 0x02
  113. #define ST_DSK_CHG 0x01
  114. #define ST_L_LOCK 0x01
  115. #define ST_CMD_OK 0x00
  116. #define ST_OP_OK 0x01
  117. #define ST_PA_OK 0x02
  118. #define ST_OP_ERROR 0x05
  119. #define ST_PA_ERROR 0x06
  120. /* Error codes (appear as command completion code from DATA register) */
  121. /* Player related errors */
  122. #define ERR_ILLCMD 0x11 /* Illegal command to player module */
  123. #define ERR_ILLPARM 0x12 /* Illegal parameter to player module */
  124. #define ERR_SLEDGE 0x13
  125. #define ERR_FOCUS 0x14
  126. #define ERR_MOTOR 0x15
  127. #define ERR_RADIAL 0x16
  128. #define ERR_PLL 0x17 /* PLL lock error */
  129. #define ERR_SUB_TIM 0x18 /* Subcode timeout error */
  130. #define ERR_SUB_NF 0x19 /* Subcode not found error */
  131. #define ERR_TRAY 0x1a
  132. #define ERR_TOC 0x1b /* Table of Contents read error */
  133. #define ERR_JUMP 0x1c
  134. /* Data errors */
  135. #define ERR_MODE 0x21
  136. #define ERR_FORM 0x22
  137. #define ERR_HEADADDR 0x23 /* Header Address not found */
  138. #define ERR_CRC 0x24
  139. #define ERR_ECC 0x25 /* Uncorrectable ECC error */
  140. #define ERR_CRC_UNC 0x26 /* CRC error and uncorrectable error */
  141. #define ERR_ILLBSYNC 0x27 /* Illegal block sync error */
  142. #define ERR_VDST 0x28 /* VDST not found */
  143. /* Timeout errors */
  144. #define ERR_READ_TIM 0x31 /* Read timeout error */
  145. #define ERR_DEC_STP 0x32 /* Decoder stopped */
  146. #define ERR_DEC_TIM 0x33 /* Decoder interrupt timeout error */
  147. /* Function abort codes */
  148. #define ERR_KEY 0x41 /* Key -Detected abort */
  149. #define ERR_READ_FINISH 0x42 /* Read Finish */
  150. /* Second Byte diagnostic codes */
  151. #define ERR_NOBSYNC 0x01 /* No block sync */
  152. #define ERR_SHORTB 0x02 /* Short block */
  153. #define ERR_LONGB 0x03 /* Long block */
  154. #define ERR_SHORTDSP 0x04 /* Short DSP word */
  155. #define ERR_LONGDSP 0x05 /* Long DSP word */
  156. /* Status availability flags read from STATUS register */
  157. #define FL_EJECT 0x20
  158. #define FL_WAIT 0x10 /* active low */
  159. #define FL_EOP 0x08 /* active low */
  160. #define FL_STEN 0x04 /* Status available when low */
  161. #define FL_DTEN 0x02 /* Data available when low */
  162. #define FL_DRQ 0x01 /* active low */
  163. #define FL_RESET 0xde /* These bits are high after a reset */
  164. #define FL_STDT (FL_STEN|FL_DTEN)
  165. /* Transfer mode, written to HCON register */
  166. #define HCON_DTS 0x08
  167. #define HCON_SDRQB 0x04
  168. #define HCON_LOHI 0x02
  169. #define HCON_DMA16 0x01
  170. /* Drive command set, written to COMIN register */
  171. /* Quick response commands */
  172. #define COMDRVST 0x20 /* Drive Status Read */
  173. #define COMERRST 0x21 /* Error Status Read */
  174. #define COMIOCTLISTAT 0x22 /* Status Read; reset disk changed bit */
  175. #define COMINITSINGLE 0x28 /* Initialize Single Speed */
  176. #define COMINITDOUBLE 0x29 /* Initialize Double Speed */
  177. #define COMUNLOCK 0x30 /* Unlock */
  178. #define COMLOCK 0x31 /* Lock */
  179. #define COMLOCKST 0x32 /* Lock/Unlock Status */
  180. #define COMVERSION 0x40 /* Get Firmware Revision */
  181. #define COMVOIDREADMODE 0x50 /* Void Data Read Mode */
  182. /* Read commands */
  183. #define COMFETCH 0x60 /* Prefetch Data */
  184. #define COMREAD 0x61 /* Read */
  185. #define COMREADRAW 0x62 /* Read Raw Data */
  186. #define COMREADALL 0x63 /* Read All 2646 Bytes */
  187. /* Player control commands */
  188. #define COMLEADIN 0x70 /* Seek To Lead-in */
  189. #define COMSEEK 0x71 /* Seek */
  190. #define COMPAUSEON 0x80 /* Pause On */
  191. #define COMPAUSEOFF 0x81 /* Pause Off */
  192. #define COMSTOP 0x82 /* Stop */
  193. #define COMOPEN 0x90 /* Open Tray Door */
  194. #define COMCLOSE 0x91 /* Close Tray Door */
  195. #define COMPLAY 0xa0 /* Audio Play */
  196. #define COMPLAY_TNO 0xa2 /* Audio Play By Track Number */
  197. #define COMSUBQ 0xb0 /* Read Sub-q Code */
  198. #define COMLOCATION 0xb1 /* Read Head Position */
  199. /* Audio control commands */
  200. #define COMCHCTRL 0xc0 /* Audio Channel Control */
  201. /* Miscellaneous (test) commands */
  202. #define COMDRVTEST 0xd0 /* Write Test Bytes */
  203. #define COMTEST 0xd1 /* Diagnostic Test */
  204. /* Low level drive interface. Only here we do actual I/O
  205. Waiting for status / data available */
  206. /* Busy wait until FLAG goes low. Return 0 on timeout. */
  207. static inline int flag_low(int flag, unsigned long timeout)
  208. {
  209. int flag_high;
  210. unsigned long count = 0;
  211. while ((flag_high = (inb(STATUS_PORT) & flag)))
  212. if (++count >= timeout)
  213. break;
  214. DEBUG((DEBUG_DRIVE_IF, "flag_low 0x%x count %ld%s",
  215. flag, count, flag_high ? " timeout" : ""));
  216. return !flag_high;
  217. }
  218. /* Timed waiting for status or data */
  219. static int sleep_timeout; /* max # of ticks to sleep */
  220. static DECLARE_WAIT_QUEUE_HEAD(waitq);
  221. static void sleep_timer(unsigned long data);
  222. static DEFINE_TIMER(delay_timer, sleep_timer, 0, 0);
  223. static DEFINE_SPINLOCK(optcd_lock);
  224. static struct request_queue *opt_queue;
  225. /* Timer routine: wake up when desired flag goes low,
  226. or when timeout expires. */
  227. static void sleep_timer(unsigned long data)
  228. {
  229. int flags = inb(STATUS_PORT) & FL_STDT;
  230. if (flags == FL_STDT && --sleep_timeout > 0) {
  231. mod_timer(&delay_timer, jiffies + HZ/100); /* multi-statement macro */
  232. } else
  233. wake_up(&waitq);
  234. }
  235. /* Sleep until FLAG goes low. Return 0 on timeout or wrong flag low. */
  236. static int sleep_flag_low(int flag, unsigned long timeout)
  237. {
  238. int flag_high;
  239. DEBUG((DEBUG_DRIVE_IF, "sleep_flag_low"));
  240. sleep_timeout = timeout;
  241. flag_high = inb(STATUS_PORT) & flag;
  242. if (flag_high && sleep_timeout > 0) {
  243. mod_timer(&delay_timer, jiffies + HZ/100);
  244. sleep_on(&waitq);
  245. flag_high = inb(STATUS_PORT) & flag;
  246. }
  247. DEBUG((DEBUG_DRIVE_IF, "flag 0x%x count %ld%s",
  248. flag, timeout, flag_high ? " timeout" : ""));
  249. return !flag_high;
  250. }
  251. /* Low level drive interface. Only here we do actual I/O
  252. Sending commands and parameters */
  253. /* Errors in the command protocol */
  254. #define ERR_IF_CMD_TIMEOUT 0x100
  255. #define ERR_IF_ERR_TIMEOUT 0x101
  256. #define ERR_IF_RESP_TIMEOUT 0x102
  257. #define ERR_IF_DATA_TIMEOUT 0x103
  258. #define ERR_IF_NOSTAT 0x104
  259. /* Send command code. Return <0 indicates error */
  260. static int send_cmd(int cmd)
  261. {
  262. unsigned char ack;
  263. DEBUG((DEBUG_DRIVE_IF, "sending command 0x%02x\n", cmd));
  264. outb(HCON_DTS, HCON_PORT); /* Enable Suspend Data Transfer */
  265. outb(cmd, COMIN_PORT); /* Send command code */
  266. if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
  267. return -ERR_IF_CMD_TIMEOUT;
  268. ack = inb(DATA_PORT); /* read command acknowledge */
  269. outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */
  270. return ack==ST_OP_OK ? 0 : -ack;
  271. }
  272. /* Send command parameters. Return <0 indicates error */
  273. static int send_params(struct cdrom_msf *params)
  274. {
  275. unsigned char ack;
  276. DEBUG((DEBUG_DRIVE_IF, "sending parameters"
  277. " %02x:%02x:%02x"
  278. " %02x:%02x:%02x",
  279. params->cdmsf_min0,
  280. params->cdmsf_sec0,
  281. params->cdmsf_frame0,
  282. params->cdmsf_min1,
  283. params->cdmsf_sec1,
  284. params->cdmsf_frame1));
  285. outb(params->cdmsf_min0, COMIN_PORT);
  286. outb(params->cdmsf_sec0, COMIN_PORT);
  287. outb(params->cdmsf_frame0, COMIN_PORT);
  288. outb(params->cdmsf_min1, COMIN_PORT);
  289. outb(params->cdmsf_sec1, COMIN_PORT);
  290. outb(params->cdmsf_frame1, COMIN_PORT);
  291. if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
  292. return -ERR_IF_CMD_TIMEOUT;
  293. ack = inb(DATA_PORT); /* read command acknowledge */
  294. return ack==ST_PA_OK ? 0 : -ack;
  295. }
  296. /* Send parameters for SEEK command. Return <0 indicates error */
  297. static int send_seek_params(struct cdrom_msf *params)
  298. {
  299. unsigned char ack;
  300. DEBUG((DEBUG_DRIVE_IF, "sending seek parameters"
  301. " %02x:%02x:%02x",
  302. params->cdmsf_min0,
  303. params->cdmsf_sec0,
  304. params->cdmsf_frame0));
  305. outb(params->cdmsf_min0, COMIN_PORT);
  306. outb(params->cdmsf_sec0, COMIN_PORT);
  307. outb(params->cdmsf_frame0, COMIN_PORT);
  308. if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
  309. return -ERR_IF_CMD_TIMEOUT;
  310. ack = inb(DATA_PORT); /* read command acknowledge */
  311. return ack==ST_PA_OK ? 0 : -ack;
  312. }
  313. /* Wait for command execution status. Choice between busy waiting
  314. and sleeping. Return value <0 indicates timeout. */
  315. static inline int get_exec_status(int busy_waiting)
  316. {
  317. unsigned char exec_status;
  318. if (busy_waiting
  319. ? !flag_low(FL_STEN, BUSY_TIMEOUT)
  320. : !sleep_flag_low(FL_STEN, SLEEP_TIMEOUT))
  321. return -ERR_IF_CMD_TIMEOUT;
  322. exec_status = inb(DATA_PORT);
  323. DEBUG((DEBUG_DRIVE_IF, "returned exec status 0x%02x", exec_status));
  324. return exec_status;
  325. }
  326. /* Wait busy for extra byte of data that a command returns.
  327. Return value <0 indicates timeout. */
  328. static inline int get_data(int short_timeout)
  329. {
  330. unsigned char data;
  331. if (!flag_low(FL_STEN, short_timeout ? FAST_TIMEOUT : BUSY_TIMEOUT))
  332. return -ERR_IF_DATA_TIMEOUT;
  333. data = inb(DATA_PORT);
  334. DEBUG((DEBUG_DRIVE_IF, "returned data 0x%02x", data));
  335. return data;
  336. }
  337. /* Returns 0 if failed */
  338. static int reset_drive(void)
  339. {
  340. unsigned long count = 0;
  341. int flags;
  342. DEBUG((DEBUG_DRIVE_IF, "reset drive"));
  343. outb(0, RESET_PORT);
  344. while (++count < RESET_WAIT)
  345. inb(DATA_PORT);
  346. count = 0;
  347. while ((flags = (inb(STATUS_PORT) & FL_RESET)) != FL_RESET)
  348. if (++count >= BUSY_TIMEOUT)
  349. break;
  350. DEBUG((DEBUG_DRIVE_IF, "reset %s",
  351. flags == FL_RESET ? "succeeded" : "failed"));
  352. if (flags != FL_RESET)
  353. return 0; /* Reset failed */
  354. outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */
  355. return 1; /* Reset succeeded */
  356. }
  357. /* Facilities for asynchronous operation */
  358. /* Read status/data availability flags FL_STEN and FL_DTEN */
  359. static inline int stdt_flags(void)
  360. {
  361. return inb(STATUS_PORT) & FL_STDT;
  362. }
  363. /* Fetch status that has previously been waited for. <0 means not available */
  364. static inline int fetch_status(void)
  365. {
  366. unsigned char status;
  367. if (inb(STATUS_PORT) & FL_STEN)
  368. return -ERR_IF_NOSTAT;
  369. status = inb(DATA_PORT);
  370. DEBUG((DEBUG_DRIVE_IF, "fetched exec status 0x%02x", status));
  371. return status;
  372. }
  373. /* Fetch data that has previously been waited for. */
  374. static inline void fetch_data(char *buf, int n)
  375. {
  376. insb(DATA_PORT, buf, n);
  377. DEBUG((DEBUG_DRIVE_IF, "fetched 0x%x bytes", n));
  378. }
  379. /* Flush status and data fifos */
  380. static inline void flush_data(void)
  381. {
  382. while ((inb(STATUS_PORT) & FL_STDT) != FL_STDT)
  383. inb(DATA_PORT);
  384. DEBUG((DEBUG_DRIVE_IF, "flushed fifos"));
  385. }
  386. /* Command protocol */
  387. /* Send a simple command and wait for response. Command codes < COMFETCH
  388. are quick response commands */
  389. static inline int exec_cmd(int cmd)
  390. {
  391. int ack = send_cmd(cmd);
  392. if (ack < 0)
  393. return ack;
  394. return get_exec_status(cmd < COMFETCH);
  395. }
  396. /* Send a command with parameters. Don't wait for the response,
  397. * which consists of data blocks read from the CD. */
  398. static inline int exec_read_cmd(int cmd, struct cdrom_msf *params)
  399. {
  400. int ack = send_cmd(cmd);
  401. if (ack < 0)
  402. return ack;
  403. return send_params(params);
  404. }
  405. /* Send a seek command with parameters and wait for response */
  406. static inline int exec_seek_cmd(int cmd, struct cdrom_msf *params)
  407. {
  408. int ack = send_cmd(cmd);
  409. if (ack < 0)
  410. return ack;
  411. ack = send_seek_params(params);
  412. if (ack < 0)
  413. return ack;
  414. return 0;
  415. }
  416. /* Send a command with parameters and wait for response */
  417. static inline int exec_long_cmd(int cmd, struct cdrom_msf *params)
  418. {
  419. int ack = exec_read_cmd(cmd, params);
  420. if (ack < 0)
  421. return ack;
  422. return get_exec_status(0);
  423. }
  424. /* Address conversion routines */
  425. /* Binary to BCD (2 digits) */
  426. static inline void single_bin2bcd(u_char *p)
  427. {
  428. DEBUG((DEBUG_CONV, "bin2bcd %02d", *p));
  429. *p = (*p % 10) | ((*p / 10) << 4);
  430. }
  431. /* Convert entire msf struct */
  432. static void bin2bcd(struct cdrom_msf *msf)
  433. {
  434. single_bin2bcd(&msf->cdmsf_min0);
  435. single_bin2bcd(&msf->cdmsf_sec0);
  436. single_bin2bcd(&msf->cdmsf_frame0);
  437. single_bin2bcd(&msf->cdmsf_min1);
  438. single_bin2bcd(&msf->cdmsf_sec1);
  439. single_bin2bcd(&msf->cdmsf_frame1);
  440. }
  441. /* Linear block address to minute, second, frame form */
  442. #define CD_FPM (CD_SECS * CD_FRAMES) /* frames per minute */
  443. static void lba2msf(int lba, struct cdrom_msf *msf)
  444. {
  445. DEBUG((DEBUG_CONV, "lba2msf %d", lba));
  446. lba += CD_MSF_OFFSET;
  447. msf->cdmsf_min0 = lba / CD_FPM; lba %= CD_FPM;
  448. msf->cdmsf_sec0 = lba / CD_FRAMES;
  449. msf->cdmsf_frame0 = lba % CD_FRAMES;
  450. msf->cdmsf_min1 = 0;
  451. msf->cdmsf_sec1 = 0;
  452. msf->cdmsf_frame1 = 0;
  453. bin2bcd(msf);
  454. }
  455. /* Two BCD digits to binary */
  456. static inline u_char bcd2bin(u_char bcd)
  457. {
  458. DEBUG((DEBUG_CONV, "bcd2bin %x%02x", bcd));
  459. return (bcd >> 4) * 10 + (bcd & 0x0f);
  460. }
  461. static void msf2lba(union cdrom_addr *addr)
  462. {
  463. addr->lba = addr->msf.minute * CD_FPM
  464. + addr->msf.second * CD_FRAMES
  465. + addr->msf.frame - CD_MSF_OFFSET;
  466. }
  467. /* Minute, second, frame address BCD to binary or to linear address,
  468. depending on MODE */
  469. static void msf_bcd2bin(union cdrom_addr *addr)
  470. {
  471. addr->msf.minute = bcd2bin(addr->msf.minute);
  472. addr->msf.second = bcd2bin(addr->msf.second);
  473. addr->msf.frame = bcd2bin(addr->msf.frame);
  474. }
  475. /* High level drive commands */
  476. static int audio_status = CDROM_AUDIO_NO_STATUS;
  477. static char toc_uptodate = 0;
  478. static char disk_changed = 1;
  479. /* Get drive status, flagging completion of audio play and disk changes. */
  480. static int drive_status(void)
  481. {
  482. int status;
  483. status = exec_cmd(COMIOCTLISTAT);
  484. DEBUG((DEBUG_DRIVE_IF, "IOCTLISTAT: %03x", status));
  485. if (status < 0)
  486. return status;
  487. if (status == 0xff) /* No status available */
  488. return -ERR_IF_NOSTAT;
  489. if (((status & ST_MODE_BITS) != ST_M_AUDIO) &&
  490. (audio_status == CDROM_AUDIO_PLAY)) {
  491. audio_status = CDROM_AUDIO_COMPLETED;
  492. }
  493. if (status & ST_DSK_CHG) {
  494. toc_uptodate = 0;
  495. disk_changed = 1;
  496. audio_status = CDROM_AUDIO_NO_STATUS;
  497. }
  498. return status;
  499. }
  500. /* Read the current Q-channel info. Also used for reading the
  501. table of contents. qp->cdsc_format must be set on entry to
  502. indicate the desired address format */
  503. static int get_q_channel(struct cdrom_subchnl *qp)
  504. {
  505. int status, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
  506. status = drive_status();
  507. if (status < 0)
  508. return status;
  509. qp->cdsc_audiostatus = audio_status;
  510. status = exec_cmd(COMSUBQ);
  511. if (status < 0)
  512. return status;
  513. d1 = get_data(0);
  514. if (d1 < 0)
  515. return d1;
  516. qp->cdsc_adr = d1;
  517. qp->cdsc_ctrl = d1 >> 4;
  518. d2 = get_data(0);
  519. if (d2 < 0)
  520. return d2;
  521. qp->cdsc_trk = bcd2bin(d2);
  522. d3 = get_data(0);
  523. if (d3 < 0)
  524. return d3;
  525. qp->cdsc_ind = bcd2bin(d3);
  526. d4 = get_data(0);
  527. if (d4 < 0)
  528. return d4;
  529. qp->cdsc_reladdr.msf.minute = d4;
  530. d5 = get_data(0);
  531. if (d5 < 0)
  532. return d5;
  533. qp->cdsc_reladdr.msf.second = d5;
  534. d6 = get_data(0);
  535. if (d6 < 0)
  536. return d6;
  537. qp->cdsc_reladdr.msf.frame = d6;
  538. d7 = get_data(0);
  539. if (d7 < 0)
  540. return d7;
  541. /* byte not used */
  542. d8 = get_data(0);
  543. if (d8 < 0)
  544. return d8;
  545. qp->cdsc_absaddr.msf.minute = d8;
  546. d9 = get_data(0);
  547. if (d9 < 0)
  548. return d9;
  549. qp->cdsc_absaddr.msf.second = d9;
  550. d10 = get_data(0);
  551. if (d10 < 0)
  552. return d10;
  553. qp->cdsc_absaddr.msf.frame = d10;
  554. DEBUG((DEBUG_TOC, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
  555. d1, d2, d3, d4, d5, d6, d7, d8, d9, d10));
  556. msf_bcd2bin(&qp->cdsc_absaddr);
  557. msf_bcd2bin(&qp->cdsc_reladdr);
  558. if (qp->cdsc_format == CDROM_LBA) {
  559. msf2lba(&qp->cdsc_absaddr);
  560. msf2lba(&qp->cdsc_reladdr);
  561. }
  562. return 0;
  563. }
  564. /* Table of contents handling */
  565. /* Errors in table of contents */
  566. #define ERR_TOC_MISSINGINFO 0x120
  567. #define ERR_TOC_MISSINGENTRY 0x121
  568. struct cdrom_disk_info {
  569. unsigned char first;
  570. unsigned char last;
  571. struct cdrom_msf0 disk_length;
  572. struct cdrom_msf0 first_track;
  573. /* Multisession info: */
  574. unsigned char next;
  575. struct cdrom_msf0 next_session;
  576. struct cdrom_msf0 last_session;
  577. unsigned char multi;
  578. unsigned char xa;
  579. unsigned char audio;
  580. };
  581. static struct cdrom_disk_info disk_info;
  582. #define MAX_TRACKS 111
  583. static struct cdrom_subchnl toc[MAX_TRACKS];
  584. #define QINFO_FIRSTTRACK 100 /* bcd2bin(0xa0) */
  585. #define QINFO_LASTTRACK 101 /* bcd2bin(0xa1) */
  586. #define QINFO_DISKLENGTH 102 /* bcd2bin(0xa2) */
  587. #define QINFO_NEXTSESSION 110 /* bcd2bin(0xb0) */
  588. #define I_FIRSTTRACK 0x01
  589. #define I_LASTTRACK 0x02
  590. #define I_DISKLENGTH 0x04
  591. #define I_NEXTSESSION 0x08
  592. #define I_ALL (I_FIRSTTRACK | I_LASTTRACK | I_DISKLENGTH)
  593. #if DEBUG_TOC
  594. static void toc_debug_info(int i)
  595. {
  596. printk(KERN_DEBUG "#%3d ctl %1x, adr %1x, track %2d index %3d"
  597. " %2d:%02d.%02d %2d:%02d.%02d\n",
  598. i, toc[i].cdsc_ctrl, toc[i].cdsc_adr,
  599. toc[i].cdsc_trk, toc[i].cdsc_ind,
  600. toc[i].cdsc_reladdr.msf.minute,
  601. toc[i].cdsc_reladdr.msf.second,
  602. toc[i].cdsc_reladdr.msf.frame,
  603. toc[i].cdsc_absaddr.msf.minute,
  604. toc[i].cdsc_absaddr.msf.second,
  605. toc[i].cdsc_absaddr.msf.frame);
  606. }
  607. #endif
  608. static int read_toc(void)
  609. {
  610. int status, limit, count;
  611. unsigned char got_info = 0;
  612. struct cdrom_subchnl q_info;
  613. #if DEBUG_TOC
  614. int i;
  615. #endif
  616. DEBUG((DEBUG_TOC, "starting read_toc"));
  617. count = 0;
  618. for (limit = 60; limit > 0; limit--) {
  619. int index;
  620. q_info.cdsc_format = CDROM_MSF;
  621. status = get_q_channel(&q_info);
  622. if (status < 0)
  623. return status;
  624. index = q_info.cdsc_ind;
  625. if (index > 0 && index < MAX_TRACKS
  626. && q_info.cdsc_trk == 0 && toc[index].cdsc_ind == 0) {
  627. toc[index] = q_info;
  628. DEBUG((DEBUG_TOC, "got %d", index));
  629. if (index < 100)
  630. count++;
  631. switch (q_info.cdsc_ind) {
  632. case QINFO_FIRSTTRACK:
  633. got_info |= I_FIRSTTRACK;
  634. break;
  635. case QINFO_LASTTRACK:
  636. got_info |= I_LASTTRACK;
  637. break;
  638. case QINFO_DISKLENGTH:
  639. got_info |= I_DISKLENGTH;
  640. break;
  641. case QINFO_NEXTSESSION:
  642. got_info |= I_NEXTSESSION;
  643. break;
  644. }
  645. }
  646. if ((got_info & I_ALL) == I_ALL
  647. && toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute + count
  648. >= toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute + 1)
  649. break;
  650. }
  651. /* Construct disk_info from TOC */
  652. if (disk_info.first == 0) {
  653. disk_info.first = toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute;
  654. disk_info.first_track.minute =
  655. toc[disk_info.first].cdsc_absaddr.msf.minute;
  656. disk_info.first_track.second =
  657. toc[disk_info.first].cdsc_absaddr.msf.second;
  658. disk_info.first_track.frame =
  659. toc[disk_info.first].cdsc_absaddr.msf.frame;
  660. }
  661. disk_info.last = toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute;
  662. disk_info.disk_length.minute =
  663. toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.minute;
  664. disk_info.disk_length.second =
  665. toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.second-2;
  666. disk_info.disk_length.frame =
  667. toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.frame;
  668. disk_info.next_session.minute =
  669. toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.minute;
  670. disk_info.next_session.second =
  671. toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.second;
  672. disk_info.next_session.frame =
  673. toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.frame;
  674. disk_info.next = toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute;
  675. disk_info.last_session.minute =
  676. toc[disk_info.next].cdsc_absaddr.msf.minute;
  677. disk_info.last_session.second =
  678. toc[disk_info.next].cdsc_absaddr.msf.second;
  679. disk_info.last_session.frame =
  680. toc[disk_info.next].cdsc_absaddr.msf.frame;
  681. toc[disk_info.last + 1].cdsc_absaddr.msf.minute =
  682. disk_info.disk_length.minute;
  683. toc[disk_info.last + 1].cdsc_absaddr.msf.second =
  684. disk_info.disk_length.second;
  685. toc[disk_info.last + 1].cdsc_absaddr.msf.frame =
  686. disk_info.disk_length.frame;
  687. #if DEBUG_TOC
  688. for (i = 1; i <= disk_info.last + 1; i++)
  689. toc_debug_info(i);
  690. toc_debug_info(QINFO_FIRSTTRACK);
  691. toc_debug_info(QINFO_LASTTRACK);
  692. toc_debug_info(QINFO_DISKLENGTH);
  693. toc_debug_info(QINFO_NEXTSESSION);
  694. #endif
  695. DEBUG((DEBUG_TOC, "exiting read_toc, got_info %x, count %d",
  696. got_info, count));
  697. if ((got_info & I_ALL) != I_ALL
  698. || toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute + count
  699. < toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute + 1)
  700. return -ERR_TOC_MISSINGINFO;
  701. return 0;
  702. }
  703. #ifdef MULTISESSION
  704. static int get_multi_disk_info(void)
  705. {
  706. int sessions, status;
  707. struct cdrom_msf multi_index;
  708. for (sessions = 2; sessions < 10 /* %%for now */; sessions++) {
  709. int count;
  710. for (count = 100; count < MAX_TRACKS; count++)
  711. toc[count].cdsc_ind = 0;
  712. multi_index.cdmsf_min0 = disk_info.next_session.minute;
  713. multi_index.cdmsf_sec0 = disk_info.next_session.second;
  714. multi_index.cdmsf_frame0 = disk_info.next_session.frame;
  715. if (multi_index.cdmsf_sec0 >= 20)
  716. multi_index.cdmsf_sec0 -= 20;
  717. else {
  718. multi_index.cdmsf_sec0 += 40;
  719. multi_index.cdmsf_min0--;
  720. }
  721. DEBUG((DEBUG_MULTIS, "Try %d: %2d:%02d.%02d", sessions,
  722. multi_index.cdmsf_min0,
  723. multi_index.cdmsf_sec0,
  724. multi_index.cdmsf_frame0));
  725. bin2bcd(&multi_index);
  726. multi_index.cdmsf_min1 = 0;
  727. multi_index.cdmsf_sec1 = 0;
  728. multi_index.cdmsf_frame1 = 1;
  729. status = exec_read_cmd(COMREAD, &multi_index);
  730. if (status < 0) {
  731. DEBUG((DEBUG_TOC, "exec_read_cmd COMREAD: %02x",
  732. -status));
  733. break;
  734. }
  735. status = sleep_flag_low(FL_DTEN, MULTI_SEEK_TIMEOUT) ?
  736. 0 : -ERR_TOC_MISSINGINFO;
  737. flush_data();
  738. if (status < 0) {
  739. DEBUG((DEBUG_TOC, "sleep_flag_low: %02x", -status));
  740. break;
  741. }
  742. status = read_toc();
  743. if (status < 0) {
  744. DEBUG((DEBUG_TOC, "read_toc: %02x", -status));
  745. break;
  746. }
  747. disk_info.multi = 1;
  748. }
  749. exec_cmd(COMSTOP);
  750. if (status < 0)
  751. return -EIO;
  752. return 0;
  753. }
  754. #endif /* MULTISESSION */
  755. static int update_toc(void)
  756. {
  757. int status, count;
  758. if (toc_uptodate)
  759. return 0;
  760. DEBUG((DEBUG_TOC, "starting update_toc"));
  761. disk_info.first = 0;
  762. for (count = 0; count < MAX_TRACKS; count++)
  763. toc[count].cdsc_ind = 0;
  764. status = exec_cmd(COMLEADIN);
  765. if (status < 0)
  766. return -EIO;
  767. status = read_toc();
  768. if (status < 0) {
  769. DEBUG((DEBUG_TOC, "read_toc: %02x", -status));
  770. return -EIO;
  771. }
  772. /* Audio disk detection. Look at first track. */
  773. disk_info.audio =
  774. (toc[disk_info.first].cdsc_ctrl & CDROM_DATA_TRACK) ? 0 : 1;
  775. /* XA detection */
  776. disk_info.xa = drive_status() & ST_MODE2TRACK;
  777. /* Multisession detection: if we want this, define MULTISESSION */
  778. disk_info.multi = 0;
  779. #ifdef MULTISESSION
  780. if (disk_info.xa)
  781. get_multi_disk_info(); /* Here disk_info.multi is set */
  782. #endif /* MULTISESSION */
  783. if (disk_info.multi)
  784. printk(KERN_WARNING "optcd: Multisession support experimental, "
  785. "see Documentation/cdrom/optcd\n");
  786. DEBUG((DEBUG_TOC, "exiting update_toc"));
  787. toc_uptodate = 1;
  788. return 0;
  789. }
  790. /* Request handling */
  791. static int current_valid(void)
  792. {
  793. return CURRENT &&
  794. CURRENT->cmd == READ &&
  795. CURRENT->sector != -1;
  796. }
  797. /* Buffers for block size conversion. */
  798. #define NOBUF -1
  799. static char buf[CD_FRAMESIZE * N_BUFS];
  800. static volatile int buf_bn[N_BUFS], next_bn;
  801. static volatile int buf_in = 0, buf_out = NOBUF;
  802. static inline void opt_invalidate_buffers(void)
  803. {
  804. int i;
  805. DEBUG((DEBUG_BUFFERS, "executing opt_invalidate_buffers"));
  806. for (i = 0; i < N_BUFS; i++)
  807. buf_bn[i] = NOBUF;
  808. buf_out = NOBUF;
  809. }
  810. /* Take care of the different block sizes between cdrom and Linux.
  811. When Linux gets variable block sizes this will probably go away. */
  812. static void transfer(void)
  813. {
  814. #if DEBUG_BUFFERS | DEBUG_REQUEST
  815. printk(KERN_DEBUG "optcd: executing transfer\n");
  816. #endif
  817. if (!current_valid())
  818. return;
  819. while (CURRENT -> nr_sectors) {
  820. int bn = CURRENT -> sector / 4;
  821. int i, offs, nr_sectors;
  822. for (i = 0; i < N_BUFS && buf_bn[i] != bn; ++i);
  823. DEBUG((DEBUG_REQUEST, "found %d", i));
  824. if (i >= N_BUFS) {
  825. buf_out = NOBUF;
  826. break;
  827. }
  828. offs = (i * 4 + (CURRENT -> sector & 3)) * 512;
  829. nr_sectors = 4 - (CURRENT -> sector & 3);
  830. if (buf_out != i) {
  831. buf_out = i;
  832. if (buf_bn[i] != bn) {
  833. buf_out = NOBUF;
  834. continue;
  835. }
  836. }
  837. if (nr_sectors > CURRENT -> nr_sectors)
  838. nr_sectors = CURRENT -> nr_sectors;
  839. memcpy(CURRENT -> buffer, buf + offs, nr_sectors * 512);
  840. CURRENT -> nr_sectors -= nr_sectors;
  841. CURRENT -> sector += nr_sectors;
  842. CURRENT -> buffer += nr_sectors * 512;
  843. }
  844. }
  845. /* State machine for reading disk blocks */
  846. enum state_e {
  847. S_IDLE, /* 0 */
  848. S_START, /* 1 */
  849. S_READ, /* 2 */
  850. S_DATA, /* 3 */
  851. S_STOP, /* 4 */
  852. S_STOPPING /* 5 */
  853. };
  854. static volatile enum state_e state = S_IDLE;
  855. #if DEBUG_STATE
  856. static volatile enum state_e state_old = S_STOP;
  857. static volatile int flags_old = 0;
  858. static volatile long state_n = 0;
  859. #endif
  860. /* Used as mutex to keep do_optcd_request (and other processes calling
  861. ioctl) out while some process is inside a VFS call.
  862. Reverse is accomplished by checking if state = S_IDLE upon entry
  863. of opt_ioctl and opt_media_change. */
  864. static int in_vfs = 0;
  865. static volatile int transfer_is_active = 0;
  866. static volatile int error = 0; /* %% do something with this?? */
  867. static int tries; /* ibid?? */
  868. static int timeout = 0;
  869. static void poll(unsigned long data);
  870. static struct timer_list req_timer = {.function = poll};
  871. static void poll(unsigned long data)
  872. {
  873. static volatile int read_count = 1;
  874. int flags;
  875. int loop_again = 1;
  876. int status = 0;
  877. int skip = 0;
  878. if (error) {
  879. printk(KERN_ERR "optcd: I/O error 0x%02x\n", error);
  880. opt_invalidate_buffers();
  881. if (!tries--) {
  882. printk(KERN_ERR "optcd: read block %d failed;"
  883. " Giving up\n", next_bn);
  884. if (transfer_is_active)
  885. loop_again = 0;
  886. if (current_valid())
  887. end_request(CURRENT, 0);
  888. tries = 5;
  889. }
  890. error = 0;
  891. state = S_STOP;
  892. }
  893. while (loop_again)
  894. {
  895. loop_again = 0; /* each case must flip this back to 1 if we want
  896. to come back up here */
  897. #if DEBUG_STATE
  898. if (state == state_old)
  899. state_n++;
  900. else {
  901. state_old = state;
  902. if (++state_n > 1)
  903. printk(KERN_DEBUG "optcd: %ld times "
  904. "in previous state\n", state_n);
  905. printk(KERN_DEBUG "optcd: state %d\n", state);
  906. state_n = 0;
  907. }
  908. #endif
  909. switch (state) {
  910. case S_IDLE:
  911. return;
  912. case S_START:
  913. if (in_vfs)
  914. break;
  915. if (send_cmd(COMDRVST)) {
  916. state = S_IDLE;
  917. while (current_valid())
  918. end_request(CURRENT, 0);
  919. return;
  920. }
  921. state = S_READ;
  922. timeout = READ_TIMEOUT;
  923. break;
  924. case S_READ: {
  925. struct cdrom_msf msf;
  926. if (!skip) {
  927. status = fetch_status();
  928. if (status < 0)
  929. break;
  930. if (status & ST_DSK_CHG) {
  931. toc_uptodate = 0;
  932. opt_invalidate_buffers();
  933. }
  934. }
  935. skip = 0;
  936. if ((status & ST_DOOR_OPEN) || (status & ST_DRVERR)) {
  937. toc_uptodate = 0;
  938. opt_invalidate_buffers();
  939. printk(KERN_WARNING "optcd: %s\n",
  940. (status & ST_DOOR_OPEN)
  941. ? "door open"
  942. : "disk removed");
  943. state = S_IDLE;
  944. while (current_valid())
  945. end_request(CURRENT, 0);
  946. return;
  947. }
  948. if (!current_valid()) {
  949. state = S_STOP;
  950. loop_again = 1;
  951. break;
  952. }
  953. next_bn = CURRENT -> sector / 4;
  954. lba2msf(next_bn, &msf);
  955. read_count = N_BUFS;
  956. msf.cdmsf_frame1 = read_count; /* Not BCD! */
  957. DEBUG((DEBUG_REQUEST, "reading %x:%x.%x %x:%x.%x",
  958. msf.cdmsf_min0,
  959. msf.cdmsf_sec0,
  960. msf.cdmsf_frame0,
  961. msf.cdmsf_min1,
  962. msf.cdmsf_sec1,
  963. msf.cdmsf_frame1));
  964. DEBUG((DEBUG_REQUEST, "next_bn:%d buf_in:%d"
  965. " buf_out:%d buf_bn:%d",
  966. next_bn,
  967. buf_in,
  968. buf_out,
  969. buf_bn[buf_in]));
  970. exec_read_cmd(COMREAD, &msf);
  971. state = S_DATA;
  972. timeout = READ_TIMEOUT;
  973. break;
  974. }
  975. case S_DATA:
  976. flags = stdt_flags() & (FL_STEN|FL_DTEN);
  977. #if DEBUG_STATE
  978. if (flags != flags_old) {
  979. flags_old = flags;
  980. printk(KERN_DEBUG "optcd: flags:%x\n", flags);
  981. }
  982. if (flags == FL_STEN)
  983. printk(KERN_DEBUG "timeout cnt: %d\n", timeout);
  984. #endif
  985. switch (flags) {
  986. case FL_DTEN: /* only STEN low */
  987. if (!tries--) {
  988. printk(KERN_ERR
  989. "optcd: read block %d failed; "
  990. "Giving up\n", next_bn);
  991. if (transfer_is_active) {
  992. tries = 0;
  993. break;
  994. }
  995. if (current_valid())
  996. end_request(CURRENT, 0);
  997. tries = 5;
  998. }
  999. state = S_START;
  1000. timeout = READ_TIMEOUT;
  1001. loop_again = 1;
  1002. case (FL_STEN|FL_DTEN): /* both high */
  1003. break;
  1004. default: /* DTEN low */
  1005. tries = 5;
  1006. if (!current_valid() && buf_in == buf_out) {
  1007. state = S_STOP;
  1008. loop_again = 1;
  1009. break;
  1010. }
  1011. if (read_count<=0)
  1012. printk(KERN_WARNING
  1013. "optcd: warning - try to read"
  1014. " 0 frames\n");
  1015. while (read_count) {
  1016. buf_bn[buf_in] = NOBUF;
  1017. if (!flag_low(FL_DTEN, BUSY_TIMEOUT)) {
  1018. /* should be no waiting here!?? */
  1019. printk(KERN_ERR
  1020. "read_count:%d "
  1021. "CURRENT->nr_sectors:%ld "
  1022. "buf_in:%d\n",
  1023. read_count,
  1024. CURRENT->nr_sectors,
  1025. buf_in);
  1026. printk(KERN_ERR
  1027. "transfer active: %x\n",
  1028. transfer_is_active);
  1029. read_count = 0;
  1030. state = S_STOP;
  1031. loop_again = 1;
  1032. end_request(CURRENT, 0);
  1033. break;
  1034. }
  1035. fetch_data(buf+
  1036. CD_FRAMESIZE*buf_in,
  1037. CD_FRAMESIZE);
  1038. read_count--;
  1039. DEBUG((DEBUG_REQUEST,
  1040. "S_DATA; ---I've read data- "
  1041. "read_count: %d",
  1042. read_count));
  1043. DEBUG((DEBUG_REQUEST,
  1044. "next_bn:%d buf_in:%d "
  1045. "buf_out:%d buf_bn:%d",
  1046. next_bn,
  1047. buf_in,
  1048. buf_out,
  1049. buf_bn[buf_in]));
  1050. buf_bn[buf_in] = next_bn++;
  1051. if (buf_out == NOBUF)
  1052. buf_out = buf_in;
  1053. buf_in = buf_in + 1 ==
  1054. N_BUFS ? 0 : buf_in + 1;
  1055. }
  1056. if (!transfer_is_active) {
  1057. while (current_valid()) {
  1058. transfer();
  1059. if (CURRENT -> nr_sectors == 0)
  1060. end_request(CURRENT, 1);
  1061. else
  1062. break;
  1063. }
  1064. }
  1065. if (current_valid()
  1066. && (CURRENT -> sector / 4 < next_bn ||
  1067. CURRENT -> sector / 4 >
  1068. next_bn + N_BUFS)) {
  1069. state = S_STOP;
  1070. loop_again = 1;
  1071. break;
  1072. }
  1073. timeout = READ_TIMEOUT;
  1074. if (read_count == 0) {
  1075. state = S_STOP;
  1076. loop_again = 1;
  1077. break;
  1078. }
  1079. }
  1080. break;
  1081. case S_STOP:
  1082. if (read_count != 0)
  1083. printk(KERN_ERR
  1084. "optcd: discard data=%x frames\n",
  1085. read_count);
  1086. flush_data();
  1087. if (send_cmd(COMDRVST)) {
  1088. state = S_IDLE;
  1089. while (current_valid())
  1090. end_request(CURRENT, 0);
  1091. return;
  1092. }
  1093. state = S_STOPPING;
  1094. timeout = STOP_TIMEOUT;
  1095. break;
  1096. case S_STOPPING:
  1097. status = fetch_status();
  1098. if (status < 0 && timeout)
  1099. break;
  1100. if ((status >= 0) && (status & ST_DSK_CHG)) {
  1101. toc_uptodate = 0;
  1102. opt_invalidate_buffers();
  1103. }
  1104. if (current_valid()) {
  1105. if (status >= 0) {
  1106. state = S_READ;
  1107. loop_again = 1;
  1108. skip = 1;
  1109. break;
  1110. } else {
  1111. state = S_START;
  1112. timeout = 1;
  1113. }
  1114. } else {
  1115. state = S_IDLE;
  1116. return;
  1117. }
  1118. break;
  1119. default:
  1120. printk(KERN_ERR "optcd: invalid state %d\n", state);
  1121. return;
  1122. } /* case */
  1123. } /* while */
  1124. if (!timeout--) {
  1125. printk(KERN_ERR "optcd: timeout in state %d\n", state);
  1126. state = S_STOP;
  1127. if (exec_cmd(COMSTOP) < 0) {
  1128. state = S_IDLE;
  1129. while (current_valid())
  1130. end_request(CURRENT, 0);
  1131. return;
  1132. }
  1133. }
  1134. mod_timer(&req_timer, jiffies + HZ/100);
  1135. }
  1136. static void do_optcd_request(request_queue_t * q)
  1137. {
  1138. DEBUG((DEBUG_REQUEST, "do_optcd_request(%ld+%ld)",
  1139. CURRENT -> sector, CURRENT -> nr_sectors));
  1140. if (disk_info.audio) {
  1141. printk(KERN_WARNING "optcd: tried to mount an Audio CD\n");
  1142. end_request(CURRENT, 0);
  1143. return;
  1144. }
  1145. transfer_is_active = 1;
  1146. while (current_valid()) {
  1147. transfer(); /* First try to transfer block from buffers */
  1148. if (CURRENT -> nr_sectors == 0) {
  1149. end_request(CURRENT, 1);
  1150. } else { /* Want to read a block not in buffer */
  1151. buf_out = NOBUF;
  1152. if (state == S_IDLE) {
  1153. /* %% Should this block the request queue?? */
  1154. if (update_toc() < 0) {
  1155. while (current_valid())
  1156. end_request(CURRENT, 0);
  1157. break;
  1158. }
  1159. /* Start state machine */
  1160. state = S_START;
  1161. timeout = READ_TIMEOUT;
  1162. tries = 5;
  1163. /* %% why not start right away?? */
  1164. mod_timer(&req_timer, jiffies + HZ/100);
  1165. }
  1166. break;
  1167. }
  1168. }
  1169. transfer_is_active = 0;
  1170. DEBUG((DEBUG_REQUEST, "next_bn:%d buf_in:%d buf_out:%d buf_bn:%d",
  1171. next_bn, buf_in, buf_out, buf_bn[buf_in]));
  1172. DEBUG((DEBUG_REQUEST, "do_optcd_request ends"));
  1173. }
  1174. /* IOCTLs */
  1175. static char auto_eject = 0;
  1176. static int cdrompause(void)
  1177. {
  1178. int status;
  1179. if (audio_status != CDROM_AUDIO_PLAY)
  1180. return -EINVAL;
  1181. status = exec_cmd(COMPAUSEON);
  1182. if (status < 0) {
  1183. DEBUG((DEBUG_VFS, "exec_cmd COMPAUSEON: %02x", -status));
  1184. return -EIO;
  1185. }
  1186. audio_status = CDROM_AUDIO_PAUSED;
  1187. return 0;
  1188. }
  1189. static int cdromresume(void)
  1190. {
  1191. int status;
  1192. if (audio_status != CDROM_AUDIO_PAUSED)
  1193. return -EINVAL;
  1194. status = exec_cmd(COMPAUSEOFF);
  1195. if (status < 0) {
  1196. DEBUG((DEBUG_VFS, "exec_cmd COMPAUSEOFF: %02x", -status));
  1197. audio_status = CDROM_AUDIO_ERROR;
  1198. return -EIO;
  1199. }
  1200. audio_status = CDROM_AUDIO_PLAY;
  1201. return 0;
  1202. }
  1203. static int cdromplaymsf(void __user *arg)
  1204. {
  1205. int status;
  1206. struct cdrom_msf msf;
  1207. if (copy_from_user(&msf, arg, sizeof msf))
  1208. return -EFAULT;
  1209. bin2bcd(&msf);
  1210. status = exec_long_cmd(COMPLAY, &msf);
  1211. if (status < 0) {
  1212. DEBUG((DEBUG_VFS, "exec_long_cmd COMPLAY: %02x", -status));
  1213. audio_status = CDROM_AUDIO_ERROR;
  1214. return -EIO;
  1215. }
  1216. audio_status = CDROM_AUDIO_PLAY;
  1217. return 0;
  1218. }
  1219. static int cdromplaytrkind(void __user *arg)
  1220. {
  1221. int status;
  1222. struct cdrom_ti ti;
  1223. struct cdrom_msf msf;
  1224. if (copy_from_user(&ti, arg, sizeof ti))
  1225. return -EFAULT;
  1226. if (ti.cdti_trk0 < disk_info.first
  1227. || ti.cdti_trk0 > disk_info.last
  1228. || ti.cdti_trk1 < ti.cdti_trk0)
  1229. return -EINVAL;
  1230. if (ti.cdti_trk1 > disk_info.last)
  1231. ti.cdti_trk1 = disk_info.last;
  1232. msf.cdmsf_min0 = toc[ti.cdti_trk0].cdsc_absaddr.msf.minute;
  1233. msf.cdmsf_sec0 = toc[ti.cdti_trk0].cdsc_absaddr.msf.second;
  1234. msf.cdmsf_frame0 = toc[ti.cdti_trk0].cdsc_absaddr.msf.frame;
  1235. msf.cdmsf_min1 = toc[ti.cdti_trk1 + 1].cdsc_absaddr.msf.minute;
  1236. msf.cdmsf_sec1 = toc[ti.cdti_trk1 + 1].cdsc_absaddr.msf.second;
  1237. msf.cdmsf_frame1 = toc[ti.cdti_trk1 + 1].cdsc_absaddr.msf.frame;
  1238. DEBUG((DEBUG_VFS, "play %02d:%02d.%02d to %02d:%02d.%02d",
  1239. msf.cdmsf_min0,
  1240. msf.cdmsf_sec0,
  1241. msf.cdmsf_frame0,
  1242. msf.cdmsf_min1,
  1243. msf.cdmsf_sec1,
  1244. msf.cdmsf_frame1));
  1245. bin2bcd(&msf);
  1246. status = exec_long_cmd(COMPLAY, &msf);
  1247. if (status < 0) {
  1248. DEBUG((DEBUG_VFS, "exec_long_cmd COMPLAY: %02x", -status));
  1249. audio_status = CDROM_AUDIO_ERROR;
  1250. return -EIO;
  1251. }
  1252. audio_status = CDROM_AUDIO_PLAY;
  1253. return 0;
  1254. }
  1255. static int cdromreadtochdr(void __user *arg)
  1256. {
  1257. struct cdrom_tochdr tochdr;
  1258. tochdr.cdth_trk0 = disk_info.first;
  1259. tochdr.cdth_trk1 = disk_info.last;
  1260. return copy_to_user(arg, &tochdr, sizeof tochdr) ? -EFAULT : 0;
  1261. }
  1262. static int cdromreadtocentry(void __user *arg)
  1263. {
  1264. struct cdrom_tocentry entry;
  1265. struct cdrom_subchnl *tocptr;
  1266. if (copy_from_user(&entry, arg, sizeof entry))
  1267. return -EFAULT;
  1268. if (entry.cdte_track == CDROM_LEADOUT)
  1269. tocptr = &toc[disk_info.last + 1];
  1270. else if (entry.cdte_track > disk_info.last
  1271. || entry.cdte_track < disk_info.first)
  1272. return -EINVAL;
  1273. else
  1274. tocptr = &toc[entry.cdte_track];
  1275. entry.cdte_adr = tocptr->cdsc_adr;
  1276. entry.cdte_ctrl = tocptr->cdsc_ctrl;
  1277. entry.cdte_addr.msf.minute = tocptr->cdsc_absaddr.msf.minute;
  1278. entry.cdte_addr.msf.second = tocptr->cdsc_absaddr.msf.second;
  1279. entry.cdte_addr.msf.frame = tocptr->cdsc_absaddr.msf.frame;
  1280. /* %% What should go into entry.cdte_datamode? */
  1281. if (entry.cdte_format == CDROM_LBA)
  1282. msf2lba(&entry.cdte_addr);
  1283. else if (entry.cdte_format != CDROM_MSF)
  1284. return -EINVAL;
  1285. return copy_to_user(arg, &entry, sizeof entry) ? -EFAULT : 0;
  1286. }
  1287. static int cdromvolctrl(void __user *arg)
  1288. {
  1289. int status;
  1290. struct cdrom_volctrl volctrl;
  1291. struct cdrom_msf msf;
  1292. if (copy_from_user(&volctrl, arg, sizeof volctrl))
  1293. return -EFAULT;
  1294. msf.cdmsf_min0 = 0x10;
  1295. msf.cdmsf_sec0 = 0x32;
  1296. msf.cdmsf_frame0 = volctrl.channel0;
  1297. msf.cdmsf_min1 = volctrl.channel1;
  1298. msf.cdmsf_sec1 = volctrl.channel2;
  1299. msf.cdmsf_frame1 = volctrl.channel3;
  1300. status = exec_long_cmd(COMCHCTRL, &msf);
  1301. if (status < 0) {
  1302. DEBUG((DEBUG_VFS, "exec_long_cmd COMCHCTRL: %02x", -status));
  1303. return -EIO;
  1304. }
  1305. return 0;
  1306. }
  1307. static int cdromsubchnl(void __user *arg)
  1308. {
  1309. int status;
  1310. struct cdrom_subchnl subchnl;
  1311. if (copy_from_user(&subchnl, arg, sizeof subchnl))
  1312. return -EFAULT;
  1313. if (subchnl.cdsc_format != CDROM_LBA
  1314. && subchnl.cdsc_format != CDROM_MSF)
  1315. return -EINVAL;
  1316. status = get_q_channel(&subchnl);
  1317. if (status < 0) {
  1318. DEBUG((DEBUG_VFS, "get_q_channel: %02x", -status));
  1319. return -EIO;
  1320. }
  1321. if (copy_to_user(arg, &subchnl, sizeof subchnl))
  1322. return -EFAULT;
  1323. return 0;
  1324. }
  1325. static struct gendisk *optcd_disk;
  1326. static int cdromread(void __user *arg, int blocksize, int cmd)
  1327. {
  1328. int status;
  1329. struct cdrom_msf msf;
  1330. if (copy_from_user(&msf, arg, sizeof msf))
  1331. return -EFAULT;
  1332. bin2bcd(&msf);
  1333. msf.cdmsf_min1 = 0;
  1334. msf.cdmsf_sec1 = 0;
  1335. msf.cdmsf_frame1 = 1; /* read only one frame */
  1336. status = exec_read_cmd(cmd, &msf);
  1337. DEBUG((DEBUG_VFS, "read cmd status 0x%x", status));
  1338. if (!sleep_flag_low(FL_DTEN, SLEEP_TIMEOUT))
  1339. return -EIO;
  1340. fetch_data(optcd_disk->private_data, blocksize);
  1341. if (copy_to_user(arg, optcd_disk->private_data, blocksize))
  1342. return -EFAULT;
  1343. return 0;
  1344. }
  1345. static int cdromseek(void __user *arg)
  1346. {
  1347. int status;
  1348. struct cdrom_msf msf;
  1349. if (copy_from_user(&msf, arg, sizeof msf))
  1350. return -EFAULT;
  1351. bin2bcd(&msf);
  1352. status = exec_seek_cmd(COMSEEK, &msf);
  1353. DEBUG((DEBUG_VFS, "COMSEEK status 0x%x", status));
  1354. if (status < 0)
  1355. return -EIO;
  1356. return 0;
  1357. }
  1358. #ifdef MULTISESSION
  1359. static int cdrommultisession(void __user *arg)
  1360. {
  1361. struct cdrom_multisession ms;
  1362. if (copy_from_user(&ms, arg, sizeof ms))
  1363. return -EFAULT;
  1364. ms.addr.msf.minute = disk_info.last_session.minute;
  1365. ms.addr.msf.second = disk_info.last_session.second;
  1366. ms.addr.msf.frame = disk_info.last_session.frame;
  1367. if (ms.addr_format != CDROM_LBA
  1368. && ms.addr_format != CDROM_MSF)
  1369. return -EINVAL;
  1370. if (ms.addr_format == CDROM_LBA)
  1371. msf2lba(&ms.addr);
  1372. ms.xa_flag = disk_info.xa;
  1373. if (copy_to_user(arg, &ms, sizeof(struct cdrom_multisession)))
  1374. return -EFAULT;
  1375. #if DEBUG_MULTIS
  1376. if (ms.addr_format == CDROM_MSF)
  1377. printk(KERN_DEBUG
  1378. "optcd: multisession xa:%d, msf:%02d:%02d.%02d\n",
  1379. ms.xa_flag,
  1380. ms.addr.msf.minute,
  1381. ms.addr.msf.second,
  1382. ms.addr.msf.frame);
  1383. else
  1384. printk(KERN_DEBUG
  1385. "optcd: multisession %d, lba:0x%08x [%02d:%02d.%02d])\n",
  1386. ms.xa_flag,
  1387. ms.addr.lba,
  1388. disk_info.last_session.minute,
  1389. disk_info.last_session.second,
  1390. disk_info.last_session.frame);
  1391. #endif /* DEBUG_MULTIS */
  1392. return 0;
  1393. }
  1394. #endif /* MULTISESSION */
  1395. static int cdromreset(void)
  1396. {
  1397. if (state != S_IDLE) {
  1398. error = 1;
  1399. tries = 0;
  1400. }
  1401. toc_uptodate = 0;
  1402. disk_changed = 1;
  1403. opt_invalidate_buffers();
  1404. audio_status = CDROM_AUDIO_NO_STATUS;
  1405. if (!reset_drive())
  1406. return -EIO;
  1407. return 0;
  1408. }
  1409. /* VFS calls */
  1410. static int opt_ioctl(struct inode *ip, struct file *fp,
  1411. unsigned int cmd, unsigned long arg)
  1412. {
  1413. int status, err, retval = 0;
  1414. void __user *argp = (void __user *)arg;
  1415. DEBUG((DEBUG_VFS, "starting opt_ioctl"));
  1416. if (!ip)
  1417. return -EINVAL;
  1418. if (cmd == CDROMRESET)
  1419. return cdromreset();
  1420. /* is do_optcd_request or another ioctl busy? */
  1421. if (state != S_IDLE || in_vfs)
  1422. return -EBUSY;
  1423. in_vfs = 1;
  1424. status = drive_status();
  1425. if (status < 0) {
  1426. DEBUG((DEBUG_VFS, "drive_status: %02x", -status));
  1427. in_vfs = 0;
  1428. return -EIO;
  1429. }
  1430. if (status & ST_DOOR_OPEN)
  1431. switch (cmd) { /* Actions that can be taken with door open */
  1432. case CDROMCLOSETRAY:
  1433. /* We do this before trying to read the toc. */
  1434. err = exec_cmd(COMCLOSE);
  1435. if (err < 0) {
  1436. DEBUG((DEBUG_VFS,
  1437. "exec_cmd COMCLOSE: %02x", -err));
  1438. in_vfs = 0;
  1439. return -EIO;
  1440. }
  1441. break;
  1442. default: in_vfs = 0;
  1443. return -EBUSY;
  1444. }
  1445. err = update_toc();
  1446. if (err < 0) {
  1447. DEBUG((DEBUG_VFS, "update_toc: %02x", -err));
  1448. in_vfs = 0;
  1449. return -EIO;
  1450. }
  1451. DEBUG((DEBUG_VFS, "ioctl cmd 0x%x", cmd));
  1452. switch (cmd) {
  1453. case CDROMPAUSE: retval = cdrompause(); break;
  1454. case CDROMRESUME: retval = cdromresume(); break;
  1455. case CDROMPLAYMSF: retval = cdromplaymsf(argp); break;
  1456. case CDROMPLAYTRKIND: retval = cdromplaytrkind(argp); break;
  1457. case CDROMREADTOCHDR: retval = cdromreadtochdr(argp); break;
  1458. case CDROMREADTOCENTRY: retval = cdromreadtocentry(argp); break;
  1459. case CDROMSTOP: err = exec_cmd(COMSTOP);
  1460. if (err < 0) {
  1461. DEBUG((DEBUG_VFS,
  1462. "exec_cmd COMSTOP: %02x",
  1463. -err));
  1464. retval = -EIO;
  1465. } else
  1466. audio_status = CDROM_AUDIO_NO_STATUS;
  1467. break;
  1468. case CDROMSTART: break; /* This is a no-op */
  1469. case CDROMEJECT: err = exec_cmd(COMUNLOCK);
  1470. if (err < 0) {
  1471. DEBUG((DEBUG_VFS,
  1472. "exec_cmd COMUNLOCK: %02x",
  1473. -err));
  1474. retval = -EIO;
  1475. break;
  1476. }
  1477. err = exec_cmd(COMOPEN);
  1478. if (err < 0) {
  1479. DEBUG((DEBUG_VFS,
  1480. "exec_cmd COMOPEN: %02x",
  1481. -err));
  1482. retval = -EIO;
  1483. }
  1484. break;
  1485. case CDROMVOLCTRL: retval = cdromvolctrl(argp); break;
  1486. case CDROMSUBCHNL: retval = cdromsubchnl(argp); break;
  1487. /* The drive detects the mode and automatically delivers the
  1488. correct 2048 bytes, so we don't need these IOCTLs */
  1489. case CDROMREADMODE2: retval = -EINVAL; break;
  1490. case CDROMREADMODE1: retval = -EINVAL; break;
  1491. /* Drive doesn't support reading audio */
  1492. case CDROMREADAUDIO: retval = -EINVAL; break;
  1493. case CDROMEJECT_SW: auto_eject = (char) arg;
  1494. break;
  1495. #ifdef MULTISESSION
  1496. case CDROMMULTISESSION: retval = cdrommultisession(argp); break;
  1497. #endif
  1498. case CDROM_GET_MCN: retval = -EINVAL; break; /* not implemented */
  1499. case CDROMVOLREAD: retval = -EINVAL; break; /* not implemented */
  1500. case CDROMREADRAW:
  1501. /* this drive delivers 2340 bytes in raw mode */
  1502. retval = cdromread(argp, CD_FRAMESIZE_RAW1, COMREADRAW);
  1503. break;
  1504. case CDROMREADCOOKED:
  1505. retval = cdromread(argp, CD_FRAMESIZE, COMREAD);
  1506. break;
  1507. case CDROMREADALL:
  1508. retval = cdromread(argp, CD_FRAMESIZE_RAWER, COMREADALL);
  1509. break;
  1510. case CDROMSEEK: retval = cdromseek(argp); break;
  1511. case CDROMPLAYBLK: retval = -EINVAL; break; /* not implemented */
  1512. case CDROMCLOSETRAY: break; /* The action was taken earlier */
  1513. default: retval = -EINVAL;
  1514. }
  1515. in_vfs = 0;
  1516. return retval;
  1517. }
  1518. static int open_count = 0;
  1519. /* Open device special file; check that a disk is in. */
  1520. static int opt_open(struct inode *ip, struct file *fp)
  1521. {
  1522. DEBUG((DEBUG_VFS, "starting opt_open"));
  1523. if (!open_count && state == S_IDLE) {
  1524. int status;
  1525. char *buf;
  1526. buf = kmalloc(CD_FRAMESIZE_RAWER, GFP_KERNEL);
  1527. if (!buf) {
  1528. printk(KERN_INFO "optcd: cannot allocate read buffer\n");
  1529. return -ENOMEM;
  1530. }
  1531. optcd_disk->private_data = buf; /* save read buffer */
  1532. toc_uptodate = 0;
  1533. opt_invalidate_buffers();
  1534. status = exec_cmd(COMCLOSE); /* close door */
  1535. if (status < 0) {
  1536. DEBUG((DEBUG_VFS, "exec_cmd COMCLOSE: %02x", -status));
  1537. }
  1538. status = drive_status();
  1539. if (status < 0) {
  1540. DEBUG((DEBUG_VFS, "drive_status: %02x", -status));
  1541. goto err_out;
  1542. }
  1543. DEBUG((DEBUG_VFS, "status: %02x", status));
  1544. if ((status & ST_DOOR_OPEN) || (status & ST_DRVERR)) {
  1545. printk(KERN_INFO "optcd: no disk or door open\n");
  1546. goto err_out;
  1547. }
  1548. status = exec_cmd(COMLOCK); /* Lock door */
  1549. if (status < 0) {
  1550. DEBUG((DEBUG_VFS, "exec_cmd COMLOCK: %02x", -status));
  1551. }
  1552. status = update_toc(); /* Read table of contents */
  1553. if (status < 0) {
  1554. DEBUG((DEBUG_VFS, "update_toc: %02x", -status));
  1555. status = exec_cmd(COMUNLOCK); /* Unlock door */
  1556. if (status < 0) {
  1557. DEBUG((DEBUG_VFS,
  1558. "exec_cmd COMUNLOCK: %02x", -status));
  1559. }
  1560. goto err_out;
  1561. }
  1562. open_count++;
  1563. }
  1564. DEBUG((DEBUG_VFS, "exiting opt_open"));
  1565. return 0;
  1566. err_out:
  1567. return -EIO;
  1568. }
  1569. /* Release device special file; flush all blocks from the buffer cache */
  1570. static int opt_release(struct inode *ip, struct file *fp)
  1571. {
  1572. int status;
  1573. DEBUG((DEBUG_VFS, "executing opt_release"));
  1574. DEBUG((DEBUG_VFS, "inode: %p, device: %s, file: %p\n",
  1575. ip, ip->i_bdev->bd_disk->disk_name, fp));
  1576. if (!--open_count) {
  1577. toc_uptodate = 0;
  1578. opt_invalidate_buffers();
  1579. status = exec_cmd(COMUNLOCK); /* Unlock door */
  1580. if (status < 0) {
  1581. DEBUG((DEBUG_VFS, "exec_cmd COMUNLOCK: %02x", -status));
  1582. }
  1583. if (auto_eject) {
  1584. status = exec_cmd(COMOPEN);
  1585. DEBUG((DEBUG_VFS, "exec_cmd COMOPEN: %02x", -status));
  1586. }
  1587. kfree(optcd_disk->private_data);
  1588. del_timer(&delay_timer);
  1589. del_timer(&req_timer);
  1590. }
  1591. return 0;
  1592. }
  1593. /* Check if disk has been changed */
  1594. static int opt_media_change(struct gendisk *disk)
  1595. {
  1596. DEBUG((DEBUG_VFS, "executing opt_media_change"));
  1597. DEBUG((DEBUG_VFS, "dev: %s; disk_changed = %d\n",
  1598. disk->disk_name, disk_changed));
  1599. if (disk_changed) {
  1600. disk_changed = 0;
  1601. return 1;
  1602. }
  1603. return 0;
  1604. }
  1605. /* Driver initialisation */
  1606. /* Returns 1 if a drive is detected with a version string
  1607. starting with "DOLPHIN". Otherwise 0. */
  1608. static int __init version_ok(void)
  1609. {
  1610. char devname[100];
  1611. int count, i, ch, status;
  1612. status = exec_cmd(COMVERSION);
  1613. if (status < 0) {
  1614. DEBUG((DEBUG_VFS, "exec_cmd COMVERSION: %02x", -status));
  1615. return 0;
  1616. }
  1617. if ((count = get_data(1)) < 0) {
  1618. DEBUG((DEBUG_VFS, "get_data(1): %02x", -count));
  1619. return 0;
  1620. }
  1621. for (i = 0, ch = -1; count > 0; count--) {
  1622. if ((ch = get_data(1)) < 0) {
  1623. DEBUG((DEBUG_VFS, "get_data(1): %02x", -ch));
  1624. break;
  1625. }
  1626. if (i < 99)
  1627. devname[i++] = ch;
  1628. }
  1629. devname[i] = '\0';
  1630. if (ch < 0)
  1631. return 0;
  1632. printk(KERN_INFO "optcd: Device %s detected\n", devname);
  1633. return ((devname[0] == 'D')
  1634. && (devname[1] == 'O')
  1635. && (devname[2] == 'L')
  1636. && (devname[3] == 'P')
  1637. && (devname[4] == 'H')
  1638. && (devname[5] == 'I')
  1639. && (devname[6] == 'N'));
  1640. }
  1641. static struct block_device_operations opt_fops = {
  1642. .owner = THIS_MODULE,
  1643. .open = opt_open,
  1644. .release = opt_release,
  1645. .ioctl = opt_ioctl,
  1646. .media_changed = opt_media_change,
  1647. };
  1648. #ifndef MODULE
  1649. /* Get kernel parameter when used as a kernel driver */
  1650. static int optcd_setup(char *str)
  1651. {
  1652. int ints[4];
  1653. (void)get_options(str, ARRAY_SIZE(ints), ints);
  1654. if (ints[0] > 0)
  1655. optcd_port = ints[1];
  1656. return 1;
  1657. }
  1658. __setup("optcd=", optcd_setup);
  1659. #endif /* MODULE */
  1660. /* Test for presence of drive and initialize it. Called at boot time
  1661. or during module initialisation. */
  1662. static int __init optcd_init(void)
  1663. {
  1664. int status;
  1665. if (optcd_port <= 0) {
  1666. printk(KERN_INFO
  1667. "optcd: no Optics Storage CDROM Initialization\n");
  1668. return -EIO;
  1669. }
  1670. optcd_disk = alloc_disk(1);
  1671. if (!optcd_disk) {
  1672. printk(KERN_ERR "optcd: can't allocate disk\n");
  1673. return -ENOMEM;
  1674. }
  1675. optcd_disk->major = MAJOR_NR;
  1676. optcd_disk->first_minor = 0;
  1677. optcd_disk->fops = &opt_fops;
  1678. sprintf(optcd_disk->disk_name, "optcd");
  1679. sprintf(optcd_disk->devfs_name, "optcd");
  1680. if (!request_region(optcd_port, 4, "optcd")) {
  1681. printk(KERN_ERR "optcd: conflict, I/O port 0x%x already used\n",
  1682. optcd_port);
  1683. put_disk(optcd_disk);
  1684. return -EIO;
  1685. }
  1686. if (!reset_drive()) {
  1687. printk(KERN_ERR "optcd: drive at 0x%x not ready\n", optcd_port);
  1688. release_region(optcd_port, 4);
  1689. put_disk(optcd_disk);
  1690. return -EIO;
  1691. }
  1692. if (!version_ok()) {
  1693. printk(KERN_ERR "optcd: unknown drive detected; aborting\n");
  1694. release_region(optcd_port, 4);
  1695. put_disk(optcd_disk);
  1696. return -EIO;
  1697. }
  1698. status = exec_cmd(COMINITDOUBLE);
  1699. if (status < 0) {
  1700. printk(KERN_ERR "optcd: cannot init double speed mode\n");
  1701. release_region(optcd_port, 4);
  1702. DEBUG((DEBUG_VFS, "exec_cmd COMINITDOUBLE: %02x", -status));
  1703. put_disk(optcd_disk);
  1704. return -EIO;
  1705. }
  1706. if (register_blkdev(MAJOR_NR, "optcd")) {
  1707. release_region(optcd_port, 4);
  1708. put_disk(optcd_disk);
  1709. return -EIO;
  1710. }
  1711. opt_queue = blk_init_queue(do_optcd_request, &optcd_lock);
  1712. if (!opt_queue) {
  1713. unregister_blkdev(MAJOR_NR, "optcd");
  1714. release_region(optcd_port, 4);
  1715. put_disk(optcd_disk);
  1716. return -ENOMEM;
  1717. }
  1718. blk_queue_hardsect_size(opt_queue, 2048);
  1719. optcd_disk->queue = opt_queue;
  1720. add_disk(optcd_disk);
  1721. printk(KERN_INFO "optcd: DOLPHIN 8000 AT CDROM at 0x%x\n", optcd_port);
  1722. return 0;
  1723. }
  1724. static void __exit optcd_exit(void)
  1725. {
  1726. del_gendisk(optcd_disk);
  1727. put_disk(optcd_disk);
  1728. if (unregister_blkdev(MAJOR_NR, "optcd") == -EINVAL) {
  1729. printk(KERN_ERR "optcd: what's that: can't unregister\n");
  1730. return;
  1731. }
  1732. blk_cleanup_queue(opt_queue);
  1733. release_region(optcd_port, 4);
  1734. printk(KERN_INFO "optcd: module released.\n");
  1735. }
  1736. module_init(optcd_init);
  1737. module_exit(optcd_exit);
  1738. MODULE_LICENSE("GPL");
  1739. MODULE_ALIAS_BLOCKDEV_MAJOR(OPTICS_CDROM_MAJOR);