Back to home page

OSCL-LXR

 
 

    


0001 /* 
0002     pcd.c   (c) 1997-8  Grant R. Guenther <grant@torque.net>
0003                     Under the terms of the GNU General Public License.
0004 
0005     This is a high-level driver for parallel port ATAPI CD-ROM
0006         drives based on chips supported by the paride module.
0007 
0008         By default, the driver will autoprobe for a single parallel
0009         port ATAPI CD-ROM drive, but if their individual parameters are
0010         specified, the driver can handle up to 4 drives.
0011 
0012         The behaviour of the pcd driver can be altered by setting
0013         some parameters from the insmod command line.  The following
0014         parameters are adjustable:
0015 
0016             drive0      These four arguments can be arrays of       
0017             drive1      1-6 integers as follows:
0018             drive2
0019             drive3      <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
0020 
0021                         Where,
0022 
0023                 <prt>   is the base of the parallel port address for
0024                         the corresponding drive.  (required)
0025 
0026                 <pro>   is the protocol number for the adapter that
0027                         supports this drive.  These numbers are
0028                         logged by 'paride' when the protocol modules
0029                         are initialised.  (0 if not given)
0030 
0031                 <uni>   for those adapters that support chained
0032                         devices, this is the unit selector for the
0033                         chain of devices on the given port.  It should
0034                         be zero for devices that don't support chaining.
0035                         (0 if not given)
0036 
0037                 <mod>   this can be -1 to choose the best mode, or one
0038                         of the mode numbers supported by the adapter.
0039                         (-1 if not given)
0040 
0041         <slv>   ATAPI CD-ROMs can be jumpered to master or slave.
0042             Set this to 0 to choose the master drive, 1 to
0043                         choose the slave, -1 (the default) to choose the
0044             first drive found.
0045 
0046                 <dly>   some parallel ports require the driver to 
0047                         go more slowly.  -1 sets a default value that
0048                         should work with the chosen protocol.  Otherwise,
0049                         set this to a small integer, the larger it is
0050                         the slower the port i/o.  In some cases, setting
0051                         this to zero will speed up the device. (default -1)
0052                         
0053             major       You may use this parameter to override the
0054                         default major number (46) that this driver
0055                         will use.  Be sure to change the device
0056                         name as well.
0057 
0058             name        This parameter is a character string that
0059                         contains the name the kernel will use for this
0060                         device (in /proc output, for instance).
0061                         (default "pcd")
0062 
0063             verbose     This parameter controls the amount of logging
0064                         that the driver will do.  Set it to 0 for
0065                         normal operation, 1 to see autoprobe progress
0066                         messages, or 2 to see additional debugging
0067                         output.  (default 0)
0068   
0069             nice        This parameter controls the driver's use of
0070                         idle CPU time, at the expense of some speed.
0071  
0072     If this driver is built into the kernel, you can use the
0073         following kernel command line parameters, with the same values
0074         as the corresponding module parameters listed above:
0075 
0076         pcd.drive0
0077         pcd.drive1
0078         pcd.drive2
0079         pcd.drive3
0080         pcd.nice
0081 
0082         In addition, you can use the parameter pcd.disable to disable
0083         the driver entirely.
0084 
0085 */
0086 
0087 /* Changes:
0088 
0089     1.01    GRG 1998.01.24  Added test unit ready support
0090     1.02    GRG 1998.05.06  Changes to pcd_completion, ready_wait,
0091                 and loosen interpretation of ATAPI
0092                     standard for clearing error status.
0093                 Use spinlocks. Eliminate sti().
0094     1.03    GRG 1998.06.16  Eliminated an Ugh
0095     1.04    GRG 1998.08.15  Added extra debugging, improvements to
0096                 pcd_completion, use HZ in loop timing
0097     1.05    GRG 1998.08.16  Conformed to "Uniform CD-ROM" standard
0098     1.06    GRG 1998.08.19  Added audio ioctl support
0099     1.07    GRG 1998.09.24  Increased reset timeout, added jumbo support
0100 
0101 */
0102 
0103 #define PCD_VERSION "1.07"
0104 #define PCD_MAJOR   46
0105 #define PCD_NAME    "pcd"
0106 #define PCD_UNITS   4
0107 
0108 /* Here are things one can override from the insmod command.
0109    Most are autoprobed by paride unless set here.  Verbose is off
0110    by default.
0111 
0112 */
0113 
0114 static int verbose = 0;
0115 static int major = PCD_MAJOR;
0116 static char *name = PCD_NAME;
0117 static int nice = 0;
0118 static int disable = 0;
0119 
0120 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
0121 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
0122 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
0123 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
0124 
0125 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
0126 static int pcd_drive_count;
0127 
0128 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
0129 
0130 /* end of parameters */
0131 
0132 #include <linux/module.h>
0133 #include <linux/init.h>
0134 #include <linux/errno.h>
0135 #include <linux/fs.h>
0136 #include <linux/kernel.h>
0137 #include <linux/delay.h>
0138 #include <linux/cdrom.h>
0139 #include <linux/spinlock.h>
0140 #include <linux/blk-mq.h>
0141 #include <linux/mutex.h>
0142 #include <linux/uaccess.h>
0143 
0144 static DEFINE_MUTEX(pcd_mutex);
0145 static DEFINE_SPINLOCK(pcd_lock);
0146 
0147 module_param(verbose, int, 0644);
0148 module_param(major, int, 0);
0149 module_param(name, charp, 0);
0150 module_param(nice, int, 0);
0151 module_param_array(drive0, int, NULL, 0);
0152 module_param_array(drive1, int, NULL, 0);
0153 module_param_array(drive2, int, NULL, 0);
0154 module_param_array(drive3, int, NULL, 0);
0155 
0156 #include "paride.h"
0157 #include "pseudo.h"
0158 
0159 #define PCD_RETRIES      5
0160 #define PCD_TMO        800  /* timeout in jiffies */
0161 #define PCD_DELAY           50  /* spin delay in uS */
0162 #define PCD_READY_TMO       20  /* in seconds */
0163 #define PCD_RESET_TMO      100  /* in tenths of a second */
0164 
0165 #define PCD_SPIN    (1000000*PCD_TMO)/(HZ*PCD_DELAY)
0166 
0167 #define IDE_ERR     0x01
0168 #define IDE_DRQ         0x08
0169 #define IDE_READY       0x40
0170 #define IDE_BUSY        0x80
0171 
0172 static int pcd_open(struct cdrom_device_info *cdi, int purpose);
0173 static void pcd_release(struct cdrom_device_info *cdi);
0174 static int pcd_drive_status(struct cdrom_device_info *cdi, int slot_nr);
0175 static unsigned int pcd_check_events(struct cdrom_device_info *cdi,
0176                      unsigned int clearing, int slot_nr);
0177 static int pcd_tray_move(struct cdrom_device_info *cdi, int position);
0178 static int pcd_lock_door(struct cdrom_device_info *cdi, int lock);
0179 static int pcd_drive_reset(struct cdrom_device_info *cdi);
0180 static int pcd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn);
0181 static int pcd_audio_ioctl(struct cdrom_device_info *cdi,
0182                unsigned int cmd, void *arg);
0183 static int pcd_packet(struct cdrom_device_info *cdi,
0184               struct packet_command *cgc);
0185 
0186 static void do_pcd_read_drq(void);
0187 static blk_status_t pcd_queue_rq(struct blk_mq_hw_ctx *hctx,
0188                  const struct blk_mq_queue_data *bd);
0189 static void do_pcd_read(void);
0190 
0191 struct pcd_unit {
0192     struct pi_adapter pia;  /* interface to paride layer */
0193     struct pi_adapter *pi;
0194     int drive;      /* master/slave */
0195     int last_sense;     /* result of last request sense */
0196     int changed;        /* media change seen */
0197     int present;        /* does this unit exist ? */
0198     char *name;     /* pcd0, pcd1, etc */
0199     struct cdrom_device_info info;  /* uniform cdrom interface */
0200     struct gendisk *disk;
0201     struct blk_mq_tag_set tag_set;
0202     struct list_head rq_list;
0203 };
0204 
0205 static struct pcd_unit pcd[PCD_UNITS];
0206 
0207 static char pcd_scratch[64];
0208 static char pcd_buffer[2048];   /* raw block buffer */
0209 static int pcd_bufblk = -1; /* block in buffer, in CD units,
0210                    -1 for nothing there. See also
0211                    pd_unit.
0212                  */
0213 
0214 /* the variables below are used mainly in the I/O request engine, which
0215    processes only one request at a time.
0216 */
0217 
0218 static struct pcd_unit *pcd_current; /* current request's drive */
0219 static struct request *pcd_req;
0220 static int pcd_retries;     /* retries on current request */
0221 static int pcd_busy;        /* request being processed ? */
0222 static int pcd_sector;      /* address of next requested sector */
0223 static int pcd_count;       /* number of blocks still to do */
0224 static char *pcd_buf;       /* buffer for request in progress */
0225 static void *par_drv;       /* reference of parport driver */
0226 
0227 /* kernel glue structures */
0228 
0229 static int pcd_block_open(struct block_device *bdev, fmode_t mode)
0230 {
0231     struct pcd_unit *cd = bdev->bd_disk->private_data;
0232     int ret;
0233 
0234     bdev_check_media_change(bdev);
0235 
0236     mutex_lock(&pcd_mutex);
0237     ret = cdrom_open(&cd->info, bdev, mode);
0238     mutex_unlock(&pcd_mutex);
0239 
0240     return ret;
0241 }
0242 
0243 static void pcd_block_release(struct gendisk *disk, fmode_t mode)
0244 {
0245     struct pcd_unit *cd = disk->private_data;
0246     mutex_lock(&pcd_mutex);
0247     cdrom_release(&cd->info, mode);
0248     mutex_unlock(&pcd_mutex);
0249 }
0250 
0251 static int pcd_block_ioctl(struct block_device *bdev, fmode_t mode,
0252                 unsigned cmd, unsigned long arg)
0253 {
0254     struct pcd_unit *cd = bdev->bd_disk->private_data;
0255     int ret;
0256 
0257     mutex_lock(&pcd_mutex);
0258     ret = cdrom_ioctl(&cd->info, bdev, mode, cmd, arg);
0259     mutex_unlock(&pcd_mutex);
0260 
0261     return ret;
0262 }
0263 
0264 static unsigned int pcd_block_check_events(struct gendisk *disk,
0265                        unsigned int clearing)
0266 {
0267     struct pcd_unit *cd = disk->private_data;
0268     return cdrom_check_events(&cd->info, clearing);
0269 }
0270 
0271 static const struct block_device_operations pcd_bdops = {
0272     .owner      = THIS_MODULE,
0273     .open       = pcd_block_open,
0274     .release    = pcd_block_release,
0275     .ioctl      = pcd_block_ioctl,
0276 #ifdef CONFIG_COMPAT
0277     .compat_ioctl   = blkdev_compat_ptr_ioctl,
0278 #endif
0279     .check_events   = pcd_block_check_events,
0280 };
0281 
0282 static const struct cdrom_device_ops pcd_dops = {
0283     .open       = pcd_open,
0284     .release    = pcd_release,
0285     .drive_status   = pcd_drive_status,
0286     .check_events   = pcd_check_events,
0287     .tray_move  = pcd_tray_move,
0288     .lock_door  = pcd_lock_door,
0289     .get_mcn    = pcd_get_mcn,
0290     .reset      = pcd_drive_reset,
0291     .audio_ioctl    = pcd_audio_ioctl,
0292     .generic_packet = pcd_packet,
0293     .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
0294               CDC_MCN | CDC_MEDIA_CHANGED | CDC_RESET |
0295               CDC_PLAY_AUDIO | CDC_GENERIC_PACKET | CDC_CD_R |
0296               CDC_CD_RW,
0297 };
0298 
0299 static const struct blk_mq_ops pcd_mq_ops = {
0300     .queue_rq   = pcd_queue_rq,
0301 };
0302 
0303 static int pcd_open(struct cdrom_device_info *cdi, int purpose)
0304 {
0305     struct pcd_unit *cd = cdi->handle;
0306     if (!cd->present)
0307         return -ENODEV;
0308     return 0;
0309 }
0310 
0311 static void pcd_release(struct cdrom_device_info *cdi)
0312 {
0313 }
0314 
0315 static inline int status_reg(struct pcd_unit *cd)
0316 {
0317     return pi_read_regr(cd->pi, 1, 6);
0318 }
0319 
0320 static inline int read_reg(struct pcd_unit *cd, int reg)
0321 {
0322     return pi_read_regr(cd->pi, 0, reg);
0323 }
0324 
0325 static inline void write_reg(struct pcd_unit *cd, int reg, int val)
0326 {
0327     pi_write_regr(cd->pi, 0, reg, val);
0328 }
0329 
0330 static int pcd_wait(struct pcd_unit *cd, int go, int stop, char *fun, char *msg)
0331 {
0332     int j, r, e, s, p;
0333 
0334     j = 0;
0335     while ((((r = status_reg(cd)) & go) || (stop && (!(r & stop))))
0336            && (j++ < PCD_SPIN))
0337         udelay(PCD_DELAY);
0338 
0339     if ((r & (IDE_ERR & stop)) || (j > PCD_SPIN)) {
0340         s = read_reg(cd, 7);
0341         e = read_reg(cd, 1);
0342         p = read_reg(cd, 2);
0343         if (j > PCD_SPIN)
0344             e |= 0x100;
0345         if (fun)
0346             printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
0347                    " loop=%d phase=%d\n",
0348                    cd->name, fun, msg, r, s, e, j, p);
0349         return (s << 8) + r;
0350     }
0351     return 0;
0352 }
0353 
0354 static int pcd_command(struct pcd_unit *cd, char *cmd, int dlen, char *fun)
0355 {
0356     pi_connect(cd->pi);
0357 
0358     write_reg(cd, 6, 0xa0 + 0x10 * cd->drive);
0359 
0360     if (pcd_wait(cd, IDE_BUSY | IDE_DRQ, 0, fun, "before command")) {
0361         pi_disconnect(cd->pi);
0362         return -1;
0363     }
0364 
0365     write_reg(cd, 4, dlen % 256);
0366     write_reg(cd, 5, dlen / 256);
0367     write_reg(cd, 7, 0xa0); /* ATAPI packet command */
0368 
0369     if (pcd_wait(cd, IDE_BUSY, IDE_DRQ, fun, "command DRQ")) {
0370         pi_disconnect(cd->pi);
0371         return -1;
0372     }
0373 
0374     if (read_reg(cd, 2) != 1) {
0375         printk("%s: %s: command phase error\n", cd->name, fun);
0376         pi_disconnect(cd->pi);
0377         return -1;
0378     }
0379 
0380     pi_write_block(cd->pi, cmd, 12);
0381 
0382     return 0;
0383 }
0384 
0385 static int pcd_completion(struct pcd_unit *cd, char *buf, char *fun)
0386 {
0387     int r, d, p, n, k, j;
0388 
0389     r = -1;
0390     k = 0;
0391     j = 0;
0392 
0393     if (!pcd_wait(cd, IDE_BUSY, IDE_DRQ | IDE_READY | IDE_ERR,
0394               fun, "completion")) {
0395         r = 0;
0396         while (read_reg(cd, 7) & IDE_DRQ) {
0397             d = read_reg(cd, 4) + 256 * read_reg(cd, 5);
0398             n = (d + 3) & 0xfffc;
0399             p = read_reg(cd, 2) & 3;
0400 
0401             if ((p == 2) && (n > 0) && (j == 0)) {
0402                 pi_read_block(cd->pi, buf, n);
0403                 if (verbose > 1)
0404                     printk("%s: %s: Read %d bytes\n",
0405                            cd->name, fun, n);
0406                 r = 0;
0407                 j++;
0408             } else {
0409                 if (verbose > 1)
0410                     printk
0411                         ("%s: %s: Unexpected phase %d, d=%d, k=%d\n",
0412                          cd->name, fun, p, d, k);
0413                 if (verbose < 2)
0414                     printk_once(
0415                         "%s: WARNING: ATAPI phase errors\n",
0416                         cd->name);
0417                 mdelay(1);
0418             }
0419             if (k++ > PCD_TMO) {
0420                 printk("%s: Stuck DRQ\n", cd->name);
0421                 break;
0422             }
0423             if (pcd_wait
0424                 (cd, IDE_BUSY, IDE_DRQ | IDE_READY | IDE_ERR, fun,
0425                  "completion")) {
0426                 r = -1;
0427                 break;
0428             }
0429         }
0430     }
0431 
0432     pi_disconnect(cd->pi);
0433 
0434     return r;
0435 }
0436 
0437 static void pcd_req_sense(struct pcd_unit *cd, char *fun)
0438 {
0439     char rs_cmd[12] = { 0x03, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
0440     char buf[16];
0441     int r, c;
0442 
0443     r = pcd_command(cd, rs_cmd, 16, "Request sense");
0444     mdelay(1);
0445     if (!r)
0446         pcd_completion(cd, buf, "Request sense");
0447 
0448     cd->last_sense = -1;
0449     c = 2;
0450     if (!r) {
0451         if (fun)
0452             printk("%s: %s: Sense key: %x, ASC: %x, ASQ: %x\n",
0453                    cd->name, fun, buf[2] & 0xf, buf[12], buf[13]);
0454         c = buf[2] & 0xf;
0455         cd->last_sense =
0456             c | ((buf[12] & 0xff) << 8) | ((buf[13] & 0xff) << 16);
0457     }
0458     if ((c == 2) || (c == 6))
0459         cd->changed = 1;
0460 }
0461 
0462 static int pcd_atapi(struct pcd_unit *cd, char *cmd, int dlen, char *buf, char *fun)
0463 {
0464     int r;
0465 
0466     r = pcd_command(cd, cmd, dlen, fun);
0467     mdelay(1);
0468     if (!r)
0469         r = pcd_completion(cd, buf, fun);
0470     if (r)
0471         pcd_req_sense(cd, fun);
0472 
0473     return r;
0474 }
0475 
0476 static int pcd_packet(struct cdrom_device_info *cdi, struct packet_command *cgc)
0477 {
0478     return pcd_atapi(cdi->handle, cgc->cmd, cgc->buflen, cgc->buffer,
0479              "generic packet");
0480 }
0481 
0482 #define DBMSG(msg)  ((verbose>1)?(msg):NULL)
0483 
0484 static unsigned int pcd_check_events(struct cdrom_device_info *cdi,
0485                      unsigned int clearing, int slot_nr)
0486 {
0487     struct pcd_unit *cd = cdi->handle;
0488     int res = cd->changed;
0489     if (res)
0490         cd->changed = 0;
0491     return res ? DISK_EVENT_MEDIA_CHANGE : 0;
0492 }
0493 
0494 static int pcd_lock_door(struct cdrom_device_info *cdi, int lock)
0495 {
0496     char un_cmd[12] = { 0x1e, 0, 0, 0, lock, 0, 0, 0, 0, 0, 0, 0 };
0497 
0498     return pcd_atapi(cdi->handle, un_cmd, 0, pcd_scratch,
0499              lock ? "lock door" : "unlock door");
0500 }
0501 
0502 static int pcd_tray_move(struct cdrom_device_info *cdi, int position)
0503 {
0504     char ej_cmd[12] = { 0x1b, 0, 0, 0, 3 - position, 0, 0, 0, 0, 0, 0, 0 };
0505 
0506     return pcd_atapi(cdi->handle, ej_cmd, 0, pcd_scratch,
0507              position ? "eject" : "close tray");
0508 }
0509 
0510 static void pcd_sleep(int cs)
0511 {
0512     schedule_timeout_interruptible(cs);
0513 }
0514 
0515 static int pcd_reset(struct pcd_unit *cd)
0516 {
0517     int i, k, flg;
0518     int expect[5] = { 1, 1, 1, 0x14, 0xeb };
0519 
0520     pi_connect(cd->pi);
0521     write_reg(cd, 6, 0xa0 + 0x10 * cd->drive);
0522     write_reg(cd, 7, 8);
0523 
0524     pcd_sleep(20 * HZ / 1000);  /* delay a bit */
0525 
0526     k = 0;
0527     while ((k++ < PCD_RESET_TMO) && (status_reg(cd) & IDE_BUSY))
0528         pcd_sleep(HZ / 10);
0529 
0530     flg = 1;
0531     for (i = 0; i < 5; i++)
0532         flg &= (read_reg(cd, i + 1) == expect[i]);
0533 
0534     if (verbose) {
0535         printk("%s: Reset (%d) signature = ", cd->name, k);
0536         for (i = 0; i < 5; i++)
0537             printk("%3x", read_reg(cd, i + 1));
0538         if (!flg)
0539             printk(" (incorrect)");
0540         printk("\n");
0541     }
0542 
0543     pi_disconnect(cd->pi);
0544     return flg - 1;
0545 }
0546 
0547 static int pcd_drive_reset(struct cdrom_device_info *cdi)
0548 {
0549     return pcd_reset(cdi->handle);
0550 }
0551 
0552 static int pcd_ready_wait(struct pcd_unit *cd, int tmo)
0553 {
0554     char tr_cmd[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0555     int k, p;
0556 
0557     k = 0;
0558     while (k < tmo) {
0559         cd->last_sense = 0;
0560         pcd_atapi(cd, tr_cmd, 0, NULL, DBMSG("test unit ready"));
0561         p = cd->last_sense;
0562         if (!p)
0563             return 0;
0564         if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
0565             return p;
0566         k++;
0567         pcd_sleep(HZ);
0568     }
0569     return 0x000020;    /* timeout */
0570 }
0571 
0572 static int pcd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
0573 {
0574     char rc_cmd[12] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0575     struct pcd_unit *cd = cdi->handle;
0576 
0577     if (pcd_ready_wait(cd, PCD_READY_TMO))
0578         return CDS_DRIVE_NOT_READY;
0579     if (pcd_atapi(cd, rc_cmd, 8, pcd_scratch, DBMSG("check media")))
0580         return CDS_NO_DISC;
0581     return CDS_DISC_OK;
0582 }
0583 
0584 static int pcd_identify(struct pcd_unit *cd)
0585 {
0586     char id_cmd[12] = { 0x12, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
0587     char id[18];
0588     int k, s;
0589 
0590     pcd_bufblk = -1;
0591 
0592     s = pcd_atapi(cd, id_cmd, 36, pcd_buffer, "identify");
0593 
0594     if (s)
0595         return -1;
0596     if ((pcd_buffer[0] & 0x1f) != 5) {
0597         if (verbose)
0598             printk("%s: %s is not a CD-ROM\n",
0599                    cd->name, cd->drive ? "Slave" : "Master");
0600         return -1;
0601     }
0602     memcpy(id, pcd_buffer + 16, 16);
0603     id[16] = 0;
0604     k = 16;
0605     while ((k >= 0) && (id[k] <= 0x20)) {
0606         id[k] = 0;
0607         k--;
0608     }
0609 
0610     printk("%s: %s: %s\n", cd->name, cd->drive ? "Slave" : "Master", id);
0611 
0612     return 0;
0613 }
0614 
0615 /*
0616  * returns 0, with id set if drive is detected, otherwise an error code.
0617  */
0618 static int pcd_probe(struct pcd_unit *cd, int ms)
0619 {
0620     if (ms == -1) {
0621         for (cd->drive = 0; cd->drive <= 1; cd->drive++)
0622             if (!pcd_reset(cd) && !pcd_identify(cd))
0623                 return 0;
0624     } else {
0625         cd->drive = ms;
0626         if (!pcd_reset(cd) && !pcd_identify(cd))
0627             return 0;
0628     }
0629     return -ENODEV;
0630 }
0631 
0632 static int pcd_probe_capabilities(struct pcd_unit *cd)
0633 {
0634     char cmd[12] = { 0x5a, 1 << 3, 0x2a, 0, 0, 0, 0, 18, 0, 0, 0, 0 };
0635     char buffer[32];
0636     int ret;
0637 
0638     ret = pcd_atapi(cd, cmd, 18, buffer, "mode sense capabilities");
0639     if (ret)
0640         return ret;
0641 
0642     /* we should now have the cap page */
0643     if ((buffer[11] & 1) == 0)
0644         cd->info.mask |= CDC_CD_R;
0645     if ((buffer[11] & 2) == 0)
0646         cd->info.mask |= CDC_CD_RW;
0647     if ((buffer[12] & 1) == 0)
0648         cd->info.mask |= CDC_PLAY_AUDIO;
0649     if ((buffer[14] & 1) == 0)
0650         cd->info.mask |= CDC_LOCK;
0651     if ((buffer[14] & 8) == 0)
0652         cd->info.mask |= CDC_OPEN_TRAY;
0653     if ((buffer[14] >> 6) == 0)
0654         cd->info.mask |= CDC_CLOSE_TRAY;
0655 
0656     return 0;
0657 }
0658 
0659 /* I/O request processing */
0660 static int pcd_queue;
0661 
0662 static int set_next_request(void)
0663 {
0664     struct pcd_unit *cd;
0665     int old_pos = pcd_queue;
0666 
0667     do {
0668         cd = &pcd[pcd_queue];
0669         if (++pcd_queue == PCD_UNITS)
0670             pcd_queue = 0;
0671         if (cd->present && !list_empty(&cd->rq_list)) {
0672             pcd_req = list_first_entry(&cd->rq_list, struct request,
0673                             queuelist);
0674             list_del_init(&pcd_req->queuelist);
0675             blk_mq_start_request(pcd_req);
0676             break;
0677         }
0678     } while (pcd_queue != old_pos);
0679 
0680     return pcd_req != NULL;
0681 }
0682 
0683 static void pcd_request(void)
0684 {
0685     struct pcd_unit *cd;
0686 
0687     if (pcd_busy)
0688         return;
0689 
0690     if (!pcd_req && !set_next_request())
0691         return;
0692 
0693     cd = pcd_req->q->disk->private_data;
0694     if (cd != pcd_current)
0695         pcd_bufblk = -1;
0696     pcd_current = cd;
0697     pcd_sector = blk_rq_pos(pcd_req);
0698     pcd_count = blk_rq_cur_sectors(pcd_req);
0699     pcd_buf = bio_data(pcd_req->bio);
0700     pcd_busy = 1;
0701     ps_set_intr(do_pcd_read, NULL, 0, nice);
0702 }
0703 
0704 static blk_status_t pcd_queue_rq(struct blk_mq_hw_ctx *hctx,
0705                  const struct blk_mq_queue_data *bd)
0706 {
0707     struct pcd_unit *cd = hctx->queue->queuedata;
0708 
0709     if (rq_data_dir(bd->rq) != READ) {
0710         blk_mq_start_request(bd->rq);
0711         return BLK_STS_IOERR;
0712     }
0713 
0714     spin_lock_irq(&pcd_lock);
0715     list_add_tail(&bd->rq->queuelist, &cd->rq_list);
0716     pcd_request();
0717     spin_unlock_irq(&pcd_lock);
0718 
0719     return BLK_STS_OK;
0720 }
0721 
0722 static inline void next_request(blk_status_t err)
0723 {
0724     unsigned long saved_flags;
0725 
0726     spin_lock_irqsave(&pcd_lock, saved_flags);
0727     if (!blk_update_request(pcd_req, err, blk_rq_cur_bytes(pcd_req))) {
0728         __blk_mq_end_request(pcd_req, err);
0729         pcd_req = NULL;
0730     }
0731     pcd_busy = 0;
0732     pcd_request();
0733     spin_unlock_irqrestore(&pcd_lock, saved_flags);
0734 }
0735 
0736 static int pcd_ready(void)
0737 {
0738     return (((status_reg(pcd_current) & (IDE_BUSY | IDE_DRQ)) == IDE_DRQ));
0739 }
0740 
0741 static void pcd_transfer(void)
0742 {
0743 
0744     while (pcd_count && (pcd_sector / 4 == pcd_bufblk)) {
0745         int o = (pcd_sector % 4) * 512;
0746         memcpy(pcd_buf, pcd_buffer + o, 512);
0747         pcd_count--;
0748         pcd_buf += 512;
0749         pcd_sector++;
0750     }
0751 }
0752 
0753 static void pcd_start(void)
0754 {
0755     int b, i;
0756     char rd_cmd[12] = { 0xa8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
0757 
0758     pcd_bufblk = pcd_sector / 4;
0759     b = pcd_bufblk;
0760     for (i = 0; i < 4; i++) {
0761         rd_cmd[5 - i] = b & 0xff;
0762         b = b >> 8;
0763     }
0764 
0765     if (pcd_command(pcd_current, rd_cmd, 2048, "read block")) {
0766         pcd_bufblk = -1;
0767         next_request(BLK_STS_IOERR);
0768         return;
0769     }
0770 
0771     mdelay(1);
0772 
0773     ps_set_intr(do_pcd_read_drq, pcd_ready, PCD_TMO, nice);
0774 }
0775 
0776 static void do_pcd_read(void)
0777 {
0778     pcd_busy = 1;
0779     pcd_retries = 0;
0780     pcd_transfer();
0781     if (!pcd_count) {
0782         next_request(0);
0783         return;
0784     }
0785 
0786     pi_do_claimed(pcd_current->pi, pcd_start);
0787 }
0788 
0789 static void do_pcd_read_drq(void)
0790 {
0791     unsigned long saved_flags;
0792 
0793     if (pcd_completion(pcd_current, pcd_buffer, "read block")) {
0794         if (pcd_retries < PCD_RETRIES) {
0795             mdelay(1);
0796             pcd_retries++;
0797             pi_do_claimed(pcd_current->pi, pcd_start);
0798             return;
0799         }
0800         pcd_bufblk = -1;
0801         next_request(BLK_STS_IOERR);
0802         return;
0803     }
0804 
0805     do_pcd_read();
0806     spin_lock_irqsave(&pcd_lock, saved_flags);
0807     pcd_request();
0808     spin_unlock_irqrestore(&pcd_lock, saved_flags);
0809 }
0810 
0811 /* the audio_ioctl stuff is adapted from sr_ioctl.c */
0812 
0813 static int pcd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
0814 {
0815     struct pcd_unit *cd = cdi->handle;
0816 
0817     switch (cmd) {
0818 
0819     case CDROMREADTOCHDR:
0820 
0821         {
0822             char cmd[12] =
0823                 { GPCMD_READ_TOC_PMA_ATIP, 0, 0, 0, 0, 0, 0, 0, 12,
0824              0, 0, 0 };
0825             struct cdrom_tochdr *tochdr =
0826                 (struct cdrom_tochdr *) arg;
0827             char buffer[32];
0828             int r;
0829 
0830             r = pcd_atapi(cd, cmd, 12, buffer, "read toc header");
0831 
0832             tochdr->cdth_trk0 = buffer[2];
0833             tochdr->cdth_trk1 = buffer[3];
0834 
0835             return r ? -EIO : 0;
0836         }
0837 
0838     case CDROMREADTOCENTRY:
0839 
0840         {
0841             char cmd[12] =
0842                 { GPCMD_READ_TOC_PMA_ATIP, 0, 0, 0, 0, 0, 0, 0, 12,
0843              0, 0, 0 };
0844 
0845             struct cdrom_tocentry *tocentry =
0846                 (struct cdrom_tocentry *) arg;
0847             unsigned char buffer[32];
0848             int r;
0849 
0850             cmd[1] =
0851                 (tocentry->cdte_format == CDROM_MSF ? 0x02 : 0);
0852             cmd[6] = tocentry->cdte_track;
0853 
0854             r = pcd_atapi(cd, cmd, 12, buffer, "read toc entry");
0855 
0856             tocentry->cdte_ctrl = buffer[5] & 0xf;
0857             tocentry->cdte_adr = buffer[5] >> 4;
0858             tocentry->cdte_datamode =
0859                 (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
0860             if (tocentry->cdte_format == CDROM_MSF) {
0861                 tocentry->cdte_addr.msf.minute = buffer[9];
0862                 tocentry->cdte_addr.msf.second = buffer[10];
0863                 tocentry->cdte_addr.msf.frame = buffer[11];
0864             } else
0865                 tocentry->cdte_addr.lba =
0866                     (((((buffer[8] << 8) + buffer[9]) << 8)
0867                       + buffer[10]) << 8) + buffer[11];
0868 
0869             return r ? -EIO : 0;
0870         }
0871 
0872     default:
0873 
0874         return -ENOSYS;
0875     }
0876 }
0877 
0878 static int pcd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
0879 {
0880     char cmd[12] =
0881         { GPCMD_READ_SUBCHANNEL, 0, 0x40, 2, 0, 0, 0, 0, 24, 0, 0, 0 };
0882     char buffer[32];
0883 
0884     if (pcd_atapi(cdi->handle, cmd, 24, buffer, "get mcn"))
0885         return -EIO;
0886 
0887     memcpy(mcn->medium_catalog_number, buffer + 9, 13);
0888     mcn->medium_catalog_number[13] = 0;
0889 
0890     return 0;
0891 }
0892 
0893 static int pcd_init_unit(struct pcd_unit *cd, bool autoprobe, int port,
0894         int mode, int unit, int protocol, int delay, int ms)
0895 {
0896     struct gendisk *disk;
0897     int ret;
0898 
0899     ret = blk_mq_alloc_sq_tag_set(&cd->tag_set, &pcd_mq_ops, 1,
0900                       BLK_MQ_F_SHOULD_MERGE);
0901     if (ret)
0902         return ret;
0903 
0904     disk = blk_mq_alloc_disk(&cd->tag_set, cd);
0905     if (IS_ERR(disk)) {
0906         ret = PTR_ERR(disk);
0907         goto out_free_tag_set;
0908     }
0909 
0910     INIT_LIST_HEAD(&cd->rq_list);
0911     blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
0912     cd->disk = disk;
0913     cd->pi = &cd->pia;
0914     cd->present = 0;
0915     cd->last_sense = 0;
0916     cd->changed = 1;
0917     cd->drive = (*drives[cd - pcd])[D_SLV];
0918 
0919     cd->name = &cd->info.name[0];
0920     snprintf(cd->name, sizeof(cd->info.name), "%s%d", name, unit);
0921     cd->info.ops = &pcd_dops;
0922     cd->info.handle = cd;
0923     cd->info.speed = 0;
0924     cd->info.capacity = 1;
0925     cd->info.mask = 0;
0926     disk->major = major;
0927     disk->first_minor = unit;
0928     disk->minors = 1;
0929     strcpy(disk->disk_name, cd->name);  /* umm... */
0930     disk->fops = &pcd_bdops;
0931     disk->flags |= GENHD_FL_NO_PART;
0932     disk->events = DISK_EVENT_MEDIA_CHANGE;
0933     disk->event_flags = DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE;
0934 
0935     if (!pi_init(cd->pi, autoprobe, port, mode, unit, protocol, delay,
0936             pcd_buffer, PI_PCD, verbose, cd->name)) {
0937         ret = -ENODEV;
0938         goto out_free_disk;
0939     }
0940     ret = pcd_probe(cd, ms);
0941     if (ret)
0942         goto out_pi_release;
0943 
0944     cd->present = 1;
0945     pcd_probe_capabilities(cd);
0946     ret = register_cdrom(cd->disk, &cd->info);
0947     if (ret)
0948         goto out_pi_release;
0949     ret = add_disk(cd->disk);
0950     if (ret)
0951         goto out_unreg_cdrom;
0952     return 0;
0953 
0954 out_unreg_cdrom:
0955     unregister_cdrom(&cd->info);
0956 out_pi_release:
0957     pi_release(cd->pi);
0958 out_free_disk:
0959     put_disk(cd->disk);
0960 out_free_tag_set:
0961     blk_mq_free_tag_set(&cd->tag_set);
0962     return ret;
0963 }
0964 
0965 static int __init pcd_init(void)
0966 {
0967     int found = 0, unit;
0968 
0969     if (disable)
0970         return -EINVAL;
0971 
0972     if (register_blkdev(major, name))
0973         return -EBUSY;
0974 
0975     pr_info("%s: %s version %s, major %d, nice %d\n",
0976         name, name, PCD_VERSION, major, nice);
0977 
0978     par_drv = pi_register_driver(name);
0979     if (!par_drv) {
0980         pr_err("failed to register %s driver\n", name);
0981         goto out_unregister_blkdev;
0982     }
0983 
0984     for (unit = 0; unit < PCD_UNITS; unit++) {
0985         if ((*drives[unit])[D_PRT])
0986             pcd_drive_count++;
0987     }
0988 
0989     if (pcd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
0990         if (!pcd_init_unit(pcd, 1, -1, -1, -1, -1, -1, -1))
0991             found++;
0992     } else {
0993         for (unit = 0; unit < PCD_UNITS; unit++) {
0994             struct pcd_unit *cd = &pcd[unit];
0995             int *conf = *drives[unit];
0996 
0997             if (!conf[D_PRT])
0998                 continue;
0999             if (!pcd_init_unit(cd, 0, conf[D_PRT], conf[D_MOD],
1000                     conf[D_UNI], conf[D_PRO], conf[D_DLY],
1001                     conf[D_SLV]))
1002                 found++;
1003         }
1004     }
1005 
1006     if (!found) {
1007         pr_info("%s: No CD-ROM drive found\n", name);
1008         goto out_unregister_pi_driver;
1009     }
1010 
1011     return 0;
1012 
1013 out_unregister_pi_driver:
1014     pi_unregister_driver(par_drv);
1015 out_unregister_blkdev:
1016     unregister_blkdev(major, name);
1017     return -ENODEV;
1018 }
1019 
1020 static void __exit pcd_exit(void)
1021 {
1022     struct pcd_unit *cd;
1023     int unit;
1024 
1025     for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
1026         if (!cd->present)
1027             continue;
1028 
1029         unregister_cdrom(&cd->info);
1030         del_gendisk(cd->disk);
1031         pi_release(cd->pi);
1032         put_disk(cd->disk);
1033 
1034         blk_mq_free_tag_set(&cd->tag_set);
1035     }
1036     pi_unregister_driver(par_drv);
1037     unregister_blkdev(major, name);
1038 }
1039 
1040 MODULE_LICENSE("GPL");
1041 module_init(pcd_init)
1042 module_exit(pcd_exit)