ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / ieee1394 / video1394.c
1 /*
2  * video1394.c - video driver for OHCI 1394 boards
3  * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
4  *                        Peter Schlaile <udbz@rz.uni-karlsruhe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /* jds -- add private data to file to keep track of iso contexts associated
22    with each open -- so release won't kill all iso transfers */
23
24 /* Damien Douxchamps: Fix failure when the number of DMA pages per frame is
25    one */
26
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/wait.h>
33 #include <linux/errno.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/pci.h>
37 #include <linux/fs.h>
38 #include <linux/poll.h>
39 #include <linux/smp_lock.h>
40 #include <linux/delay.h>
41 #include <linux/devfs_fs_kernel.h>
42 #include <linux/bitops.h>
43 #include <linux/types.h>
44 #include <linux/vmalloc.h>
45 #include <linux/timex.h>
46 #include <linux/mm.h>
47 #include <linux/ioctl32.h>
48 #include <linux/compat.h>
49 #include <linux/cdev.h>
50
51 #include "ieee1394.h"
52 #include "ieee1394_types.h"
53 #include "hosts.h"
54 #include "ieee1394_core.h"
55 #include "highlevel.h"
56 #include "video1394.h"
57 #include "nodemgr.h"
58 #include "dma.h"
59
60 #include "ohci1394.h"
61
62 #define ISO_CHANNELS 64
63
64 #ifndef virt_to_page
65 #define virt_to_page(x) MAP_NR(x)
66 #endif
67
68 #ifndef vmalloc_32
69 #define vmalloc_32(x) vmalloc(x)
70 #endif
71
72 struct it_dma_prg {
73         struct dma_cmd begin;
74         quadlet_t data[4];
75         struct dma_cmd end;
76         quadlet_t pad[4]; /* FIXME: quick hack for memory alignment */
77 };
78
79 struct dma_iso_ctx {
80         struct ti_ohci *ohci;
81         int type; /* OHCI_ISO_TRANSMIT or OHCI_ISO_RECEIVE */
82         struct ohci1394_iso_tasklet iso_tasklet;
83         int channel;
84         int ctx;
85         int last_buffer;
86         int * next_buffer;  /* For ISO Transmit of video packets
87                                to write the correct SYT field
88                                into the next block */
89         unsigned int num_desc;
90         unsigned int buf_size;
91         unsigned int frame_size;
92         unsigned int packet_size;
93         unsigned int left_size;
94         unsigned int nb_cmd;
95
96         struct dma_region dma;
97
98         struct dma_prog_region *prg_reg;
99
100         struct dma_cmd **ir_prg;
101         struct it_dma_prg **it_prg;
102
103         unsigned int *buffer_status;
104         struct timeval *buffer_time; /* time when the buffer was received */
105         unsigned int *last_used_cmd; /* For ISO Transmit with
106                                         variable sized packets only ! */
107         int ctrlClear;
108         int ctrlSet;
109         int cmdPtr;
110         int ctxMatch;
111         wait_queue_head_t waitq;
112         spinlock_t lock;
113         unsigned int syt_offset;
114         int flags;
115
116         struct list_head link;
117 };
118
119
120 struct file_ctx {
121         struct ti_ohci *ohci;
122         struct list_head context_list;
123         struct dma_iso_ctx *current_ctx;
124 };
125
126 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
127 #define VIDEO1394_DEBUG
128 #endif
129
130 #ifdef DBGMSG
131 #undef DBGMSG
132 #endif
133
134 #ifdef VIDEO1394_DEBUG
135 #define DBGMSG(card, fmt, args...) \
136 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
137 #else
138 #define DBGMSG(card, fmt, args...)
139 #endif
140
141 /* print general (card independent) information */
142 #define PRINT_G(level, fmt, args...) \
143 printk(level "video1394: " fmt "\n" , ## args)
144
145 /* print card specific information */
146 #define PRINT(level, card, fmt, args...) \
147 printk(level "video1394_%d: " fmt "\n" , card , ## args)
148
149 void wakeup_dma_ir_ctx(unsigned long l);
150 void wakeup_dma_it_ctx(unsigned long l);
151
152 static struct hpsb_highlevel video1394_highlevel;
153
154 static int free_dma_iso_ctx(struct dma_iso_ctx *d)
155 {
156         int i;
157
158         DBGMSG(d->ohci->host->id, "Freeing dma_iso_ctx %d", d->ctx);
159
160         ohci1394_stop_context(d->ohci, d->ctrlClear, NULL);
161         if (d->iso_tasklet.link.next != NULL)
162                 ohci1394_unregister_iso_tasklet(d->ohci, &d->iso_tasklet);
163
164         dma_region_free(&d->dma);
165
166         if (d->prg_reg) {
167                 for (i = 0; i < d->num_desc; i++)
168                         dma_prog_region_free(&d->prg_reg[i]);
169                 kfree(d->prg_reg);
170         }
171
172         if (d->ir_prg)
173                 kfree(d->ir_prg);
174
175         if (d->it_prg)
176                 kfree(d->it_prg);
177
178         if (d->buffer_status)
179                 kfree(d->buffer_status);
180         if (d->buffer_time)
181                 kfree(d->buffer_time);
182         if (d->last_used_cmd)
183                 kfree(d->last_used_cmd);
184         if (d->next_buffer)
185                 kfree(d->next_buffer);
186
187         list_del(&d->link);
188
189         kfree(d);
190
191         return 0;
192 }
193
194 static struct dma_iso_ctx *
195 alloc_dma_iso_ctx(struct ti_ohci *ohci, int type, int num_desc,
196                   int buf_size, int channel, unsigned int packet_size)
197 {
198         struct dma_iso_ctx *d;
199         int i;
200
201         d = kmalloc(sizeof(struct dma_iso_ctx), GFP_KERNEL);
202         if (d == NULL) {
203                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate dma_iso_ctx");
204                 return NULL;
205         }
206
207         memset(d, 0, sizeof *d);
208
209         d->ohci = ohci;
210         d->type = type;
211         d->channel = channel;
212         d->num_desc = num_desc;
213         d->frame_size = buf_size;
214         d->buf_size = PAGE_ALIGN(buf_size);
215         d->last_buffer = -1;
216         INIT_LIST_HEAD(&d->link);
217         init_waitqueue_head(&d->waitq);
218
219         /* Init the regions for easy cleanup */
220         dma_region_init(&d->dma);
221
222         if (dma_region_alloc(&d->dma, d->num_desc * d->buf_size, ohci->dev,
223                              PCI_DMA_BIDIRECTIONAL)) {
224                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate dma buffer");
225                 free_dma_iso_ctx(d);
226                 return NULL;
227         }
228
229         if (type == OHCI_ISO_RECEIVE)
230                 ohci1394_init_iso_tasklet(&d->iso_tasklet, type,
231                                           wakeup_dma_ir_ctx,
232                                           (unsigned long) d);
233         else
234                 ohci1394_init_iso_tasklet(&d->iso_tasklet, type,
235                                           wakeup_dma_it_ctx,
236                                           (unsigned long) d);
237
238         if (ohci1394_register_iso_tasklet(ohci, &d->iso_tasklet) < 0) {
239                 PRINT(KERN_ERR, ohci->host->id, "no free iso %s contexts",
240                       type == OHCI_ISO_RECEIVE ? "receive" : "transmit");
241                 free_dma_iso_ctx(d);
242                 return NULL;
243         }
244         d->ctx = d->iso_tasklet.context;
245
246         d->prg_reg = kmalloc(d->num_desc * sizeof(struct dma_prog_region),
247                         GFP_KERNEL);
248         if (d->prg_reg == NULL) {
249                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate ir prg regs");
250                 free_dma_iso_ctx(d);
251                 return NULL;
252         }
253         /* Makes for easier cleanup */
254         for (i = 0; i < d->num_desc; i++)
255                 dma_prog_region_init(&d->prg_reg[i]);
256
257         if (type == OHCI_ISO_RECEIVE) {
258                 d->ctrlSet = OHCI1394_IsoRcvContextControlSet+32*d->ctx;
259                 d->ctrlClear = OHCI1394_IsoRcvContextControlClear+32*d->ctx;
260                 d->cmdPtr = OHCI1394_IsoRcvCommandPtr+32*d->ctx;
261                 d->ctxMatch = OHCI1394_IsoRcvContextMatch+32*d->ctx;
262
263                 d->ir_prg = kmalloc(d->num_desc * sizeof(struct dma_cmd *),
264                                     GFP_KERNEL);
265
266                 if (d->ir_prg == NULL) {
267                         PRINT(KERN_ERR, ohci->host->id, "Failed to allocate dma ir prg");
268                         free_dma_iso_ctx(d);
269                         return NULL;
270                 }
271                 memset(d->ir_prg, 0, d->num_desc * sizeof(struct dma_cmd *));
272
273                 d->nb_cmd = d->buf_size / PAGE_SIZE + 1;
274                 d->left_size = (d->frame_size % PAGE_SIZE) ?
275                         d->frame_size % PAGE_SIZE : PAGE_SIZE;
276
277                 for (i = 0;i < d->num_desc; i++) {
278                         if (dma_prog_region_alloc(&d->prg_reg[i], d->nb_cmd *
279                                                   sizeof(struct dma_cmd), ohci->dev)) {
280                                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate dma ir prg");
281                                 free_dma_iso_ctx(d);
282                                 return NULL;
283                         }
284                         d->ir_prg[i] = (struct dma_cmd *)d->prg_reg[i].kvirt;
285                 }
286
287         } else {  /* OHCI_ISO_TRANSMIT */
288                 d->ctrlSet = OHCI1394_IsoXmitContextControlSet+16*d->ctx;
289                 d->ctrlClear = OHCI1394_IsoXmitContextControlClear+16*d->ctx;
290                 d->cmdPtr = OHCI1394_IsoXmitCommandPtr+16*d->ctx;
291
292                 d->it_prg = kmalloc(d->num_desc * sizeof(struct it_dma_prg *),
293                                     GFP_KERNEL);
294
295                 if (d->it_prg == NULL) {
296                         PRINT(KERN_ERR, ohci->host->id,
297                               "Failed to allocate dma it prg");
298                         free_dma_iso_ctx(d);
299                         return NULL;
300                 }
301                 memset(d->it_prg, 0, d->num_desc*sizeof(struct it_dma_prg *));
302
303                 d->packet_size = packet_size;
304
305                 if (PAGE_SIZE % packet_size || packet_size>4096) {
306                         PRINT(KERN_ERR, ohci->host->id,
307                               "Packet size %d (page_size: %ld) "
308                               "not yet supported\n",
309                               packet_size, PAGE_SIZE);
310                         free_dma_iso_ctx(d);
311                         return NULL;
312                 }
313
314                 d->nb_cmd = d->frame_size / d->packet_size;
315                 if (d->frame_size % d->packet_size) {
316                         d->nb_cmd++;
317                         d->left_size = d->frame_size % d->packet_size;
318                 } else
319                         d->left_size = d->packet_size;
320
321                 for (i = 0; i < d->num_desc; i++) {
322                         if (dma_prog_region_alloc(&d->prg_reg[i], d->nb_cmd *
323                                                 sizeof(struct it_dma_prg), ohci->dev)) {
324                                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate dma it prg");
325                                 free_dma_iso_ctx(d);
326                                 return NULL;
327                         }
328                         d->it_prg[i] = (struct it_dma_prg *)d->prg_reg[i].kvirt;
329                 }
330         }
331
332         d->buffer_status = kmalloc(d->num_desc * sizeof(unsigned int),
333                                    GFP_KERNEL);
334         d->buffer_time = kmalloc(d->num_desc * sizeof(struct timeval),
335                                    GFP_KERNEL);
336         d->last_used_cmd = kmalloc(d->num_desc * sizeof(unsigned int),
337                                    GFP_KERNEL);
338         d->next_buffer = kmalloc(d->num_desc * sizeof(int),
339                                  GFP_KERNEL);
340
341         if (d->buffer_status == NULL) {
342                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate buffer_status");
343                 free_dma_iso_ctx(d);
344                 return NULL;
345         }
346         if (d->buffer_time == NULL) {
347                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate buffer_time");
348                 free_dma_iso_ctx(d);
349                 return NULL;
350         }
351         if (d->last_used_cmd == NULL) {
352                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate last_used_cmd");
353                 free_dma_iso_ctx(d);
354                 return NULL;
355         }
356         if (d->next_buffer == NULL) {
357                 PRINT(KERN_ERR, ohci->host->id, "Failed to allocate next_buffer");
358                 free_dma_iso_ctx(d);
359                 return NULL;
360         }
361         memset(d->buffer_status, 0, d->num_desc * sizeof(unsigned int));
362         memset(d->buffer_time, 0, d->num_desc * sizeof(struct timeval));
363         memset(d->last_used_cmd, 0, d->num_desc * sizeof(unsigned int));
364         memset(d->next_buffer, -1, d->num_desc * sizeof(int));
365
366         spin_lock_init(&d->lock);
367
368         PRINT(KERN_INFO, ohci->host->id, "Iso %s DMA: %d buffers "
369               "of size %d allocated for a frame size %d, each with %d prgs",
370               (type == OHCI_ISO_RECEIVE) ? "receive" : "transmit",
371               d->num_desc, d->buf_size, d->frame_size, d->nb_cmd);
372
373         return d;
374 }
375
376 static void reset_ir_status(struct dma_iso_ctx *d, int n)
377 {
378         int i;
379         d->ir_prg[n][0].status = cpu_to_le32(4);
380         d->ir_prg[n][1].status = cpu_to_le32(PAGE_SIZE-4);
381         for (i = 2; i < d->nb_cmd - 1; i++)
382                 d->ir_prg[n][i].status = cpu_to_le32(PAGE_SIZE);
383         d->ir_prg[n][i].status = cpu_to_le32(d->left_size);
384 }
385
386 static void initialize_dma_ir_prg(struct dma_iso_ctx *d, int n, int flags)
387 {
388         struct dma_cmd *ir_prg = d->ir_prg[n];
389         struct dma_prog_region *ir_reg = &d->prg_reg[n];
390         unsigned long buf = (unsigned long)d->dma.kvirt + n * d->buf_size;
391         int i;
392
393         /* the first descriptor will read only 4 bytes */
394         ir_prg[0].control = cpu_to_le32(DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE |
395                 DMA_CTL_BRANCH | 4);
396
397         /* set the sync flag */
398         if (flags & VIDEO1394_SYNC_FRAMES)
399                 ir_prg[0].control |= cpu_to_le32(DMA_CTL_WAIT);
400
401         ir_prg[0].address = cpu_to_le32(dma_region_offset_to_bus(&d->dma, buf -
402                                 (unsigned long)d->dma.kvirt));
403         ir_prg[0].branchAddress = cpu_to_le32((dma_prog_region_offset_to_bus(ir_reg,
404                                         1 * sizeof(struct dma_cmd)) & 0xfffffff0) | 0x1);
405
406         /* If there is *not* only one DMA page per frame (hence, d->nb_cmd==2) */
407         if (d->nb_cmd > 2) {
408                 /* The second descriptor will read PAGE_SIZE-4 bytes */
409                 ir_prg[1].control = cpu_to_le32(DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE |
410                                                 DMA_CTL_BRANCH | (PAGE_SIZE-4));
411                 ir_prg[1].address = cpu_to_le32(dma_region_offset_to_bus(&d->dma, (buf + 4) -
412                                                 (unsigned long)d->dma.kvirt));
413                 ir_prg[1].branchAddress = cpu_to_le32((dma_prog_region_offset_to_bus(ir_reg,
414                                                       2 * sizeof(struct dma_cmd)) & 0xfffffff0) | 0x1);
415
416                 for (i = 2; i < d->nb_cmd - 1; i++) {
417                         ir_prg[i].control = cpu_to_le32(DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE |
418                                                         DMA_CTL_BRANCH | PAGE_SIZE);
419                         ir_prg[i].address = cpu_to_le32(dma_region_offset_to_bus(&d->dma,
420                                                         (buf+(i-1)*PAGE_SIZE) -
421                                                         (unsigned long)d->dma.kvirt));
422
423                         ir_prg[i].branchAddress =
424                                 cpu_to_le32((dma_prog_region_offset_to_bus(ir_reg,
425                                             (i + 1) * sizeof(struct dma_cmd)) & 0xfffffff0) | 0x1);
426                 }
427
428                 /* The last descriptor will generate an interrupt */
429                 ir_prg[i].control = cpu_to_le32(DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE |
430                                                 DMA_CTL_IRQ | DMA_CTL_BRANCH | d->left_size);
431                 ir_prg[i].address = cpu_to_le32(dma_region_offset_to_bus(&d->dma,
432                                                 (buf+(i-1)*PAGE_SIZE) -
433                                                 (unsigned long)d->dma.kvirt));
434         } else {
435                 /* Only one DMA page is used. Read d->left_size immediately and */
436                 /* generate an interrupt as this is also the last page. */
437                 ir_prg[1].control = cpu_to_le32(DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE |
438                                                 DMA_CTL_IRQ | DMA_CTL_BRANCH | (d->left_size-4));
439                 ir_prg[1].address = cpu_to_le32(dma_region_offset_to_bus(&d->dma,
440                                                 (buf + 4) - (unsigned long)d->dma.kvirt));
441         }
442 }
443
444 static void initialize_dma_ir_ctx(struct dma_iso_ctx *d, int tag, int flags)
445 {
446         struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
447         int i;
448
449         d->flags = flags;
450
451         ohci1394_stop_context(ohci, d->ctrlClear, NULL);
452
453         for (i=0;i<d->num_desc;i++) {
454                 initialize_dma_ir_prg(d, i, flags);
455                 reset_ir_status(d, i);
456         }
457
458         /* reset the ctrl register */
459         reg_write(ohci, d->ctrlClear, 0xf0000000);
460
461         /* Set bufferFill */
462         reg_write(ohci, d->ctrlSet, 0x80000000);
463
464         /* Set isoch header */
465         if (flags & VIDEO1394_INCLUDE_ISO_HEADERS)
466                 reg_write(ohci, d->ctrlSet, 0x40000000);
467
468         /* Set the context match register to match on all tags,
469            sync for sync tag, and listen to d->channel */
470         reg_write(ohci, d->ctxMatch, 0xf0000000|((tag&0xf)<<8)|d->channel);
471
472         /* Set up isoRecvIntMask to generate interrupts */
473         reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1<<d->ctx);
474 }
475
476 /* find which context is listening to this channel */
477 static struct dma_iso_ctx *
478 find_ctx(struct list_head *list, int type, int channel)
479 {
480         struct dma_iso_ctx *ctx;
481
482         list_for_each_entry(ctx, list, link) {
483                 if (ctx->type == type && ctx->channel == channel)
484                         return ctx;
485         }
486
487         return NULL;
488 }
489
490 void wakeup_dma_ir_ctx(unsigned long l)
491 {
492         struct dma_iso_ctx *d = (struct dma_iso_ctx *) l;
493         int i;
494
495         spin_lock(&d->lock);
496
497         for (i = 0; i < d->num_desc; i++) {
498                 if (d->ir_prg[i][d->nb_cmd-1].status & cpu_to_le32(0xFFFF0000)) {
499                         reset_ir_status(d, i);
500                         d->buffer_status[i] = VIDEO1394_BUFFER_READY;
501                         do_gettimeofday(&d->buffer_time[i]);
502                 }
503         }
504
505         spin_unlock(&d->lock);
506
507         if (waitqueue_active(&d->waitq))
508                 wake_up_interruptible(&d->waitq);
509 }
510
511 static inline void put_timestamp(struct ti_ohci *ohci, struct dma_iso_ctx * d,
512                                  int n)
513 {
514         unsigned char* buf = d->dma.kvirt + n * d->buf_size;
515         u32 cycleTimer;
516         u32 timeStamp;
517
518         if (n == -1) {
519           return;
520         }
521
522         cycleTimer = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
523
524         timeStamp = ((cycleTimer & 0x0fff) + d->syt_offset); /* 11059 = 450 us */
525         timeStamp = (timeStamp % 3072 + ((timeStamp / 3072) << 12)
526                 + (cycleTimer & 0xf000)) & 0xffff;
527
528         buf[6] = timeStamp >> 8;
529         buf[7] = timeStamp & 0xff;
530
531     /* if first packet is empty packet, then put timestamp into the next full one too */
532     if ( (le32_to_cpu(d->it_prg[n][0].data[1]) >>16) == 0x008) {
533             buf += d->packet_size;
534         buf[6] = timeStamp >> 8;
535             buf[7] = timeStamp & 0xff;
536         }
537
538     /* do the next buffer frame too in case of irq latency */
539         n = d->next_buffer[n];
540         if (n == -1) {
541           return;
542         }
543         buf = d->dma.kvirt + n * d->buf_size;
544
545         timeStamp += (d->last_used_cmd[n] << 12) & 0xffff;
546
547         buf[6] = timeStamp >> 8;
548         buf[7] = timeStamp & 0xff;
549
550     /* if first packet is empty packet, then put timestamp into the next full one too */
551     if ( (le32_to_cpu(d->it_prg[n][0].data[1]) >>16) == 0x008) {
552             buf += d->packet_size;
553         buf[6] = timeStamp >> 8;
554             buf[7] = timeStamp & 0xff;
555         }
556
557 #if 0
558         printk("curr: %d, next: %d, cycleTimer: %08x timeStamp: %08x\n",
559                curr, n, cycleTimer, timeStamp);
560 #endif
561 }
562
563 void wakeup_dma_it_ctx(unsigned long l)
564 {
565         struct dma_iso_ctx *d = (struct dma_iso_ctx *) l;
566         struct ti_ohci *ohci = d->ohci;
567         int i;
568
569         spin_lock(&d->lock);
570
571         for (i = 0; i < d->num_desc; i++) {
572                 if (d->it_prg[i][d->last_used_cmd[i]].end.status &
573                     cpu_to_le32(0xFFFF0000)) {
574                         int next = d->next_buffer[i];
575                         put_timestamp(ohci, d, next);
576                         d->it_prg[i][d->last_used_cmd[i]].end.status = 0;
577                         d->buffer_status[i] = VIDEO1394_BUFFER_READY;
578                 }
579         }
580
581         spin_unlock(&d->lock);
582
583         if (waitqueue_active(&d->waitq))
584                 wake_up_interruptible(&d->waitq);
585 }
586
587 static void initialize_dma_it_prg(struct dma_iso_ctx *d, int n, int sync_tag)
588 {
589         struct it_dma_prg *it_prg = d->it_prg[n];
590         struct dma_prog_region *it_reg = &d->prg_reg[n];
591         unsigned long buf = (unsigned long)d->dma.kvirt + n * d->buf_size;
592         int i;
593         d->last_used_cmd[n] = d->nb_cmd - 1;
594         for (i=0;i<d->nb_cmd;i++) {
595
596                 it_prg[i].begin.control = cpu_to_le32(DMA_CTL_OUTPUT_MORE |
597                         DMA_CTL_IMMEDIATE | 8) ;
598                 it_prg[i].begin.address = 0;
599
600                 it_prg[i].begin.status = 0;
601
602                 it_prg[i].data[0] = cpu_to_le32(
603                         (IEEE1394_SPEED_100 << 16)
604                         | (/* tag */ 1 << 14)
605                         | (d->channel << 8)
606                         | (TCODE_ISO_DATA << 4));
607                 if (i==0) it_prg[i].data[0] |= cpu_to_le32(sync_tag);
608                 it_prg[i].data[1] = cpu_to_le32(d->packet_size << 16);
609                 it_prg[i].data[2] = 0;
610                 it_prg[i].data[3] = 0;
611
612                 it_prg[i].end.control = cpu_to_le32(DMA_CTL_OUTPUT_LAST |
613                                              DMA_CTL_BRANCH);
614                 it_prg[i].end.address =
615                         cpu_to_le32(dma_region_offset_to_bus(&d->dma, (buf+i*d->packet_size) -
616                                                 (unsigned long)d->dma.kvirt));
617
618                 if (i<d->nb_cmd-1) {
619                         it_prg[i].end.control |= cpu_to_le32(d->packet_size);
620                         it_prg[i].begin.branchAddress =
621                                 cpu_to_le32((dma_prog_region_offset_to_bus(it_reg, (i + 1) *
622                                         sizeof(struct it_dma_prg)) & 0xfffffff0) | 0x3);
623                         it_prg[i].end.branchAddress =
624                                 cpu_to_le32((dma_prog_region_offset_to_bus(it_reg, (i + 1) *
625                                         sizeof(struct it_dma_prg)) & 0xfffffff0) | 0x3);
626                 } else {
627                         /* the last prg generates an interrupt */
628                         it_prg[i].end.control |= cpu_to_le32(DMA_CTL_UPDATE |
629                                 DMA_CTL_IRQ | d->left_size);
630                         /* the last prg doesn't branch */
631                         it_prg[i].begin.branchAddress = 0;
632                         it_prg[i].end.branchAddress = 0;
633                 }
634                 it_prg[i].end.status = 0;
635         }
636 }
637
638 static void initialize_dma_it_prg_var_packet_queue(
639         struct dma_iso_ctx *d, int n, unsigned int * packet_sizes,
640         struct ti_ohci *ohci)
641 {
642         struct it_dma_prg *it_prg = d->it_prg[n];
643         struct dma_prog_region *it_reg = &d->prg_reg[n];
644         int i;
645
646 #if 0
647         if (n != -1) {
648                 put_timestamp(ohci, d, n);
649         }
650 #endif
651         d->last_used_cmd[n] = d->nb_cmd - 1;
652
653         for (i = 0; i < d->nb_cmd; i++) {
654                 unsigned int size;
655                 if (packet_sizes[i] > d->packet_size) {
656                         size = d->packet_size;
657                 } else {
658                         size = packet_sizes[i];
659                 }
660                 it_prg[i].data[1] = cpu_to_le32(size << 16);
661                 it_prg[i].end.control = cpu_to_le32(DMA_CTL_OUTPUT_LAST | DMA_CTL_BRANCH);
662
663                 if (i < d->nb_cmd-1 && packet_sizes[i+1] != 0) {
664                         it_prg[i].end.control |= cpu_to_le32(size);
665                         it_prg[i].begin.branchAddress =
666                                 cpu_to_le32((dma_prog_region_offset_to_bus(it_reg, (i + 1) *
667                                         sizeof(struct it_dma_prg)) & 0xfffffff0) | 0x3);
668                         it_prg[i].end.branchAddress =
669                                 cpu_to_le32((dma_prog_region_offset_to_bus(it_reg, (i + 1) *
670                                         sizeof(struct it_dma_prg)) & 0xfffffff0) | 0x3);
671                 } else {
672                         /* the last prg generates an interrupt */
673                         it_prg[i].end.control |= cpu_to_le32(DMA_CTL_UPDATE |
674                                 DMA_CTL_IRQ | size);
675                         /* the last prg doesn't branch */
676                         it_prg[i].begin.branchAddress = 0;
677                         it_prg[i].end.branchAddress = 0;
678                         d->last_used_cmd[n] = i;
679                         break;
680                 }
681         }
682 }
683
684 static void initialize_dma_it_ctx(struct dma_iso_ctx *d, int sync_tag,
685                                   unsigned int syt_offset, int flags)
686 {
687         struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
688         int i;
689
690         d->flags = flags;
691         d->syt_offset = (syt_offset == 0 ? 11000 : syt_offset);
692
693         ohci1394_stop_context(ohci, d->ctrlClear, NULL);
694
695         for (i=0;i<d->num_desc;i++)
696                 initialize_dma_it_prg(d, i, sync_tag);
697
698         /* Set up isoRecvIntMask to generate interrupts */
699         reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1<<d->ctx);
700 }
701
702 static int video1394_ioctl(struct inode *inode, struct file *file,
703                            unsigned int cmd, unsigned long arg)
704 {
705         struct file_ctx *ctx = (struct file_ctx *)file->private_data;
706         struct ti_ohci *ohci = ctx->ohci;
707         unsigned long flags;
708
709         switch(cmd)
710         {
711         case VIDEO1394_IOC_LISTEN_CHANNEL:
712         case VIDEO1394_IOC_TALK_CHANNEL:
713         {
714                 struct video1394_mmap v;
715                 u64 mask;
716                 struct dma_iso_ctx *d;
717                 int i;
718
719                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
720                         return -EFAULT;
721
722                 /* if channel < 0, find lowest available one */
723                 if (v.channel < 0) {
724                     mask = (u64)0x1;
725                     for (i=0; i<ISO_CHANNELS; i++) {
726                         if (!(ohci->ISO_channel_usage & mask)) {
727                             v.channel = i;
728                             PRINT(KERN_INFO, ohci->host->id, "Found free channel %d", i);
729                             break;
730                         }
731                         mask = mask << 1;
732                     }
733                 }
734
735                 if (v.channel<0 || v.channel>(ISO_CHANNELS-1)) {
736                         PRINT(KERN_ERR, ohci->host->id,
737                               "Iso channel %d out of bounds", v.channel);
738                         return -EFAULT;
739                 }
740                 mask = (u64)0x1<<v.channel;
741                 printk("mask: %08X%08X usage: %08X%08X\n",
742                        (u32)(mask>>32),(u32)(mask&0xffffffff),
743                        (u32)(ohci->ISO_channel_usage>>32),
744                        (u32)(ohci->ISO_channel_usage&0xffffffff));
745                 if (ohci->ISO_channel_usage & mask) {
746                         PRINT(KERN_ERR, ohci->host->id,
747                               "Channel %d is already taken", v.channel);
748                         return -EFAULT;
749                 }
750                 ohci->ISO_channel_usage |= mask;
751
752                 if (v.buf_size == 0 || v.buf_size > VIDEO1394_MAX_SIZE) {
753                         PRINT(KERN_ERR, ohci->host->id,
754                               "Invalid %d length buffer requested",v.buf_size);
755                         return -EFAULT;
756                 }
757
758                 if (v.nb_buffers == 0 || v.nb_buffers > VIDEO1394_MAX_SIZE) {
759                         PRINT(KERN_ERR, ohci->host->id,
760                               "Invalid %d buffers requested",v.nb_buffers);
761                         return -EFAULT;
762                 }
763
764                 if (v.nb_buffers * v.buf_size > VIDEO1394_MAX_SIZE) {
765                         PRINT(KERN_ERR, ohci->host->id,
766                               "%d buffers of size %d bytes is too big",
767                               v.nb_buffers, v.buf_size);
768                         return -EFAULT;
769                 }
770
771                 if (cmd == VIDEO1394_IOC_LISTEN_CHANNEL) {
772                         d = alloc_dma_iso_ctx(ohci, OHCI_ISO_RECEIVE,
773                                               v.nb_buffers, v.buf_size,
774                                               v.channel, 0);
775
776                         if (d == NULL) {
777                                 PRINT(KERN_ERR, ohci->host->id,
778                                       "Couldn't allocate ir context");
779                                 return -EFAULT;
780                         }
781                         initialize_dma_ir_ctx(d, v.sync_tag, v.flags);
782
783                         ctx->current_ctx = d;
784
785                         v.buf_size = d->buf_size;
786                         list_add_tail(&d->link, &ctx->context_list);
787
788                         PRINT(KERN_INFO, ohci->host->id,
789                               "iso context %d listen on channel %d",
790                               d->ctx, v.channel);
791                 }
792                 else {
793                         d = alloc_dma_iso_ctx(ohci, OHCI_ISO_TRANSMIT,
794                                               v.nb_buffers, v.buf_size,
795                                               v.channel, v.packet_size);
796
797                         if (d == NULL) {
798                                 PRINT(KERN_ERR, ohci->host->id,
799                                       "Couldn't allocate it context");
800                                 return -EFAULT;
801                         }
802                         initialize_dma_it_ctx(d, v.sync_tag,
803                                               v.syt_offset, v.flags);
804
805                         ctx->current_ctx = d;
806
807                         v.buf_size = d->buf_size;
808
809                         list_add_tail(&d->link, &ctx->context_list);
810
811                         PRINT(KERN_INFO, ohci->host->id,
812                               "Iso context %d talk on channel %d", d->ctx,
813                               v.channel);
814                 }
815
816                 if (copy_to_user((void *)arg, &v, sizeof(v)))
817                         return -EFAULT;
818
819                 return 0;
820         }
821         case VIDEO1394_IOC_UNLISTEN_CHANNEL:
822         case VIDEO1394_IOC_UNTALK_CHANNEL:
823         {
824                 int channel;
825                 u64 mask;
826                 struct dma_iso_ctx *d;
827
828                 if (copy_from_user(&channel, (void *)arg, sizeof(int)))
829                         return -EFAULT;
830
831                 if (channel<0 || channel>(ISO_CHANNELS-1)) {
832                         PRINT(KERN_ERR, ohci->host->id,
833                               "Iso channel %d out of bound", channel);
834                         return -EFAULT;
835                 }
836                 mask = (u64)0x1<<channel;
837                 if (!(ohci->ISO_channel_usage & mask)) {
838                         PRINT(KERN_ERR, ohci->host->id,
839                               "Channel %d is not being used", channel);
840                         return -EFAULT;
841                 }
842
843                 /* Mark this channel as unused */
844                 ohci->ISO_channel_usage &= ~mask;
845
846                 if (cmd == VIDEO1394_IOC_UNLISTEN_CHANNEL)
847                         d = find_ctx(&ctx->context_list, OHCI_ISO_RECEIVE, channel);
848                 else
849                         d = find_ctx(&ctx->context_list, OHCI_ISO_TRANSMIT, channel);
850
851                 if (d == NULL) return -EFAULT;
852                 PRINT(KERN_INFO, ohci->host->id, "Iso context %d "
853                       "stop talking on channel %d", d->ctx, channel);
854                 free_dma_iso_ctx(d);
855
856                 return 0;
857         }
858         case VIDEO1394_IOC_LISTEN_QUEUE_BUFFER:
859         {
860                 struct video1394_wait v;
861                 struct dma_iso_ctx *d;
862
863                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
864                         return -EFAULT;
865
866                 d = find_ctx(&ctx->context_list, OHCI_ISO_RECEIVE, v.channel);
867
868                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
869                         PRINT(KERN_ERR, ohci->host->id,
870                               "Buffer %d out of range",v.buffer);
871                         return -EFAULT;
872                 }
873
874                 spin_lock_irqsave(&d->lock,flags);
875
876                 if (d->buffer_status[v.buffer]==VIDEO1394_BUFFER_QUEUED) {
877                         PRINT(KERN_ERR, ohci->host->id,
878                               "Buffer %d is already used",v.buffer);
879                         spin_unlock_irqrestore(&d->lock,flags);
880                         return -EFAULT;
881                 }
882
883                 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
884
885                 if (d->last_buffer>=0)
886                         d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress =
887                                 cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0)
888                                         & 0xfffffff0) | 0x1);
889
890                 d->last_buffer = v.buffer;
891
892                 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = 0;
893
894                 spin_unlock_irqrestore(&d->lock,flags);
895
896                 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
897                 {
898                         DBGMSG(ohci->host->id, "Starting iso DMA ctx=%d",d->ctx);
899
900                         /* Tell the controller where the first program is */
901                         reg_write(ohci, d->cmdPtr,
902                                 dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0) | 0x1);
903
904                         /* Run IR context */
905                         reg_write(ohci, d->ctrlSet, 0x8000);
906                 }
907                 else {
908                         /* Wake up dma context if necessary */
909                         if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
910                                 PRINT(KERN_INFO, ohci->host->id,
911                                       "Waking up iso dma ctx=%d", d->ctx);
912                                 reg_write(ohci, d->ctrlSet, 0x1000);
913                         }
914                 }
915                 return 0;
916
917         }
918         case VIDEO1394_IOC_LISTEN_WAIT_BUFFER:
919         case VIDEO1394_IOC_LISTEN_POLL_BUFFER:
920         {
921                 struct video1394_wait v;
922                 struct dma_iso_ctx *d;
923                 int i;
924
925                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
926                         return -EFAULT;
927
928                 d = find_ctx(&ctx->context_list, OHCI_ISO_RECEIVE, v.channel);
929
930                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
931                         PRINT(KERN_ERR, ohci->host->id,
932                               "Buffer %d out of range",v.buffer);
933                         return -EFAULT;
934                 }
935
936                 /*
937                  * I change the way it works so that it returns
938                  * the last received frame.
939                  */
940                 spin_lock_irqsave(&d->lock, flags);
941                 switch(d->buffer_status[v.buffer]) {
942                 case VIDEO1394_BUFFER_READY:
943                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
944                         break;
945                 case VIDEO1394_BUFFER_QUEUED:
946                         if (cmd == VIDEO1394_IOC_LISTEN_POLL_BUFFER) {
947                             /* for polling, return error code EINTR */
948                             spin_unlock_irqrestore(&d->lock, flags);
949                             return -EINTR;
950                         }
951
952 #if 1
953                         while (d->buffer_status[v.buffer]!=
954                               VIDEO1394_BUFFER_READY) {
955                                 spin_unlock_irqrestore(&d->lock, flags);
956                                 interruptible_sleep_on(&d->waitq);
957                                 spin_lock_irqsave(&d->lock, flags);
958                                 if (signal_pending(current)) {
959                                         spin_unlock_irqrestore(&d->lock,flags);
960                                         return -EINTR;
961                                 }
962                         }
963 #else
964                         if (wait_event_interruptible(d->waitq,
965                                                      d->buffer_status[v.buffer]
966                                                      == VIDEO1394_BUFFER_READY)
967                             == -ERESTARTSYS)
968                                 return -EINTR;
969 #endif
970                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
971                         break;
972                 default:
973                         PRINT(KERN_ERR, ohci->host->id,
974                               "Buffer %d is not queued",v.buffer);
975                         spin_unlock_irqrestore(&d->lock, flags);
976                         return -EFAULT;
977                 }
978
979                 /* set time of buffer */
980                 v.filltime = d->buffer_time[v.buffer];
981 //              printk("Buffer %d time %d\n", v.buffer, (d->buffer_time[v.buffer]).tv_usec);
982
983                 /*
984                  * Look ahead to see how many more buffers have been received
985                  */
986                 i=0;
987                 while (d->buffer_status[(v.buffer+1)%d->num_desc]==
988                        VIDEO1394_BUFFER_READY) {
989                         v.buffer=(v.buffer+1)%d->num_desc;
990                         i++;
991                 }
992                 spin_unlock_irqrestore(&d->lock, flags);
993
994                 v.buffer=i;
995                 if (copy_to_user((void *)arg, &v, sizeof(v)))
996                         return -EFAULT;
997
998                 return 0;
999         }
1000         case VIDEO1394_IOC_TALK_QUEUE_BUFFER:
1001         {
1002                 struct video1394_wait v;
1003                 struct video1394_queue_variable qv;
1004                 struct dma_iso_ctx *d;
1005
1006                 qv.packet_sizes = NULL;
1007
1008                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
1009                         return -EFAULT;
1010
1011                 d = find_ctx(&ctx->context_list, OHCI_ISO_TRANSMIT, v.channel);
1012
1013                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1014                         PRINT(KERN_ERR, ohci->host->id,
1015                               "Buffer %d out of range",v.buffer);
1016                         return -EFAULT;
1017                 }
1018
1019                 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1020                         unsigned int *psizes;
1021                         int buf_size = d->nb_cmd * sizeof(unsigned int);
1022
1023                         if (copy_from_user(&qv, (void *)arg, sizeof(qv)))
1024                                 return -EFAULT;
1025
1026                         psizes = kmalloc(buf_size, GFP_KERNEL);
1027                         if (!psizes)
1028                                 return -ENOMEM;
1029
1030                         if (copy_from_user(psizes, qv.packet_sizes, buf_size)) {
1031                                 kfree(psizes);
1032                                 return -EFAULT;
1033                         }
1034
1035                         qv.packet_sizes = psizes;
1036                 }
1037
1038                 spin_lock_irqsave(&d->lock,flags);
1039
1040                 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
1041                         PRINT(KERN_ERR, ohci->host->id,
1042                               "Buffer %d is already used",v.buffer);
1043                         spin_unlock_irqrestore(&d->lock,flags);
1044                         if (qv.packet_sizes)
1045                                 kfree(qv.packet_sizes);
1046                         return -EFAULT;
1047                 }
1048
1049                 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1050                         initialize_dma_it_prg_var_packet_queue(
1051                                 d, v.buffer, qv.packet_sizes,
1052                                 ohci);
1053                 }
1054
1055                 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
1056
1057                 if (d->last_buffer >= 0) {
1058                         d->it_prg[d->last_buffer]
1059                                 [ d->last_used_cmd[d->last_buffer] ].end.branchAddress =
1060                                         cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer],
1061                                                 0) & 0xfffffff0) | 0x3);
1062
1063                         d->it_prg[d->last_buffer]
1064                                 [ d->last_used_cmd[d->last_buffer] ].begin.branchAddress =
1065                                         cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer],
1066                                                 0) & 0xfffffff0) | 0x3);
1067                         d->next_buffer[d->last_buffer] = v.buffer;
1068                 }
1069                 d->last_buffer = v.buffer;
1070                 d->next_buffer[d->last_buffer] = -1;
1071
1072                 d->it_prg[d->last_buffer][d->last_used_cmd[d->last_buffer]].end.branchAddress = 0;
1073
1074                 spin_unlock_irqrestore(&d->lock,flags);
1075
1076                 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
1077                 {
1078                         DBGMSG(ohci->host->id, "Starting iso transmit DMA ctx=%d",
1079                                d->ctx);
1080                         put_timestamp(ohci, d, d->last_buffer);
1081
1082                         /* Tell the controller where the first program is */
1083                         reg_write(ohci, d->cmdPtr,
1084                                 dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0) | 0x3);
1085
1086                         /* Run IT context */
1087                         reg_write(ohci, d->ctrlSet, 0x8000);
1088                 }
1089                 else {
1090                         /* Wake up dma context if necessary */
1091                         if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
1092                                 PRINT(KERN_INFO, ohci->host->id,
1093                                       "Waking up iso transmit dma ctx=%d",
1094                                       d->ctx);
1095                                 put_timestamp(ohci, d, d->last_buffer);
1096                                 reg_write(ohci, d->ctrlSet, 0x1000);
1097                         }
1098                 }
1099
1100                 if (qv.packet_sizes)
1101                         kfree(qv.packet_sizes);
1102
1103                 return 0;
1104
1105         }
1106         case VIDEO1394_IOC_TALK_WAIT_BUFFER:
1107         {
1108                 struct video1394_wait v;
1109                 struct dma_iso_ctx *d;
1110
1111                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
1112                         return -EFAULT;
1113
1114                 d = find_ctx(&ctx->context_list, OHCI_ISO_TRANSMIT, v.channel);
1115
1116                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1117                         PRINT(KERN_ERR, ohci->host->id,
1118                               "Buffer %d out of range",v.buffer);
1119                         return -EFAULT;
1120                 }
1121
1122                 switch(d->buffer_status[v.buffer]) {
1123                 case VIDEO1394_BUFFER_READY:
1124                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1125                         return 0;
1126                 case VIDEO1394_BUFFER_QUEUED:
1127 #if 1
1128                         while (d->buffer_status[v.buffer]!=
1129                               VIDEO1394_BUFFER_READY) {
1130                                 interruptible_sleep_on(&d->waitq);
1131                                 if (signal_pending(current)) return -EINTR;
1132                         }
1133 #else
1134                         if (wait_event_interruptible(d->waitq,
1135                                                      d->buffer_status[v.buffer]
1136                                                      == VIDEO1394_BUFFER_READY)
1137                             == -ERESTARTSYS)
1138                                 return -EINTR;
1139 #endif
1140                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1141                         return 0;
1142                 default:
1143                         PRINT(KERN_ERR, ohci->host->id,
1144                               "Buffer %d is not queued",v.buffer);
1145                         return -EFAULT;
1146                 }
1147         }
1148         default:
1149                 return -EINVAL;
1150         }
1151 }
1152
1153 /*
1154  *      This maps the vmalloced and reserved buffer to user space.
1155  *
1156  *  FIXME:
1157  *  - PAGE_READONLY should suffice!?
1158  *  - remap_page_range is kind of inefficient for page by page remapping.
1159  *    But e.g. pte_alloc() does not work in modules ... :-(
1160  */
1161
1162 int video1394_mmap(struct file *file, struct vm_area_struct *vma)
1163 {
1164         struct file_ctx *ctx = (struct file_ctx *)file->private_data;
1165         int res = -EINVAL;
1166
1167         lock_kernel();
1168         if (ctx->current_ctx == NULL) {
1169                 PRINT(KERN_ERR, ctx->ohci->host->id, "Current iso context not set");
1170         } else
1171                 res = dma_region_mmap(&ctx->current_ctx->dma, file, vma);
1172         unlock_kernel();
1173
1174         return res;
1175 }
1176
1177 static int video1394_open(struct inode *inode, struct file *file)
1178 {
1179         int i = ieee1394_file_to_instance(file);
1180         struct ti_ohci *ohci;
1181         struct file_ctx *ctx;
1182
1183         ohci = hpsb_get_hostinfo_bykey(&video1394_highlevel, i);
1184         if (ohci == NULL)
1185                 return -EIO;
1186
1187         ctx = kmalloc(sizeof(struct file_ctx), GFP_KERNEL);
1188         if (ctx == NULL)  {
1189                 PRINT(KERN_ERR, ohci->host->id, "Cannot malloc file_ctx");
1190                 return -ENOMEM;
1191         }
1192
1193         memset(ctx, 0, sizeof(struct file_ctx));
1194         ctx->ohci = ohci;
1195         INIT_LIST_HEAD(&ctx->context_list);
1196         ctx->current_ctx = NULL;
1197         file->private_data = ctx;
1198
1199         return 0;
1200 }
1201
1202 static int video1394_release(struct inode *inode, struct file *file)
1203 {
1204         struct file_ctx *ctx = (struct file_ctx *)file->private_data;
1205         struct ti_ohci *ohci = ctx->ohci;
1206         struct list_head *lh, *next;
1207         u64 mask;
1208
1209         lock_kernel();
1210         list_for_each_safe(lh, next, &ctx->context_list) {
1211                 struct dma_iso_ctx *d;
1212                 d = list_entry(lh, struct dma_iso_ctx, link);
1213                 mask = (u64) 1 << d->channel;
1214
1215                 if (!(ohci->ISO_channel_usage & mask))
1216                         PRINT(KERN_ERR, ohci->host->id, "On release: Channel %d "
1217                               "is not being used", d->channel);
1218                 else
1219                         ohci->ISO_channel_usage &= ~mask;
1220                 PRINT(KERN_INFO, ohci->host->id, "On release: Iso %s context "
1221                       "%d stop listening on channel %d",
1222                       d->type == OHCI_ISO_RECEIVE ? "receive" : "transmit",
1223                       d->ctx, d->channel);
1224                 free_dma_iso_ctx(d);
1225         }
1226
1227         kfree(ctx);
1228         file->private_data = NULL;
1229
1230         unlock_kernel();
1231         return 0;
1232 }
1233
1234 static struct cdev video1394_cdev;
1235 static struct file_operations video1394_fops=
1236 {
1237         .owner =        THIS_MODULE,
1238         .ioctl =        video1394_ioctl,
1239         .mmap =         video1394_mmap,
1240         .open =         video1394_open,
1241         .release =      video1394_release
1242 };
1243
1244 /*** HOTPLUG STUFF **********************************************************/
1245 /*
1246  * Export information about protocols/devices supported by this driver.
1247  */
1248 static struct ieee1394_device_id video1394_id_table[] = {
1249         {
1250                 .match_flags    = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
1251                 .specifier_id   = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
1252                 .version        = CAMERA_SW_VERSION_ENTRY & 0xffffff
1253         },
1254         { }
1255 };
1256
1257 MODULE_DEVICE_TABLE(ieee1394, video1394_id_table);
1258
1259 static struct hpsb_protocol_driver video1394_driver = {
1260         .name           = "1394 Digital Camera Driver",
1261         .id_table       = video1394_id_table,
1262         .driver         = {
1263                 .name   = VIDEO1394_DRIVER_NAME,
1264                 .bus    = &ieee1394_bus_type,
1265         },
1266 };
1267
1268
1269 static void video1394_add_host (struct hpsb_host *host)
1270 {
1271         struct ti_ohci *ohci;
1272         int minor;
1273
1274         /* We only work with the OHCI-1394 driver */
1275         if (strcmp(host->driver->name, OHCI1394_DRIVER_NAME))
1276                 return;
1277
1278         ohci = (struct ti_ohci *)host->hostdata;
1279
1280         if (!hpsb_create_hostinfo(&video1394_highlevel, host, 0)) {
1281                 PRINT(KERN_ERR, ohci->host->id, "Cannot allocate hostinfo");
1282                 return;
1283         }
1284
1285         hpsb_set_hostinfo(&video1394_highlevel, host, ohci);
1286         hpsb_set_hostinfo_key(&video1394_highlevel, host, ohci->host->id);
1287
1288         minor = IEEE1394_MINOR_BLOCK_VIDEO1394 * 16 + ohci->host->id;
1289         devfs_mk_cdev(MKDEV(IEEE1394_MAJOR, minor),
1290                        S_IFCHR | S_IRUSR | S_IWUSR,
1291                        "%s/%d", VIDEO1394_DRIVER_NAME, ohci->host->id);
1292 }
1293
1294
1295 static void video1394_remove_host (struct hpsb_host *host)
1296 {
1297         struct ti_ohci *ohci = hpsb_get_hostinfo(&video1394_highlevel, host);
1298
1299         if (ohci)
1300                 devfs_remove("%s/%d", VIDEO1394_DRIVER_NAME, ohci->host->id);
1301
1302         return;
1303 }
1304
1305
1306 static struct hpsb_highlevel video1394_highlevel = {
1307         .name =         VIDEO1394_DRIVER_NAME,
1308         .add_host =     video1394_add_host,
1309         .remove_host =  video1394_remove_host,
1310 };
1311
1312 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1313 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1314 MODULE_SUPPORTED_DEVICE(VIDEO1394_DRIVER_NAME);
1315 MODULE_LICENSE("GPL");
1316
1317 #ifdef CONFIG_COMPAT
1318
1319 #define VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER     \
1320         _IOW ('#', 0x12, struct video1394_wait32)
1321 #define VIDEO1394_IOC32_LISTEN_WAIT_BUFFER      \
1322         _IOWR('#', 0x13, struct video1394_wait32)
1323 #define VIDEO1394_IOC32_TALK_WAIT_BUFFER        \
1324         _IOW ('#', 0x17, struct video1394_wait32)
1325 #define VIDEO1394_IOC32_LISTEN_POLL_BUFFER      \
1326         _IOWR('#', 0x18, struct video1394_wait32)
1327
1328 struct video1394_wait32 {
1329         u32 channel;
1330         u32 buffer;
1331         struct compat_timeval filltime;
1332 };
1333
1334 static int video1394_wr_wait32(unsigned int fd, unsigned int cmd, unsigned long arg,
1335                                struct file *file)
1336 {
1337         struct video1394_wait32 wait32;
1338         struct video1394_wait wait;
1339         mm_segment_t old_fs;
1340         int ret;
1341
1342         if (file->f_op->ioctl != video1394_ioctl)
1343                 return -EFAULT;
1344
1345         if (copy_from_user(&wait32, (void *)arg, sizeof(wait32)))
1346                 return -EFAULT;
1347
1348         wait.channel = wait32.channel;
1349         wait.buffer = wait32.buffer;
1350         wait.filltime.tv_sec = (time_t)wait32.filltime.tv_sec;
1351         wait.filltime.tv_usec = (suseconds_t)wait32.filltime.tv_usec;
1352
1353         old_fs = get_fs();
1354         set_fs(KERNEL_DS);
1355         if (cmd == VIDEO1394_IOC32_LISTEN_WAIT_BUFFER)
1356                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1357                                       VIDEO1394_IOC_LISTEN_WAIT_BUFFER,
1358                                       (unsigned long) &wait);
1359         else
1360                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1361                                       VIDEO1394_IOC_LISTEN_POLL_BUFFER,
1362                                       (unsigned long) &wait);
1363         set_fs(old_fs);
1364
1365         if (!ret) {
1366                 wait32.channel = wait.channel;
1367                 wait32.buffer = wait.buffer;
1368                 wait32.filltime.tv_sec = (int)wait.filltime.tv_sec;
1369                 wait32.filltime.tv_usec = (int)wait.filltime.tv_usec;
1370
1371                 if (copy_to_user((struct video1394_wait32 *)arg, &wait32, sizeof(wait32)))
1372                         ret = -EFAULT;
1373         }
1374
1375         return ret;
1376 }
1377
1378 static int video1394_w_wait32(unsigned int fd, unsigned int cmd, unsigned long arg,
1379                               struct file *file)
1380 {
1381         struct video1394_wait32 wait32;
1382         struct video1394_wait wait;
1383         mm_segment_t old_fs;
1384         int ret;
1385
1386         if (file->f_op->ioctl != video1394_ioctl)
1387                 return -EFAULT;
1388
1389         if (copy_from_user(&wait32, (void *)arg, sizeof(wait32)))
1390                 return -EFAULT;
1391
1392         wait.channel = wait32.channel;
1393         wait.buffer = wait32.buffer;
1394         wait.filltime.tv_sec = (time_t)wait32.filltime.tv_sec;
1395         wait.filltime.tv_usec = (suseconds_t)wait32.filltime.tv_usec;
1396
1397         old_fs = get_fs();
1398         set_fs(KERNEL_DS);
1399         if (cmd == VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER)
1400                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1401                                       VIDEO1394_IOC_LISTEN_QUEUE_BUFFER,
1402                                       (unsigned long) &wait);
1403         else
1404                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1405                                       VIDEO1394_IOC_TALK_WAIT_BUFFER,
1406                                       (unsigned long) &wait);
1407         set_fs(old_fs);
1408
1409         return ret;
1410 }
1411
1412 static int video1394_queue_buf32(unsigned int fd, unsigned int cmd, unsigned long arg,
1413                                  struct file *file)
1414 {
1415         if (file->f_op->ioctl != video1394_ioctl)
1416                 return -EFAULT;
1417
1418         return -EFAULT;
1419
1420         return video1394_ioctl(file->f_dentry->d_inode, file,
1421                                 VIDEO1394_IOC_TALK_QUEUE_BUFFER, arg);
1422 }
1423
1424 #endif /* CONFIG_COMPAT */
1425
1426 static void __exit video1394_exit_module (void)
1427 {
1428 #ifdef CONFIG_COMPAT
1429         int ret;
1430
1431         ret = unregister_ioctl32_conversion(VIDEO1394_IOC_LISTEN_CHANNEL);
1432         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_UNLISTEN_CHANNEL);
1433         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_TALK_CHANNEL);
1434         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_UNTALK_CHANNEL);
1435         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER);
1436         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_WAIT_BUFFER);
1437         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_TALK_QUEUE_BUFFER);
1438         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_TALK_WAIT_BUFFER);
1439         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_POLL_BUFFER);
1440         if (ret)
1441                 PRINT_G(KERN_CRIT, "Error unregistering ioctl32 translations");
1442 #endif
1443
1444         hpsb_unregister_protocol(&video1394_driver);
1445
1446         hpsb_unregister_highlevel(&video1394_highlevel);
1447
1448         devfs_remove(VIDEO1394_DRIVER_NAME);
1449         cdev_del(&video1394_cdev);
1450
1451         PRINT_G(KERN_INFO, "Removed " VIDEO1394_DRIVER_NAME " module");
1452 }
1453
1454 static int __init video1394_init_module (void)
1455 {
1456         int ret;
1457
1458         cdev_init(&video1394_cdev, &video1394_fops);
1459         video1394_cdev.owner = THIS_MODULE;
1460         kobject_set_name(&video1394_cdev.kobj, VIDEO1394_DRIVER_NAME);
1461         ret = cdev_add(&video1394_cdev, IEEE1394_VIDEO1394_DEV, 16);
1462         if (ret) {
1463                 PRINT_G(KERN_ERR, "video1394: unable to get minor device block");
1464                 return ret;
1465         }
1466
1467         devfs_mk_dir(VIDEO1394_DRIVER_NAME);
1468
1469         hpsb_register_highlevel(&video1394_highlevel);
1470
1471         ret = hpsb_register_protocol(&video1394_driver);
1472         if (ret) {
1473                 PRINT_G(KERN_ERR, "video1394: failed to register protocol");
1474                 hpsb_unregister_highlevel(&video1394_highlevel);
1475                 devfs_remove(VIDEO1394_DRIVER_NAME);
1476                 cdev_del(&video1394_cdev);
1477                 return ret;
1478         }
1479
1480 #ifdef CONFIG_COMPAT
1481         {
1482                 /* First the compatible ones */
1483                 ret = register_ioctl32_conversion(VIDEO1394_IOC_LISTEN_CHANNEL, NULL);
1484                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_UNLISTEN_CHANNEL, NULL);
1485                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_TALK_CHANNEL, NULL);
1486                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_UNTALK_CHANNEL, NULL);
1487
1488                 /* These need translation */
1489                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER,
1490                                             video1394_w_wait32);
1491                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_WAIT_BUFFER,
1492                                             video1394_wr_wait32);
1493                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_TALK_QUEUE_BUFFER,
1494                                             video1394_queue_buf32);
1495                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_TALK_WAIT_BUFFER,
1496                                             video1394_w_wait32);
1497                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_POLL_BUFFER,
1498                                             video1394_wr_wait32);
1499                 if (ret)
1500                         PRINT_G(KERN_INFO, "Error registering ioctl32 translations");
1501         }
1502 #endif
1503
1504         PRINT_G(KERN_INFO, "Installed " VIDEO1394_DRIVER_NAME " module");
1505         return 0;
1506 }
1507
1508
1509 module_init(video1394_init_module);
1510 module_exit(video1394_exit_module);
1511 MODULE_ALIAS_CHARDEV(IEEE1394_MAJOR, IEEE1394_MINOR_BLOCK_VIDEO1394 * 16);