amdtp.c 33 KB

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