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