xref: /linux/drivers/ata/pata_parport/pata_parport.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2023 Ondrej Zary
4  * based on paride.c by Grant R. Guenther <grant@torque.net>
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/parport.h>
9 #include "pata_parport.h"
10 
11 #define DRV_NAME "pata_parport"
12 
13 static DEFINE_IDR(parport_list);
14 static DEFINE_IDR(protocols);
15 static DEFINE_IDA(pata_parport_bus_dev_ids);
16 static DEFINE_MUTEX(pi_mutex);
17 
18 static bool probe = true;
19 module_param(probe, bool, 0644);
20 MODULE_PARM_DESC(probe, "Enable automatic device probing (0=off, 1=on [default])");
21 
22 /*
23  * libata drivers cannot sleep so this driver claims parport before activating
24  * the ata host and keeps it claimed (and protocol connected) until the ata
25  * host is removed. Unfortunately, this means that you cannot use any chained
26  * devices (neither other pata_parport devices nor a printer).
27  */
pi_connect(struct pi_adapter * pi)28 static void pi_connect(struct pi_adapter *pi)
29 {
30 	parport_claim_or_block(pi->pardev);
31 	pi->proto->connect(pi);
32 }
33 
pi_disconnect(struct pi_adapter * pi)34 static void pi_disconnect(struct pi_adapter *pi)
35 {
36 	pi->proto->disconnect(pi);
37 	parport_release(pi->pardev);
38 }
39 
pata_parport_dev_select(struct ata_port * ap,unsigned int device)40 static void pata_parport_dev_select(struct ata_port *ap, unsigned int device)
41 {
42 	struct pi_adapter *pi = ap->host->private_data;
43 	u8 tmp;
44 
45 	if (device == 0)
46 		tmp = ATA_DEVICE_OBS;
47 	else
48 		tmp = ATA_DEVICE_OBS | ATA_DEV1;
49 
50 	pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tmp);
51 	ata_sff_pause(ap);
52 }
53 
pata_parport_set_devctl(struct ata_port * ap,u8 ctl)54 static void pata_parport_set_devctl(struct ata_port *ap, u8 ctl)
55 {
56 	struct pi_adapter *pi = ap->host->private_data;
57 
58 	pi->proto->write_regr(pi, 1, 6, ctl);
59 }
60 
pata_parport_devchk(struct ata_port * ap,unsigned int device)61 static bool pata_parport_devchk(struct ata_port *ap, unsigned int device)
62 {
63 	struct pi_adapter *pi = ap->host->private_data;
64 	u8 nsect, lbal;
65 
66 	pata_parport_dev_select(ap, device);
67 
68 	pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
69 	pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
70 
71 	pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0xaa);
72 	pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0x55);
73 
74 	pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
75 	pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
76 
77 	nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
78 	lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
79 
80 	return (nsect == 0x55) && (lbal == 0xaa);
81 }
82 
pata_parport_wait_after_reset(struct ata_link * link,unsigned int devmask,unsigned long deadline)83 static int pata_parport_wait_after_reset(struct ata_link *link,
84 					 unsigned int devmask,
85 					 unsigned long deadline)
86 {
87 	struct ata_port *ap = link->ap;
88 	struct pi_adapter *pi = ap->host->private_data;
89 	unsigned int dev0 = devmask & (1 << 0);
90 	unsigned int dev1 = devmask & (1 << 1);
91 	int rc, ret = 0;
92 
93 	ata_msleep(ap, ATA_WAIT_AFTER_RESET);
94 
95 	/* always check readiness of the master device */
96 	rc = ata_sff_wait_ready(link, deadline);
97 	if (rc) {
98 		/*
99 		 * some adapters return bogus values if master device is not
100 		 * present, so don't abort now if a slave device is present
101 		 */
102 		if (!dev1)
103 			return rc;
104 		ret = -ENODEV;
105 	}
106 
107 	/*
108 	 * if device 1 was found in ata_devchk, wait for register
109 	 * access briefly, then wait for BSY to clear.
110 	 */
111 	if (dev1) {
112 		int i;
113 
114 		pata_parport_dev_select(ap, 1);
115 
116 		/*
117 		 * Wait for register access.  Some ATAPI devices fail
118 		 * to set nsect/lbal after reset, so don't waste too
119 		 * much time on it.  We're gonna wait for !BSY anyway.
120 		 */
121 		for (i = 0; i < 2; i++) {
122 			u8 nsect, lbal;
123 
124 			nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
125 			lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
126 			if (nsect == 1 && lbal == 1)
127 				break;
128 			/* give drive a breather */
129 			ata_msleep(ap, 50);
130 		}
131 
132 		rc = ata_sff_wait_ready(link, deadline);
133 		if (rc) {
134 			if (rc != -ENODEV)
135 				return rc;
136 			ret = rc;
137 		}
138 	}
139 
140 	pata_parport_dev_select(ap, 0);
141 	if (dev1)
142 		pata_parport_dev_select(ap, 1);
143 	if (dev0)
144 		pata_parport_dev_select(ap, 0);
145 
146 	return ret;
147 }
148 
pata_parport_bus_softreset(struct ata_port * ap,unsigned int devmask,unsigned long deadline)149 static int pata_parport_bus_softreset(struct ata_port *ap, unsigned int devmask,
150 				      unsigned long deadline)
151 {
152 	struct pi_adapter *pi = ap->host->private_data;
153 
154 	/* software reset.  causes dev0 to be selected */
155 	pi->proto->write_regr(pi, 1, 6, ap->ctl);
156 	udelay(20);
157 	pi->proto->write_regr(pi, 1, 6, ap->ctl | ATA_SRST);
158 	udelay(20);
159 	pi->proto->write_regr(pi, 1, 6, ap->ctl);
160 	ap->last_ctl = ap->ctl;
161 
162 	/* wait the port to become ready */
163 	return pata_parport_wait_after_reset(&ap->link, devmask, deadline);
164 }
165 
pata_parport_softreset(struct ata_link * link,unsigned int * classes,unsigned long deadline)166 static int pata_parport_softreset(struct ata_link *link, unsigned int *classes,
167 				  unsigned long deadline)
168 {
169 	struct ata_port *ap = link->ap;
170 	unsigned int devmask = 0;
171 	int rc;
172 	u8 err;
173 
174 	/* determine if device 0/1 are present */
175 	if (pata_parport_devchk(ap, 0))
176 		devmask |= (1 << 0);
177 	if (pata_parport_devchk(ap, 1))
178 		devmask |= (1 << 1);
179 
180 	/* select device 0 again */
181 	pata_parport_dev_select(ap, 0);
182 
183 	/* issue bus reset */
184 	rc = pata_parport_bus_softreset(ap, devmask, deadline);
185 	if (rc && rc != -ENODEV) {
186 		ata_link_err(link, "SRST failed (errno=%d)\n", rc);
187 		return rc;
188 	}
189 
190 	/* determine by signature whether we have ATA or ATAPI devices */
191 	classes[0] = ata_sff_dev_classify(&link->device[0],
192 					  devmask & (1 << 0), &err);
193 	if (err != 0x81)
194 		classes[1] = ata_sff_dev_classify(&link->device[1],
195 						  devmask & (1 << 1), &err);
196 
197 	return 0;
198 }
199 
pata_parport_check_status(struct ata_port * ap)200 static u8 pata_parport_check_status(struct ata_port *ap)
201 {
202 	struct pi_adapter *pi = ap->host->private_data;
203 
204 	return pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
205 }
206 
pata_parport_check_altstatus(struct ata_port * ap)207 static u8 pata_parport_check_altstatus(struct ata_port *ap)
208 {
209 	struct pi_adapter *pi = ap->host->private_data;
210 
211 	return pi->proto->read_regr(pi, 1, 6);
212 }
213 
pata_parport_tf_load(struct ata_port * ap,const struct ata_taskfile * tf)214 static void pata_parport_tf_load(struct ata_port *ap,
215 				 const struct ata_taskfile *tf)
216 {
217 	struct pi_adapter *pi = ap->host->private_data;
218 
219 	if (tf->ctl != ap->last_ctl) {
220 		pi->proto->write_regr(pi, 1, 6, tf->ctl);
221 		ap->last_ctl = tf->ctl;
222 		ata_wait_idle(ap);
223 	}
224 
225 	if (tf->flags & ATA_TFLAG_ISADDR) {
226 		if (tf->flags & ATA_TFLAG_LBA48) {
227 			pi->proto->write_regr(pi, 0, ATA_REG_FEATURE,
228 					      tf->hob_feature);
229 			pi->proto->write_regr(pi, 0, ATA_REG_NSECT,
230 					      tf->hob_nsect);
231 			pi->proto->write_regr(pi, 0, ATA_REG_LBAL,
232 					      tf->hob_lbal);
233 			pi->proto->write_regr(pi, 0, ATA_REG_LBAM,
234 					      tf->hob_lbam);
235 			pi->proto->write_regr(pi, 0, ATA_REG_LBAH,
236 					      tf->hob_lbah);
237 		}
238 		pi->proto->write_regr(pi, 0, ATA_REG_FEATURE, tf->feature);
239 		pi->proto->write_regr(pi, 0, ATA_REG_NSECT, tf->nsect);
240 		pi->proto->write_regr(pi, 0, ATA_REG_LBAL, tf->lbal);
241 		pi->proto->write_regr(pi, 0, ATA_REG_LBAM, tf->lbam);
242 		pi->proto->write_regr(pi, 0, ATA_REG_LBAH, tf->lbah);
243 	}
244 
245 	if (tf->flags & ATA_TFLAG_DEVICE)
246 		pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tf->device);
247 
248 	ata_wait_idle(ap);
249 }
250 
pata_parport_tf_read(struct ata_port * ap,struct ata_taskfile * tf)251 static void pata_parport_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
252 {
253 	struct pi_adapter *pi = ap->host->private_data;
254 
255 	tf->status = pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
256 	tf->error = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
257 	tf->nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
258 	tf->lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
259 	tf->lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
260 	tf->lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
261 	tf->device = pi->proto->read_regr(pi, 0, ATA_REG_DEVICE);
262 
263 	if (tf->flags & ATA_TFLAG_LBA48) {
264 		pi->proto->write_regr(pi, 1, 6, tf->ctl | ATA_HOB);
265 		tf->hob_feature = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
266 		tf->hob_nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
267 		tf->hob_lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
268 		tf->hob_lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
269 		tf->hob_lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
270 		pi->proto->write_regr(pi, 1, 6, tf->ctl);
271 		ap->last_ctl = tf->ctl;
272 	}
273 }
274 
pata_parport_exec_command(struct ata_port * ap,const struct ata_taskfile * tf)275 static void pata_parport_exec_command(struct ata_port *ap,
276 				      const struct ata_taskfile *tf)
277 {
278 	struct pi_adapter *pi = ap->host->private_data;
279 
280 	pi->proto->write_regr(pi, 0, ATA_REG_CMD, tf->command);
281 	ata_sff_pause(ap);
282 }
283 
pata_parport_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)284 static unsigned int pata_parport_data_xfer(struct ata_queued_cmd *qc,
285 				unsigned char *buf, unsigned int buflen, int rw)
286 {
287 	struct ata_port *ap = qc->dev->link->ap;
288 	struct pi_adapter *pi = ap->host->private_data;
289 
290 	if (rw == READ)
291 		pi->proto->read_block(pi, buf, buflen);
292 	else
293 		pi->proto->write_block(pi, buf, buflen);
294 
295 	return buflen;
296 }
297 
pata_parport_drain_fifo(struct ata_queued_cmd * qc)298 static void pata_parport_drain_fifo(struct ata_queued_cmd *qc)
299 {
300 	int count;
301 	struct ata_port *ap;
302 	struct pi_adapter *pi;
303 	char junk[2];
304 
305 	/* We only need to flush incoming data when a command was running */
306 	if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
307 		return;
308 
309 	ap = qc->ap;
310 	pi = ap->host->private_data;
311 	/* Drain up to 64K of data before we give up this recovery method */
312 	for (count = 0; (pata_parport_check_status(ap) & ATA_DRQ)
313 						&& count < 65536; count += 2) {
314 		pi->proto->read_block(pi, junk, 2);
315 	}
316 
317 	if (count)
318 		ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
319 }
320 
321 static struct ata_port_operations pata_parport_port_ops = {
322 	.inherits		= &ata_sff_port_ops,
323 
324 	.softreset		= pata_parport_softreset,
325 	.hardreset		= NULL,
326 
327 	.sff_dev_select		= pata_parport_dev_select,
328 	.sff_set_devctl		= pata_parport_set_devctl,
329 	.sff_check_status	= pata_parport_check_status,
330 	.sff_check_altstatus	= pata_parport_check_altstatus,
331 	.sff_tf_load		= pata_parport_tf_load,
332 	.sff_tf_read		= pata_parport_tf_read,
333 	.sff_exec_command	= pata_parport_exec_command,
334 	.sff_data_xfer		= pata_parport_data_xfer,
335 	.sff_drain_fifo		= pata_parport_drain_fifo,
336 };
337 
338 static const struct ata_port_info pata_parport_port_info = {
339 	.flags		= ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_POLLING,
340 	.pio_mask	= ATA_PIO0,
341 	/* No DMA */
342 	.port_ops	= &pata_parport_port_ops,
343 };
344 
pi_release(struct pi_adapter * pi)345 static void pi_release(struct pi_adapter *pi)
346 {
347 	parport_unregister_device(pi->pardev);
348 	if (pi->proto->release_proto)
349 		pi->proto->release_proto(pi);
350 	module_put(pi->proto->owner);
351 }
352 
default_test_proto(struct pi_adapter * pi)353 static int default_test_proto(struct pi_adapter *pi)
354 {
355 	int j, k;
356 	int e[2] = { 0, 0 };
357 
358 	pi->proto->connect(pi);
359 
360 	for (j = 0; j < 2; j++) {
361 		pi->proto->write_regr(pi, 0, 6, 0xa0 + j * 0x10);
362 		for (k = 0; k < 256; k++) {
363 			pi->proto->write_regr(pi, 0, 2, k ^ 0xaa);
364 			pi->proto->write_regr(pi, 0, 3, k ^ 0x55);
365 			if (pi->proto->read_regr(pi, 0, 2) != (k ^ 0xaa))
366 				e[j]++;
367 		}
368 	}
369 	pi->proto->disconnect(pi);
370 
371 	dev_dbg(&pi->dev, "%s: port 0x%x, mode %d, test=(%d,%d)\n",
372 		pi->proto->name, pi->port, pi->mode, e[0], e[1]);
373 
374 	return e[0] && e[1];	/* not here if both > 0 */
375 }
376 
pi_test_proto(struct pi_adapter * pi)377 static int pi_test_proto(struct pi_adapter *pi)
378 {
379 	int res;
380 
381 	parport_claim_or_block(pi->pardev);
382 	if (pi->proto->test_proto)
383 		res = pi->proto->test_proto(pi);
384 	else
385 		res = default_test_proto(pi);
386 	parport_release(pi->pardev);
387 
388 	return res;
389 }
390 
pi_probe_mode(struct pi_adapter * pi,int max)391 static bool pi_probe_mode(struct pi_adapter *pi, int max)
392 {
393 	int best, range;
394 
395 	if (pi->mode != -1) {
396 		if (pi->mode >= max)
397 			return false;
398 		range = 3;
399 		if (pi->mode >= pi->proto->epp_first)
400 			range = 8;
401 		if (range == 8 && pi->port % 8)
402 			return false;
403 		return !pi_test_proto(pi);
404 	}
405 	best = -1;
406 	for (pi->mode = 0; pi->mode < max; pi->mode++) {
407 		range = 3;
408 		if (pi->mode >= pi->proto->epp_first)
409 			range = 8;
410 		if (range == 8 && pi->port % 8)
411 			break;
412 		if (!pi_test_proto(pi))
413 			best = pi->mode;
414 	}
415 	pi->mode = best;
416 	return best > -1;
417 }
418 
pi_probe_unit(struct pi_adapter * pi,int unit)419 static bool pi_probe_unit(struct pi_adapter *pi, int unit)
420 {
421 	int max, s, e;
422 
423 	s = unit;
424 	e = s + 1;
425 
426 	if (s == -1) {
427 		s = 0;
428 		e = pi->proto->max_units;
429 	}
430 
431 	if (pi->proto->test_port) {
432 		parport_claim_or_block(pi->pardev);
433 		max = pi->proto->test_port(pi);
434 		parport_release(pi->pardev);
435 	} else {
436 		max = pi->proto->max_mode;
437 	}
438 
439 	if (pi->proto->probe_unit) {
440 		parport_claim_or_block(pi->pardev);
441 		for (pi->unit = s; pi->unit < e; pi->unit++) {
442 			if (pi->proto->probe_unit(pi)) {
443 				parport_release(pi->pardev);
444 				return pi_probe_mode(pi, max);
445 			}
446 		}
447 		parport_release(pi->pardev);
448 		return false;
449 	}
450 
451 	return pi_probe_mode(pi, max);
452 }
453 
pata_parport_dev_release(struct device * dev)454 static void pata_parport_dev_release(struct device *dev)
455 {
456 	struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
457 
458 	ida_free(&pata_parport_bus_dev_ids, dev->id);
459 	kfree(pi);
460 }
461 
pata_parport_bus_release(struct device * dev)462 static void pata_parport_bus_release(struct device *dev)
463 {
464 	/* nothing to do here but required to avoid warning on device removal */
465 }
466 
467 static const struct bus_type pata_parport_bus_type = {
468 	.name = DRV_NAME,
469 };
470 
471 static struct device pata_parport_bus = {
472 	.init_name = DRV_NAME,
473 	.release = pata_parport_bus_release,
474 };
475 
476 static const struct scsi_host_template pata_parport_sht = {
477 	PATA_PARPORT_SHT("pata_parport")
478 };
479 
480 struct pi_device_match {
481 	struct parport *parport;
482 	struct pi_protocol *proto;
483 };
484 
pi_find_dev(struct device * dev,void * data)485 static int pi_find_dev(struct device *dev, void *data)
486 {
487 	struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
488 	struct pi_device_match *match = data;
489 
490 	return pi->pardev->port == match->parport && pi->proto == match->proto;
491 }
492 
pi_init_one(struct parport * parport,struct pi_protocol * pr,int mode,int unit,int delay)493 static struct pi_adapter *pi_init_one(struct parport *parport,
494 			struct pi_protocol *pr, int mode, int unit, int delay)
495 {
496 	struct pardev_cb par_cb = { };
497 	const struct ata_port_info *ppi[] = { &pata_parport_port_info };
498 	struct ata_host *host;
499 	struct pi_adapter *pi;
500 	struct pi_device_match match = { .parport = parport, .proto = pr };
501 	int id;
502 
503 	/*
504 	 * Abort if there's a device already registered on the same parport
505 	 * using the same protocol.
506 	 */
507 	if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev))
508 		return NULL;
509 
510 	id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
511 	if (id < 0)
512 		return NULL;
513 
514 	pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL);
515 	if (!pi) {
516 		ida_free(&pata_parport_bus_dev_ids, id);
517 		return NULL;
518 	}
519 
520 	/* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
521 	pi->dev.parent = &pata_parport_bus;
522 	pi->dev.bus = &pata_parport_bus_type;
523 	pi->dev.driver = &pr->driver;
524 	pi->dev.release = pata_parport_dev_release;
525 	pi->dev.id = id;
526 	dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
527 	if (device_register(&pi->dev)) {
528 		put_device(&pi->dev);
529 		/* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
530 		return NULL;
531 	}
532 
533 	pi->proto = pr;
534 
535 	if (!try_module_get(pi->proto->owner))
536 		goto out_unreg_dev;
537 	if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
538 		goto out_module_put;
539 
540 	pi->delay = (delay == -1) ? pi->proto->default_delay : delay;
541 	pi->mode = mode;
542 	pi->port = parport->base;
543 
544 	par_cb.private = pi;
545 	pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, id);
546 	if (!pi->pardev)
547 		goto out_module_put;
548 
549 	if (!pi_probe_unit(pi, unit)) {
550 		dev_info(&pi->dev, "Adapter not found\n");
551 		goto out_unreg_parport;
552 	}
553 
554 	pi->proto->log_adapter(pi);
555 
556 	host = ata_host_alloc_pinfo(&pi->pardev->dev, ppi, 1);
557 	if (!host)
558 		goto out_unreg_parport;
559 	dev_set_drvdata(&pi->dev, host);
560 	host->private_data = pi;
561 
562 	ata_port_desc(host->ports[0], "port %s", pi->pardev->port->name);
563 	ata_port_desc(host->ports[0], "protocol %s", pi->proto->name);
564 
565 	pi_connect(pi);
566 	if (ata_host_activate(host, 0, NULL, 0, &pata_parport_sht))
567 		goto out_disconnect;
568 
569 	return pi;
570 
571 out_disconnect:
572 	pi_disconnect(pi);
573 out_unreg_parport:
574 	parport_unregister_device(pi->pardev);
575 	if (pi->proto->release_proto)
576 		pi->proto->release_proto(pi);
577 out_module_put:
578 	module_put(pi->proto->owner);
579 out_unreg_dev:
580 	device_unregister(&pi->dev);
581 	/* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
582 	return NULL;
583 }
584 
pata_parport_register_driver(struct pi_protocol * pr)585 int pata_parport_register_driver(struct pi_protocol *pr)
586 {
587 	int error;
588 	struct parport *parport;
589 	int port_num;
590 
591 	pr->driver.bus = &pata_parport_bus_type;
592 	pr->driver.name = pr->name;
593 	error = driver_register(&pr->driver);
594 	if (error)
595 		return error;
596 
597 	mutex_lock(&pi_mutex);
598 	error = idr_alloc(&protocols, pr, 0, 0, GFP_KERNEL);
599 	if (error < 0) {
600 		driver_unregister(&pr->driver);
601 		mutex_unlock(&pi_mutex);
602 		return error;
603 	}
604 
605 	pr_info("pata_parport: protocol %s registered\n", pr->name);
606 
607 	if (probe) {
608 		/* probe all parports using this protocol */
609 		idr_for_each_entry(&parport_list, parport, port_num)
610 			pi_init_one(parport, pr, -1, -1, -1);
611 	}
612 	mutex_unlock(&pi_mutex);
613 
614 	return 0;
615 }
616 EXPORT_SYMBOL_GPL(pata_parport_register_driver);
617 
pata_parport_unregister_driver(struct pi_protocol * pr)618 void pata_parport_unregister_driver(struct pi_protocol *pr)
619 {
620 	struct pi_protocol *pr_iter;
621 	int id = -1;
622 
623 	mutex_lock(&pi_mutex);
624 	idr_for_each_entry(&protocols, pr_iter, id) {
625 		if (pr_iter == pr)
626 			break;
627 	}
628 	idr_remove(&protocols, id);
629 	mutex_unlock(&pi_mutex);
630 	driver_unregister(&pr->driver);
631 }
632 EXPORT_SYMBOL_GPL(pata_parport_unregister_driver);
633 
new_device_store(const struct bus_type * bus,const char * buf,size_t count)634 static ssize_t new_device_store(const struct bus_type *bus, const char *buf, size_t count)
635 {
636 	char port[12] = "auto";
637 	char protocol[8] = "auto";
638 	int mode = -1, unit = -1, delay = -1;
639 	struct pi_protocol *pr, *pr_wanted;
640 	struct device_driver *drv;
641 	struct parport *parport;
642 	int port_num, port_wanted, pr_num;
643 	bool ok = false;
644 
645 	if (sscanf(buf, "%11s %7s %d %d %d",
646 			port, protocol, &mode, &unit, &delay) < 1)
647 		return -EINVAL;
648 
649 	if (sscanf(port, "parport%u", &port_wanted) < 1) {
650 		if (strcmp(port, "auto")) {
651 			pr_err("invalid port name %s\n", port);
652 			return -EINVAL;
653 		}
654 		port_wanted = -1;
655 	}
656 
657 	drv = driver_find(protocol, &pata_parport_bus_type);
658 	if (!drv) {
659 		if (strcmp(protocol, "auto")) {
660 			pr_err("protocol %s not found\n", protocol);
661 			return -EINVAL;
662 		}
663 		pr_wanted = NULL;
664 	} else {
665 		pr_wanted = container_of(drv, struct pi_protocol, driver);
666 	}
667 
668 	mutex_lock(&pi_mutex);
669 	/* walk all parports */
670 	idr_for_each_entry(&parport_list, parport, port_num) {
671 		if (port_num == port_wanted || port_wanted == -1) {
672 			parport = parport_find_number(port_num);
673 			if (!parport) {
674 				pr_err("no such port %s\n", port);
675 				mutex_unlock(&pi_mutex);
676 				return -ENODEV;
677 			}
678 			/* walk all protocols */
679 			idr_for_each_entry(&protocols, pr, pr_num) {
680 				if (pr == pr_wanted || !pr_wanted)
681 					if (pi_init_one(parport, pr, mode, unit,
682 							delay))
683 						ok = true;
684 			}
685 			parport_put_port(parport);
686 		}
687 	}
688 	mutex_unlock(&pi_mutex);
689 	if (!ok)
690 		return -ENODEV;
691 
692 	return count;
693 }
694 static BUS_ATTR_WO(new_device);
695 
pi_remove_one(struct device * dev)696 static void pi_remove_one(struct device *dev)
697 {
698 	struct ata_host *host = dev_get_drvdata(dev);
699 	struct pi_adapter *pi = host->private_data;
700 
701 	ata_host_detach(host);
702 	pi_disconnect(pi);
703 	pi_release(pi);
704 	device_unregister(dev);
705 	/* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
706 }
707 
delete_device_store(const struct bus_type * bus,const char * buf,size_t count)708 static ssize_t delete_device_store(const struct bus_type *bus, const char *buf, size_t count)
709 {
710 	struct device *dev;
711 
712 	mutex_lock(&pi_mutex);
713 	dev = bus_find_device_by_name(bus, NULL, buf);
714 	if (!dev) {
715 		mutex_unlock(&pi_mutex);
716 		return -ENODEV;
717 	}
718 
719 	pi_remove_one(dev);
720 	put_device(dev);
721 	mutex_unlock(&pi_mutex);
722 
723 	return count;
724 }
725 static BUS_ATTR_WO(delete_device);
726 
pata_parport_attach(struct parport * port)727 static void pata_parport_attach(struct parport *port)
728 {
729 	struct pi_protocol *pr;
730 	int pr_num, id;
731 
732 	mutex_lock(&pi_mutex);
733 	id = idr_alloc(&parport_list, port, port->number, port->number,
734 		       GFP_KERNEL);
735 	if (id < 0) {
736 		mutex_unlock(&pi_mutex);
737 		return;
738 	}
739 
740 	if (probe) {
741 		/* probe this port using all protocols */
742 		idr_for_each_entry(&protocols, pr, pr_num)
743 			pi_init_one(port, pr, -1, -1, -1);
744 	}
745 	mutex_unlock(&pi_mutex);
746 }
747 
pi_remove_port(struct device * dev,void * p)748 static int pi_remove_port(struct device *dev, void *p)
749 {
750 	struct ata_host *host = dev_get_drvdata(dev);
751 	struct pi_adapter *pi = host->private_data;
752 
753 	if (pi->pardev->port == p)
754 		pi_remove_one(dev);
755 
756 	return 0;
757 }
758 
pata_parport_detach(struct parport * port)759 static void pata_parport_detach(struct parport *port)
760 {
761 	mutex_lock(&pi_mutex);
762 	bus_for_each_dev(&pata_parport_bus_type, NULL, port, pi_remove_port);
763 	idr_remove(&parport_list, port->number);
764 	mutex_unlock(&pi_mutex);
765 }
766 
767 static struct parport_driver pata_parport_driver = {
768 	.name = DRV_NAME,
769 	.match_port = pata_parport_attach,
770 	.detach = pata_parport_detach,
771 };
772 
pata_parport_init(void)773 static __init int pata_parport_init(void)
774 {
775 	int error;
776 
777 	error = bus_register(&pata_parport_bus_type);
778 	if (error) {
779 		pr_err("failed to register pata_parport bus, error: %d\n", error);
780 		return error;
781 	}
782 
783 	error = device_register(&pata_parport_bus);
784 	if (error) {
785 		pr_err("failed to register pata_parport bus, error: %d\n", error);
786 		goto out_unregister_bus;
787 	}
788 
789 	error = bus_create_file(&pata_parport_bus_type, &bus_attr_new_device);
790 	if (error) {
791 		pr_err("unable to create sysfs file, error: %d\n", error);
792 		goto out_unregister_dev;
793 	}
794 
795 	error = bus_create_file(&pata_parport_bus_type, &bus_attr_delete_device);
796 	if (error) {
797 		pr_err("unable to create sysfs file, error: %d\n", error);
798 		goto out_remove_new;
799 	}
800 
801 	error = parport_register_driver(&pata_parport_driver);
802 	if (error) {
803 		pr_err("unable to register parport driver, error: %d\n", error);
804 		goto out_remove_del;
805 	}
806 
807 	return 0;
808 
809 out_remove_del:
810 	bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
811 out_remove_new:
812 	bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
813 out_unregister_dev:
814 	device_unregister(&pata_parport_bus);
815 out_unregister_bus:
816 	bus_unregister(&pata_parport_bus_type);
817 	return error;
818 }
819 
pata_parport_exit(void)820 static __exit void pata_parport_exit(void)
821 {
822 	parport_unregister_driver(&pata_parport_driver);
823 	bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
824 	bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
825 	device_unregister(&pata_parport_bus);
826 	bus_unregister(&pata_parport_bus_type);
827 }
828 
829 MODULE_AUTHOR("Ondrej Zary");
830 MODULE_DESCRIPTION("driver for parallel port ATA adapters");
831 MODULE_LICENSE("GPL");
832 MODULE_ALIAS("paride");
833 
834 module_init(pata_parport_init);
835 module_exit(pata_parport_exit);
836