amdtp.c 33 KB

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