amdtp.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * amdtp.c - Audio and Music Data Transmission Protocol Driver
  4. * Copyright (C) 2001 Kristian Høgsberg
  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. /* OVERVIEW
  21. * --------
  22. *
  23. * The AMDTP driver is designed to expose the IEEE1394 bus as a
  24. * regular OSS soundcard, i.e. you can link /dev/dsp to /dev/amdtp and
  25. * then your favourite MP3 player, game or whatever sound program will
  26. * output to an IEEE1394 isochronous channel. The signal destination
  27. * could be a set of IEEE1394 loudspeakers (if and when such things
  28. * become available) or an amplifier with IEEE1394 input (like the
  29. * Sony STR-LSA1). The driver only handles the actual streaming, some
  30. * connection management is also required for this to actually work.
  31. * That is outside the scope of this driver, and furthermore it is not
  32. * really standardized yet.
  33. *
  34. * The Audio and Music Data Tranmission Protocol is available at
  35. *
  36. * http://www.1394ta.org/Download/Technology/Specifications/2001/AM20Final-jf2.pdf
  37. *
  38. *
  39. * TODO
  40. * ----
  41. *
  42. * - We should be able to change input sample format between LE/BE, as
  43. * we already shift the bytes around when we construct the iso
  44. * packets.
  45. *
  46. * - Fix DMA stop after bus reset!
  47. *
  48. * - Clean up iso context handling in ohci1394.
  49. *
  50. *
  51. * MAYBE TODO
  52. * ----------
  53. *
  54. * - Receive data for local playback or recording. Playback requires
  55. * soft syncing with the sound card.
  56. *
  57. * - Signal processing, i.e. receive packets, do some processing, and
  58. * transmit them again using the same packet structure and timestamps
  59. * offset by processing time.
  60. *
  61. * - Maybe make an ALSA interface, that is, create a file_ops
  62. * implementation that recognizes ALSA ioctls and uses defaults for
  63. * things that can't be controlled through ALSA (iso channel).
  64. *
  65. * Changes:
  66. *
  67. * - Audit copy_from_user in amdtp_write.
  68. * Daniele Bellucci <bellucda@tiscali.it>
  69. *
  70. */
  71. #include <linux/module.h>
  72. #include <linux/list.h>
  73. #include <linux/sched.h>
  74. #include <linux/types.h>
  75. #include <linux/fs.h>
  76. #include <linux/ioctl.h>
  77. #include <linux/wait.h>
  78. #include <linux/pci.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/poll.h>
  81. #include <linux/compat.h>
  82. #include <linux/cdev.h>
  83. #include <asm/uaccess.h>
  84. #include <asm/atomic.h>
  85. #include "hosts.h"
  86. #include "highlevel.h"
  87. #include "ieee1394.h"
  88. #include "ieee1394_core.h"
  89. #include "ohci1394.h"
  90. #include "amdtp.h"
  91. #include "cmp.h"
  92. #define FMT_AMDTP 0x10
  93. #define FDF_AM824 0x00
  94. #define FDF_SFC_32KHZ 0x00
  95. #define FDF_SFC_44K1HZ 0x01
  96. #define FDF_SFC_48KHZ 0x02
  97. #define FDF_SFC_88K2HZ 0x03
  98. #define FDF_SFC_96KHZ 0x04
  99. #define FDF_SFC_176K4HZ 0x05
  100. #define FDF_SFC_192KHZ 0x06
  101. struct descriptor_block {
  102. struct output_more_immediate {
  103. u32 control;
  104. u32 pad0;
  105. u32 skip;
  106. u32 pad1;
  107. u32 header[4];
  108. } header_desc;
  109. struct output_last {
  110. u32 control;
  111. u32 data_address;
  112. u32 branch;
  113. u32 status;
  114. } payload_desc;
  115. };
  116. struct packet {
  117. struct descriptor_block *db;
  118. dma_addr_t db_bus;
  119. struct iso_packet *payload;
  120. dma_addr_t payload_bus;
  121. };
  122. #include <asm/byteorder.h>
  123. #if defined __BIG_ENDIAN_BITFIELD
  124. struct iso_packet {
  125. /* First quadlet */
  126. unsigned int dbs : 8;
  127. unsigned int eoh0 : 2;
  128. unsigned int sid : 6;
  129. unsigned int dbc : 8;
  130. unsigned int fn : 2;
  131. unsigned int qpc : 3;
  132. unsigned int sph : 1;
  133. unsigned int reserved : 2;
  134. /* Second quadlet */
  135. unsigned int fdf : 8;
  136. unsigned int eoh1 : 2;
  137. unsigned int fmt : 6;
  138. unsigned int syt : 16;
  139. quadlet_t data[0];
  140. };
  141. #elif defined __LITTLE_ENDIAN_BITFIELD
  142. struct iso_packet {
  143. /* First quadlet */
  144. unsigned int sid : 6;
  145. unsigned int eoh0 : 2;
  146. unsigned int dbs : 8;
  147. unsigned int reserved : 2;
  148. unsigned int sph : 1;
  149. unsigned int qpc : 3;
  150. unsigned int fn : 2;
  151. unsigned int dbc : 8;
  152. /* Second quadlet */
  153. unsigned int fmt : 6;
  154. unsigned int eoh1 : 2;
  155. unsigned int fdf : 8;
  156. unsigned int syt : 16;
  157. quadlet_t data[0];
  158. };
  159. #else
  160. #error Unknown bitfield type
  161. #endif
  162. struct fraction {
  163. int integer;
  164. int numerator;
  165. int denominator;
  166. };
  167. #define PACKET_LIST_SIZE 256
  168. #define MAX_PACKET_LISTS 4
  169. struct packet_list {
  170. struct list_head link;
  171. int last_cycle_count;
  172. struct packet packets[PACKET_LIST_SIZE];
  173. };
  174. #define BUFFER_SIZE 128
  175. /* This implements a circular buffer for incoming samples. */
  176. struct buffer {
  177. size_t head, tail, length, size;
  178. unsigned char data[0];
  179. };
  180. struct stream {
  181. int iso_channel;
  182. int format;
  183. int rate;
  184. int dimension;
  185. int fdf;
  186. int mode;
  187. int sample_format;
  188. struct cmp_pcr *opcr;
  189. /* Input samples are copied here. */
  190. struct buffer *input;
  191. /* ISO Packer state */
  192. unsigned char dbc;
  193. struct packet_list *current_packet_list;
  194. int current_packet;
  195. struct fraction ready_samples, samples_per_cycle;
  196. /* We use these to generate control bits when we are packing
  197. * iec958 data.
  198. */
  199. int iec958_frame_count;
  200. int iec958_rate_code;
  201. /* The cycle_count and cycle_offset fields are used for the
  202. * synchronization timestamps (syt) in the cip header. They
  203. * are incremented by at least a cycle every time we put a
  204. * time stamp in a packet. As we don't time stamp all
  205. * packages, cycle_count isn't updated in every cycle, and
  206. * sometimes it's incremented by 2. Thus, we have
  207. * cycle_count2, which is simply incremented by one with each
  208. * packet, so we can compare it to the transmission time
  209. * written back in the dma programs.
  210. */
  211. atomic_t cycle_count, cycle_count2;
  212. struct fraction cycle_offset, ticks_per_syt_offset;
  213. int syt_interval;
  214. int stale_count;
  215. /* Theses fields control the sample output to the DMA engine.
  216. * The dma_packet_lists list holds packet lists currently
  217. * queued for dma; the head of the list is currently being
  218. * processed. The last program in a packet list generates an
  219. * interrupt, which removes the head from dma_packet_lists and
  220. * puts it back on the free list.
  221. */
  222. struct list_head dma_packet_lists;
  223. struct list_head free_packet_lists;
  224. wait_queue_head_t packet_list_wait;
  225. spinlock_t packet_list_lock;
  226. struct ohci1394_iso_tasklet iso_tasklet;
  227. struct pci_pool *descriptor_pool, *packet_pool;
  228. /* Streams at a host controller are chained through this field. */
  229. struct list_head link;
  230. struct amdtp_host *host;
  231. };
  232. struct amdtp_host {
  233. struct hpsb_host *host;
  234. struct ti_ohci *ohci;
  235. struct list_head stream_list;
  236. spinlock_t stream_list_lock;
  237. };
  238. static struct hpsb_highlevel amdtp_highlevel;
  239. /* FIXME: This doesn't belong here... */
  240. #define OHCI1394_CONTEXT_CYCLE_MATCH 0x80000000
  241. #define OHCI1394_CONTEXT_RUN 0x00008000
  242. #define OHCI1394_CONTEXT_WAKE 0x00001000
  243. #define OHCI1394_CONTEXT_DEAD 0x00000800
  244. #define OHCI1394_CONTEXT_ACTIVE 0x00000400
  245. static void ohci1394_start_it_ctx(struct ti_ohci *ohci, int ctx,
  246. dma_addr_t first_cmd, int z, int cycle_match)
  247. {
  248. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << ctx);
  249. reg_write(ohci, OHCI1394_IsoXmitCommandPtr + ctx * 16, first_cmd | z);
  250. reg_write(ohci, OHCI1394_IsoXmitContextControlClear + ctx * 16, ~0);
  251. wmb();
  252. reg_write(ohci, OHCI1394_IsoXmitContextControlSet + ctx * 16,
  253. OHCI1394_CONTEXT_CYCLE_MATCH | (cycle_match << 16) |
  254. OHCI1394_CONTEXT_RUN);
  255. }
  256. static void ohci1394_wake_it_ctx(struct ti_ohci *ohci, int ctx)
  257. {
  258. reg_write(ohci, OHCI1394_IsoXmitContextControlSet + ctx * 16,
  259. OHCI1394_CONTEXT_WAKE);
  260. }
  261. static void ohci1394_stop_it_ctx(struct ti_ohci *ohci, int ctx, int synchronous)
  262. {
  263. u32 control;
  264. int wait;
  265. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << ctx);
  266. reg_write(ohci, OHCI1394_IsoXmitContextControlClear + ctx * 16,
  267. OHCI1394_CONTEXT_RUN);
  268. wmb();
  269. if (synchronous) {
  270. for (wait = 0; wait < 5; wait++) {
  271. control = reg_read(ohci, OHCI1394_IsoXmitContextControlSet + ctx * 16);
  272. if ((control & OHCI1394_CONTEXT_ACTIVE) == 0)
  273. break;
  274. schedule_timeout_interruptible(1);
  275. }
  276. }
  277. }
  278. /* Note: we can test if free_packet_lists is empty without aquiring
  279. * the packet_list_lock. The interrupt handler only adds to the free
  280. * list, there is no race condition between testing the list non-empty
  281. * and acquiring the lock.
  282. */
  283. static struct packet_list *stream_get_free_packet_list(struct stream *s)
  284. {
  285. struct packet_list *pl;
  286. unsigned long flags;
  287. if (list_empty(&s->free_packet_lists))
  288. return NULL;
  289. spin_lock_irqsave(&s->packet_list_lock, flags);
  290. pl = list_entry(s->free_packet_lists.next, struct packet_list, link);
  291. list_del(&pl->link);
  292. spin_unlock_irqrestore(&s->packet_list_lock, flags);
  293. return pl;
  294. }
  295. static void stream_start_dma(struct stream *s, struct packet_list *pl)
  296. {
  297. u32 syt_cycle, cycle_count, start_cycle;
  298. cycle_count = reg_read(s->host->ohci,
  299. OHCI1394_IsochronousCycleTimer) >> 12;
  300. syt_cycle = (pl->last_cycle_count - PACKET_LIST_SIZE + 1) & 0x0f;
  301. /* We program the DMA controller to start transmission at
  302. * least 17 cycles from now - this happens when the lower four
  303. * bits of cycle_count is 0x0f and syt_cycle is 0, in this
  304. * case the start cycle is cycle_count - 15 + 32. */
  305. start_cycle = (cycle_count & ~0x0f) + 32 + syt_cycle;
  306. if ((start_cycle & 0x1fff) >= 8000)
  307. start_cycle = start_cycle - 8000 + 0x2000;
  308. ohci1394_start_it_ctx(s->host->ohci, s->iso_tasklet.context,
  309. pl->packets[0].db_bus, 3,
  310. start_cycle & 0x7fff);
  311. }
  312. static void stream_put_dma_packet_list(struct stream *s,
  313. struct packet_list *pl)
  314. {
  315. unsigned long flags;
  316. struct packet_list *prev;
  317. /* Remember the cycle_count used for timestamping the last packet. */
  318. pl->last_cycle_count = atomic_read(&s->cycle_count2) - 1;
  319. pl->packets[PACKET_LIST_SIZE - 1].db->payload_desc.branch = 0;
  320. spin_lock_irqsave(&s->packet_list_lock, flags);
  321. list_add_tail(&pl->link, &s->dma_packet_lists);
  322. spin_unlock_irqrestore(&s->packet_list_lock, flags);
  323. prev = list_entry(pl->link.prev, struct packet_list, link);
  324. if (pl->link.prev != &s->dma_packet_lists) {
  325. struct packet *last = &prev->packets[PACKET_LIST_SIZE - 1];
  326. last->db->payload_desc.branch = pl->packets[0].db_bus | 3;
  327. last->db->header_desc.skip = pl->packets[0].db_bus | 3;
  328. ohci1394_wake_it_ctx(s->host->ohci, s->iso_tasklet.context);
  329. }
  330. else
  331. stream_start_dma(s, pl);
  332. }
  333. static void stream_shift_packet_lists(unsigned long l)
  334. {
  335. struct stream *s = (struct stream *) l;
  336. struct packet_list *pl;
  337. struct packet *last;
  338. int diff;
  339. if (list_empty(&s->dma_packet_lists)) {
  340. HPSB_ERR("empty dma_packet_lists in %s", __FUNCTION__);
  341. return;
  342. }
  343. /* Now that we know the list is non-empty, we can get the head
  344. * of the list without locking, because the process context
  345. * only adds to the tail.
  346. */
  347. pl = list_entry(s->dma_packet_lists.next, struct packet_list, link);
  348. last = &pl->packets[PACKET_LIST_SIZE - 1];
  349. /* This is weird... if we stop dma processing in the middle of
  350. * a packet list, the dma context immediately generates an
  351. * interrupt if we enable it again later. This only happens
  352. * when amdtp_release is interrupted while waiting for dma to
  353. * complete, though. Anyway, we detect this by seeing that
  354. * the status of the dma descriptor that we expected an
  355. * interrupt from is still 0.
  356. */
  357. if (last->db->payload_desc.status == 0) {
  358. HPSB_INFO("weird interrupt...");
  359. return;
  360. }
  361. /* If the last descriptor block does not specify a branch
  362. * address, we have a sample underflow.
  363. */
  364. if (last->db->payload_desc.branch == 0)
  365. HPSB_INFO("FIXME: sample underflow...");
  366. /* Here we check when (which cycle) the last packet was sent
  367. * and compare it to what the iso packer was using at the
  368. * time. If there is a mismatch, we adjust the cycle count in
  369. * the iso packer. However, there are still up to
  370. * MAX_PACKET_LISTS packet lists queued with bad time stamps,
  371. * so we disable time stamp monitoring for the next
  372. * MAX_PACKET_LISTS packet lists.
  373. */
  374. diff = (last->db->payload_desc.status - pl->last_cycle_count) & 0xf;
  375. if (diff > 0 && s->stale_count == 0) {
  376. atomic_add(diff, &s->cycle_count);
  377. atomic_add(diff, &s->cycle_count2);
  378. s->stale_count = MAX_PACKET_LISTS;
  379. }
  380. if (s->stale_count > 0)
  381. s->stale_count--;
  382. /* Finally, we move the packet list that was just processed
  383. * back to the free list, and notify any waiters.
  384. */
  385. spin_lock(&s->packet_list_lock);
  386. list_del(&pl->link);
  387. list_add_tail(&pl->link, &s->free_packet_lists);
  388. spin_unlock(&s->packet_list_lock);
  389. wake_up_interruptible(&s->packet_list_wait);
  390. }
  391. static struct packet *stream_current_packet(struct stream *s)
  392. {
  393. if (s->current_packet_list == NULL &&
  394. (s->current_packet_list = stream_get_free_packet_list(s)) == NULL)
  395. return NULL;
  396. return &s->current_packet_list->packets[s->current_packet];
  397. }
  398. static void stream_queue_packet(struct stream *s)
  399. {
  400. s->current_packet++;
  401. if (s->current_packet == PACKET_LIST_SIZE) {
  402. stream_put_dma_packet_list(s, s->current_packet_list);
  403. s->current_packet_list = NULL;
  404. s->current_packet = 0;
  405. }
  406. }
  407. /* Integer fractional math. When we transmit a 44k1Hz signal we must
  408. * send 5 41/80 samples per isochronous cycle, as these occur 8000
  409. * times a second. Of course, we must send an integral number of
  410. * samples in a packet, so we use the integer math to alternate
  411. * between sending 5 and 6 samples per packet.
  412. */
  413. static void fraction_init(struct fraction *f, int numerator, int denominator)
  414. {
  415. f->integer = numerator / denominator;
  416. f->numerator = numerator % denominator;
  417. f->denominator = denominator;
  418. }
  419. static __inline__ void fraction_add(struct fraction *dst,
  420. struct fraction *src1,
  421. struct fraction *src2)
  422. {
  423. /* assert: src1->denominator == src2->denominator */
  424. int sum, denom;
  425. /* We use these two local variables to allow gcc to optimize
  426. * the division and the modulo into only one division. */
  427. sum = src1->numerator + src2->numerator;
  428. denom = src1->denominator;
  429. dst->integer = src1->integer + src2->integer + sum / denom;
  430. dst->numerator = sum % denom;
  431. dst->denominator = denom;
  432. }
  433. static __inline__ void fraction_sub_int(struct fraction *dst,
  434. struct fraction *src, int integer)
  435. {
  436. dst->integer = src->integer - integer;
  437. dst->numerator = src->numerator;
  438. dst->denominator = src->denominator;
  439. }
  440. static __inline__ int fraction_floor(struct fraction *frac)
  441. {
  442. return frac->integer;
  443. }
  444. static __inline__ int fraction_ceil(struct fraction *frac)
  445. {
  446. return frac->integer + (frac->numerator > 0 ? 1 : 0);
  447. }
  448. static void packet_initialize(struct packet *p, struct packet *next)
  449. {
  450. /* Here we initialize the dma descriptor block for
  451. * transferring one iso packet. We use two descriptors per
  452. * packet: an OUTPUT_MORE_IMMMEDIATE descriptor for the
  453. * IEEE1394 iso packet header and an OUTPUT_LAST descriptor
  454. * for the payload.
  455. */
  456. p->db->header_desc.control =
  457. DMA_CTL_OUTPUT_MORE | DMA_CTL_IMMEDIATE | 8;
  458. if (next) {
  459. p->db->payload_desc.control =
  460. DMA_CTL_OUTPUT_LAST | DMA_CTL_BRANCH;
  461. p->db->payload_desc.branch = next->db_bus | 3;
  462. p->db->header_desc.skip = next->db_bus | 3;
  463. }
  464. else {
  465. p->db->payload_desc.control =
  466. DMA_CTL_OUTPUT_LAST | DMA_CTL_BRANCH |
  467. DMA_CTL_UPDATE | DMA_CTL_IRQ;
  468. p->db->payload_desc.branch = 0;
  469. p->db->header_desc.skip = 0;
  470. }
  471. p->db->payload_desc.data_address = p->payload_bus;
  472. p->db->payload_desc.status = 0;
  473. }
  474. static struct packet_list *packet_list_alloc(struct stream *s)
  475. {
  476. int i;
  477. struct packet_list *pl;
  478. struct packet *next;
  479. pl = kmalloc(sizeof *pl, SLAB_KERNEL);
  480. if (pl == NULL)
  481. return NULL;
  482. for (i = 0; i < PACKET_LIST_SIZE; i++) {
  483. struct packet *p = &pl->packets[i];
  484. p->db = pci_pool_alloc(s->descriptor_pool, SLAB_KERNEL,
  485. &p->db_bus);
  486. p->payload = pci_pool_alloc(s->packet_pool, SLAB_KERNEL,
  487. &p->payload_bus);
  488. }
  489. for (i = 0; i < PACKET_LIST_SIZE; i++) {
  490. if (i < PACKET_LIST_SIZE - 1)
  491. next = &pl->packets[i + 1];
  492. else
  493. next = NULL;
  494. packet_initialize(&pl->packets[i], next);
  495. }
  496. return pl;
  497. }
  498. static void packet_list_free(struct packet_list *pl, struct stream *s)
  499. {
  500. int i;
  501. for (i = 0; i < PACKET_LIST_SIZE; i++) {
  502. struct packet *p = &pl->packets[i];
  503. pci_pool_free(s->descriptor_pool, p->db, p->db_bus);
  504. pci_pool_free(s->packet_pool, p->payload, p->payload_bus);
  505. }
  506. kfree(pl);
  507. }
  508. static struct buffer *buffer_alloc(int size)
  509. {
  510. struct buffer *b;
  511. b = kmalloc(sizeof *b + size, SLAB_KERNEL);
  512. if (b == NULL)
  513. return NULL;
  514. b->head = 0;
  515. b->tail = 0;
  516. b->length = 0;
  517. b->size = size;
  518. return b;
  519. }
  520. static unsigned char *buffer_get_bytes(struct buffer *buffer, int size)
  521. {
  522. unsigned char *p;
  523. if (buffer->head + size > buffer->size)
  524. BUG();
  525. p = &buffer->data[buffer->head];
  526. buffer->head += size;
  527. if (buffer->head == buffer->size)
  528. buffer->head = 0;
  529. buffer->length -= size;
  530. return p;
  531. }
  532. static unsigned char *buffer_put_bytes(struct buffer *buffer,
  533. size_t max, size_t *actual)
  534. {
  535. size_t length;
  536. unsigned char *p;
  537. p = &buffer->data[buffer->tail];
  538. length = min(buffer->size - buffer->length, max);
  539. if (buffer->tail + length < buffer->size) {
  540. *actual = length;
  541. buffer->tail += length;
  542. }
  543. else {
  544. *actual = buffer->size - buffer->tail;
  545. buffer->tail = 0;
  546. }
  547. buffer->length += *actual;
  548. return p;
  549. }
  550. static u32 get_iec958_header_bits(struct stream *s, int sub_frame, u32 sample)
  551. {
  552. int csi, parity, shift;
  553. int block_start;
  554. u32 bits;
  555. switch (s->iec958_frame_count) {
  556. case 1:
  557. csi = s->format == AMDTP_FORMAT_IEC958_AC3;
  558. break;
  559. case 2:
  560. case 9:
  561. csi = 1;
  562. break;
  563. case 24 ... 27:
  564. csi = (s->iec958_rate_code >> (27 - s->iec958_frame_count)) & 0x01;
  565. break;
  566. default:
  567. csi = 0;
  568. break;
  569. }
  570. block_start = (s->iec958_frame_count == 0 && sub_frame == 0);
  571. /* The parity bit is the xor of the sample bits and the
  572. * channel status info bit. */
  573. for (shift = 16, parity = sample ^ csi; shift > 0; shift >>= 1)
  574. parity ^= (parity >> shift);
  575. bits = (block_start << 5) | /* Block start bit */
  576. ((sub_frame == 0) << 4) | /* Subframe bit */
  577. ((parity & 1) << 3) | /* Parity bit */
  578. (csi << 2); /* Channel status info bit */
  579. return bits;
  580. }
  581. static u32 get_header_bits(struct stream *s, int sub_frame, u32 sample)
  582. {
  583. switch (s->format) {
  584. case AMDTP_FORMAT_IEC958_PCM:
  585. case AMDTP_FORMAT_IEC958_AC3:
  586. return get_iec958_header_bits(s, sub_frame, sample);
  587. case AMDTP_FORMAT_RAW:
  588. return 0x40;
  589. default:
  590. return 0;
  591. }
  592. }
  593. static void fill_payload_le16(struct stream *s, quadlet_t *data, int nevents)
  594. {
  595. quadlet_t *event, sample, bits;
  596. unsigned char *p;
  597. int i, j;
  598. for (i = 0, event = data; i < nevents; i++) {
  599. for (j = 0; j < s->dimension; j++) {
  600. p = buffer_get_bytes(s->input, 2);
  601. sample = (p[1] << 16) | (p[0] << 8);
  602. bits = get_header_bits(s, j, sample);
  603. event[j] = cpu_to_be32((bits << 24) | sample);
  604. }
  605. event += s->dimension;
  606. if (++s->iec958_frame_count == 192)
  607. s->iec958_frame_count = 0;
  608. }
  609. }
  610. static void fill_packet(struct stream *s, struct packet *packet, int nevents)
  611. {
  612. int syt_index, syt, size;
  613. u32 control;
  614. size = (nevents * s->dimension + 2) * sizeof(quadlet_t);
  615. /* Update DMA descriptors */
  616. packet->db->payload_desc.status = 0;
  617. control = packet->db->payload_desc.control & 0xffff0000;
  618. packet->db->payload_desc.control = control | size;
  619. /* Fill IEEE1394 headers */
  620. packet->db->header_desc.header[0] =
  621. (IEEE1394_SPEED_100 << 16) | (0x01 << 14) |
  622. (s->iso_channel << 8) | (TCODE_ISO_DATA << 4);
  623. packet->db->header_desc.header[1] = size << 16;
  624. /* Calculate synchronization timestamp (syt). First we
  625. * determine syt_index, that is, the index in the packet of
  626. * the sample for which the timestamp is valid. */
  627. syt_index = (s->syt_interval - s->dbc) & (s->syt_interval - 1);
  628. if (syt_index < nevents) {
  629. syt = ((atomic_read(&s->cycle_count) << 12) |
  630. s->cycle_offset.integer) & 0xffff;
  631. fraction_add(&s->cycle_offset,
  632. &s->cycle_offset, &s->ticks_per_syt_offset);
  633. /* This next addition should be modulo 8000 (0x1f40),
  634. * but we only use the lower 4 bits of cycle_count, so
  635. * we don't need the modulo. */
  636. atomic_add(s->cycle_offset.integer / 3072, &s->cycle_count);
  637. s->cycle_offset.integer %= 3072;
  638. }
  639. else
  640. syt = 0xffff;
  641. atomic_inc(&s->cycle_count2);
  642. /* Fill cip header */
  643. packet->payload->eoh0 = 0;
  644. packet->payload->sid = s->host->host->node_id & 0x3f;
  645. packet->payload->dbs = s->dimension;
  646. packet->payload->fn = 0;
  647. packet->payload->qpc = 0;
  648. packet->payload->sph = 0;
  649. packet->payload->reserved = 0;
  650. packet->payload->dbc = s->dbc;
  651. packet->payload->eoh1 = 2;
  652. packet->payload->fmt = FMT_AMDTP;
  653. packet->payload->fdf = s->fdf;
  654. packet->payload->syt = cpu_to_be16(syt);
  655. switch (s->sample_format) {
  656. case AMDTP_INPUT_LE16:
  657. fill_payload_le16(s, packet->payload->data, nevents);
  658. break;
  659. }
  660. s->dbc += nevents;
  661. }
  662. static void stream_flush(struct stream *s)
  663. {
  664. struct packet *p;
  665. int nevents;
  666. struct fraction next;
  667. /* The AMDTP specifies two transmission modes: blocking and
  668. * non-blocking. In blocking mode you always transfer
  669. * syt_interval or zero samples, whereas in non-blocking mode
  670. * you send as many samples as you have available at transfer
  671. * time.
  672. *
  673. * The fraction samples_per_cycle specifies the number of
  674. * samples that become available per cycle. We add this to
  675. * the fraction ready_samples, which specifies the number of
  676. * leftover samples from the previous transmission. The sum,
  677. * stored in the fraction next, specifies the number of
  678. * samples available for transmission, and from this we
  679. * determine the number of samples to actually transmit.
  680. */
  681. while (1) {
  682. fraction_add(&next, &s->ready_samples, &s->samples_per_cycle);
  683. if (s->mode == AMDTP_MODE_BLOCKING) {
  684. if (fraction_floor(&next) >= s->syt_interval)
  685. nevents = s->syt_interval;
  686. else
  687. nevents = 0;
  688. }
  689. else
  690. nevents = fraction_floor(&next);
  691. p = stream_current_packet(s);
  692. if (s->input->length < nevents * s->dimension * 2 || p == NULL)
  693. break;
  694. fill_packet(s, p, nevents);
  695. stream_queue_packet(s);
  696. /* Now that we have successfully queued the packet for
  697. * transmission, we update the fraction ready_samples. */
  698. fraction_sub_int(&s->ready_samples, &next, nevents);
  699. }
  700. }
  701. static int stream_alloc_packet_lists(struct stream *s)
  702. {
  703. int max_nevents, max_packet_size, i;
  704. if (s->mode == AMDTP_MODE_BLOCKING)
  705. max_nevents = s->syt_interval;
  706. else
  707. max_nevents = fraction_ceil(&s->samples_per_cycle);
  708. max_packet_size = max_nevents * s->dimension * 4 + 8;
  709. s->packet_pool = pci_pool_create("packet pool", s->host->ohci->dev,
  710. max_packet_size, 0, 0);
  711. if (s->packet_pool == NULL)
  712. return -1;
  713. INIT_LIST_HEAD(&s->free_packet_lists);
  714. INIT_LIST_HEAD(&s->dma_packet_lists);
  715. for (i = 0; i < MAX_PACKET_LISTS; i++) {
  716. struct packet_list *pl = packet_list_alloc(s);
  717. if (pl == NULL)
  718. break;
  719. list_add_tail(&pl->link, &s->free_packet_lists);
  720. }
  721. return i < MAX_PACKET_LISTS ? -1 : 0;
  722. }
  723. static void stream_free_packet_lists(struct stream *s)
  724. {
  725. struct packet_list *packet_l, *packet_l_next;
  726. if (s->current_packet_list != NULL)
  727. packet_list_free(s->current_packet_list, s);
  728. list_for_each_entry_safe(packet_l, packet_l_next, &s->dma_packet_lists, link)
  729. packet_list_free(packet_l, s);
  730. list_for_each_entry_safe(packet_l, packet_l_next, &s->free_packet_lists, link)
  731. packet_list_free(packet_l, s);
  732. if (s->packet_pool != NULL)
  733. pci_pool_destroy(s->packet_pool);
  734. s->current_packet_list = NULL;
  735. INIT_LIST_HEAD(&s->free_packet_lists);
  736. INIT_LIST_HEAD(&s->dma_packet_lists);
  737. s->packet_pool = NULL;
  738. }
  739. static void plug_update(struct cmp_pcr *plug, void *data)
  740. {
  741. struct stream *s = data;
  742. HPSB_INFO("plug update: p2p_count=%d, channel=%d",
  743. plug->p2p_count, plug->channel);
  744. s->iso_channel = plug->channel;
  745. if (plug->p2p_count > 0) {
  746. struct packet_list *pl;
  747. pl = list_entry(s->dma_packet_lists.next, struct packet_list, link);
  748. stream_start_dma(s, pl);
  749. }
  750. else {
  751. ohci1394_stop_it_ctx(s->host->ohci, s->iso_tasklet.context, 0);
  752. }
  753. }
  754. static int stream_configure(struct stream *s, int cmd, struct amdtp_ioctl *cfg)
  755. {
  756. const int transfer_delay = 9000;
  757. if (cfg->format <= AMDTP_FORMAT_IEC958_AC3)
  758. s->format = cfg->format;
  759. else
  760. return -EINVAL;
  761. switch (cfg->rate) {
  762. case 32000:
  763. s->syt_interval = 8;
  764. s->fdf = FDF_SFC_32KHZ;
  765. s->iec958_rate_code = 0x0c;
  766. break;
  767. case 44100:
  768. s->syt_interval = 8;
  769. s->fdf = FDF_SFC_44K1HZ;
  770. s->iec958_rate_code = 0x00;
  771. break;
  772. case 48000:
  773. s->syt_interval = 8;
  774. s->fdf = FDF_SFC_48KHZ;
  775. s->iec958_rate_code = 0x04;
  776. break;
  777. case 88200:
  778. s->syt_interval = 16;
  779. s->fdf = FDF_SFC_88K2HZ;
  780. s->iec958_rate_code = 0x00;
  781. break;
  782. case 96000:
  783. s->syt_interval = 16;
  784. s->fdf = FDF_SFC_96KHZ;
  785. s->iec958_rate_code = 0x00;
  786. break;
  787. case 176400:
  788. s->syt_interval = 32;
  789. s->fdf = FDF_SFC_176K4HZ;
  790. s->iec958_rate_code = 0x00;
  791. break;
  792. case 192000:
  793. s->syt_interval = 32;
  794. s->fdf = FDF_SFC_192KHZ;
  795. s->iec958_rate_code = 0x00;
  796. break;
  797. default:
  798. return -EINVAL;
  799. }
  800. s->rate = cfg->rate;
  801. fraction_init(&s->samples_per_cycle, s->rate, 8000);
  802. fraction_init(&s->ready_samples, 0, 8000);
  803. /* The ticks_per_syt_offset is initialized to the number of
  804. * ticks between syt_interval events. The number of ticks per
  805. * second is 24.576e6, so the number of ticks between
  806. * syt_interval events is 24.576e6 * syt_interval / rate.
  807. */
  808. fraction_init(&s->ticks_per_syt_offset,
  809. 24576000 * s->syt_interval, s->rate);
  810. fraction_init(&s->cycle_offset, (transfer_delay % 3072) * s->rate, s->rate);
  811. atomic_set(&s->cycle_count, transfer_delay / 3072);
  812. atomic_set(&s->cycle_count2, 0);
  813. s->mode = cfg->mode;
  814. s->sample_format = AMDTP_INPUT_LE16;
  815. /* When using the AM824 raw subformat we can stream signals of
  816. * any dimension. The IEC958 subformat, however, only
  817. * supports 2 channels.
  818. */
  819. if (s->format == AMDTP_FORMAT_RAW || cfg->dimension == 2)
  820. s->dimension = cfg->dimension;
  821. else
  822. return -EINVAL;
  823. if (s->opcr != NULL) {
  824. cmp_unregister_opcr(s->host->host, s->opcr);
  825. s->opcr = NULL;
  826. }
  827. switch(cmd) {
  828. case AMDTP_IOC_PLUG:
  829. s->opcr = cmp_register_opcr(s->host->host, cfg->u.plug,
  830. /*payload*/ 12, plug_update, s);
  831. if (s->opcr == NULL)
  832. return -EINVAL;
  833. s->iso_channel = s->opcr->channel;
  834. break;
  835. case AMDTP_IOC_CHANNEL:
  836. if (cfg->u.channel >= 0 && cfg->u.channel < 64)
  837. s->iso_channel = cfg->u.channel;
  838. else
  839. return -EINVAL;
  840. break;
  841. }
  842. /* The ioctl settings were all valid, so we realloc the packet
  843. * lists to make sure the packet size is big enough.
  844. */
  845. if (s->packet_pool != NULL)
  846. stream_free_packet_lists(s);
  847. if (stream_alloc_packet_lists(s) < 0) {
  848. stream_free_packet_lists(s);
  849. return -ENOMEM;
  850. }
  851. return 0;
  852. }
  853. static struct stream *stream_alloc(struct amdtp_host *host)
  854. {
  855. struct stream *s;
  856. unsigned long flags;
  857. s = kmalloc(sizeof(struct stream), SLAB_KERNEL);
  858. if (s == NULL)
  859. return NULL;
  860. memset(s, 0, sizeof(struct stream));
  861. s->host = host;
  862. s->input = buffer_alloc(BUFFER_SIZE);
  863. if (s->input == NULL) {
  864. kfree(s);
  865. return NULL;
  866. }
  867. s->descriptor_pool = pci_pool_create("descriptor pool", host->ohci->dev,
  868. sizeof(struct descriptor_block),
  869. 16, 0);
  870. if (s->descriptor_pool == NULL) {
  871. kfree(s->input);
  872. kfree(s);
  873. return NULL;
  874. }
  875. INIT_LIST_HEAD(&s->free_packet_lists);
  876. INIT_LIST_HEAD(&s->dma_packet_lists);
  877. init_waitqueue_head(&s->packet_list_wait);
  878. spin_lock_init(&s->packet_list_lock);
  879. ohci1394_init_iso_tasklet(&s->iso_tasklet, OHCI_ISO_TRANSMIT,
  880. stream_shift_packet_lists,
  881. (unsigned long) s);
  882. if (ohci1394_register_iso_tasklet(host->ohci, &s->iso_tasklet) < 0) {
  883. pci_pool_destroy(s->descriptor_pool);
  884. kfree(s->input);
  885. kfree(s);
  886. return NULL;
  887. }
  888. spin_lock_irqsave(&host->stream_list_lock, flags);
  889. list_add_tail(&s->link, &host->stream_list);
  890. spin_unlock_irqrestore(&host->stream_list_lock, flags);
  891. return s;
  892. }
  893. static void stream_free(struct stream *s)
  894. {
  895. unsigned long flags;
  896. /* Stop the DMA. We wait for the dma packet list to become
  897. * empty and let the dma controller run out of programs. This
  898. * seems to be more reliable than stopping it directly, since
  899. * that sometimes generates an it transmit interrupt if we
  900. * later re-enable the context.
  901. */
  902. wait_event_interruptible(s->packet_list_wait,
  903. list_empty(&s->dma_packet_lists));
  904. ohci1394_stop_it_ctx(s->host->ohci, s->iso_tasklet.context, 1);
  905. ohci1394_unregister_iso_tasklet(s->host->ohci, &s->iso_tasklet);
  906. if (s->opcr != NULL)
  907. cmp_unregister_opcr(s->host->host, s->opcr);
  908. spin_lock_irqsave(&s->host->stream_list_lock, flags);
  909. list_del(&s->link);
  910. spin_unlock_irqrestore(&s->host->stream_list_lock, flags);
  911. kfree(s->input);
  912. stream_free_packet_lists(s);
  913. pci_pool_destroy(s->descriptor_pool);
  914. kfree(s);
  915. }
  916. /* File operations */
  917. static ssize_t amdtp_write(struct file *file, const char __user *buffer, size_t count,
  918. loff_t *offset_is_ignored)
  919. {
  920. struct stream *s = file->private_data;
  921. unsigned char *p;
  922. int i;
  923. size_t length;
  924. if (s->packet_pool == NULL)
  925. return -EBADFD;
  926. /* Fill the circular buffer from the input buffer and call the
  927. * iso packer when the buffer is full. The iso packer may
  928. * leave bytes in the buffer for two reasons: either the
  929. * remaining bytes wasn't enough to build a new packet, or
  930. * there were no free packet lists. In the first case we
  931. * re-fill the buffer and call the iso packer again or return
  932. * if we used all the data from userspace. In the second
  933. * case, the wait_event_interruptible will block until the irq
  934. * handler frees a packet list.
  935. */
  936. for (i = 0; i < count; i += length) {
  937. p = buffer_put_bytes(s->input, count - i, &length);
  938. if (copy_from_user(p, buffer + i, length))
  939. return -EFAULT;
  940. if (s->input->length < s->input->size)
  941. continue;
  942. stream_flush(s);
  943. if (s->current_packet_list != NULL)
  944. continue;
  945. if (file->f_flags & O_NONBLOCK)
  946. return i + length > 0 ? i + length : -EAGAIN;
  947. if (wait_event_interruptible(s->packet_list_wait,
  948. !list_empty(&s->free_packet_lists)))
  949. return -EINTR;
  950. }
  951. return count;
  952. }
  953. static long amdtp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  954. {
  955. struct stream *s = file->private_data;
  956. struct amdtp_ioctl cfg;
  957. int err;
  958. lock_kernel();
  959. switch(cmd)
  960. {
  961. case AMDTP_IOC_PLUG:
  962. case AMDTP_IOC_CHANNEL:
  963. if (copy_from_user(&cfg, (struct amdtp_ioctl __user *) arg, sizeof cfg))
  964. err = -EFAULT;
  965. else
  966. err = stream_configure(s, cmd, &cfg);
  967. break;
  968. default:
  969. err = -EINVAL;
  970. break;
  971. }
  972. unlock_kernel();
  973. return err;
  974. }
  975. static unsigned int amdtp_poll(struct file *file, poll_table *pt)
  976. {
  977. struct stream *s = file->private_data;
  978. poll_wait(file, &s->packet_list_wait, pt);
  979. if (!list_empty(&s->free_packet_lists))
  980. return POLLOUT | POLLWRNORM;
  981. else
  982. return 0;
  983. }
  984. static int amdtp_open(struct inode *inode, struct file *file)
  985. {
  986. struct amdtp_host *host;
  987. int i = ieee1394_file_to_instance(file);
  988. host = hpsb_get_hostinfo_bykey(&amdtp_highlevel, i);
  989. if (host == NULL)
  990. return -ENODEV;
  991. file->private_data = stream_alloc(host);
  992. if (file->private_data == NULL)
  993. return -ENOMEM;
  994. return 0;
  995. }
  996. static int amdtp_release(struct inode *inode, struct file *file)
  997. {
  998. struct stream *s = file->private_data;
  999. stream_free(s);
  1000. return 0;
  1001. }
  1002. static struct cdev amdtp_cdev;
  1003. static struct file_operations amdtp_fops =
  1004. {
  1005. .owner = THIS_MODULE,
  1006. .write = amdtp_write,
  1007. .poll = amdtp_poll,
  1008. .unlocked_ioctl = amdtp_ioctl,
  1009. .compat_ioctl = amdtp_ioctl, /* All amdtp ioctls are compatible */
  1010. .open = amdtp_open,
  1011. .release = amdtp_release
  1012. };
  1013. /* IEEE1394 Subsystem functions */
  1014. static void amdtp_add_host(struct hpsb_host *host)
  1015. {
  1016. struct amdtp_host *ah;
  1017. int minor;
  1018. if (strcmp(host->driver->name, OHCI1394_DRIVER_NAME) != 0)
  1019. return;
  1020. ah = hpsb_create_hostinfo(&amdtp_highlevel, host, sizeof(*ah));
  1021. if (!ah) {
  1022. HPSB_ERR("amdtp: Unable able to alloc hostinfo");
  1023. return;
  1024. }
  1025. ah->host = host;
  1026. ah->ohci = host->hostdata;
  1027. hpsb_set_hostinfo_key(&amdtp_highlevel, host, ah->host->id);
  1028. minor = IEEE1394_MINOR_BLOCK_AMDTP * 16 + ah->host->id;
  1029. INIT_LIST_HEAD(&ah->stream_list);
  1030. spin_lock_init(&ah->stream_list_lock);
  1031. devfs_mk_cdev(MKDEV(IEEE1394_MAJOR, minor),
  1032. S_IFCHR|S_IRUSR|S_IWUSR, "amdtp/%d", ah->host->id);
  1033. }
  1034. static void amdtp_remove_host(struct hpsb_host *host)
  1035. {
  1036. struct amdtp_host *ah = hpsb_get_hostinfo(&amdtp_highlevel, host);
  1037. if (ah)
  1038. devfs_remove("amdtp/%d", ah->host->id);
  1039. return;
  1040. }
  1041. static struct hpsb_highlevel amdtp_highlevel = {
  1042. .name = "amdtp",
  1043. .add_host = amdtp_add_host,
  1044. .remove_host = amdtp_remove_host,
  1045. };
  1046. /* Module interface */
  1047. MODULE_AUTHOR("Kristian Hogsberg <hogsberg@users.sf.net>");
  1048. MODULE_DESCRIPTION("Driver for Audio & Music Data Transmission Protocol "
  1049. "on OHCI boards.");
  1050. MODULE_SUPPORTED_DEVICE("amdtp");
  1051. MODULE_LICENSE("GPL");
  1052. static int __init amdtp_init_module (void)
  1053. {
  1054. cdev_init(&amdtp_cdev, &amdtp_fops);
  1055. amdtp_cdev.owner = THIS_MODULE;
  1056. kobject_set_name(&amdtp_cdev.kobj, "amdtp");
  1057. if (cdev_add(&amdtp_cdev, IEEE1394_AMDTP_DEV, 16)) {
  1058. HPSB_ERR("amdtp: unable to add char device");
  1059. return -EIO;
  1060. }
  1061. devfs_mk_dir("amdtp");
  1062. hpsb_register_highlevel(&amdtp_highlevel);
  1063. HPSB_INFO("Loaded AMDTP driver");
  1064. return 0;
  1065. }
  1066. static void __exit amdtp_exit_module (void)
  1067. {
  1068. hpsb_unregister_highlevel(&amdtp_highlevel);
  1069. devfs_remove("amdtp");
  1070. cdev_del(&amdtp_cdev);
  1071. HPSB_INFO("Unloaded AMDTP driver");
  1072. }
  1073. module_init(amdtp_init_module);
  1074. module_exit(amdtp_exit_module);