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