video1394.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:38k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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