Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / misc / ppi / ppi.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ppbus/ppi.c,v 1.21.2.3 2000/08/07 18:24:43 peter Exp $
27  * $DragonFly: src/sys/dev/misc/ppi/ppi.c,v 1.2 2003/06/17 04:28:29 dillon Exp $
28  *
29  */
30 #include "opt_ppb_1284.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/uio.h>
39 #include <sys/fcntl.h>
40
41 #include <machine/clock.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45
46 #include <dev/ppbus/ppbconf.h>
47 #include <dev/ppbus/ppb_msq.h>
48
49 #ifdef PERIPH_1284
50 #include <dev/ppbus/ppb_1284.h>
51 #endif
52
53 #include <dev/ppbus/ppi.h>
54
55 #include "ppbus_if.h"
56
57 #include <dev/ppbus/ppbio.h>
58
59 #define BUFSIZE         512
60
61 struct ppi_data {
62
63     int         ppi_unit;
64     int         ppi_flags;
65 #define HAVE_PPBUS      (1<<0)
66 #define HAD_PPBUS       (1<<1)
67
68     int         ppi_count;
69     int         ppi_mode;                       /* IEEE1284 mode */
70     char        ppi_buffer[BUFSIZE];
71
72 #ifdef PERIPH_1284
73     struct resource *intr_resource;     /* interrupt resource */
74     void *intr_cookie;                  /* interrupt registration cookie */
75 #endif /* PERIPH_1284 */
76 };
77
78 #define DEVTOSOFTC(dev) \
79         ((struct ppi_data *)device_get_softc(dev))
80 #define UNITOSOFTC(unit) \
81         ((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
82 #define UNITODEVICE(unit) \
83         (devclass_get_device(ppi_devclass, (unit)))
84
85 static devclass_t ppi_devclass;
86
87 static  d_open_t        ppiopen;
88 static  d_close_t       ppiclose;
89 static  d_ioctl_t       ppiioctl;
90 static  d_write_t       ppiwrite;
91 static  d_read_t        ppiread;
92
93 #define CDEV_MAJOR 82
94 static struct cdevsw ppi_cdevsw = {
95         /* open */      ppiopen,
96         /* close */     ppiclose,
97         /* read */      ppiread,
98         /* write */     ppiwrite,
99         /* ioctl */     ppiioctl,
100         /* poll */      nopoll,
101         /* mmap */      nommap,
102         /* strategy */  nostrategy,
103         /* name */      "ppi",
104         /* maj */       CDEV_MAJOR,
105         /* dump */      nodump,
106         /* psize */     nopsize,
107         /* flags */     0,
108         /* bmaj */      -1
109 };
110
111 #ifdef PERIPH_1284
112
113 static void
114 ppi_enable_intr(device_t ppidev)
115 {
116         char r;
117         device_t ppbus = device_get_parent(ppidev);
118
119         r = ppb_rctr(ppbus);
120         ppb_wctr(ppbus, r | IRQENABLE);
121
122         return;
123 }
124
125 static void
126 ppi_disable_intr(device_t ppidev)
127 {
128         char r;
129         device_t ppbus = device_get_parent(ppidev);
130
131         r = ppb_rctr(ppbus);
132         ppb_wctr(ppbus, r & ~IRQENABLE);
133
134         return;
135 }
136
137 #endif /* PERIPH_1284 */
138
139 static void
140 ppi_identify(driver_t *driver, device_t parent)
141 {
142
143         BUS_ADD_CHILD(parent, 0, "ppi", 0);
144 }
145
146 /*
147  * ppi_probe()
148  */
149 static int
150 ppi_probe(device_t dev)
151 {
152         struct ppi_data *ppi;
153
154         /* probe is always ok */
155         device_set_desc(dev, "Parallel I/O");
156
157         ppi = DEVTOSOFTC(dev);
158         bzero(ppi, sizeof(struct ppi_data));
159
160         return (0);
161 }
162
163 /*
164  * ppi_attach()
165  */
166 static int
167 ppi_attach(device_t dev)
168 {
169 #ifdef PERIPH_1284
170         uintptr_t irq;
171         int zero = 0;
172         struct ppi_data *ppi = DEVTOSOFTC(dev);
173
174         /* retrive the irq */
175         BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
176
177         /* declare our interrupt handler */
178         ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
179                                                 &zero, irq, irq, 1, RF_ACTIVE);
180 #endif /* PERIPH_1284 */
181
182         make_dev(&ppi_cdevsw, device_get_unit(dev),     /* XXX cleanup */
183                  UID_ROOT, GID_WHEEL,
184                  0600, "ppi%d", device_get_unit(dev));
185
186         return (0);
187 }
188
189 #ifdef PERIPH_1284
190 /*
191  * Cable
192  * -----
193  *
194  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
195  *
196  * nStrobe   <-> nAck           1  <-> 10
197  * nAutofd   <-> Busy           11 <-> 14
198  * nSelectin <-> Select         17 <-> 13
199  * nInit     <-> nFault         15 <-> 16
200  *
201  */
202 static void
203 ppiintr(void *arg)
204 {
205         device_t ppidev = (device_t)arg;
206         device_t ppbus = device_get_parent(ppidev);
207         struct ppi_data *ppi = DEVTOSOFTC(ppidev);
208
209         ppi_disable_intr(ppidev);
210
211         switch (ppb_1284_get_state(ppbus)) {
212
213         /* accept IEEE1284 negociation then wakeup an waiting process to
214          * continue negociation at process level */
215         case PPB_FORWARD_IDLE:
216                 /* Event 1 */
217                 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
218                                                         (SELECT | nBUSY)) {
219                         /* IEEE1284 negociation */
220 #ifdef DEBUG_1284
221                         printf("N");
222 #endif
223
224                         /* Event 2 - prepare for reading the ext. value */
225                         ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
226
227                         ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
228
229                 } else {
230 #ifdef DEBUG_1284
231                         printf("0x%x", ppb_rstr(ppbus));
232 #endif
233                         ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
234                         break;
235                 }
236
237                 /* wake up any process waiting for negociation from
238                  * remote master host */
239
240                 /* XXX should set a variable to warn the process about
241                  * the interrupt */
242
243                 wakeup(ppi);
244                 break;
245         default:
246 #ifdef DEBUG_1284
247                 printf("?%d", ppb_1284_get_state(ppbus));
248 #endif
249                 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
250                 ppb_set_mode(ppbus, PPB_COMPATIBLE);
251                 break;
252         }
253
254         ppi_enable_intr(ppidev);
255
256         return;
257 }
258 #endif /* PERIPH_1284 */
259
260 static int
261 ppiopen(dev_t dev, int flags, int fmt, struct proc *p)
262 {
263         u_int unit = minor(dev);
264         struct ppi_data *ppi = UNITOSOFTC(unit);
265         device_t ppidev = UNITODEVICE(unit);
266         device_t ppbus = device_get_parent(ppidev);
267         int res;
268
269         if (!ppi)
270                 return (ENXIO);
271
272         if (!(ppi->ppi_flags & HAVE_PPBUS)) {
273                 if ((res = ppb_request_bus(ppbus, ppidev,
274                         (flags & O_NONBLOCK) ? PPB_DONTWAIT :
275                                                 (PPB_WAIT | PPB_INTR))))
276                         return (res);
277
278                 ppi->ppi_flags |= HAVE_PPBUS;
279
280 #ifdef PERIPH_1284
281                 if (ppi->intr_resource) {
282                         /* register our interrupt handler */
283                         BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
284                                        INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
285                 }
286 #endif /* PERIPH_1284 */
287         }
288         ppi->ppi_count += 1;
289
290         return (0);
291 }
292
293 static int
294 ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
295 {
296         u_int unit = minor(dev);
297         struct ppi_data *ppi = UNITOSOFTC(unit);
298         device_t ppidev = UNITODEVICE(unit);
299         device_t ppbus = device_get_parent(ppidev);
300
301         ppi->ppi_count --;
302         if (!ppi->ppi_count) {
303
304 #ifdef PERIPH_1284
305                 switch (ppb_1284_get_state(ppbus)) {
306                 case PPB_PERIPHERAL_IDLE:
307                         ppb_peripheral_terminate(ppbus, 0);
308                         break;
309                 case PPB_REVERSE_IDLE:
310                 case PPB_EPP_IDLE:
311                 case PPB_ECP_FORWARD_IDLE:
312                 default:
313                         ppb_1284_terminate(ppbus);
314                         break;
315                 }
316 #endif /* PERIPH_1284 */
317
318                 /* unregistration of interrupt forced by release */
319                 ppb_release_bus(ppbus, ppidev);
320
321                 ppi->ppi_flags &= ~HAVE_PPBUS;
322         }
323
324         return (0);
325 }
326
327 /*
328  * ppiread()
329  *
330  * IEEE1284 compliant read.
331  *
332  * First, try negociation to BYTE then NIBBLE mode
333  * If no data is available, wait for it otherwise transfer as much as possible
334  */
335 static int
336 ppiread(dev_t dev, struct uio *uio, int ioflag)
337 {
338 #ifdef PERIPH_1284
339         u_int unit = minor(dev);
340         struct ppi_data *ppi = UNITOSOFTC(unit);
341         device_t ppidev = UNITODEVICE(unit);
342         device_t ppbus = device_get_parent(ppidev);
343         int len, error = 0;
344
345         switch (ppb_1284_get_state(ppbus)) {
346         case PPB_PERIPHERAL_IDLE:
347                 ppb_peripheral_terminate(ppbus, 0);
348                 /* fall throught */
349
350         case PPB_FORWARD_IDLE:
351                 /* if can't negociate NIBBLE mode then try BYTE mode,
352                  * the peripheral may be a computer
353                  */
354                 if ((ppb_1284_negociate(ppbus,
355                         ppi->ppi_mode = PPB_NIBBLE, 0))) {
356
357                         /* XXX Wait 2 seconds to let the remote host some
358                          * time to terminate its interrupt
359                          */
360                         tsleep(ppi, PPBPRI, "ppiread", 2*hz);
361                         
362                         if ((error = ppb_1284_negociate(ppbus,
363                                 ppi->ppi_mode = PPB_BYTE, 0)))
364                                 return (error);
365                 }
366                 break;
367
368         case PPB_REVERSE_IDLE:
369         case PPB_EPP_IDLE:
370         case PPB_ECP_FORWARD_IDLE:
371         default:
372                 break;
373         }
374
375 #ifdef DEBUG_1284
376         printf("N");
377 #endif
378         /* read data */
379         len = 0;
380         while (uio->uio_resid) {
381                 if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
382                         ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
383                         &len))) {
384                         goto error;
385                 }
386
387                 if (!len)
388                         goto error;             /* no more data */
389
390 #ifdef DEBUG_1284
391                 printf("d");
392 #endif
393                 if ((error = uiomove(ppi->ppi_buffer, len, uio)))
394                         goto error;
395         }
396
397 error:
398
399 #else /* PERIPH_1284 */
400         int error = ENODEV;
401 #endif
402
403         return (error);
404 }
405
406 /*
407  * ppiwrite()
408  *
409  * IEEE1284 compliant write
410  *
411  * Actually, this is the peripheral side of a remote IEEE1284 read
412  *
413  * The first part of the negociation (IEEE1284 device detection) is
414  * done at interrupt level, then the remaining is done by the writing
415  * process
416  *
417  * Once negociation done, transfer data
418  */
419 static int
420 ppiwrite(dev_t dev, struct uio *uio, int ioflag)
421 {
422 #ifdef PERIPH_1284
423         u_int unit = minor(dev);
424         struct ppi_data *ppi = UNITOSOFTC(unit);
425         device_t ppidev = UNITODEVICE(unit);
426         device_t ppbus = device_get_parent(ppidev);
427         int len, error = 0, sent;
428
429 #if 0
430         int ret;
431
432         #define ADDRESS         MS_PARAM(0, 0, MS_TYP_PTR)
433         #define LENGTH          MS_PARAM(0, 1, MS_TYP_INT)
434
435         struct ppb_microseq msq[] = {
436                   { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
437                   MS_RET(0)
438         };
439
440         /* negociate ECP mode */
441         if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
442                 printf("ppiwrite: ECP negociation failed\n");
443         }
444
445         while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
446                 uiomove(ppi->ppi_buffer, len, uio);
447
448                 ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
449
450                 error = ppb_MS_microseq(ppbus, msq, &ret);
451         }
452 #endif
453
454         /* we have to be peripheral to be able to send data, so
455          * wait for the appropriate state
456          */
457         if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
458                 ppb_1284_terminate(ppbus);
459
460         while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
461                 /* XXX should check a variable before sleeping */
462 #ifdef DEBUG_1284
463                 printf("s");
464 #endif
465
466                 ppi_enable_intr(ppidev);
467
468                 /* sleep until IEEE1284 negociation starts */
469                 error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
470
471                 switch (error) {
472                 case 0:
473                         /* negociate peripheral side with BYTE mode */
474                         ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
475                         break;
476                 case EWOULDBLOCK:
477                         break;
478                 default:
479                         goto error;
480                 }
481         }
482 #ifdef DEBUG_1284
483         printf("N");
484 #endif
485
486         /* negociation done, write bytes to master host */
487         while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
488                 uiomove(ppi->ppi_buffer, len, uio);
489                 if ((error = byte_peripheral_write(ppbus,
490                                                 ppi->ppi_buffer, len, &sent)))
491                         goto error;
492 #ifdef DEBUG_1284
493                 printf("d");
494 #endif
495         }
496
497 error:
498
499 #else /* PERIPH_1284 */
500         int error = ENODEV;
501 #endif
502
503         return (error);
504 }
505
506 static int
507 ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
508 {
509         u_int unit = minor(dev);
510         device_t ppidev = UNITODEVICE(unit);
511         device_t ppbus = device_get_parent(ppidev);
512         int error = 0;
513         u_int8_t *val = (u_int8_t *)data;
514
515         switch (cmd) {
516
517         case PPIGDATA:                  /* get data register */
518                 *val = ppb_rdtr(ppbus);
519                 break;
520         case PPIGSTATUS:                /* get status bits */
521                 *val = ppb_rstr(ppbus);
522                 break;
523         case PPIGCTRL:                  /* get control bits */
524                 *val = ppb_rctr(ppbus);
525                 break;
526         case PPIGEPPD:                  /* get EPP data bits */
527                 *val = ppb_repp_D(ppbus);
528                 break;
529         case PPIGECR:                   /* get ECP bits */
530                 *val = ppb_recr(ppbus);
531                 break;
532         case PPIGFIFO:                  /* read FIFO */
533                 *val = ppb_rfifo(ppbus);
534                 break;
535         case PPISDATA:                  /* set data register */
536                 ppb_wdtr(ppbus, *val);
537                 break;
538         case PPISSTATUS:                /* set status bits */
539                 ppb_wstr(ppbus, *val);
540                 break;
541         case PPISCTRL:                  /* set control bits */
542                 ppb_wctr(ppbus, *val);
543                 break;
544         case PPISEPPD:                  /* set EPP data bits */
545                 ppb_wepp_D(ppbus, *val);
546                 break;
547         case PPISECR:                   /* set ECP bits */
548                 ppb_wecr(ppbus, *val);
549                 break;
550         case PPISFIFO:                  /* write FIFO */
551                 ppb_wfifo(ppbus, *val);
552                 break;
553         case PPIGEPPA:                  /* get EPP address bits */
554                 *val = ppb_repp_A(ppbus);
555                 break;
556         case PPISEPPA:                  /* set EPP address bits */
557                 ppb_wepp_A(ppbus, *val);
558                 break;
559         default:
560                 error = ENOTTY;
561                 break;
562         }
563     
564         return (error);
565 }
566
567 static device_method_t ppi_methods[] = {
568         /* device interface */
569         DEVMETHOD(device_identify,      ppi_identify),
570         DEVMETHOD(device_probe,         ppi_probe),
571         DEVMETHOD(device_attach,        ppi_attach),
572
573         { 0, 0 }
574 };
575
576 static driver_t ppi_driver = {
577         "ppi",
578         ppi_methods,
579         sizeof(struct ppi_data),
580 };
581 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);