1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/parport.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/sched/signal.h>
27
28#include <asm/current.h>
29#include <linux/uaccess.h>
30
31#undef DEBUG
32
33#ifdef DEBUG
34#define DPRINTK(stuff...) printk(stuff)
35#else
36#define DPRINTK(stuff...)
37#endif
38
39static struct daisydev {
40 struct daisydev *next;
41 struct parport *port;
42 int daisy;
43 int devnum;
44} *topology = NULL;
45static DEFINE_SPINLOCK(topology_lock);
46
47static int numdevs;
48static bool daisy_init_done;
49
50
51static int mux_present(struct parport *port);
52static int num_mux_ports(struct parport *port);
53static int select_port(struct parport *port);
54static int assign_addrs(struct parport *port);
55
56
57static void add_dev(int devnum, struct parport *port, int daisy)
58{
59 struct daisydev *newdev, **p;
60 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
61 if (newdev) {
62 newdev->port = port;
63 newdev->daisy = daisy;
64 newdev->devnum = devnum;
65 spin_lock(&topology_lock);
66 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
67 ;
68 newdev->next = *p;
69 *p = newdev;
70 spin_unlock(&topology_lock);
71 }
72}
73
74
75static struct parport *clone_parport(struct parport *real, int muxport)
76{
77 struct parport *extra = parport_register_port(real->base,
78 real->irq,
79 real->dma,
80 real->ops);
81 if (extra) {
82 extra->portnum = real->portnum;
83 extra->physport = real;
84 extra->muxport = muxport;
85 real->slaves[muxport-1] = extra;
86 }
87
88 return extra;
89}
90
91static int daisy_drv_probe(struct pardevice *par_dev)
92{
93 struct device_driver *drv = par_dev->dev.driver;
94
95 if (strcmp(drv->name, "daisy_drv"))
96 return -ENODEV;
97 if (strcmp(par_dev->name, daisy_dev_name))
98 return -ENODEV;
99
100 return 0;
101}
102
103static struct parport_driver daisy_driver = {
104 .name = "daisy_drv",
105 .probe = daisy_drv_probe,
106 .devmodel = true,
107};
108
109
110
111int parport_daisy_init(struct parport *port)
112{
113 int detected = 0;
114 char *deviceid;
115 static const char *th[] = { "th", "st", "nd", "rd", "th" };
116 int num_ports;
117 int i;
118 int last_try = 0;
119
120 if (!daisy_init_done) {
121
122
123
124
125
126
127
128 daisy_init_done = true;
129 i = parport_register_driver(&daisy_driver);
130 if (i) {
131 pr_err("daisy registration failed\n");
132 daisy_init_done = false;
133 return i;
134 }
135 }
136
137again:
138
139
140
141
142
143 if (port->muxport < 0 && mux_present(port) &&
144
145 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
146
147 port->muxport = 0;
148 printk(KERN_INFO
149 "%s: 1st (default) port of %d-way multiplexor\n",
150 port->name, num_ports);
151 for (i = 1; i < num_ports; i++) {
152
153 struct parport *extra = clone_parport(port, i);
154 if (!extra) {
155 if (signal_pending(current))
156 break;
157
158 schedule();
159 continue;
160 }
161
162 printk(KERN_INFO
163 "%s: %d%s port of %d-way multiplexor on %s\n",
164 extra->name, i + 1, th[i + 1], num_ports,
165 port->name);
166
167
168
169
170 parport_daisy_init(extra);
171 }
172 }
173
174 if (port->muxport >= 0)
175 select_port(port);
176
177 parport_daisy_deselect_all(port);
178 detected += assign_addrs(port);
179
180
181 add_dev(numdevs++, port, -1);
182
183
184 deviceid = kmalloc(1024, GFP_KERNEL);
185 if (deviceid) {
186 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
187 detected++;
188
189 kfree(deviceid);
190 }
191
192 if (!detected && !last_try) {
193
194
195
196 parport_daisy_fini(port);
197 parport_write_control(port, PARPORT_CONTROL_SELECT);
198 udelay(50);
199 parport_write_control(port,
200 PARPORT_CONTROL_SELECT |
201 PARPORT_CONTROL_INIT);
202 udelay(50);
203 last_try = 1;
204 goto again;
205 }
206
207 return detected;
208}
209
210
211void parport_daisy_fini(struct parport *port)
212{
213 struct daisydev **p;
214
215 spin_lock(&topology_lock);
216 p = &topology;
217 while (*p) {
218 struct daisydev *dev = *p;
219 if (dev->port != port) {
220 p = &dev->next;
221 continue;
222 }
223 *p = dev->next;
224 kfree(dev);
225 }
226
227
228
229
230 if (!topology) numdevs = 0;
231 spin_unlock(&topology_lock);
232 return;
233}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249struct pardevice *parport_open(int devnum, const char *name)
250{
251 struct daisydev *p = topology;
252 struct pardev_cb par_cb;
253 struct parport *port;
254 struct pardevice *dev;
255 int daisy;
256
257 memset(&par_cb, 0, sizeof(par_cb));
258 spin_lock(&topology_lock);
259 while (p && p->devnum != devnum)
260 p = p->next;
261
262 if (!p) {
263 spin_unlock(&topology_lock);
264 return NULL;
265 }
266
267 daisy = p->daisy;
268 port = parport_get_port(p->port);
269 spin_unlock(&topology_lock);
270
271 dev = parport_register_dev_model(port, name, &par_cb, devnum);
272 parport_put_port(port);
273 if (!dev)
274 return NULL;
275
276 dev->daisy = daisy;
277
278
279 if (daisy >= 0) {
280 int selected;
281 parport_claim_or_block(dev);
282 selected = port->daisy;
283 parport_release(dev);
284
285 if (selected != daisy) {
286
287 parport_unregister_device(dev);
288 return NULL;
289 }
290 }
291
292 return dev;
293}
294
295
296
297
298
299
300
301
302
303void parport_close(struct pardevice *dev)
304{
305 parport_unregister_device(dev);
306}
307
308
309static int cpp_daisy(struct parport *port, int cmd)
310{
311 unsigned char s;
312
313 parport_data_forward(port);
314 parport_write_data(port, 0xaa); udelay(2);
315 parport_write_data(port, 0x55); udelay(2);
316 parport_write_data(port, 0x00); udelay(2);
317 parport_write_data(port, 0xff); udelay(2);
318 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
319 | PARPORT_STATUS_PAPEROUT
320 | PARPORT_STATUS_SELECT
321 | PARPORT_STATUS_ERROR);
322 if (s != (PARPORT_STATUS_BUSY
323 | PARPORT_STATUS_PAPEROUT
324 | PARPORT_STATUS_SELECT
325 | PARPORT_STATUS_ERROR)) {
326 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
327 port->name, s);
328 return -ENXIO;
329 }
330
331 parport_write_data(port, 0x87); udelay(2);
332 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
333 | PARPORT_STATUS_PAPEROUT
334 | PARPORT_STATUS_SELECT
335 | PARPORT_STATUS_ERROR);
336 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
337 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
338 port->name, s);
339 return -ENXIO;
340 }
341
342 parport_write_data(port, 0x78); udelay(2);
343 parport_write_data(port, cmd); udelay(2);
344 parport_frob_control(port,
345 PARPORT_CONTROL_STROBE,
346 PARPORT_CONTROL_STROBE);
347 udelay(1);
348 s = parport_read_status(port);
349 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
350 udelay(1);
351 parport_write_data(port, 0xff); udelay(2);
352
353 return s;
354}
355
356
357static int cpp_mux(struct parport *port, int cmd)
358{
359 unsigned char s;
360 int rc;
361
362 parport_data_forward(port);
363 parport_write_data(port, 0xaa); udelay(2);
364 parport_write_data(port, 0x55); udelay(2);
365 parport_write_data(port, 0xf0); udelay(2);
366 parport_write_data(port, 0x0f); udelay(2);
367 parport_write_data(port, 0x52); udelay(2);
368 parport_write_data(port, 0xad); udelay(2);
369 parport_write_data(port, cmd); udelay(2);
370
371 s = parport_read_status(port);
372 if (!(s & PARPORT_STATUS_ACK)) {
373 DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
374 port->name, cmd, s);
375 return -EIO;
376 }
377
378 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
379 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
380 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
381 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
382
383 return rc;
384}
385
386void parport_daisy_deselect_all(struct parport *port)
387{
388 cpp_daisy(port, 0x30);
389}
390
391int parport_daisy_select(struct parport *port, int daisy, int mode)
392{
393 switch (mode)
394 {
395
396 case IEEE1284_MODE_EPP:
397 case IEEE1284_MODE_EPPSL:
398 case IEEE1284_MODE_EPPSWE:
399 return !(cpp_daisy(port, 0x20 + daisy) &
400 PARPORT_STATUS_ERROR);
401
402
403 case IEEE1284_MODE_ECP:
404 case IEEE1284_MODE_ECPRLE:
405 case IEEE1284_MODE_ECPSWE:
406 return !(cpp_daisy(port, 0xd0 + daisy) &
407 PARPORT_STATUS_ERROR);
408
409
410
411 case IEEE1284_MODE_BECP:
412
413 case IEEE1284_MODE_NIBBLE:
414 case IEEE1284_MODE_BYTE:
415 case IEEE1284_MODE_COMPAT:
416 default:
417 return !(cpp_daisy(port, 0xe0 + daisy) &
418 PARPORT_STATUS_ERROR);
419 }
420}
421
422static int mux_present(struct parport *port)
423{
424 return cpp_mux(port, 0x51) == 3;
425}
426
427static int num_mux_ports(struct parport *port)
428{
429 return cpp_mux(port, 0x58);
430}
431
432static int select_port(struct parport *port)
433{
434 int muxport = port->muxport;
435 return cpp_mux(port, 0x60 + muxport) == muxport;
436}
437
438static int assign_addrs(struct parport *port)
439{
440 unsigned char s;
441 unsigned char daisy;
442 int thisdev = numdevs;
443 int detected;
444 char *deviceid;
445
446 parport_data_forward(port);
447 parport_write_data(port, 0xaa); udelay(2);
448 parport_write_data(port, 0x55); udelay(2);
449 parport_write_data(port, 0x00); udelay(2);
450 parport_write_data(port, 0xff); udelay(2);
451 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
452 | PARPORT_STATUS_PAPEROUT
453 | PARPORT_STATUS_SELECT
454 | PARPORT_STATUS_ERROR);
455 if (s != (PARPORT_STATUS_BUSY
456 | PARPORT_STATUS_PAPEROUT
457 | PARPORT_STATUS_SELECT
458 | PARPORT_STATUS_ERROR)) {
459 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
460 port->name, s);
461 return 0;
462 }
463
464 parport_write_data(port, 0x87); udelay(2);
465 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
466 | PARPORT_STATUS_PAPEROUT
467 | PARPORT_STATUS_SELECT
468 | PARPORT_STATUS_ERROR);
469 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
470 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
471 port->name, s);
472 return 0;
473 }
474
475 parport_write_data(port, 0x78); udelay(2);
476 s = parport_read_status(port);
477
478 for (daisy = 0;
479 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
480 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
481 && daisy < 4;
482 ++daisy) {
483 parport_write_data(port, daisy);
484 udelay(2);
485 parport_frob_control(port,
486 PARPORT_CONTROL_STROBE,
487 PARPORT_CONTROL_STROBE);
488 udelay(1);
489 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
490 udelay(1);
491
492 add_dev(numdevs++, port, daisy);
493
494
495
496 if (!(s & PARPORT_STATUS_BUSY))
497 break;
498
499
500
501
502
503 s = parport_read_status(port);
504 }
505
506 parport_write_data(port, 0xff); udelay(2);
507 detected = numdevs - thisdev;
508 DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
509 detected);
510
511
512 deviceid = kmalloc(1024, GFP_KERNEL);
513 if (!deviceid) return 0;
514
515 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
516 parport_device_id(thisdev, deviceid, 1024);
517
518 kfree(deviceid);
519 return detected;
520}
521