172f2b0b2SOndrej Zary // SPDX-License-Identifier: GPL-2.0-only
272f2b0b2SOndrej Zary /*
372f2b0b2SOndrej Zary * Copyright 2023 Ondrej Zary
472f2b0b2SOndrej Zary * based on paride.c by Grant R. Guenther <grant@torque.net>
572f2b0b2SOndrej Zary */
672f2b0b2SOndrej Zary #include <linux/kernel.h>
772f2b0b2SOndrej Zary #include <linux/module.h>
872f2b0b2SOndrej Zary #include <linux/parport.h>
9fe027ff9SOndrej Zary #include "pata_parport.h"
1072f2b0b2SOndrej Zary
1172f2b0b2SOndrej Zary #define DRV_NAME "pata_parport"
1272f2b0b2SOndrej Zary
1372f2b0b2SOndrej Zary static DEFINE_IDR(parport_list);
1472f2b0b2SOndrej Zary static DEFINE_IDR(protocols);
1572f2b0b2SOndrej Zary static DEFINE_IDA(pata_parport_bus_dev_ids);
1672f2b0b2SOndrej Zary static DEFINE_MUTEX(pi_mutex);
1772f2b0b2SOndrej Zary
1872f2b0b2SOndrej Zary static bool probe = true;
1972f2b0b2SOndrej Zary module_param(probe, bool, 0644);
2072f2b0b2SOndrej Zary MODULE_PARM_DESC(probe, "Enable automatic device probing (0=off, 1=on [default])");
2172f2b0b2SOndrej Zary
2272f2b0b2SOndrej Zary /*
2372f2b0b2SOndrej Zary * libata drivers cannot sleep so this driver claims parport before activating
2472f2b0b2SOndrej Zary * the ata host and keeps it claimed (and protocol connected) until the ata
2572f2b0b2SOndrej Zary * host is removed. Unfortunately, this means that you cannot use any chained
2672f2b0b2SOndrej Zary * devices (neither other pata_parport devices nor a printer).
2772f2b0b2SOndrej Zary */
pi_connect(struct pi_adapter * pi)2872f2b0b2SOndrej Zary static void pi_connect(struct pi_adapter *pi)
2972f2b0b2SOndrej Zary {
3072f2b0b2SOndrej Zary parport_claim_or_block(pi->pardev);
3172f2b0b2SOndrej Zary pi->proto->connect(pi);
3272f2b0b2SOndrej Zary }
3372f2b0b2SOndrej Zary
pi_disconnect(struct pi_adapter * pi)3472f2b0b2SOndrej Zary static void pi_disconnect(struct pi_adapter *pi)
3572f2b0b2SOndrej Zary {
3672f2b0b2SOndrej Zary pi->proto->disconnect(pi);
3772f2b0b2SOndrej Zary parport_release(pi->pardev);
3872f2b0b2SOndrej Zary }
3972f2b0b2SOndrej Zary
pata_parport_dev_select(struct ata_port * ap,unsigned int device)4072f2b0b2SOndrej Zary static void pata_parport_dev_select(struct ata_port *ap, unsigned int device)
4172f2b0b2SOndrej Zary {
4272f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
4372f2b0b2SOndrej Zary u8 tmp;
4472f2b0b2SOndrej Zary
4572f2b0b2SOndrej Zary if (device == 0)
4672f2b0b2SOndrej Zary tmp = ATA_DEVICE_OBS;
4772f2b0b2SOndrej Zary else
4872f2b0b2SOndrej Zary tmp = ATA_DEVICE_OBS | ATA_DEV1;
4972f2b0b2SOndrej Zary
5072f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tmp);
5172f2b0b2SOndrej Zary ata_sff_pause(ap);
5272f2b0b2SOndrej Zary }
5372f2b0b2SOndrej Zary
pata_parport_set_devctl(struct ata_port * ap,u8 ctl)54d2302427SOndrej Zary static void pata_parport_set_devctl(struct ata_port *ap, u8 ctl)
55d2302427SOndrej Zary {
56d2302427SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
57d2302427SOndrej Zary
58d2302427SOndrej Zary pi->proto->write_regr(pi, 1, 6, ctl);
59d2302427SOndrej Zary }
60d2302427SOndrej Zary
pata_parport_devchk(struct ata_port * ap,unsigned int device)6172f2b0b2SOndrej Zary static bool pata_parport_devchk(struct ata_port *ap, unsigned int device)
6272f2b0b2SOndrej Zary {
6372f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
6472f2b0b2SOndrej Zary u8 nsect, lbal;
6572f2b0b2SOndrej Zary
6672f2b0b2SOndrej Zary pata_parport_dev_select(ap, device);
6772f2b0b2SOndrej Zary
6872f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
6972f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
7072f2b0b2SOndrej Zary
7172f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0xaa);
7272f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0x55);
7372f2b0b2SOndrej Zary
74b555aa66SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
7572f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
7672f2b0b2SOndrej Zary
7772f2b0b2SOndrej Zary nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
7872f2b0b2SOndrej Zary lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
7972f2b0b2SOndrej Zary
8072f2b0b2SOndrej Zary return (nsect == 0x55) && (lbal == 0xaa);
8172f2b0b2SOndrej Zary }
8272f2b0b2SOndrej Zary
pata_parport_wait_after_reset(struct ata_link * link,unsigned int devmask,unsigned long deadline)83*f343e578SOndrej Zary static int pata_parport_wait_after_reset(struct ata_link *link,
84*f343e578SOndrej Zary unsigned int devmask,
85*f343e578SOndrej Zary unsigned long deadline)
86*f343e578SOndrej Zary {
87*f343e578SOndrej Zary struct ata_port *ap = link->ap;
88*f343e578SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
89*f343e578SOndrej Zary unsigned int dev0 = devmask & (1 << 0);
90*f343e578SOndrej Zary unsigned int dev1 = devmask & (1 << 1);
91*f343e578SOndrej Zary int rc, ret = 0;
92*f343e578SOndrej Zary
93*f343e578SOndrej Zary ata_msleep(ap, ATA_WAIT_AFTER_RESET);
94*f343e578SOndrej Zary
95*f343e578SOndrej Zary /* always check readiness of the master device */
96*f343e578SOndrej Zary rc = ata_sff_wait_ready(link, deadline);
97*f343e578SOndrej Zary if (rc) {
98*f343e578SOndrej Zary /*
99*f343e578SOndrej Zary * some adapters return bogus values if master device is not
100*f343e578SOndrej Zary * present, so don't abort now if a slave device is present
101*f343e578SOndrej Zary */
102*f343e578SOndrej Zary if (!dev1)
103*f343e578SOndrej Zary return rc;
104*f343e578SOndrej Zary ret = -ENODEV;
105*f343e578SOndrej Zary }
106*f343e578SOndrej Zary
107*f343e578SOndrej Zary /*
108*f343e578SOndrej Zary * if device 1 was found in ata_devchk, wait for register
109*f343e578SOndrej Zary * access briefly, then wait for BSY to clear.
110*f343e578SOndrej Zary */
111*f343e578SOndrej Zary if (dev1) {
112*f343e578SOndrej Zary int i;
113*f343e578SOndrej Zary
114*f343e578SOndrej Zary pata_parport_dev_select(ap, 1);
115*f343e578SOndrej Zary
116*f343e578SOndrej Zary /*
117*f343e578SOndrej Zary * Wait for register access. Some ATAPI devices fail
118*f343e578SOndrej Zary * to set nsect/lbal after reset, so don't waste too
119*f343e578SOndrej Zary * much time on it. We're gonna wait for !BSY anyway.
120*f343e578SOndrej Zary */
121*f343e578SOndrej Zary for (i = 0; i < 2; i++) {
122*f343e578SOndrej Zary u8 nsect, lbal;
123*f343e578SOndrej Zary
124*f343e578SOndrej Zary nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
125*f343e578SOndrej Zary lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
126*f343e578SOndrej Zary if (nsect == 1 && lbal == 1)
127*f343e578SOndrej Zary break;
128*f343e578SOndrej Zary /* give drive a breather */
129*f343e578SOndrej Zary ata_msleep(ap, 50);
130*f343e578SOndrej Zary }
131*f343e578SOndrej Zary
132*f343e578SOndrej Zary rc = ata_sff_wait_ready(link, deadline);
133*f343e578SOndrej Zary if (rc) {
134*f343e578SOndrej Zary if (rc != -ENODEV)
135*f343e578SOndrej Zary return rc;
136*f343e578SOndrej Zary ret = rc;
137*f343e578SOndrej Zary }
138*f343e578SOndrej Zary }
139*f343e578SOndrej Zary
140*f343e578SOndrej Zary pata_parport_dev_select(ap, 0);
141*f343e578SOndrej Zary if (dev1)
142*f343e578SOndrej Zary pata_parport_dev_select(ap, 1);
143*f343e578SOndrej Zary if (dev0)
144*f343e578SOndrej Zary pata_parport_dev_select(ap, 0);
145*f343e578SOndrej Zary
146*f343e578SOndrej Zary return ret;
147*f343e578SOndrej Zary }
148*f343e578SOndrej Zary
pata_parport_bus_softreset(struct ata_port * ap,unsigned int devmask,unsigned long deadline)14972f2b0b2SOndrej Zary static int pata_parport_bus_softreset(struct ata_port *ap, unsigned int devmask,
15072f2b0b2SOndrej Zary unsigned long deadline)
15172f2b0b2SOndrej Zary {
15272f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
15372f2b0b2SOndrej Zary
15472f2b0b2SOndrej Zary /* software reset. causes dev0 to be selected */
15572f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, ap->ctl);
15672f2b0b2SOndrej Zary udelay(20);
15772f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, ap->ctl | ATA_SRST);
15872f2b0b2SOndrej Zary udelay(20);
15972f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, ap->ctl);
16072f2b0b2SOndrej Zary ap->last_ctl = ap->ctl;
16172f2b0b2SOndrej Zary
16272f2b0b2SOndrej Zary /* wait the port to become ready */
163*f343e578SOndrej Zary return pata_parport_wait_after_reset(&ap->link, devmask, deadline);
16472f2b0b2SOndrej Zary }
16572f2b0b2SOndrej Zary
pata_parport_softreset(struct ata_link * link,unsigned int * classes,unsigned long deadline)16672f2b0b2SOndrej Zary static int pata_parport_softreset(struct ata_link *link, unsigned int *classes,
16772f2b0b2SOndrej Zary unsigned long deadline)
16872f2b0b2SOndrej Zary {
16972f2b0b2SOndrej Zary struct ata_port *ap = link->ap;
17072f2b0b2SOndrej Zary unsigned int devmask = 0;
17172f2b0b2SOndrej Zary int rc;
17272f2b0b2SOndrej Zary u8 err;
17372f2b0b2SOndrej Zary
17472f2b0b2SOndrej Zary /* determine if device 0/1 are present */
17572f2b0b2SOndrej Zary if (pata_parport_devchk(ap, 0))
17672f2b0b2SOndrej Zary devmask |= (1 << 0);
17772f2b0b2SOndrej Zary if (pata_parport_devchk(ap, 1))
17872f2b0b2SOndrej Zary devmask |= (1 << 1);
17972f2b0b2SOndrej Zary
18072f2b0b2SOndrej Zary /* select device 0 again */
18172f2b0b2SOndrej Zary pata_parport_dev_select(ap, 0);
18272f2b0b2SOndrej Zary
18372f2b0b2SOndrej Zary /* issue bus reset */
18472f2b0b2SOndrej Zary rc = pata_parport_bus_softreset(ap, devmask, deadline);
18572f2b0b2SOndrej Zary if (rc && rc != -ENODEV) {
18672f2b0b2SOndrej Zary ata_link_err(link, "SRST failed (errno=%d)\n", rc);
18772f2b0b2SOndrej Zary return rc;
18872f2b0b2SOndrej Zary }
18972f2b0b2SOndrej Zary
19072f2b0b2SOndrej Zary /* determine by signature whether we have ATA or ATAPI devices */
19172f2b0b2SOndrej Zary classes[0] = ata_sff_dev_classify(&link->device[0],
19272f2b0b2SOndrej Zary devmask & (1 << 0), &err);
19372f2b0b2SOndrej Zary if (err != 0x81)
19472f2b0b2SOndrej Zary classes[1] = ata_sff_dev_classify(&link->device[1],
19572f2b0b2SOndrej Zary devmask & (1 << 1), &err);
19672f2b0b2SOndrej Zary
19772f2b0b2SOndrej Zary return 0;
19872f2b0b2SOndrej Zary }
19972f2b0b2SOndrej Zary
pata_parport_check_status(struct ata_port * ap)20072f2b0b2SOndrej Zary static u8 pata_parport_check_status(struct ata_port *ap)
20172f2b0b2SOndrej Zary {
20272f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
20372f2b0b2SOndrej Zary
20472f2b0b2SOndrej Zary return pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
20572f2b0b2SOndrej Zary }
20672f2b0b2SOndrej Zary
pata_parport_check_altstatus(struct ata_port * ap)20772f2b0b2SOndrej Zary static u8 pata_parport_check_altstatus(struct ata_port *ap)
20872f2b0b2SOndrej Zary {
20972f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
21072f2b0b2SOndrej Zary
21172f2b0b2SOndrej Zary return pi->proto->read_regr(pi, 1, 6);
21272f2b0b2SOndrej Zary }
21372f2b0b2SOndrej Zary
pata_parport_tf_load(struct ata_port * ap,const struct ata_taskfile * tf)21472f2b0b2SOndrej Zary static void pata_parport_tf_load(struct ata_port *ap,
21572f2b0b2SOndrej Zary const struct ata_taskfile *tf)
21672f2b0b2SOndrej Zary {
21772f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
21872f2b0b2SOndrej Zary
21972f2b0b2SOndrej Zary if (tf->ctl != ap->last_ctl) {
22072f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, tf->ctl);
22172f2b0b2SOndrej Zary ap->last_ctl = tf->ctl;
22272f2b0b2SOndrej Zary ata_wait_idle(ap);
22372f2b0b2SOndrej Zary }
22472f2b0b2SOndrej Zary
22572f2b0b2SOndrej Zary if (tf->flags & ATA_TFLAG_ISADDR) {
22672f2b0b2SOndrej Zary if (tf->flags & ATA_TFLAG_LBA48) {
22772f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_FEATURE,
22872f2b0b2SOndrej Zary tf->hob_feature);
22972f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_NSECT,
23072f2b0b2SOndrej Zary tf->hob_nsect);
23172f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAL,
23272f2b0b2SOndrej Zary tf->hob_lbal);
23372f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAM,
23472f2b0b2SOndrej Zary tf->hob_lbam);
23572f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAH,
23672f2b0b2SOndrej Zary tf->hob_lbah);
23772f2b0b2SOndrej Zary }
23872f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_FEATURE, tf->feature);
23972f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_NSECT, tf->nsect);
24072f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAL, tf->lbal);
24172f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAM, tf->lbam);
24272f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_LBAH, tf->lbah);
24372f2b0b2SOndrej Zary }
24472f2b0b2SOndrej Zary
24572f2b0b2SOndrej Zary if (tf->flags & ATA_TFLAG_DEVICE)
24672f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tf->device);
24772f2b0b2SOndrej Zary
24872f2b0b2SOndrej Zary ata_wait_idle(ap);
24972f2b0b2SOndrej Zary }
25072f2b0b2SOndrej Zary
pata_parport_tf_read(struct ata_port * ap,struct ata_taskfile * tf)25172f2b0b2SOndrej Zary static void pata_parport_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
25272f2b0b2SOndrej Zary {
25372f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
25472f2b0b2SOndrej Zary
25572f2b0b2SOndrej Zary tf->status = pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
25672f2b0b2SOndrej Zary tf->error = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
25772f2b0b2SOndrej Zary tf->nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
25872f2b0b2SOndrej Zary tf->lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
25972f2b0b2SOndrej Zary tf->lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
26072f2b0b2SOndrej Zary tf->lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
26172f2b0b2SOndrej Zary tf->device = pi->proto->read_regr(pi, 0, ATA_REG_DEVICE);
26272f2b0b2SOndrej Zary
26372f2b0b2SOndrej Zary if (tf->flags & ATA_TFLAG_LBA48) {
26472f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, tf->ctl | ATA_HOB);
26572f2b0b2SOndrej Zary tf->hob_feature = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
26672f2b0b2SOndrej Zary tf->hob_nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
26772f2b0b2SOndrej Zary tf->hob_lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
26872f2b0b2SOndrej Zary tf->hob_lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
26972f2b0b2SOndrej Zary tf->hob_lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
27072f2b0b2SOndrej Zary pi->proto->write_regr(pi, 1, 6, tf->ctl);
27172f2b0b2SOndrej Zary ap->last_ctl = tf->ctl;
27272f2b0b2SOndrej Zary }
27372f2b0b2SOndrej Zary }
27472f2b0b2SOndrej Zary
pata_parport_exec_command(struct ata_port * ap,const struct ata_taskfile * tf)27572f2b0b2SOndrej Zary static void pata_parport_exec_command(struct ata_port *ap,
27672f2b0b2SOndrej Zary const struct ata_taskfile *tf)
27772f2b0b2SOndrej Zary {
27872f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
27972f2b0b2SOndrej Zary
28072f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, ATA_REG_CMD, tf->command);
28172f2b0b2SOndrej Zary ata_sff_pause(ap);
28272f2b0b2SOndrej Zary }
28372f2b0b2SOndrej Zary
pata_parport_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)28472f2b0b2SOndrej Zary static unsigned int pata_parport_data_xfer(struct ata_queued_cmd *qc,
28572f2b0b2SOndrej Zary unsigned char *buf, unsigned int buflen, int rw)
28672f2b0b2SOndrej Zary {
28772f2b0b2SOndrej Zary struct ata_port *ap = qc->dev->link->ap;
28872f2b0b2SOndrej Zary struct pi_adapter *pi = ap->host->private_data;
28972f2b0b2SOndrej Zary
29072f2b0b2SOndrej Zary if (rw == READ)
29172f2b0b2SOndrej Zary pi->proto->read_block(pi, buf, buflen);
29272f2b0b2SOndrej Zary else
29372f2b0b2SOndrej Zary pi->proto->write_block(pi, buf, buflen);
29472f2b0b2SOndrej Zary
29572f2b0b2SOndrej Zary return buflen;
29672f2b0b2SOndrej Zary }
29772f2b0b2SOndrej Zary
pata_parport_drain_fifo(struct ata_queued_cmd * qc)29872f2b0b2SOndrej Zary static void pata_parport_drain_fifo(struct ata_queued_cmd *qc)
29972f2b0b2SOndrej Zary {
30072f2b0b2SOndrej Zary int count;
30172f2b0b2SOndrej Zary struct ata_port *ap;
30272f2b0b2SOndrej Zary struct pi_adapter *pi;
30372f2b0b2SOndrej Zary char junk[2];
30472f2b0b2SOndrej Zary
30572f2b0b2SOndrej Zary /* We only need to flush incoming data when a command was running */
30672f2b0b2SOndrej Zary if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
30772f2b0b2SOndrej Zary return;
30872f2b0b2SOndrej Zary
30972f2b0b2SOndrej Zary ap = qc->ap;
31072f2b0b2SOndrej Zary pi = ap->host->private_data;
31172f2b0b2SOndrej Zary /* Drain up to 64K of data before we give up this recovery method */
31272f2b0b2SOndrej Zary for (count = 0; (pata_parport_check_status(ap) & ATA_DRQ)
31372f2b0b2SOndrej Zary && count < 65536; count += 2) {
31472f2b0b2SOndrej Zary pi->proto->read_block(pi, junk, 2);
31572f2b0b2SOndrej Zary }
31672f2b0b2SOndrej Zary
31772f2b0b2SOndrej Zary if (count)
31872f2b0b2SOndrej Zary ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
31972f2b0b2SOndrej Zary }
32072f2b0b2SOndrej Zary
32172f2b0b2SOndrej Zary static struct ata_port_operations pata_parport_port_ops = {
32272f2b0b2SOndrej Zary .inherits = &ata_sff_port_ops,
32372f2b0b2SOndrej Zary
32472f2b0b2SOndrej Zary .softreset = pata_parport_softreset,
32572f2b0b2SOndrej Zary .hardreset = NULL,
32672f2b0b2SOndrej Zary
32772f2b0b2SOndrej Zary .sff_dev_select = pata_parport_dev_select,
328d2302427SOndrej Zary .sff_set_devctl = pata_parport_set_devctl,
32972f2b0b2SOndrej Zary .sff_check_status = pata_parport_check_status,
33072f2b0b2SOndrej Zary .sff_check_altstatus = pata_parport_check_altstatus,
33172f2b0b2SOndrej Zary .sff_tf_load = pata_parport_tf_load,
33272f2b0b2SOndrej Zary .sff_tf_read = pata_parport_tf_read,
33372f2b0b2SOndrej Zary .sff_exec_command = pata_parport_exec_command,
33472f2b0b2SOndrej Zary .sff_data_xfer = pata_parport_data_xfer,
33572f2b0b2SOndrej Zary .sff_drain_fifo = pata_parport_drain_fifo,
33672f2b0b2SOndrej Zary };
33772f2b0b2SOndrej Zary
33872f2b0b2SOndrej Zary static const struct ata_port_info pata_parport_port_info = {
33972f2b0b2SOndrej Zary .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_POLLING,
34072f2b0b2SOndrej Zary .pio_mask = ATA_PIO0,
34172f2b0b2SOndrej Zary /* No DMA */
34272f2b0b2SOndrej Zary .port_ops = &pata_parport_port_ops,
34372f2b0b2SOndrej Zary };
34472f2b0b2SOndrej Zary
pi_release(struct pi_adapter * pi)34572f2b0b2SOndrej Zary static void pi_release(struct pi_adapter *pi)
34672f2b0b2SOndrej Zary {
34772f2b0b2SOndrej Zary parport_unregister_device(pi->pardev);
34872f2b0b2SOndrej Zary if (pi->proto->release_proto)
34972f2b0b2SOndrej Zary pi->proto->release_proto(pi);
35072f2b0b2SOndrej Zary module_put(pi->proto->owner);
35172f2b0b2SOndrej Zary }
35272f2b0b2SOndrej Zary
default_test_proto(struct pi_adapter * pi)353b42251a8SOndrej Zary static int default_test_proto(struct pi_adapter *pi)
35472f2b0b2SOndrej Zary {
35572f2b0b2SOndrej Zary int j, k;
35672f2b0b2SOndrej Zary int e[2] = { 0, 0 };
35772f2b0b2SOndrej Zary
35872f2b0b2SOndrej Zary pi->proto->connect(pi);
35972f2b0b2SOndrej Zary
36072f2b0b2SOndrej Zary for (j = 0; j < 2; j++) {
36172f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, 6, 0xa0 + j * 0x10);
36272f2b0b2SOndrej Zary for (k = 0; k < 256; k++) {
36372f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, 2, k ^ 0xaa);
36472f2b0b2SOndrej Zary pi->proto->write_regr(pi, 0, 3, k ^ 0x55);
36572f2b0b2SOndrej Zary if (pi->proto->read_regr(pi, 0, 2) != (k ^ 0xaa))
36672f2b0b2SOndrej Zary e[j]++;
36772f2b0b2SOndrej Zary }
36872f2b0b2SOndrej Zary }
36972f2b0b2SOndrej Zary pi->proto->disconnect(pi);
37072f2b0b2SOndrej Zary
37172f2b0b2SOndrej Zary dev_dbg(&pi->dev, "%s: port 0x%x, mode %d, test=(%d,%d)\n",
37272f2b0b2SOndrej Zary pi->proto->name, pi->port, pi->mode, e[0], e[1]);
37372f2b0b2SOndrej Zary
37472f2b0b2SOndrej Zary return e[0] && e[1]; /* not here if both > 0 */
37572f2b0b2SOndrej Zary }
37672f2b0b2SOndrej Zary
pi_test_proto(struct pi_adapter * pi)377b42251a8SOndrej Zary static int pi_test_proto(struct pi_adapter *pi)
37872f2b0b2SOndrej Zary {
37972f2b0b2SOndrej Zary int res;
38072f2b0b2SOndrej Zary
38172f2b0b2SOndrej Zary parport_claim_or_block(pi->pardev);
38272f2b0b2SOndrej Zary if (pi->proto->test_proto)
383b42251a8SOndrej Zary res = pi->proto->test_proto(pi);
38472f2b0b2SOndrej Zary else
385b42251a8SOndrej Zary res = default_test_proto(pi);
38672f2b0b2SOndrej Zary parport_release(pi->pardev);
38772f2b0b2SOndrej Zary
38872f2b0b2SOndrej Zary return res;
38972f2b0b2SOndrej Zary }
39072f2b0b2SOndrej Zary
pi_probe_mode(struct pi_adapter * pi,int max)391b42251a8SOndrej Zary static bool pi_probe_mode(struct pi_adapter *pi, int max)
39272f2b0b2SOndrej Zary {
39372f2b0b2SOndrej Zary int best, range;
39472f2b0b2SOndrej Zary
39572f2b0b2SOndrej Zary if (pi->mode != -1) {
39672f2b0b2SOndrej Zary if (pi->mode >= max)
39772f2b0b2SOndrej Zary return false;
39872f2b0b2SOndrej Zary range = 3;
39972f2b0b2SOndrej Zary if (pi->mode >= pi->proto->epp_first)
40072f2b0b2SOndrej Zary range = 8;
40172f2b0b2SOndrej Zary if (range == 8 && pi->port % 8)
40272f2b0b2SOndrej Zary return false;
403b42251a8SOndrej Zary return !pi_test_proto(pi);
40472f2b0b2SOndrej Zary }
40572f2b0b2SOndrej Zary best = -1;
40672f2b0b2SOndrej Zary for (pi->mode = 0; pi->mode < max; pi->mode++) {
40772f2b0b2SOndrej Zary range = 3;
40872f2b0b2SOndrej Zary if (pi->mode >= pi->proto->epp_first)
40972f2b0b2SOndrej Zary range = 8;
41072f2b0b2SOndrej Zary if (range == 8 && pi->port % 8)
41172f2b0b2SOndrej Zary break;
412b42251a8SOndrej Zary if (!pi_test_proto(pi))
41372f2b0b2SOndrej Zary best = pi->mode;
41472f2b0b2SOndrej Zary }
41572f2b0b2SOndrej Zary pi->mode = best;
41672f2b0b2SOndrej Zary return best > -1;
41772f2b0b2SOndrej Zary }
41872f2b0b2SOndrej Zary
pi_probe_unit(struct pi_adapter * pi,int unit)419b42251a8SOndrej Zary static bool pi_probe_unit(struct pi_adapter *pi, int unit)
42072f2b0b2SOndrej Zary {
42172f2b0b2SOndrej Zary int max, s, e;
42272f2b0b2SOndrej Zary
42372f2b0b2SOndrej Zary s = unit;
42472f2b0b2SOndrej Zary e = s + 1;
42572f2b0b2SOndrej Zary
42672f2b0b2SOndrej Zary if (s == -1) {
42772f2b0b2SOndrej Zary s = 0;
42872f2b0b2SOndrej Zary e = pi->proto->max_units;
42972f2b0b2SOndrej Zary }
43072f2b0b2SOndrej Zary
43172f2b0b2SOndrej Zary if (pi->proto->test_port) {
43272f2b0b2SOndrej Zary parport_claim_or_block(pi->pardev);
43372f2b0b2SOndrej Zary max = pi->proto->test_port(pi);
43472f2b0b2SOndrej Zary parport_release(pi->pardev);
43572f2b0b2SOndrej Zary } else {
43672f2b0b2SOndrej Zary max = pi->proto->max_mode;
43772f2b0b2SOndrej Zary }
43872f2b0b2SOndrej Zary
43972f2b0b2SOndrej Zary if (pi->proto->probe_unit) {
44072f2b0b2SOndrej Zary parport_claim_or_block(pi->pardev);
44172f2b0b2SOndrej Zary for (pi->unit = s; pi->unit < e; pi->unit++) {
44272f2b0b2SOndrej Zary if (pi->proto->probe_unit(pi)) {
44372f2b0b2SOndrej Zary parport_release(pi->pardev);
444b42251a8SOndrej Zary return pi_probe_mode(pi, max);
44572f2b0b2SOndrej Zary }
44672f2b0b2SOndrej Zary }
44772f2b0b2SOndrej Zary parport_release(pi->pardev);
44872f2b0b2SOndrej Zary return false;
44972f2b0b2SOndrej Zary }
45072f2b0b2SOndrej Zary
451b42251a8SOndrej Zary return pi_probe_mode(pi, max);
45272f2b0b2SOndrej Zary }
45372f2b0b2SOndrej Zary
pata_parport_dev_release(struct device * dev)45472f2b0b2SOndrej Zary static void pata_parport_dev_release(struct device *dev)
45572f2b0b2SOndrej Zary {
45672f2b0b2SOndrej Zary struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
45772f2b0b2SOndrej Zary
4585bc9e2d4SOndrej Zary ida_free(&pata_parport_bus_dev_ids, dev->id);
45972f2b0b2SOndrej Zary kfree(pi);
46072f2b0b2SOndrej Zary }
46172f2b0b2SOndrej Zary
pata_parport_bus_release(struct device * dev)46272f2b0b2SOndrej Zary static void pata_parport_bus_release(struct device *dev)
46372f2b0b2SOndrej Zary {
46472f2b0b2SOndrej Zary /* nothing to do here but required to avoid warning on device removal */
46572f2b0b2SOndrej Zary }
46672f2b0b2SOndrej Zary
46772f2b0b2SOndrej Zary static struct bus_type pata_parport_bus_type = {
46872f2b0b2SOndrej Zary .name = DRV_NAME,
46972f2b0b2SOndrej Zary };
47072f2b0b2SOndrej Zary
47172f2b0b2SOndrej Zary static struct device pata_parport_bus = {
47272f2b0b2SOndrej Zary .init_name = DRV_NAME,
47372f2b0b2SOndrej Zary .release = pata_parport_bus_release,
47472f2b0b2SOndrej Zary };
47572f2b0b2SOndrej Zary
47625df73d9SBart Van Assche static const struct scsi_host_template pata_parport_sht = {
47772f2b0b2SOndrej Zary PATA_PARPORT_SHT("pata_parport")
47872f2b0b2SOndrej Zary };
47972f2b0b2SOndrej Zary
48072f2b0b2SOndrej Zary struct pi_device_match {
48172f2b0b2SOndrej Zary struct parport *parport;
48272f2b0b2SOndrej Zary struct pi_protocol *proto;
48372f2b0b2SOndrej Zary };
48472f2b0b2SOndrej Zary
pi_find_dev(struct device * dev,void * data)48572f2b0b2SOndrej Zary static int pi_find_dev(struct device *dev, void *data)
48672f2b0b2SOndrej Zary {
48772f2b0b2SOndrej Zary struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
48872f2b0b2SOndrej Zary struct pi_device_match *match = data;
48972f2b0b2SOndrej Zary
49072f2b0b2SOndrej Zary return pi->pardev->port == match->parport && pi->proto == match->proto;
49172f2b0b2SOndrej Zary }
49272f2b0b2SOndrej Zary
pi_init_one(struct parport * parport,struct pi_protocol * pr,int mode,int unit,int delay)49372f2b0b2SOndrej Zary static struct pi_adapter *pi_init_one(struct parport *parport,
49472f2b0b2SOndrej Zary struct pi_protocol *pr, int mode, int unit, int delay)
49572f2b0b2SOndrej Zary {
49672f2b0b2SOndrej Zary struct pardev_cb par_cb = { };
49772f2b0b2SOndrej Zary const struct ata_port_info *ppi[] = { &pata_parport_port_info };
49872f2b0b2SOndrej Zary struct ata_host *host;
49972f2b0b2SOndrej Zary struct pi_adapter *pi;
50072f2b0b2SOndrej Zary struct pi_device_match match = { .parport = parport, .proto = pr };
5018844f0aaSOndrej Zary int id;
50272f2b0b2SOndrej Zary
50372f2b0b2SOndrej Zary /*
50472f2b0b2SOndrej Zary * Abort if there's a device already registered on the same parport
50572f2b0b2SOndrej Zary * using the same protocol.
50672f2b0b2SOndrej Zary */
50772f2b0b2SOndrej Zary if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev))
50872f2b0b2SOndrej Zary return NULL;
50972f2b0b2SOndrej Zary
5105bc9e2d4SOndrej Zary id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
5115bc9e2d4SOndrej Zary if (id < 0)
51272f2b0b2SOndrej Zary return NULL;
51372f2b0b2SOndrej Zary
5145bc9e2d4SOndrej Zary pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL);
5155bc9e2d4SOndrej Zary if (!pi) {
5165bc9e2d4SOndrej Zary ida_free(&pata_parport_bus_dev_ids, id);
5175bc9e2d4SOndrej Zary return NULL;
5185bc9e2d4SOndrej Zary }
5195bc9e2d4SOndrej Zary
52072f2b0b2SOndrej Zary /* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
52172f2b0b2SOndrej Zary pi->dev.parent = &pata_parport_bus;
52272f2b0b2SOndrej Zary pi->dev.bus = &pata_parport_bus_type;
52372f2b0b2SOndrej Zary pi->dev.driver = &pr->driver;
52472f2b0b2SOndrej Zary pi->dev.release = pata_parport_dev_release;
5258844f0aaSOndrej Zary pi->dev.id = id;
52672f2b0b2SOndrej Zary dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
52772f2b0b2SOndrej Zary if (device_register(&pi->dev)) {
52872f2b0b2SOndrej Zary put_device(&pi->dev);
5295bc9e2d4SOndrej Zary /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
5305bc9e2d4SOndrej Zary return NULL;
53172f2b0b2SOndrej Zary }
53272f2b0b2SOndrej Zary
53372f2b0b2SOndrej Zary pi->proto = pr;
53472f2b0b2SOndrej Zary
53572f2b0b2SOndrej Zary if (!try_module_get(pi->proto->owner))
53672f2b0b2SOndrej Zary goto out_unreg_dev;
53772f2b0b2SOndrej Zary if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
53872f2b0b2SOndrej Zary goto out_module_put;
53972f2b0b2SOndrej Zary
54072f2b0b2SOndrej Zary pi->delay = (delay == -1) ? pi->proto->default_delay : delay;
54172f2b0b2SOndrej Zary pi->mode = mode;
54272f2b0b2SOndrej Zary pi->port = parport->base;
54372f2b0b2SOndrej Zary
54472f2b0b2SOndrej Zary par_cb.private = pi;
5455bc9e2d4SOndrej Zary pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, id);
54672f2b0b2SOndrej Zary if (!pi->pardev)
54772f2b0b2SOndrej Zary goto out_module_put;
54872f2b0b2SOndrej Zary
549b42251a8SOndrej Zary if (!pi_probe_unit(pi, unit)) {
55072f2b0b2SOndrej Zary dev_info(&pi->dev, "Adapter not found\n");
55172f2b0b2SOndrej Zary goto out_unreg_parport;
55272f2b0b2SOndrej Zary }
55372f2b0b2SOndrej Zary
5545b77db9cSOndrej Zary pi->proto->log_adapter(pi);
55572f2b0b2SOndrej Zary
55672f2b0b2SOndrej Zary host = ata_host_alloc_pinfo(&pi->pardev->dev, ppi, 1);
55772f2b0b2SOndrej Zary if (!host)
55872f2b0b2SOndrej Zary goto out_unreg_parport;
55972f2b0b2SOndrej Zary dev_set_drvdata(&pi->dev, host);
56072f2b0b2SOndrej Zary host->private_data = pi;
56172f2b0b2SOndrej Zary
56272f2b0b2SOndrej Zary ata_port_desc(host->ports[0], "port %s", pi->pardev->port->name);
56372f2b0b2SOndrej Zary ata_port_desc(host->ports[0], "protocol %s", pi->proto->name);
56472f2b0b2SOndrej Zary
56572f2b0b2SOndrej Zary pi_connect(pi);
56672f2b0b2SOndrej Zary if (ata_host_activate(host, 0, NULL, 0, &pata_parport_sht))
567dc472c76SOndrej Zary goto out_disconnect;
56872f2b0b2SOndrej Zary
56972f2b0b2SOndrej Zary return pi;
57072f2b0b2SOndrej Zary
571dc472c76SOndrej Zary out_disconnect:
57272f2b0b2SOndrej Zary pi_disconnect(pi);
573dc472c76SOndrej Zary out_unreg_parport:
57472f2b0b2SOndrej Zary parport_unregister_device(pi->pardev);
57572f2b0b2SOndrej Zary if (pi->proto->release_proto)
57672f2b0b2SOndrej Zary pi->proto->release_proto(pi);
57772f2b0b2SOndrej Zary out_module_put:
57872f2b0b2SOndrej Zary module_put(pi->proto->owner);
57972f2b0b2SOndrej Zary out_unreg_dev:
58072f2b0b2SOndrej Zary device_unregister(&pi->dev);
5815bc9e2d4SOndrej Zary /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
58272f2b0b2SOndrej Zary return NULL;
58372f2b0b2SOndrej Zary }
58472f2b0b2SOndrej Zary
pata_parport_register_driver(struct pi_protocol * pr)58572f2b0b2SOndrej Zary int pata_parport_register_driver(struct pi_protocol *pr)
58672f2b0b2SOndrej Zary {
58772f2b0b2SOndrej Zary int error;
58872f2b0b2SOndrej Zary struct parport *parport;
58972f2b0b2SOndrej Zary int port_num;
59072f2b0b2SOndrej Zary
59172f2b0b2SOndrej Zary pr->driver.bus = &pata_parport_bus_type;
59272f2b0b2SOndrej Zary pr->driver.name = pr->name;
59372f2b0b2SOndrej Zary error = driver_register(&pr->driver);
59472f2b0b2SOndrej Zary if (error)
59572f2b0b2SOndrej Zary return error;
59672f2b0b2SOndrej Zary
59772f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
59872f2b0b2SOndrej Zary error = idr_alloc(&protocols, pr, 0, 0, GFP_KERNEL);
59972f2b0b2SOndrej Zary if (error < 0) {
60072f2b0b2SOndrej Zary driver_unregister(&pr->driver);
60172f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
60272f2b0b2SOndrej Zary return error;
60372f2b0b2SOndrej Zary }
60472f2b0b2SOndrej Zary
60572f2b0b2SOndrej Zary pr_info("pata_parport: protocol %s registered\n", pr->name);
60672f2b0b2SOndrej Zary
60772f2b0b2SOndrej Zary if (probe) {
60872f2b0b2SOndrej Zary /* probe all parports using this protocol */
60972f2b0b2SOndrej Zary idr_for_each_entry(&parport_list, parport, port_num)
6104f747dc1SOndrej Zary pi_init_one(parport, pr, -1, -1, -1);
61172f2b0b2SOndrej Zary }
61272f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
61372f2b0b2SOndrej Zary
61472f2b0b2SOndrej Zary return 0;
61572f2b0b2SOndrej Zary }
61672f2b0b2SOndrej Zary EXPORT_SYMBOL_GPL(pata_parport_register_driver);
61772f2b0b2SOndrej Zary
pata_parport_unregister_driver(struct pi_protocol * pr)61872f2b0b2SOndrej Zary void pata_parport_unregister_driver(struct pi_protocol *pr)
61972f2b0b2SOndrej Zary {
62072f2b0b2SOndrej Zary struct pi_protocol *pr_iter;
62172f2b0b2SOndrej Zary int id = -1;
62272f2b0b2SOndrej Zary
62372f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
62472f2b0b2SOndrej Zary idr_for_each_entry(&protocols, pr_iter, id) {
62572f2b0b2SOndrej Zary if (pr_iter == pr)
62672f2b0b2SOndrej Zary break;
62772f2b0b2SOndrej Zary }
62872f2b0b2SOndrej Zary idr_remove(&protocols, id);
62972f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
63072f2b0b2SOndrej Zary driver_unregister(&pr->driver);
63172f2b0b2SOndrej Zary }
63272f2b0b2SOndrej Zary EXPORT_SYMBOL_GPL(pata_parport_unregister_driver);
63372f2b0b2SOndrej Zary
new_device_store(const struct bus_type * bus,const char * buf,size_t count)63475cff725SGreg Kroah-Hartman static ssize_t new_device_store(const struct bus_type *bus, const char *buf, size_t count)
63572f2b0b2SOndrej Zary {
63672f2b0b2SOndrej Zary char port[12] = "auto";
63772f2b0b2SOndrej Zary char protocol[8] = "auto";
63872f2b0b2SOndrej Zary int mode = -1, unit = -1, delay = -1;
63972f2b0b2SOndrej Zary struct pi_protocol *pr, *pr_wanted;
64072f2b0b2SOndrej Zary struct device_driver *drv;
64172f2b0b2SOndrej Zary struct parport *parport;
64272f2b0b2SOndrej Zary int port_num, port_wanted, pr_num;
64372f2b0b2SOndrej Zary bool ok = false;
64472f2b0b2SOndrej Zary
64572f2b0b2SOndrej Zary if (sscanf(buf, "%11s %7s %d %d %d",
64672f2b0b2SOndrej Zary port, protocol, &mode, &unit, &delay) < 1)
64772f2b0b2SOndrej Zary return -EINVAL;
64872f2b0b2SOndrej Zary
64972f2b0b2SOndrej Zary if (sscanf(port, "parport%u", &port_wanted) < 1) {
65072f2b0b2SOndrej Zary if (strcmp(port, "auto")) {
65172f2b0b2SOndrej Zary pr_err("invalid port name %s\n", port);
65272f2b0b2SOndrej Zary return -EINVAL;
65372f2b0b2SOndrej Zary }
65472f2b0b2SOndrej Zary port_wanted = -1;
65572f2b0b2SOndrej Zary }
65672f2b0b2SOndrej Zary
65772f2b0b2SOndrej Zary drv = driver_find(protocol, &pata_parport_bus_type);
65872f2b0b2SOndrej Zary if (!drv) {
65972f2b0b2SOndrej Zary if (strcmp(protocol, "auto")) {
66072f2b0b2SOndrej Zary pr_err("protocol %s not found\n", protocol);
66172f2b0b2SOndrej Zary return -EINVAL;
66272f2b0b2SOndrej Zary }
66372f2b0b2SOndrej Zary pr_wanted = NULL;
66472f2b0b2SOndrej Zary } else {
66572f2b0b2SOndrej Zary pr_wanted = container_of(drv, struct pi_protocol, driver);
66672f2b0b2SOndrej Zary }
66772f2b0b2SOndrej Zary
66872f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
66972f2b0b2SOndrej Zary /* walk all parports */
67072f2b0b2SOndrej Zary idr_for_each_entry(&parport_list, parport, port_num) {
67172f2b0b2SOndrej Zary if (port_num == port_wanted || port_wanted == -1) {
67272f2b0b2SOndrej Zary parport = parport_find_number(port_num);
67372f2b0b2SOndrej Zary if (!parport) {
67472f2b0b2SOndrej Zary pr_err("no such port %s\n", port);
67572f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
67672f2b0b2SOndrej Zary return -ENODEV;
67772f2b0b2SOndrej Zary }
67872f2b0b2SOndrej Zary /* walk all protocols */
67972f2b0b2SOndrej Zary idr_for_each_entry(&protocols, pr, pr_num) {
68072f2b0b2SOndrej Zary if (pr == pr_wanted || !pr_wanted)
68172f2b0b2SOndrej Zary if (pi_init_one(parport, pr, mode, unit,
68272f2b0b2SOndrej Zary delay))
68372f2b0b2SOndrej Zary ok = true;
68472f2b0b2SOndrej Zary }
68572f2b0b2SOndrej Zary parport_put_port(parport);
68672f2b0b2SOndrej Zary }
68772f2b0b2SOndrej Zary }
68872f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
68972f2b0b2SOndrej Zary if (!ok)
69072f2b0b2SOndrej Zary return -ENODEV;
69172f2b0b2SOndrej Zary
69272f2b0b2SOndrej Zary return count;
69372f2b0b2SOndrej Zary }
69472f2b0b2SOndrej Zary static BUS_ATTR_WO(new_device);
69572f2b0b2SOndrej Zary
pi_remove_one(struct device * dev)69672f2b0b2SOndrej Zary static void pi_remove_one(struct device *dev)
69772f2b0b2SOndrej Zary {
69872f2b0b2SOndrej Zary struct ata_host *host = dev_get_drvdata(dev);
69972f2b0b2SOndrej Zary struct pi_adapter *pi = host->private_data;
70072f2b0b2SOndrej Zary
70172f2b0b2SOndrej Zary ata_host_detach(host);
70272f2b0b2SOndrej Zary pi_disconnect(pi);
70372f2b0b2SOndrej Zary pi_release(pi);
70472f2b0b2SOndrej Zary device_unregister(dev);
7055bc9e2d4SOndrej Zary /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
70672f2b0b2SOndrej Zary }
70772f2b0b2SOndrej Zary
delete_device_store(const struct bus_type * bus,const char * buf,size_t count)70875cff725SGreg Kroah-Hartman static ssize_t delete_device_store(const struct bus_type *bus, const char *buf, size_t count)
70972f2b0b2SOndrej Zary {
71072f2b0b2SOndrej Zary struct device *dev;
71172f2b0b2SOndrej Zary
71272f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
71372f2b0b2SOndrej Zary dev = bus_find_device_by_name(bus, NULL, buf);
71472f2b0b2SOndrej Zary if (!dev) {
71572f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
71672f2b0b2SOndrej Zary return -ENODEV;
71772f2b0b2SOndrej Zary }
71872f2b0b2SOndrej Zary
71972f2b0b2SOndrej Zary pi_remove_one(dev);
7205bc9e2d4SOndrej Zary put_device(dev);
72172f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
72272f2b0b2SOndrej Zary
72372f2b0b2SOndrej Zary return count;
72472f2b0b2SOndrej Zary }
72572f2b0b2SOndrej Zary static BUS_ATTR_WO(delete_device);
72672f2b0b2SOndrej Zary
pata_parport_attach(struct parport * port)72772f2b0b2SOndrej Zary static void pata_parport_attach(struct parport *port)
72872f2b0b2SOndrej Zary {
72972f2b0b2SOndrej Zary struct pi_protocol *pr;
73072f2b0b2SOndrej Zary int pr_num, id;
73172f2b0b2SOndrej Zary
73272f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
73372f2b0b2SOndrej Zary id = idr_alloc(&parport_list, port, port->number, port->number,
73472f2b0b2SOndrej Zary GFP_KERNEL);
73572f2b0b2SOndrej Zary if (id < 0) {
73672f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
73772f2b0b2SOndrej Zary return;
73872f2b0b2SOndrej Zary }
73972f2b0b2SOndrej Zary
74072f2b0b2SOndrej Zary if (probe) {
74172f2b0b2SOndrej Zary /* probe this port using all protocols */
74272f2b0b2SOndrej Zary idr_for_each_entry(&protocols, pr, pr_num)
7434f747dc1SOndrej Zary pi_init_one(port, pr, -1, -1, -1);
74472f2b0b2SOndrej Zary }
74572f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
74672f2b0b2SOndrej Zary }
74772f2b0b2SOndrej Zary
pi_remove_port(struct device * dev,void * p)74872f2b0b2SOndrej Zary static int pi_remove_port(struct device *dev, void *p)
74972f2b0b2SOndrej Zary {
75072f2b0b2SOndrej Zary struct ata_host *host = dev_get_drvdata(dev);
75172f2b0b2SOndrej Zary struct pi_adapter *pi = host->private_data;
75272f2b0b2SOndrej Zary
75372f2b0b2SOndrej Zary if (pi->pardev->port == p)
75472f2b0b2SOndrej Zary pi_remove_one(dev);
75572f2b0b2SOndrej Zary
75672f2b0b2SOndrej Zary return 0;
75772f2b0b2SOndrej Zary }
75872f2b0b2SOndrej Zary
pata_parport_detach(struct parport * port)75972f2b0b2SOndrej Zary static void pata_parport_detach(struct parport *port)
76072f2b0b2SOndrej Zary {
76172f2b0b2SOndrej Zary mutex_lock(&pi_mutex);
76272f2b0b2SOndrej Zary bus_for_each_dev(&pata_parport_bus_type, NULL, port, pi_remove_port);
76372f2b0b2SOndrej Zary idr_remove(&parport_list, port->number);
76472f2b0b2SOndrej Zary mutex_unlock(&pi_mutex);
76572f2b0b2SOndrej Zary }
76672f2b0b2SOndrej Zary
76772f2b0b2SOndrej Zary static struct parport_driver pata_parport_driver = {
76872f2b0b2SOndrej Zary .name = DRV_NAME,
76972f2b0b2SOndrej Zary .match_port = pata_parport_attach,
77072f2b0b2SOndrej Zary .detach = pata_parport_detach,
77172f2b0b2SOndrej Zary .devmodel = true,
77272f2b0b2SOndrej Zary };
77372f2b0b2SOndrej Zary
pata_parport_init(void)77472f2b0b2SOndrej Zary static __init int pata_parport_init(void)
77572f2b0b2SOndrej Zary {
77672f2b0b2SOndrej Zary int error;
77772f2b0b2SOndrej Zary
77872f2b0b2SOndrej Zary error = bus_register(&pata_parport_bus_type);
77972f2b0b2SOndrej Zary if (error) {
78072f2b0b2SOndrej Zary pr_err("failed to register pata_parport bus, error: %d\n", error);
78172f2b0b2SOndrej Zary return error;
78272f2b0b2SOndrej Zary }
78372f2b0b2SOndrej Zary
78472f2b0b2SOndrej Zary error = device_register(&pata_parport_bus);
78572f2b0b2SOndrej Zary if (error) {
78672f2b0b2SOndrej Zary pr_err("failed to register pata_parport bus, error: %d\n", error);
78772f2b0b2SOndrej Zary goto out_unregister_bus;
78872f2b0b2SOndrej Zary }
78972f2b0b2SOndrej Zary
79072f2b0b2SOndrej Zary error = bus_create_file(&pata_parport_bus_type, &bus_attr_new_device);
79172f2b0b2SOndrej Zary if (error) {
79272f2b0b2SOndrej Zary pr_err("unable to create sysfs file, error: %d\n", error);
79372f2b0b2SOndrej Zary goto out_unregister_dev;
79472f2b0b2SOndrej Zary }
79572f2b0b2SOndrej Zary
79672f2b0b2SOndrej Zary error = bus_create_file(&pata_parport_bus_type, &bus_attr_delete_device);
79772f2b0b2SOndrej Zary if (error) {
79872f2b0b2SOndrej Zary pr_err("unable to create sysfs file, error: %d\n", error);
79972f2b0b2SOndrej Zary goto out_remove_new;
80072f2b0b2SOndrej Zary }
80172f2b0b2SOndrej Zary
80272f2b0b2SOndrej Zary error = parport_register_driver(&pata_parport_driver);
80372f2b0b2SOndrej Zary if (error) {
80472f2b0b2SOndrej Zary pr_err("unable to register parport driver, error: %d\n", error);
80572f2b0b2SOndrej Zary goto out_remove_del;
80672f2b0b2SOndrej Zary }
80772f2b0b2SOndrej Zary
80872f2b0b2SOndrej Zary return 0;
80972f2b0b2SOndrej Zary
81072f2b0b2SOndrej Zary out_remove_del:
81172f2b0b2SOndrej Zary bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
81272f2b0b2SOndrej Zary out_remove_new:
81372f2b0b2SOndrej Zary bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
81472f2b0b2SOndrej Zary out_unregister_dev:
81572f2b0b2SOndrej Zary device_unregister(&pata_parport_bus);
81672f2b0b2SOndrej Zary out_unregister_bus:
81772f2b0b2SOndrej Zary bus_unregister(&pata_parport_bus_type);
81872f2b0b2SOndrej Zary return error;
81972f2b0b2SOndrej Zary }
82072f2b0b2SOndrej Zary
pata_parport_exit(void)82172f2b0b2SOndrej Zary static __exit void pata_parport_exit(void)
82272f2b0b2SOndrej Zary {
82372f2b0b2SOndrej Zary parport_unregister_driver(&pata_parport_driver);
82472f2b0b2SOndrej Zary bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
82572f2b0b2SOndrej Zary bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
82672f2b0b2SOndrej Zary device_unregister(&pata_parport_bus);
82772f2b0b2SOndrej Zary bus_unregister(&pata_parport_bus_type);
82872f2b0b2SOndrej Zary }
82972f2b0b2SOndrej Zary
83072f2b0b2SOndrej Zary MODULE_AUTHOR("Ondrej Zary");
83172f2b0b2SOndrej Zary MODULE_DESCRIPTION("driver for parallel port ATA adapters");
83272f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
83372f2b0b2SOndrej Zary MODULE_ALIAS("paride");
83472f2b0b2SOndrej Zary
83572f2b0b2SOndrej Zary module_init(pata_parport_init);
83672f2b0b2SOndrej Zary module_exit(pata_parport_exit);
837