patch-2_6_7-vs1_9_1_12
[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                 if (d == NULL) return -EFAULT;
868
869                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
870                         PRINT(KERN_ERR, ohci->host->id,
871                               "Buffer %d out of range",v.buffer);
872                         return -EFAULT;
873                 }
874
875                 spin_lock_irqsave(&d->lock,flags);
876
877                 if (d->buffer_status[v.buffer]==VIDEO1394_BUFFER_QUEUED) {
878                         PRINT(KERN_ERR, ohci->host->id,
879                               "Buffer %d is already used",v.buffer);
880                         spin_unlock_irqrestore(&d->lock,flags);
881                         return -EFAULT;
882                 }
883
884                 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
885
886                 if (d->last_buffer>=0)
887                         d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress =
888                                 cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0)
889                                         & 0xfffffff0) | 0x1);
890
891                 d->last_buffer = v.buffer;
892
893                 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = 0;
894
895                 spin_unlock_irqrestore(&d->lock,flags);
896
897                 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
898                 {
899                         DBGMSG(ohci->host->id, "Starting iso DMA ctx=%d",d->ctx);
900
901                         /* Tell the controller where the first program is */
902                         reg_write(ohci, d->cmdPtr,
903                                 dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0) | 0x1);
904
905                         /* Run IR context */
906                         reg_write(ohci, d->ctrlSet, 0x8000);
907                 }
908                 else {
909                         /* Wake up dma context if necessary */
910                         if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
911                                 PRINT(KERN_INFO, ohci->host->id,
912                                       "Waking up iso dma ctx=%d", d->ctx);
913                                 reg_write(ohci, d->ctrlSet, 0x1000);
914                         }
915                 }
916                 return 0;
917
918         }
919         case VIDEO1394_IOC_LISTEN_WAIT_BUFFER:
920         case VIDEO1394_IOC_LISTEN_POLL_BUFFER:
921         {
922                 struct video1394_wait v;
923                 struct dma_iso_ctx *d;
924                 int i;
925
926                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
927                         return -EFAULT;
928
929                 d = find_ctx(&ctx->context_list, OHCI_ISO_RECEIVE, v.channel);
930                 if (d == NULL) return -EFAULT;
931
932                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
933                         PRINT(KERN_ERR, ohci->host->id,
934                               "Buffer %d out of range",v.buffer);
935                         return -EFAULT;
936                 }
937
938                 /*
939                  * I change the way it works so that it returns
940                  * the last received frame.
941                  */
942                 spin_lock_irqsave(&d->lock, flags);
943                 switch(d->buffer_status[v.buffer]) {
944                 case VIDEO1394_BUFFER_READY:
945                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
946                         break;
947                 case VIDEO1394_BUFFER_QUEUED:
948                         if (cmd == VIDEO1394_IOC_LISTEN_POLL_BUFFER) {
949                             /* for polling, return error code EINTR */
950                             spin_unlock_irqrestore(&d->lock, flags);
951                             return -EINTR;
952                         }
953
954 #if 1
955                         while (d->buffer_status[v.buffer]!=
956                               VIDEO1394_BUFFER_READY) {
957                                 spin_unlock_irqrestore(&d->lock, flags);
958                                 interruptible_sleep_on(&d->waitq);
959                                 spin_lock_irqsave(&d->lock, flags);
960                                 if (signal_pending(current)) {
961                                         spin_unlock_irqrestore(&d->lock,flags);
962                                         return -EINTR;
963                                 }
964                         }
965 #else
966                         if (wait_event_interruptible(d->waitq,
967                                                      d->buffer_status[v.buffer]
968                                                      == VIDEO1394_BUFFER_READY)
969                             == -ERESTARTSYS)
970                                 return -EINTR;
971 #endif
972                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
973                         break;
974                 default:
975                         PRINT(KERN_ERR, ohci->host->id,
976                               "Buffer %d is not queued",v.buffer);
977                         spin_unlock_irqrestore(&d->lock, flags);
978                         return -EFAULT;
979                 }
980
981                 /* set time of buffer */
982                 v.filltime = d->buffer_time[v.buffer];
983 //              printk("Buffer %d time %d\n", v.buffer, (d->buffer_time[v.buffer]).tv_usec);
984
985                 /*
986                  * Look ahead to see how many more buffers have been received
987                  */
988                 i=0;
989                 while (d->buffer_status[(v.buffer+1)%d->num_desc]==
990                        VIDEO1394_BUFFER_READY) {
991                         v.buffer=(v.buffer+1)%d->num_desc;
992                         i++;
993                 }
994                 spin_unlock_irqrestore(&d->lock, flags);
995
996                 v.buffer=i;
997                 if (copy_to_user((void *)arg, &v, sizeof(v)))
998                         return -EFAULT;
999
1000                 return 0;
1001         }
1002         case VIDEO1394_IOC_TALK_QUEUE_BUFFER:
1003         {
1004                 struct video1394_wait v;
1005                 struct video1394_queue_variable qv;
1006                 struct dma_iso_ctx *d;
1007
1008                 qv.packet_sizes = NULL;
1009
1010                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
1011                         return -EFAULT;
1012
1013                 d = find_ctx(&ctx->context_list, OHCI_ISO_TRANSMIT, v.channel);
1014                 if (d == NULL) return -EFAULT;
1015
1016                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1017                         PRINT(KERN_ERR, ohci->host->id,
1018                               "Buffer %d out of range",v.buffer);
1019                         return -EFAULT;
1020                 }
1021
1022                 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1023                         unsigned int *psizes;
1024                         int buf_size = d->nb_cmd * sizeof(unsigned int);
1025
1026                         if (copy_from_user(&qv, (void *)arg, sizeof(qv)))
1027                                 return -EFAULT;
1028
1029                         psizes = kmalloc(buf_size, GFP_KERNEL);
1030                         if (!psizes)
1031                                 return -ENOMEM;
1032
1033                         if (copy_from_user(psizes, qv.packet_sizes, buf_size)) {
1034                                 kfree(psizes);
1035                                 return -EFAULT;
1036                         }
1037
1038                         qv.packet_sizes = psizes;
1039                 }
1040
1041                 spin_lock_irqsave(&d->lock,flags);
1042
1043                 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
1044                         PRINT(KERN_ERR, ohci->host->id,
1045                               "Buffer %d is already used",v.buffer);
1046                         spin_unlock_irqrestore(&d->lock,flags);
1047                         if (qv.packet_sizes)
1048                                 kfree(qv.packet_sizes);
1049                         return -EFAULT;
1050                 }
1051
1052                 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1053                         initialize_dma_it_prg_var_packet_queue(
1054                                 d, v.buffer, qv.packet_sizes,
1055                                 ohci);
1056                 }
1057
1058                 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
1059
1060                 if (d->last_buffer >= 0) {
1061                         d->it_prg[d->last_buffer]
1062                                 [ d->last_used_cmd[d->last_buffer] ].end.branchAddress =
1063                                         cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer],
1064                                                 0) & 0xfffffff0) | 0x3);
1065
1066                         d->it_prg[d->last_buffer]
1067                                 [ d->last_used_cmd[d->last_buffer] ].begin.branchAddress =
1068                                         cpu_to_le32((dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer],
1069                                                 0) & 0xfffffff0) | 0x3);
1070                         d->next_buffer[d->last_buffer] = v.buffer;
1071                 }
1072                 d->last_buffer = v.buffer;
1073                 d->next_buffer[d->last_buffer] = -1;
1074
1075                 d->it_prg[d->last_buffer][d->last_used_cmd[d->last_buffer]].end.branchAddress = 0;
1076
1077                 spin_unlock_irqrestore(&d->lock,flags);
1078
1079                 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
1080                 {
1081                         DBGMSG(ohci->host->id, "Starting iso transmit DMA ctx=%d",
1082                                d->ctx);
1083                         put_timestamp(ohci, d, d->last_buffer);
1084
1085                         /* Tell the controller where the first program is */
1086                         reg_write(ohci, d->cmdPtr,
1087                                 dma_prog_region_offset_to_bus(&d->prg_reg[v.buffer], 0) | 0x3);
1088
1089                         /* Run IT context */
1090                         reg_write(ohci, d->ctrlSet, 0x8000);
1091                 }
1092                 else {
1093                         /* Wake up dma context if necessary */
1094                         if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
1095                                 PRINT(KERN_INFO, ohci->host->id,
1096                                       "Waking up iso transmit dma ctx=%d",
1097                                       d->ctx);
1098                                 put_timestamp(ohci, d, d->last_buffer);
1099                                 reg_write(ohci, d->ctrlSet, 0x1000);
1100                         }
1101                 }
1102
1103                 if (qv.packet_sizes)
1104                         kfree(qv.packet_sizes);
1105
1106                 return 0;
1107
1108         }
1109         case VIDEO1394_IOC_TALK_WAIT_BUFFER:
1110         {
1111                 struct video1394_wait v;
1112                 struct dma_iso_ctx *d;
1113
1114                 if (copy_from_user(&v, (void *)arg, sizeof(v)))
1115                         return -EFAULT;
1116
1117                 d = find_ctx(&ctx->context_list, OHCI_ISO_TRANSMIT, v.channel);
1118                 if (d == NULL) return -EFAULT;
1119
1120                 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1121                         PRINT(KERN_ERR, ohci->host->id,
1122                               "Buffer %d out of range",v.buffer);
1123                         return -EFAULT;
1124                 }
1125
1126                 switch(d->buffer_status[v.buffer]) {
1127                 case VIDEO1394_BUFFER_READY:
1128                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1129                         return 0;
1130                 case VIDEO1394_BUFFER_QUEUED:
1131 #if 1
1132                         while (d->buffer_status[v.buffer]!=
1133                               VIDEO1394_BUFFER_READY) {
1134                                 interruptible_sleep_on(&d->waitq);
1135                                 if (signal_pending(current)) return -EINTR;
1136                         }
1137 #else
1138                         if (wait_event_interruptible(d->waitq,
1139                                                      d->buffer_status[v.buffer]
1140                                                      == VIDEO1394_BUFFER_READY)
1141                             == -ERESTARTSYS)
1142                                 return -EINTR;
1143 #endif
1144                         d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1145                         return 0;
1146                 default:
1147                         PRINT(KERN_ERR, ohci->host->id,
1148                               "Buffer %d is not queued",v.buffer);
1149                         return -EFAULT;
1150                 }
1151         }
1152         default:
1153                 return -EINVAL;
1154         }
1155 }
1156
1157 /*
1158  *      This maps the vmalloced and reserved buffer to user space.
1159  *
1160  *  FIXME:
1161  *  - PAGE_READONLY should suffice!?
1162  *  - remap_page_range is kind of inefficient for page by page remapping.
1163  *    But e.g. pte_alloc() does not work in modules ... :-(
1164  */
1165
1166 int video1394_mmap(struct file *file, struct vm_area_struct *vma)
1167 {
1168         struct file_ctx *ctx = (struct file_ctx *)file->private_data;
1169         int res = -EINVAL;
1170
1171         lock_kernel();
1172         if (ctx->current_ctx == NULL) {
1173                 PRINT(KERN_ERR, ctx->ohci->host->id, "Current iso context not set");
1174         } else
1175                 res = dma_region_mmap(&ctx->current_ctx->dma, file, vma);
1176         unlock_kernel();
1177
1178         return res;
1179 }
1180
1181 static int video1394_open(struct inode *inode, struct file *file)
1182 {
1183         int i = ieee1394_file_to_instance(file);
1184         struct ti_ohci *ohci;
1185         struct file_ctx *ctx;
1186
1187         ohci = hpsb_get_hostinfo_bykey(&video1394_highlevel, i);
1188         if (ohci == NULL)
1189                 return -EIO;
1190
1191         ctx = kmalloc(sizeof(struct file_ctx), GFP_KERNEL);
1192         if (ctx == NULL)  {
1193                 PRINT(KERN_ERR, ohci->host->id, "Cannot malloc file_ctx");
1194                 return -ENOMEM;
1195         }
1196
1197         memset(ctx, 0, sizeof(struct file_ctx));
1198         ctx->ohci = ohci;
1199         INIT_LIST_HEAD(&ctx->context_list);
1200         ctx->current_ctx = NULL;
1201         file->private_data = ctx;
1202
1203         return 0;
1204 }
1205
1206 static int video1394_release(struct inode *inode, struct file *file)
1207 {
1208         struct file_ctx *ctx = (struct file_ctx *)file->private_data;
1209         struct ti_ohci *ohci = ctx->ohci;
1210         struct list_head *lh, *next;
1211         u64 mask;
1212
1213         lock_kernel();
1214         list_for_each_safe(lh, next, &ctx->context_list) {
1215                 struct dma_iso_ctx *d;
1216                 d = list_entry(lh, struct dma_iso_ctx, link);
1217                 mask = (u64) 1 << d->channel;
1218
1219                 if (!(ohci->ISO_channel_usage & mask))
1220                         PRINT(KERN_ERR, ohci->host->id, "On release: Channel %d "
1221                               "is not being used", d->channel);
1222                 else
1223                         ohci->ISO_channel_usage &= ~mask;
1224                 PRINT(KERN_INFO, ohci->host->id, "On release: Iso %s context "
1225                       "%d stop listening on channel %d",
1226                       d->type == OHCI_ISO_RECEIVE ? "receive" : "transmit",
1227                       d->ctx, d->channel);
1228                 free_dma_iso_ctx(d);
1229         }
1230
1231         kfree(ctx);
1232         file->private_data = NULL;
1233
1234         unlock_kernel();
1235         return 0;
1236 }
1237
1238 static struct cdev video1394_cdev;
1239 static struct file_operations video1394_fops=
1240 {
1241         .owner =        THIS_MODULE,
1242         .ioctl =        video1394_ioctl,
1243         .mmap =         video1394_mmap,
1244         .open =         video1394_open,
1245         .release =      video1394_release
1246 };
1247
1248 /*** HOTPLUG STUFF **********************************************************/
1249 /*
1250  * Export information about protocols/devices supported by this driver.
1251  */
1252 static struct ieee1394_device_id video1394_id_table[] = {
1253         {
1254                 .match_flags    = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
1255                 .specifier_id   = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
1256                 .version        = CAMERA_SW_VERSION_ENTRY & 0xffffff
1257         },
1258         { }
1259 };
1260
1261 MODULE_DEVICE_TABLE(ieee1394, video1394_id_table);
1262
1263 static struct hpsb_protocol_driver video1394_driver = {
1264         .name           = "1394 Digital Camera Driver",
1265         .id_table       = video1394_id_table,
1266         .driver         = {
1267                 .name   = VIDEO1394_DRIVER_NAME,
1268                 .bus    = &ieee1394_bus_type,
1269         },
1270 };
1271
1272
1273 static void video1394_add_host (struct hpsb_host *host)
1274 {
1275         struct ti_ohci *ohci;
1276         int minor;
1277
1278         /* We only work with the OHCI-1394 driver */
1279         if (strcmp(host->driver->name, OHCI1394_DRIVER_NAME))
1280                 return;
1281
1282         ohci = (struct ti_ohci *)host->hostdata;
1283
1284         if (!hpsb_create_hostinfo(&video1394_highlevel, host, 0)) {
1285                 PRINT(KERN_ERR, ohci->host->id, "Cannot allocate hostinfo");
1286                 return;
1287         }
1288
1289         hpsb_set_hostinfo(&video1394_highlevel, host, ohci);
1290         hpsb_set_hostinfo_key(&video1394_highlevel, host, ohci->host->id);
1291
1292         minor = IEEE1394_MINOR_BLOCK_VIDEO1394 * 16 + ohci->host->id;
1293         devfs_mk_cdev(MKDEV(IEEE1394_MAJOR, minor),
1294                        S_IFCHR | S_IRUSR | S_IWUSR,
1295                        "%s/%d", VIDEO1394_DRIVER_NAME, ohci->host->id);
1296 }
1297
1298
1299 static void video1394_remove_host (struct hpsb_host *host)
1300 {
1301         struct ti_ohci *ohci = hpsb_get_hostinfo(&video1394_highlevel, host);
1302
1303         if (ohci)
1304                 devfs_remove("%s/%d", VIDEO1394_DRIVER_NAME, ohci->host->id);
1305
1306         return;
1307 }
1308
1309
1310 static struct hpsb_highlevel video1394_highlevel = {
1311         .name =         VIDEO1394_DRIVER_NAME,
1312         .add_host =     video1394_add_host,
1313         .remove_host =  video1394_remove_host,
1314 };
1315
1316 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1317 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1318 MODULE_SUPPORTED_DEVICE(VIDEO1394_DRIVER_NAME);
1319 MODULE_LICENSE("GPL");
1320
1321 #ifdef CONFIG_COMPAT
1322
1323 #define VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER     \
1324         _IOW ('#', 0x12, struct video1394_wait32)
1325 #define VIDEO1394_IOC32_LISTEN_WAIT_BUFFER      \
1326         _IOWR('#', 0x13, struct video1394_wait32)
1327 #define VIDEO1394_IOC32_TALK_WAIT_BUFFER        \
1328         _IOW ('#', 0x17, struct video1394_wait32)
1329 #define VIDEO1394_IOC32_LISTEN_POLL_BUFFER      \
1330         _IOWR('#', 0x18, struct video1394_wait32)
1331
1332 struct video1394_wait32 {
1333         u32 channel;
1334         u32 buffer;
1335         struct compat_timeval filltime;
1336 };
1337
1338 static int video1394_wr_wait32(unsigned int fd, unsigned int cmd, unsigned long arg,
1339                                struct file *file)
1340 {
1341         struct video1394_wait32 wait32;
1342         struct video1394_wait wait;
1343         mm_segment_t old_fs;
1344         int ret;
1345
1346         if (file->f_op->ioctl != video1394_ioctl)
1347                 return -EFAULT;
1348
1349         if (copy_from_user(&wait32, (void *)arg, sizeof(wait32)))
1350                 return -EFAULT;
1351
1352         wait.channel = wait32.channel;
1353         wait.buffer = wait32.buffer;
1354         wait.filltime.tv_sec = (time_t)wait32.filltime.tv_sec;
1355         wait.filltime.tv_usec = (suseconds_t)wait32.filltime.tv_usec;
1356
1357         old_fs = get_fs();
1358         set_fs(KERNEL_DS);
1359         if (cmd == VIDEO1394_IOC32_LISTEN_WAIT_BUFFER)
1360                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1361                                       VIDEO1394_IOC_LISTEN_WAIT_BUFFER,
1362                                       (unsigned long) &wait);
1363         else
1364                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1365                                       VIDEO1394_IOC_LISTEN_POLL_BUFFER,
1366                                       (unsigned long) &wait);
1367         set_fs(old_fs);
1368
1369         if (!ret) {
1370                 wait32.channel = wait.channel;
1371                 wait32.buffer = wait.buffer;
1372                 wait32.filltime.tv_sec = (int)wait.filltime.tv_sec;
1373                 wait32.filltime.tv_usec = (int)wait.filltime.tv_usec;
1374
1375                 if (copy_to_user((struct video1394_wait32 *)arg, &wait32, sizeof(wait32)))
1376                         ret = -EFAULT;
1377         }
1378
1379         return ret;
1380 }
1381
1382 static int video1394_w_wait32(unsigned int fd, unsigned int cmd, unsigned long arg,
1383                               struct file *file)
1384 {
1385         struct video1394_wait32 wait32;
1386         struct video1394_wait wait;
1387         mm_segment_t old_fs;
1388         int ret;
1389
1390         if (file->f_op->ioctl != video1394_ioctl)
1391                 return -EFAULT;
1392
1393         if (copy_from_user(&wait32, (void *)arg, sizeof(wait32)))
1394                 return -EFAULT;
1395
1396         wait.channel = wait32.channel;
1397         wait.buffer = wait32.buffer;
1398         wait.filltime.tv_sec = (time_t)wait32.filltime.tv_sec;
1399         wait.filltime.tv_usec = (suseconds_t)wait32.filltime.tv_usec;
1400
1401         old_fs = get_fs();
1402         set_fs(KERNEL_DS);
1403         if (cmd == VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER)
1404                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1405                                       VIDEO1394_IOC_LISTEN_QUEUE_BUFFER,
1406                                       (unsigned long) &wait);
1407         else
1408                 ret = video1394_ioctl(file->f_dentry->d_inode, file,
1409                                       VIDEO1394_IOC_TALK_WAIT_BUFFER,
1410                                       (unsigned long) &wait);
1411         set_fs(old_fs);
1412
1413         return ret;
1414 }
1415
1416 static int video1394_queue_buf32(unsigned int fd, unsigned int cmd, unsigned long arg,
1417                                  struct file *file)
1418 {
1419         if (file->f_op->ioctl != video1394_ioctl)
1420                 return -EFAULT;
1421
1422         return -EFAULT;
1423
1424         return video1394_ioctl(file->f_dentry->d_inode, file,
1425                                 VIDEO1394_IOC_TALK_QUEUE_BUFFER, arg);
1426 }
1427
1428 #endif /* CONFIG_COMPAT */
1429
1430 static void __exit video1394_exit_module (void)
1431 {
1432 #ifdef CONFIG_COMPAT
1433         int ret;
1434
1435         ret = unregister_ioctl32_conversion(VIDEO1394_IOC_LISTEN_CHANNEL);
1436         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_UNLISTEN_CHANNEL);
1437         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_TALK_CHANNEL);
1438         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_UNTALK_CHANNEL);
1439         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER);
1440         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_WAIT_BUFFER);
1441         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC_TALK_QUEUE_BUFFER);
1442         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_TALK_WAIT_BUFFER);
1443         ret |= unregister_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_POLL_BUFFER);
1444         if (ret)
1445                 PRINT_G(KERN_CRIT, "Error unregistering ioctl32 translations");
1446 #endif
1447
1448         hpsb_unregister_protocol(&video1394_driver);
1449
1450         hpsb_unregister_highlevel(&video1394_highlevel);
1451
1452         devfs_remove(VIDEO1394_DRIVER_NAME);
1453         cdev_del(&video1394_cdev);
1454
1455         PRINT_G(KERN_INFO, "Removed " VIDEO1394_DRIVER_NAME " module");
1456 }
1457
1458 static int __init video1394_init_module (void)
1459 {
1460         int ret;
1461
1462         cdev_init(&video1394_cdev, &video1394_fops);
1463         video1394_cdev.owner = THIS_MODULE;
1464         kobject_set_name(&video1394_cdev.kobj, VIDEO1394_DRIVER_NAME);
1465         ret = cdev_add(&video1394_cdev, IEEE1394_VIDEO1394_DEV, 16);
1466         if (ret) {
1467                 PRINT_G(KERN_ERR, "video1394: unable to get minor device block");
1468                 return ret;
1469         }
1470
1471         devfs_mk_dir(VIDEO1394_DRIVER_NAME);
1472
1473         hpsb_register_highlevel(&video1394_highlevel);
1474
1475         ret = hpsb_register_protocol(&video1394_driver);
1476         if (ret) {
1477                 PRINT_G(KERN_ERR, "video1394: failed to register protocol");
1478                 hpsb_unregister_highlevel(&video1394_highlevel);
1479                 devfs_remove(VIDEO1394_DRIVER_NAME);
1480                 cdev_del(&video1394_cdev);
1481                 return ret;
1482         }
1483
1484 #ifdef CONFIG_COMPAT
1485         {
1486                 /* First the compatible ones */
1487                 ret = register_ioctl32_conversion(VIDEO1394_IOC_LISTEN_CHANNEL, NULL);
1488                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_UNLISTEN_CHANNEL, NULL);
1489                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_TALK_CHANNEL, NULL);
1490                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_UNTALK_CHANNEL, NULL);
1491
1492                 /* These need translation */
1493                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_QUEUE_BUFFER,
1494                                             video1394_w_wait32);
1495                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_WAIT_BUFFER,
1496                                             video1394_wr_wait32);
1497                 ret |= register_ioctl32_conversion(VIDEO1394_IOC_TALK_QUEUE_BUFFER,
1498                                             video1394_queue_buf32);
1499                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_TALK_WAIT_BUFFER,
1500                                             video1394_w_wait32);
1501                 ret |= register_ioctl32_conversion(VIDEO1394_IOC32_LISTEN_POLL_BUFFER,
1502                                             video1394_wr_wait32);
1503                 if (ret)
1504                         PRINT_G(KERN_INFO, "Error registering ioctl32 translations");
1505         }
1506 #endif
1507
1508         PRINT_G(KERN_INFO, "Installed " VIDEO1394_DRIVER_NAME " module");
1509         return 0;
1510 }
1511
1512
1513 module_init(video1394_init_module);
1514 module_exit(video1394_exit_module);
1515 MODULE_ALIAS_CHARDEV(IEEE1394_MAJOR, IEEE1394_MINOR_BLOCK_VIDEO1394 * 16);