pf.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. pf.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. This is the high-level driver for parallel port ATAPI disk
  5. drives based on chips supported by the paride module.
  6. By default, the driver will autoprobe for a single parallel
  7. port ATAPI disk drive, but if their individual parameters are
  8. specified, the driver can handle up to 4 drives.
  9. The behaviour of the pf driver can be altered by setting
  10. some parameters from the insmod command line. The following
  11. parameters are adjustable:
  12. drive0 These four arguments can be arrays of
  13. drive1 1-7 integers as follows:
  14. drive2
  15. drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<lun>,<dly>
  16. Where,
  17. <prt> is the base of the parallel port address for
  18. the corresponding drive. (required)
  19. <pro> is the protocol number for the adapter that
  20. supports this drive. These numbers are
  21. logged by 'paride' when the protocol modules
  22. are initialised. (0 if not given)
  23. <uni> for those adapters that support chained
  24. devices, this is the unit selector for the
  25. chain of devices on the given port. It should
  26. be zero for devices that don't support chaining.
  27. (0 if not given)
  28. <mod> this can be -1 to choose the best mode, or one
  29. of the mode numbers supported by the adapter.
  30. (-1 if not given)
  31. <slv> ATAPI CDroms can be jumpered to master or slave.
  32. Set this to 0 to choose the master drive, 1 to
  33. choose the slave, -1 (the default) to choose the
  34. first drive found.
  35. <lun> Some ATAPI devices support multiple LUNs.
  36. One example is the ATAPI PD/CD drive from
  37. Matshita/Panasonic. This device has a
  38. CD drive on LUN 0 and a PD drive on LUN 1.
  39. By default, the driver will search for the
  40. first LUN with a supported device. Set
  41. this parameter to force it to use a specific
  42. LUN. (default -1)
  43. <dly> some parallel ports require the driver to
  44. go more slowly. -1 sets a default value that
  45. should work with the chosen protocol. Otherwise,
  46. set this to a small integer, the larger it is
  47. the slower the port i/o. In some cases, setting
  48. this to zero will speed up the device. (default -1)
  49. major You may use this parameter to overide the
  50. default major number (47) that this driver
  51. will use. Be sure to change the device
  52. name as well.
  53. name This parameter is a character string that
  54. contains the name the kernel will use for this
  55. device (in /proc output, for instance).
  56. (default "pf").
  57. cluster The driver will attempt to aggregate requests
  58. for adjacent blocks into larger multi-block
  59. clusters. The maximum cluster size (in 512
  60. byte sectors) is set with this parameter.
  61. (default 64)
  62. verbose This parameter controls the amount of logging
  63. that the driver will do. Set it to 0 for
  64. normal operation, 1 to see autoprobe progress
  65. messages, or 2 to see additional debugging
  66. output. (default 0)
  67. nice This parameter controls the driver's use of
  68. idle CPU time, at the expense of some speed.
  69. If this driver is built into the kernel, you can use the
  70. following command line parameters, with the same values
  71. as the corresponding module parameters listed above:
  72. pf.drive0
  73. pf.drive1
  74. pf.drive2
  75. pf.drive3
  76. pf.cluster
  77. pf.nice
  78. In addition, you can use the parameter pf.disable to disable
  79. the driver entirely.
  80. */
  81. /* Changes:
  82. 1.01 GRG 1998.05.03 Changes for SMP. Eliminate sti().
  83. Fix for drives that don't clear STAT_ERR
  84. until after next CDB delivered.
  85. Small change in pf_completion to round
  86. up transfer size.
  87. 1.02 GRG 1998.06.16 Eliminated an Ugh
  88. 1.03 GRG 1998.08.16 Use HZ in loop timings, extra debugging
  89. 1.04 GRG 1998.09.24 Added jumbo support
  90. */
  91. #define PF_VERSION "1.04"
  92. #define PF_MAJOR 47
  93. #define PF_NAME "pf"
  94. #define PF_UNITS 4
  95. #include <linux/types.h>
  96. /* Here are things one can override from the insmod command.
  97. Most are autoprobed by paride unless set here. Verbose is off
  98. by default.
  99. */
  100. static bool verbose = 0;
  101. static int major = PF_MAJOR;
  102. static char *name = PF_NAME;
  103. static int cluster = 64;
  104. static int nice = 0;
  105. static int disable = 0;
  106. static int drive0[7] = { 0, 0, 0, -1, -1, -1, -1 };
  107. static int drive1[7] = { 0, 0, 0, -1, -1, -1, -1 };
  108. static int drive2[7] = { 0, 0, 0, -1, -1, -1, -1 };
  109. static int drive3[7] = { 0, 0, 0, -1, -1, -1, -1 };
  110. static int (*drives[4])[7] = {&drive0, &drive1, &drive2, &drive3};
  111. static int pf_drive_count;
  112. enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_LUN, D_DLY};
  113. /* end of parameters */
  114. #include <linux/module.h>
  115. #include <linux/init.h>
  116. #include <linux/fs.h>
  117. #include <linux/delay.h>
  118. #include <linux/hdreg.h>
  119. #include <linux/cdrom.h>
  120. #include <linux/spinlock.h>
  121. #include <linux/blkdev.h>
  122. #include <linux/blkpg.h>
  123. #include <linux/mutex.h>
  124. #include <asm/uaccess.h>
  125. static DEFINE_MUTEX(pf_mutex);
  126. static DEFINE_SPINLOCK(pf_spin_lock);
  127. module_param(verbose, bool, 0644);
  128. module_param(major, int, 0);
  129. module_param(name, charp, 0);
  130. module_param(cluster, int, 0);
  131. module_param(nice, int, 0);
  132. module_param_array(drive0, int, NULL, 0);
  133. module_param_array(drive1, int, NULL, 0);
  134. module_param_array(drive2, int, NULL, 0);
  135. module_param_array(drive3, int, NULL, 0);
  136. #include "paride.h"
  137. #include "pseudo.h"
  138. /* constants for faking geometry numbers */
  139. #define PF_FD_MAX 8192 /* use FD geometry under this size */
  140. #define PF_FD_HDS 2
  141. #define PF_FD_SPT 18
  142. #define PF_HD_HDS 64
  143. #define PF_HD_SPT 32
  144. #define PF_MAX_RETRIES 5
  145. #define PF_TMO 800 /* interrupt timeout in jiffies */
  146. #define PF_SPIN_DEL 50 /* spin delay in micro-seconds */
  147. #define PF_SPIN (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
  148. #define STAT_ERR 0x00001
  149. #define STAT_INDEX 0x00002
  150. #define STAT_ECC 0x00004
  151. #define STAT_DRQ 0x00008
  152. #define STAT_SEEK 0x00010
  153. #define STAT_WRERR 0x00020
  154. #define STAT_READY 0x00040
  155. #define STAT_BUSY 0x00080
  156. #define ATAPI_REQ_SENSE 0x03
  157. #define ATAPI_LOCK 0x1e
  158. #define ATAPI_DOOR 0x1b
  159. #define ATAPI_MODE_SENSE 0x5a
  160. #define ATAPI_CAPACITY 0x25
  161. #define ATAPI_IDENTIFY 0x12
  162. #define ATAPI_READ_10 0x28
  163. #define ATAPI_WRITE_10 0x2a
  164. static int pf_open(struct block_device *bdev, fmode_t mode);
  165. static void do_pf_request(struct request_queue * q);
  166. static int pf_ioctl(struct block_device *bdev, fmode_t mode,
  167. unsigned int cmd, unsigned long arg);
  168. static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  169. static void pf_release(struct gendisk *disk, fmode_t mode);
  170. static int pf_detect(void);
  171. static void do_pf_read(void);
  172. static void do_pf_read_start(void);
  173. static void do_pf_write(void);
  174. static void do_pf_write_start(void);
  175. static void do_pf_read_drq(void);
  176. static void do_pf_write_done(void);
  177. #define PF_NM 0
  178. #define PF_RO 1
  179. #define PF_RW 2
  180. #define PF_NAMELEN 8
  181. struct pf_unit {
  182. struct pi_adapter pia; /* interface to paride layer */
  183. struct pi_adapter *pi;
  184. int removable; /* removable media device ? */
  185. int media_status; /* media present ? WP ? */
  186. int drive; /* drive */
  187. int lun;
  188. int access; /* count of active opens ... */
  189. int present; /* device present ? */
  190. char name[PF_NAMELEN]; /* pf0, pf1, ... */
  191. struct gendisk *disk;
  192. };
  193. static struct pf_unit units[PF_UNITS];
  194. static int pf_identify(struct pf_unit *pf);
  195. static void pf_lock(struct pf_unit *pf, int func);
  196. static void pf_eject(struct pf_unit *pf);
  197. static unsigned int pf_check_events(struct gendisk *disk,
  198. unsigned int clearing);
  199. static char pf_scratch[512]; /* scratch block buffer */
  200. /* the variables below are used mainly in the I/O request engine, which
  201. processes only one request at a time.
  202. */
  203. static int pf_retries = 0; /* i/o error retry count */
  204. static int pf_busy = 0; /* request being processed ? */
  205. static struct request *pf_req; /* current request */
  206. static int pf_block; /* address of next requested block */
  207. static int pf_count; /* number of blocks still to do */
  208. static int pf_run; /* sectors in current cluster */
  209. static int pf_cmd; /* current command READ/WRITE */
  210. static struct pf_unit *pf_current;/* unit of current request */
  211. static int pf_mask; /* stopper for pseudo-int */
  212. static char *pf_buf; /* buffer for request in progress */
  213. static void *par_drv; /* reference of parport driver */
  214. /* kernel glue structures */
  215. static const struct block_device_operations pf_fops = {
  216. .owner = THIS_MODULE,
  217. .open = pf_open,
  218. .release = pf_release,
  219. .ioctl = pf_ioctl,
  220. .getgeo = pf_getgeo,
  221. .check_events = pf_check_events,
  222. };
  223. static void __init pf_init_units(void)
  224. {
  225. struct pf_unit *pf;
  226. int unit;
  227. pf_drive_count = 0;
  228. for (unit = 0, pf = units; unit < PF_UNITS; unit++, pf++) {
  229. struct gendisk *disk = alloc_disk(1);
  230. if (!disk)
  231. continue;
  232. pf->disk = disk;
  233. pf->pi = &pf->pia;
  234. pf->media_status = PF_NM;
  235. pf->drive = (*drives[unit])[D_SLV];
  236. pf->lun = (*drives[unit])[D_LUN];
  237. snprintf(pf->name, PF_NAMELEN, "%s%d", name, unit);
  238. disk->major = major;
  239. disk->first_minor = unit;
  240. strcpy(disk->disk_name, pf->name);
  241. disk->fops = &pf_fops;
  242. if (!(*drives[unit])[D_PRT])
  243. pf_drive_count++;
  244. }
  245. }
  246. static int pf_open(struct block_device *bdev, fmode_t mode)
  247. {
  248. struct pf_unit *pf = bdev->bd_disk->private_data;
  249. int ret;
  250. mutex_lock(&pf_mutex);
  251. pf_identify(pf);
  252. ret = -ENODEV;
  253. if (pf->media_status == PF_NM)
  254. goto out;
  255. ret = -EROFS;
  256. if ((pf->media_status == PF_RO) && (mode & FMODE_WRITE))
  257. goto out;
  258. ret = 0;
  259. pf->access++;
  260. if (pf->removable)
  261. pf_lock(pf, 1);
  262. out:
  263. mutex_unlock(&pf_mutex);
  264. return ret;
  265. }
  266. static int pf_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  267. {
  268. struct pf_unit *pf = bdev->bd_disk->private_data;
  269. sector_t capacity = get_capacity(pf->disk);
  270. if (capacity < PF_FD_MAX) {
  271. geo->cylinders = sector_div(capacity, PF_FD_HDS * PF_FD_SPT);
  272. geo->heads = PF_FD_HDS;
  273. geo->sectors = PF_FD_SPT;
  274. } else {
  275. geo->cylinders = sector_div(capacity, PF_HD_HDS * PF_HD_SPT);
  276. geo->heads = PF_HD_HDS;
  277. geo->sectors = PF_HD_SPT;
  278. }
  279. return 0;
  280. }
  281. static int pf_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
  282. {
  283. struct pf_unit *pf = bdev->bd_disk->private_data;
  284. if (cmd != CDROMEJECT)
  285. return -EINVAL;
  286. if (pf->access != 1)
  287. return -EBUSY;
  288. mutex_lock(&pf_mutex);
  289. pf_eject(pf);
  290. mutex_unlock(&pf_mutex);
  291. return 0;
  292. }
  293. static void pf_release(struct gendisk *disk, fmode_t mode)
  294. {
  295. struct pf_unit *pf = disk->private_data;
  296. mutex_lock(&pf_mutex);
  297. if (pf->access <= 0) {
  298. mutex_unlock(&pf_mutex);
  299. WARN_ON(1);
  300. return;
  301. }
  302. pf->access--;
  303. if (!pf->access && pf->removable)
  304. pf_lock(pf, 0);
  305. mutex_unlock(&pf_mutex);
  306. }
  307. static unsigned int pf_check_events(struct gendisk *disk, unsigned int clearing)
  308. {
  309. return DISK_EVENT_MEDIA_CHANGE;
  310. }
  311. static inline int status_reg(struct pf_unit *pf)
  312. {
  313. return pi_read_regr(pf->pi, 1, 6);
  314. }
  315. static inline int read_reg(struct pf_unit *pf, int reg)
  316. {
  317. return pi_read_regr(pf->pi, 0, reg);
  318. }
  319. static inline void write_reg(struct pf_unit *pf, int reg, int val)
  320. {
  321. pi_write_regr(pf->pi, 0, reg, val);
  322. }
  323. static int pf_wait(struct pf_unit *pf, int go, int stop, char *fun, char *msg)
  324. {
  325. int j, r, e, s, p;
  326. j = 0;
  327. while ((((r = status_reg(pf)) & go) || (stop && (!(r & stop))))
  328. && (j++ < PF_SPIN))
  329. udelay(PF_SPIN_DEL);
  330. if ((r & (STAT_ERR & stop)) || (j > PF_SPIN)) {
  331. s = read_reg(pf, 7);
  332. e = read_reg(pf, 1);
  333. p = read_reg(pf, 2);
  334. if (j > PF_SPIN)
  335. e |= 0x100;
  336. if (fun)
  337. printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
  338. " loop=%d phase=%d\n",
  339. pf->name, fun, msg, r, s, e, j, p);
  340. return (e << 8) + s;
  341. }
  342. return 0;
  343. }
  344. static int pf_command(struct pf_unit *pf, char *cmd, int dlen, char *fun)
  345. {
  346. pi_connect(pf->pi);
  347. write_reg(pf, 6, 0xa0+0x10*pf->drive);
  348. if (pf_wait(pf, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
  349. pi_disconnect(pf->pi);
  350. return -1;
  351. }
  352. write_reg(pf, 4, dlen % 256);
  353. write_reg(pf, 5, dlen / 256);
  354. write_reg(pf, 7, 0xa0); /* ATAPI packet command */
  355. if (pf_wait(pf, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
  356. pi_disconnect(pf->pi);
  357. return -1;
  358. }
  359. if (read_reg(pf, 2) != 1) {
  360. printk("%s: %s: command phase error\n", pf->name, fun);
  361. pi_disconnect(pf->pi);
  362. return -1;
  363. }
  364. pi_write_block(pf->pi, cmd, 12);
  365. return 0;
  366. }
  367. static int pf_completion(struct pf_unit *pf, char *buf, char *fun)
  368. {
  369. int r, s, n;
  370. r = pf_wait(pf, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
  371. fun, "completion");
  372. if ((read_reg(pf, 2) & 2) && (read_reg(pf, 7) & STAT_DRQ)) {
  373. n = (((read_reg(pf, 4) + 256 * read_reg(pf, 5)) +
  374. 3) & 0xfffc);
  375. pi_read_block(pf->pi, buf, n);
  376. }
  377. s = pf_wait(pf, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
  378. pi_disconnect(pf->pi);
  379. return (r ? r : s);
  380. }
  381. static void pf_req_sense(struct pf_unit *pf, int quiet)
  382. {
  383. char rs_cmd[12] =
  384. { ATAPI_REQ_SENSE, pf->lun << 5, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
  385. char buf[16];
  386. int r;
  387. r = pf_command(pf, rs_cmd, 16, "Request sense");
  388. mdelay(1);
  389. if (!r)
  390. pf_completion(pf, buf, "Request sense");
  391. if ((!r) && (!quiet))
  392. printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
  393. pf->name, buf[2] & 0xf, buf[12], buf[13]);
  394. }
  395. static int pf_atapi(struct pf_unit *pf, char *cmd, int dlen, char *buf, char *fun)
  396. {
  397. int r;
  398. r = pf_command(pf, cmd, dlen, fun);
  399. mdelay(1);
  400. if (!r)
  401. r = pf_completion(pf, buf, fun);
  402. if (r)
  403. pf_req_sense(pf, !fun);
  404. return r;
  405. }
  406. static void pf_lock(struct pf_unit *pf, int func)
  407. {
  408. char lo_cmd[12] = { ATAPI_LOCK, pf->lun << 5, 0, 0, func, 0, 0, 0, 0, 0, 0, 0 };
  409. pf_atapi(pf, lo_cmd, 0, pf_scratch, func ? "lock" : "unlock");
  410. }
  411. static void pf_eject(struct pf_unit *pf)
  412. {
  413. char ej_cmd[12] = { ATAPI_DOOR, pf->lun << 5, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
  414. pf_lock(pf, 0);
  415. pf_atapi(pf, ej_cmd, 0, pf_scratch, "eject");
  416. }
  417. #define PF_RESET_TMO 30 /* in tenths of a second */
  418. static void pf_sleep(int cs)
  419. {
  420. schedule_timeout_interruptible(cs);
  421. }
  422. /* the ATAPI standard actually specifies the contents of all 7 registers
  423. after a reset, but the specification is ambiguous concerning the last
  424. two bytes, and different drives interpret the standard differently.
  425. */
  426. static int pf_reset(struct pf_unit *pf)
  427. {
  428. int i, k, flg;
  429. int expect[5] = { 1, 1, 1, 0x14, 0xeb };
  430. pi_connect(pf->pi);
  431. write_reg(pf, 6, 0xa0+0x10*pf->drive);
  432. write_reg(pf, 7, 8);
  433. pf_sleep(20 * HZ / 1000);
  434. k = 0;
  435. while ((k++ < PF_RESET_TMO) && (status_reg(pf) & STAT_BUSY))
  436. pf_sleep(HZ / 10);
  437. flg = 1;
  438. for (i = 0; i < 5; i++)
  439. flg &= (read_reg(pf, i + 1) == expect[i]);
  440. if (verbose) {
  441. printk("%s: Reset (%d) signature = ", pf->name, k);
  442. for (i = 0; i < 5; i++)
  443. printk("%3x", read_reg(pf, i + 1));
  444. if (!flg)
  445. printk(" (incorrect)");
  446. printk("\n");
  447. }
  448. pi_disconnect(pf->pi);
  449. return flg - 1;
  450. }
  451. static void pf_mode_sense(struct pf_unit *pf)
  452. {
  453. char ms_cmd[12] =
  454. { ATAPI_MODE_SENSE, pf->lun << 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0 };
  455. char buf[8];
  456. pf_atapi(pf, ms_cmd, 8, buf, "mode sense");
  457. pf->media_status = PF_RW;
  458. if (buf[3] & 0x80)
  459. pf->media_status = PF_RO;
  460. }
  461. static void xs(char *buf, char *targ, int offs, int len)
  462. {
  463. int j, k, l;
  464. j = 0;
  465. l = 0;
  466. for (k = 0; k < len; k++)
  467. if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
  468. l = targ[j++] = buf[k + offs];
  469. if (l == 0x20)
  470. j--;
  471. targ[j] = 0;
  472. }
  473. static int xl(char *buf, int offs)
  474. {
  475. int v, k;
  476. v = 0;
  477. for (k = 0; k < 4; k++)
  478. v = v * 256 + (buf[k + offs] & 0xff);
  479. return v;
  480. }
  481. static void pf_get_capacity(struct pf_unit *pf)
  482. {
  483. char rc_cmd[12] = { ATAPI_CAPACITY, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  484. char buf[8];
  485. int bs;
  486. if (pf_atapi(pf, rc_cmd, 8, buf, "get capacity")) {
  487. pf->media_status = PF_NM;
  488. return;
  489. }
  490. set_capacity(pf->disk, xl(buf, 0) + 1);
  491. bs = xl(buf, 4);
  492. if (bs != 512) {
  493. set_capacity(pf->disk, 0);
  494. if (verbose)
  495. printk("%s: Drive %d, LUN %d,"
  496. " unsupported block size %d\n",
  497. pf->name, pf->drive, pf->lun, bs);
  498. }
  499. }
  500. static int pf_identify(struct pf_unit *pf)
  501. {
  502. int dt, s;
  503. char *ms[2] = { "master", "slave" };
  504. char mf[10], id[18];
  505. char id_cmd[12] =
  506. { ATAPI_IDENTIFY, pf->lun << 5, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
  507. char buf[36];
  508. s = pf_atapi(pf, id_cmd, 36, buf, "identify");
  509. if (s)
  510. return -1;
  511. dt = buf[0] & 0x1f;
  512. if ((dt != 0) && (dt != 7)) {
  513. if (verbose)
  514. printk("%s: Drive %d, LUN %d, unsupported type %d\n",
  515. pf->name, pf->drive, pf->lun, dt);
  516. return -1;
  517. }
  518. xs(buf, mf, 8, 8);
  519. xs(buf, id, 16, 16);
  520. pf->removable = (buf[1] & 0x80);
  521. pf_mode_sense(pf);
  522. pf_mode_sense(pf);
  523. pf_mode_sense(pf);
  524. pf_get_capacity(pf);
  525. printk("%s: %s %s, %s LUN %d, type %d",
  526. pf->name, mf, id, ms[pf->drive], pf->lun, dt);
  527. if (pf->removable)
  528. printk(", removable");
  529. if (pf->media_status == PF_NM)
  530. printk(", no media\n");
  531. else {
  532. if (pf->media_status == PF_RO)
  533. printk(", RO");
  534. printk(", %llu blocks\n",
  535. (unsigned long long)get_capacity(pf->disk));
  536. }
  537. return 0;
  538. }
  539. /* returns 0, with id set if drive is detected
  540. -1, if drive detection failed
  541. */
  542. static int pf_probe(struct pf_unit *pf)
  543. {
  544. if (pf->drive == -1) {
  545. for (pf->drive = 0; pf->drive <= 1; pf->drive++)
  546. if (!pf_reset(pf)) {
  547. if (pf->lun != -1)
  548. return pf_identify(pf);
  549. else
  550. for (pf->lun = 0; pf->lun < 8; pf->lun++)
  551. if (!pf_identify(pf))
  552. return 0;
  553. }
  554. } else {
  555. if (pf_reset(pf))
  556. return -1;
  557. if (pf->lun != -1)
  558. return pf_identify(pf);
  559. for (pf->lun = 0; pf->lun < 8; pf->lun++)
  560. if (!pf_identify(pf))
  561. return 0;
  562. }
  563. return -1;
  564. }
  565. static int pf_detect(void)
  566. {
  567. struct pf_unit *pf = units;
  568. int k, unit;
  569. printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
  570. name, name, PF_VERSION, major, cluster, nice);
  571. par_drv = pi_register_driver(name);
  572. if (!par_drv) {
  573. pr_err("failed to register %s driver\n", name);
  574. return -1;
  575. }
  576. k = 0;
  577. if (pf_drive_count == 0) {
  578. if (pi_init(pf->pi, 1, -1, -1, -1, -1, -1, pf_scratch, PI_PF,
  579. verbose, pf->name)) {
  580. if (!pf_probe(pf) && pf->disk) {
  581. pf->present = 1;
  582. k++;
  583. } else
  584. pi_release(pf->pi);
  585. }
  586. } else
  587. for (unit = 0; unit < PF_UNITS; unit++, pf++) {
  588. int *conf = *drives[unit];
  589. if (!conf[D_PRT])
  590. continue;
  591. if (pi_init(pf->pi, 0, conf[D_PRT], conf[D_MOD],
  592. conf[D_UNI], conf[D_PRO], conf[D_DLY],
  593. pf_scratch, PI_PF, verbose, pf->name)) {
  594. if (pf->disk && !pf_probe(pf)) {
  595. pf->present = 1;
  596. k++;
  597. } else
  598. pi_release(pf->pi);
  599. }
  600. }
  601. if (k)
  602. return 0;
  603. printk("%s: No ATAPI disk detected\n", name);
  604. for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++)
  605. put_disk(pf->disk);
  606. pi_unregister_driver(par_drv);
  607. return -1;
  608. }
  609. /* The i/o request engine */
  610. static int pf_start(struct pf_unit *pf, int cmd, int b, int c)
  611. {
  612. int i;
  613. char io_cmd[12] = { cmd, pf->lun << 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  614. for (i = 0; i < 4; i++) {
  615. io_cmd[5 - i] = b & 0xff;
  616. b = b >> 8;
  617. }
  618. io_cmd[8] = c & 0xff;
  619. io_cmd[7] = (c >> 8) & 0xff;
  620. i = pf_command(pf, io_cmd, c * 512, "start i/o");
  621. mdelay(1);
  622. return i;
  623. }
  624. static int pf_ready(void)
  625. {
  626. return (((status_reg(pf_current) & (STAT_BUSY | pf_mask)) == pf_mask));
  627. }
  628. static struct request_queue *pf_queue;
  629. static void pf_end_request(int err)
  630. {
  631. if (pf_req && !__blk_end_request_cur(pf_req, err))
  632. pf_req = NULL;
  633. }
  634. static void do_pf_request(struct request_queue * q)
  635. {
  636. if (pf_busy)
  637. return;
  638. repeat:
  639. if (!pf_req) {
  640. pf_req = blk_fetch_request(q);
  641. if (!pf_req)
  642. return;
  643. }
  644. pf_current = pf_req->rq_disk->private_data;
  645. pf_block = blk_rq_pos(pf_req);
  646. pf_run = blk_rq_sectors(pf_req);
  647. pf_count = blk_rq_cur_sectors(pf_req);
  648. if (pf_block + pf_count > get_capacity(pf_req->rq_disk)) {
  649. pf_end_request(-EIO);
  650. goto repeat;
  651. }
  652. pf_cmd = rq_data_dir(pf_req);
  653. pf_buf = bio_data(pf_req->bio);
  654. pf_retries = 0;
  655. pf_busy = 1;
  656. if (pf_cmd == READ)
  657. pi_do_claimed(pf_current->pi, do_pf_read);
  658. else if (pf_cmd == WRITE)
  659. pi_do_claimed(pf_current->pi, do_pf_write);
  660. else {
  661. pf_busy = 0;
  662. pf_end_request(-EIO);
  663. goto repeat;
  664. }
  665. }
  666. static int pf_next_buf(void)
  667. {
  668. unsigned long saved_flags;
  669. pf_count--;
  670. pf_run--;
  671. pf_buf += 512;
  672. pf_block++;
  673. if (!pf_run)
  674. return 1;
  675. if (!pf_count) {
  676. spin_lock_irqsave(&pf_spin_lock, saved_flags);
  677. pf_end_request(0);
  678. spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
  679. if (!pf_req)
  680. return 1;
  681. pf_count = blk_rq_cur_sectors(pf_req);
  682. pf_buf = bio_data(pf_req->bio);
  683. }
  684. return 0;
  685. }
  686. static inline void next_request(int err)
  687. {
  688. unsigned long saved_flags;
  689. spin_lock_irqsave(&pf_spin_lock, saved_flags);
  690. pf_end_request(err);
  691. pf_busy = 0;
  692. do_pf_request(pf_queue);
  693. spin_unlock_irqrestore(&pf_spin_lock, saved_flags);
  694. }
  695. /* detach from the calling context - in case the spinlock is held */
  696. static void do_pf_read(void)
  697. {
  698. ps_set_intr(do_pf_read_start, NULL, 0, nice);
  699. }
  700. static void do_pf_read_start(void)
  701. {
  702. pf_busy = 1;
  703. if (pf_start(pf_current, ATAPI_READ_10, pf_block, pf_run)) {
  704. pi_disconnect(pf_current->pi);
  705. if (pf_retries < PF_MAX_RETRIES) {
  706. pf_retries++;
  707. pi_do_claimed(pf_current->pi, do_pf_read_start);
  708. return;
  709. }
  710. next_request(-EIO);
  711. return;
  712. }
  713. pf_mask = STAT_DRQ;
  714. ps_set_intr(do_pf_read_drq, pf_ready, PF_TMO, nice);
  715. }
  716. static void do_pf_read_drq(void)
  717. {
  718. while (1) {
  719. if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
  720. "read block", "completion") & STAT_ERR) {
  721. pi_disconnect(pf_current->pi);
  722. if (pf_retries < PF_MAX_RETRIES) {
  723. pf_req_sense(pf_current, 0);
  724. pf_retries++;
  725. pi_do_claimed(pf_current->pi, do_pf_read_start);
  726. return;
  727. }
  728. next_request(-EIO);
  729. return;
  730. }
  731. pi_read_block(pf_current->pi, pf_buf, 512);
  732. if (pf_next_buf())
  733. break;
  734. }
  735. pi_disconnect(pf_current->pi);
  736. next_request(0);
  737. }
  738. static void do_pf_write(void)
  739. {
  740. ps_set_intr(do_pf_write_start, NULL, 0, nice);
  741. }
  742. static void do_pf_write_start(void)
  743. {
  744. pf_busy = 1;
  745. if (pf_start(pf_current, ATAPI_WRITE_10, pf_block, pf_run)) {
  746. pi_disconnect(pf_current->pi);
  747. if (pf_retries < PF_MAX_RETRIES) {
  748. pf_retries++;
  749. pi_do_claimed(pf_current->pi, do_pf_write_start);
  750. return;
  751. }
  752. next_request(-EIO);
  753. return;
  754. }
  755. while (1) {
  756. if (pf_wait(pf_current, STAT_BUSY, STAT_DRQ | STAT_ERR,
  757. "write block", "data wait") & STAT_ERR) {
  758. pi_disconnect(pf_current->pi);
  759. if (pf_retries < PF_MAX_RETRIES) {
  760. pf_retries++;
  761. pi_do_claimed(pf_current->pi, do_pf_write_start);
  762. return;
  763. }
  764. next_request(-EIO);
  765. return;
  766. }
  767. pi_write_block(pf_current->pi, pf_buf, 512);
  768. if (pf_next_buf())
  769. break;
  770. }
  771. pf_mask = 0;
  772. ps_set_intr(do_pf_write_done, pf_ready, PF_TMO, nice);
  773. }
  774. static void do_pf_write_done(void)
  775. {
  776. if (pf_wait(pf_current, STAT_BUSY, 0, "write block", "done") & STAT_ERR) {
  777. pi_disconnect(pf_current->pi);
  778. if (pf_retries < PF_MAX_RETRIES) {
  779. pf_retries++;
  780. pi_do_claimed(pf_current->pi, do_pf_write_start);
  781. return;
  782. }
  783. next_request(-EIO);
  784. return;
  785. }
  786. pi_disconnect(pf_current->pi);
  787. next_request(0);
  788. }
  789. static int __init pf_init(void)
  790. { /* preliminary initialisation */
  791. struct pf_unit *pf;
  792. int unit;
  793. if (disable)
  794. return -EINVAL;
  795. pf_init_units();
  796. if (pf_detect())
  797. return -ENODEV;
  798. pf_busy = 0;
  799. if (register_blkdev(major, name)) {
  800. for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++)
  801. put_disk(pf->disk);
  802. return -EBUSY;
  803. }
  804. pf_queue = blk_init_queue(do_pf_request, &pf_spin_lock);
  805. if (!pf_queue) {
  806. unregister_blkdev(major, name);
  807. for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++)
  808. put_disk(pf->disk);
  809. return -ENOMEM;
  810. }
  811. blk_queue_max_segments(pf_queue, cluster);
  812. for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++) {
  813. struct gendisk *disk = pf->disk;
  814. if (!pf->present)
  815. continue;
  816. disk->private_data = pf;
  817. disk->queue = pf_queue;
  818. add_disk(disk);
  819. }
  820. return 0;
  821. }
  822. static void __exit pf_exit(void)
  823. {
  824. struct pf_unit *pf;
  825. int unit;
  826. unregister_blkdev(major, name);
  827. for (pf = units, unit = 0; unit < PF_UNITS; pf++, unit++) {
  828. if (!pf->present)
  829. continue;
  830. del_gendisk(pf->disk);
  831. put_disk(pf->disk);
  832. pi_release(pf->pi);
  833. }
  834. blk_cleanup_queue(pf_queue);
  835. }
  836. MODULE_LICENSE("GPL");
  837. module_init(pf_init)
  838. module_exit(pf_exit)