video1394.c 41 KB

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