pvrusb2-io.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  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
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include "pvrusb2-io.h"
  21. #include "pvrusb2-debug.h"
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. #include <linux/mutex.h>
  26. static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state);
  27. #define BUFFER_SIG 0x47653271
  28. // #define SANITY_CHECK_BUFFERS
  29. #ifdef SANITY_CHECK_BUFFERS
  30. #define BUFFER_CHECK(bp) do { \
  31. if ((bp)->signature != BUFFER_SIG) { \
  32. pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
  33. "Buffer %p is bad at %s:%d", \
  34. (bp),__FILE__,__LINE__); \
  35. pvr2_buffer_describe(bp,"BadSig"); \
  36. BUG(); \
  37. } \
  38. } while (0)
  39. #else
  40. #define BUFFER_CHECK(bp) do {} while(0)
  41. #endif
  42. struct pvr2_stream {
  43. /* Buffers queued for reading */
  44. struct list_head queued_list;
  45. unsigned int q_count;
  46. unsigned int q_bcount;
  47. /* Buffers with retrieved data */
  48. struct list_head ready_list;
  49. unsigned int r_count;
  50. unsigned int r_bcount;
  51. /* Buffers available for use */
  52. struct list_head idle_list;
  53. unsigned int i_count;
  54. unsigned int i_bcount;
  55. /* Pointers to all buffers */
  56. struct pvr2_buffer **buffers;
  57. /* Array size of buffers */
  58. unsigned int buffer_slot_count;
  59. /* Total buffers actually in circulation */
  60. unsigned int buffer_total_count;
  61. /* Designed number of buffers to be in circulation */
  62. unsigned int buffer_target_count;
  63. /* Executed when ready list become non-empty */
  64. pvr2_stream_callback callback_func;
  65. void *callback_data;
  66. /* Context for transfer endpoint */
  67. struct usb_device *dev;
  68. int endpoint;
  69. /* Overhead for mutex enforcement */
  70. spinlock_t list_lock;
  71. struct mutex mutex;
  72. /* Tracking state for tolerating errors */
  73. unsigned int fail_count;
  74. unsigned int fail_tolerance;
  75. unsigned int buffers_processed;
  76. unsigned int buffers_failed;
  77. unsigned int bytes_processed;
  78. };
  79. struct pvr2_buffer {
  80. int id;
  81. int signature;
  82. enum pvr2_buffer_state state;
  83. void *ptr; /* Pointer to storage area */
  84. unsigned int max_count; /* Size of storage area */
  85. unsigned int used_count; /* Amount of valid data in storage area */
  86. int status; /* Transfer result status */
  87. struct pvr2_stream *stream;
  88. struct list_head list_overhead;
  89. struct urb *purb;
  90. };
  91. static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st)
  92. {
  93. switch (st) {
  94. case pvr2_buffer_state_none: return "none";
  95. case pvr2_buffer_state_idle: return "idle";
  96. case pvr2_buffer_state_queued: return "queued";
  97. case pvr2_buffer_state_ready: return "ready";
  98. }
  99. return "unknown";
  100. }
  101. #ifdef SANITY_CHECK_BUFFERS
  102. static void pvr2_buffer_describe(struct pvr2_buffer *bp,const char *msg)
  103. {
  104. pvr2_trace(PVR2_TRACE_INFO,
  105. "buffer%s%s %p state=%s id=%d status=%d"
  106. " stream=%p purb=%p sig=0x%x",
  107. (msg ? " " : ""),
  108. (msg ? msg : ""),
  109. bp,
  110. (bp ? pvr2_buffer_state_decode(bp->state) : "(invalid)"),
  111. (bp ? bp->id : 0),
  112. (bp ? bp->status : 0),
  113. (bp ? bp->stream : NULL),
  114. (bp ? bp->purb : NULL),
  115. (bp ? bp->signature : 0));
  116. }
  117. #endif /* SANITY_CHECK_BUFFERS */
  118. static void pvr2_buffer_remove(struct pvr2_buffer *bp)
  119. {
  120. unsigned int *cnt;
  121. unsigned int *bcnt;
  122. unsigned int ccnt;
  123. struct pvr2_stream *sp = bp->stream;
  124. switch (bp->state) {
  125. case pvr2_buffer_state_idle:
  126. cnt = &sp->i_count;
  127. bcnt = &sp->i_bcount;
  128. ccnt = bp->max_count;
  129. break;
  130. case pvr2_buffer_state_queued:
  131. cnt = &sp->q_count;
  132. bcnt = &sp->q_bcount;
  133. ccnt = bp->max_count;
  134. break;
  135. case pvr2_buffer_state_ready:
  136. cnt = &sp->r_count;
  137. bcnt = &sp->r_bcount;
  138. ccnt = bp->used_count;
  139. break;
  140. default:
  141. return;
  142. }
  143. list_del_init(&bp->list_overhead);
  144. (*cnt)--;
  145. (*bcnt) -= ccnt;
  146. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  147. "/*---TRACE_FLOW---*/"
  148. " bufferPool %8s dec cap=%07d cnt=%02d",
  149. pvr2_buffer_state_decode(bp->state),*bcnt,*cnt);
  150. bp->state = pvr2_buffer_state_none;
  151. }
  152. static void pvr2_buffer_set_none(struct pvr2_buffer *bp)
  153. {
  154. unsigned long irq_flags;
  155. struct pvr2_stream *sp;
  156. BUFFER_CHECK(bp);
  157. sp = bp->stream;
  158. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  159. "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
  160. bp,
  161. pvr2_buffer_state_decode(bp->state),
  162. pvr2_buffer_state_decode(pvr2_buffer_state_none));
  163. spin_lock_irqsave(&sp->list_lock,irq_flags);
  164. pvr2_buffer_remove(bp);
  165. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  166. }
  167. static int pvr2_buffer_set_ready(struct pvr2_buffer *bp)
  168. {
  169. int fl;
  170. unsigned long irq_flags;
  171. struct pvr2_stream *sp;
  172. BUFFER_CHECK(bp);
  173. sp = bp->stream;
  174. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  175. "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
  176. bp,
  177. pvr2_buffer_state_decode(bp->state),
  178. pvr2_buffer_state_decode(pvr2_buffer_state_ready));
  179. spin_lock_irqsave(&sp->list_lock,irq_flags);
  180. fl = (sp->r_count == 0);
  181. pvr2_buffer_remove(bp);
  182. list_add_tail(&bp->list_overhead,&sp->ready_list);
  183. bp->state = pvr2_buffer_state_ready;
  184. (sp->r_count)++;
  185. sp->r_bcount += bp->used_count;
  186. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  187. "/*---TRACE_FLOW---*/"
  188. " bufferPool %8s inc cap=%07d cnt=%02d",
  189. pvr2_buffer_state_decode(bp->state),
  190. sp->r_bcount,sp->r_count);
  191. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  192. return fl;
  193. }
  194. static void pvr2_buffer_set_idle(struct pvr2_buffer *bp)
  195. {
  196. unsigned long irq_flags;
  197. struct pvr2_stream *sp;
  198. BUFFER_CHECK(bp);
  199. sp = bp->stream;
  200. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  201. "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
  202. bp,
  203. pvr2_buffer_state_decode(bp->state),
  204. pvr2_buffer_state_decode(pvr2_buffer_state_idle));
  205. spin_lock_irqsave(&sp->list_lock,irq_flags);
  206. pvr2_buffer_remove(bp);
  207. list_add_tail(&bp->list_overhead,&sp->idle_list);
  208. bp->state = pvr2_buffer_state_idle;
  209. (sp->i_count)++;
  210. sp->i_bcount += bp->max_count;
  211. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  212. "/*---TRACE_FLOW---*/"
  213. " bufferPool %8s inc cap=%07d cnt=%02d",
  214. pvr2_buffer_state_decode(bp->state),
  215. sp->i_bcount,sp->i_count);
  216. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  217. }
  218. static void pvr2_buffer_set_queued(struct pvr2_buffer *bp)
  219. {
  220. unsigned long irq_flags;
  221. struct pvr2_stream *sp;
  222. BUFFER_CHECK(bp);
  223. sp = bp->stream;
  224. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  225. "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
  226. bp,
  227. pvr2_buffer_state_decode(bp->state),
  228. pvr2_buffer_state_decode(pvr2_buffer_state_queued));
  229. spin_lock_irqsave(&sp->list_lock,irq_flags);
  230. pvr2_buffer_remove(bp);
  231. list_add_tail(&bp->list_overhead,&sp->queued_list);
  232. bp->state = pvr2_buffer_state_queued;
  233. (sp->q_count)++;
  234. sp->q_bcount += bp->max_count;
  235. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  236. "/*---TRACE_FLOW---*/"
  237. " bufferPool %8s inc cap=%07d cnt=%02d",
  238. pvr2_buffer_state_decode(bp->state),
  239. sp->q_bcount,sp->q_count);
  240. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  241. }
  242. static void pvr2_buffer_wipe(struct pvr2_buffer *bp)
  243. {
  244. if (bp->state == pvr2_buffer_state_queued) {
  245. usb_kill_urb(bp->purb);
  246. }
  247. }
  248. static int pvr2_buffer_init(struct pvr2_buffer *bp,
  249. struct pvr2_stream *sp,
  250. unsigned int id)
  251. {
  252. memset(bp,0,sizeof(*bp));
  253. bp->signature = BUFFER_SIG;
  254. bp->id = id;
  255. pvr2_trace(PVR2_TRACE_BUF_POOL,
  256. "/*---TRACE_FLOW---*/ bufferInit %p stream=%p",bp,sp);
  257. bp->stream = sp;
  258. bp->state = pvr2_buffer_state_none;
  259. INIT_LIST_HEAD(&bp->list_overhead);
  260. bp->purb = usb_alloc_urb(0,GFP_KERNEL);
  261. if (! bp->purb) return -ENOMEM;
  262. #ifdef SANITY_CHECK_BUFFERS
  263. pvr2_buffer_describe(bp,"create");
  264. #endif
  265. return 0;
  266. }
  267. static void pvr2_buffer_done(struct pvr2_buffer *bp)
  268. {
  269. #ifdef SANITY_CHECK_BUFFERS
  270. pvr2_buffer_describe(bp,"delete");
  271. #endif
  272. pvr2_buffer_wipe(bp);
  273. pvr2_buffer_set_none(bp);
  274. bp->signature = 0;
  275. bp->stream = NULL;
  276. usb_free_urb(bp->purb);
  277. pvr2_trace(PVR2_TRACE_BUF_POOL,"/*---TRACE_FLOW---*/"
  278. " bufferDone %p",bp);
  279. }
  280. static int pvr2_stream_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
  281. {
  282. int ret;
  283. unsigned int scnt;
  284. /* Allocate buffers pointer array in multiples of 32 entries */
  285. if (cnt == sp->buffer_total_count) return 0;
  286. pvr2_trace(PVR2_TRACE_BUF_POOL,
  287. "/*---TRACE_FLOW---*/ poolResize "
  288. " stream=%p cur=%d adj=%+d",
  289. sp,
  290. sp->buffer_total_count,
  291. cnt-sp->buffer_total_count);
  292. scnt = cnt & ~0x1f;
  293. if (cnt > scnt) scnt += 0x20;
  294. if (cnt > sp->buffer_total_count) {
  295. if (scnt > sp->buffer_slot_count) {
  296. struct pvr2_buffer **nb;
  297. nb = kmalloc(scnt * sizeof(*nb),GFP_KERNEL);
  298. if (!nb) return -ENOMEM;
  299. if (sp->buffer_slot_count) {
  300. memcpy(nb,sp->buffers,
  301. sp->buffer_slot_count * sizeof(*nb));
  302. kfree(sp->buffers);
  303. }
  304. sp->buffers = nb;
  305. sp->buffer_slot_count = scnt;
  306. }
  307. while (sp->buffer_total_count < cnt) {
  308. struct pvr2_buffer *bp;
  309. bp = kmalloc(sizeof(*bp),GFP_KERNEL);
  310. if (!bp) return -ENOMEM;
  311. ret = pvr2_buffer_init(bp,sp,sp->buffer_total_count);
  312. if (ret) {
  313. kfree(bp);
  314. return -ENOMEM;
  315. }
  316. sp->buffers[sp->buffer_total_count] = bp;
  317. (sp->buffer_total_count)++;
  318. pvr2_buffer_set_idle(bp);
  319. }
  320. } else {
  321. while (sp->buffer_total_count > cnt) {
  322. struct pvr2_buffer *bp;
  323. bp = sp->buffers[sp->buffer_total_count - 1];
  324. /* Paranoia */
  325. sp->buffers[sp->buffer_total_count - 1] = NULL;
  326. (sp->buffer_total_count)--;
  327. pvr2_buffer_done(bp);
  328. kfree(bp);
  329. }
  330. if (scnt < sp->buffer_slot_count) {
  331. struct pvr2_buffer **nb = NULL;
  332. if (scnt) {
  333. nb = kmemdup(sp->buffers, scnt * sizeof(*nb),
  334. GFP_KERNEL);
  335. if (!nb) return -ENOMEM;
  336. }
  337. kfree(sp->buffers);
  338. sp->buffers = nb;
  339. sp->buffer_slot_count = scnt;
  340. }
  341. }
  342. return 0;
  343. }
  344. static int pvr2_stream_achieve_buffer_count(struct pvr2_stream *sp)
  345. {
  346. struct pvr2_buffer *bp;
  347. unsigned int cnt;
  348. if (sp->buffer_total_count == sp->buffer_target_count) return 0;
  349. pvr2_trace(PVR2_TRACE_BUF_POOL,
  350. "/*---TRACE_FLOW---*/"
  351. " poolCheck stream=%p cur=%d tgt=%d",
  352. sp,sp->buffer_total_count,sp->buffer_target_count);
  353. if (sp->buffer_total_count < sp->buffer_target_count) {
  354. return pvr2_stream_buffer_count(sp,sp->buffer_target_count);
  355. }
  356. cnt = 0;
  357. while ((sp->buffer_total_count - cnt) > sp->buffer_target_count) {
  358. bp = sp->buffers[sp->buffer_total_count - (cnt + 1)];
  359. if (bp->state != pvr2_buffer_state_idle) break;
  360. cnt++;
  361. }
  362. if (cnt) {
  363. pvr2_stream_buffer_count(sp,sp->buffer_total_count - cnt);
  364. }
  365. return 0;
  366. }
  367. static void pvr2_stream_internal_flush(struct pvr2_stream *sp)
  368. {
  369. struct list_head *lp;
  370. struct pvr2_buffer *bp1;
  371. while ((lp = sp->queued_list.next) != &sp->queued_list) {
  372. bp1 = list_entry(lp,struct pvr2_buffer,list_overhead);
  373. pvr2_buffer_wipe(bp1);
  374. /* At this point, we should be guaranteed that no
  375. completion callback may happen on this buffer. But it's
  376. possible that it might have completed after we noticed
  377. it but before we wiped it. So double check its status
  378. here first. */
  379. if (bp1->state != pvr2_buffer_state_queued) continue;
  380. pvr2_buffer_set_idle(bp1);
  381. }
  382. if (sp->buffer_total_count != sp->buffer_target_count) {
  383. pvr2_stream_achieve_buffer_count(sp);
  384. }
  385. }
  386. static void pvr2_stream_init(struct pvr2_stream *sp)
  387. {
  388. spin_lock_init(&sp->list_lock);
  389. mutex_init(&sp->mutex);
  390. INIT_LIST_HEAD(&sp->queued_list);
  391. INIT_LIST_HEAD(&sp->ready_list);
  392. INIT_LIST_HEAD(&sp->idle_list);
  393. }
  394. static void pvr2_stream_done(struct pvr2_stream *sp)
  395. {
  396. mutex_lock(&sp->mutex); do {
  397. pvr2_stream_internal_flush(sp);
  398. pvr2_stream_buffer_count(sp,0);
  399. } while (0); mutex_unlock(&sp->mutex);
  400. }
  401. static void buffer_complete(struct urb *urb)
  402. {
  403. struct pvr2_buffer *bp = urb->context;
  404. struct pvr2_stream *sp;
  405. unsigned long irq_flags;
  406. BUFFER_CHECK(bp);
  407. sp = bp->stream;
  408. bp->used_count = 0;
  409. bp->status = 0;
  410. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  411. "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
  412. bp,urb->status,urb->actual_length);
  413. spin_lock_irqsave(&sp->list_lock,irq_flags);
  414. if ((!(urb->status)) ||
  415. (urb->status == -ENOENT) ||
  416. (urb->status == -ECONNRESET) ||
  417. (urb->status == -ESHUTDOWN)) {
  418. (sp->buffers_processed)++;
  419. sp->bytes_processed += urb->actual_length;
  420. bp->used_count = urb->actual_length;
  421. if (sp->fail_count) {
  422. pvr2_trace(PVR2_TRACE_TOLERANCE,
  423. "stream %p transfer ok"
  424. " - fail count reset",sp);
  425. sp->fail_count = 0;
  426. }
  427. } else if (sp->fail_count < sp->fail_tolerance) {
  428. // We can tolerate this error, because we're below the
  429. // threshold...
  430. (sp->fail_count)++;
  431. (sp->buffers_failed)++;
  432. pvr2_trace(PVR2_TRACE_TOLERANCE,
  433. "stream %p ignoring error %d"
  434. " - fail count increased to %u",
  435. sp,urb->status,sp->fail_count);
  436. } else {
  437. (sp->buffers_failed)++;
  438. bp->status = urb->status;
  439. }
  440. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  441. pvr2_buffer_set_ready(bp);
  442. if (sp && sp->callback_func) {
  443. sp->callback_func(sp->callback_data);
  444. }
  445. }
  446. struct pvr2_stream *pvr2_stream_create(void)
  447. {
  448. struct pvr2_stream *sp;
  449. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  450. if (!sp) return sp;
  451. pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_create: sp=%p",sp);
  452. pvr2_stream_init(sp);
  453. return sp;
  454. }
  455. void pvr2_stream_destroy(struct pvr2_stream *sp)
  456. {
  457. if (!sp) return;
  458. pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_destroy: sp=%p",sp);
  459. pvr2_stream_done(sp);
  460. kfree(sp);
  461. }
  462. void pvr2_stream_setup(struct pvr2_stream *sp,
  463. struct usb_device *dev,
  464. int endpoint,
  465. unsigned int tolerance)
  466. {
  467. mutex_lock(&sp->mutex); do {
  468. pvr2_stream_internal_flush(sp);
  469. sp->dev = dev;
  470. sp->endpoint = endpoint;
  471. sp->fail_tolerance = tolerance;
  472. } while(0); mutex_unlock(&sp->mutex);
  473. }
  474. void pvr2_stream_set_callback(struct pvr2_stream *sp,
  475. pvr2_stream_callback func,
  476. void *data)
  477. {
  478. unsigned long irq_flags;
  479. mutex_lock(&sp->mutex);
  480. do {
  481. spin_lock_irqsave(&sp->list_lock,irq_flags);
  482. sp->callback_data = data;
  483. sp->callback_func = func;
  484. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  485. } while(0);
  486. mutex_unlock(&sp->mutex);
  487. }
  488. void pvr2_stream_get_stats(struct pvr2_stream *sp,
  489. struct pvr2_stream_stats *stats,
  490. int zero_counts)
  491. {
  492. unsigned long irq_flags;
  493. spin_lock_irqsave(&sp->list_lock,irq_flags);
  494. if (stats) {
  495. stats->buffers_in_queue = sp->q_count;
  496. stats->buffers_in_idle = sp->i_count;
  497. stats->buffers_in_ready = sp->r_count;
  498. stats->buffers_processed = sp->buffers_processed;
  499. stats->buffers_failed = sp->buffers_failed;
  500. stats->bytes_processed = sp->bytes_processed;
  501. }
  502. if (zero_counts) {
  503. sp->buffers_processed = 0;
  504. sp->buffers_failed = 0;
  505. sp->bytes_processed = 0;
  506. }
  507. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  508. }
  509. /* Query / set the nominal buffer count */
  510. int pvr2_stream_get_buffer_count(struct pvr2_stream *sp)
  511. {
  512. return sp->buffer_target_count;
  513. }
  514. int pvr2_stream_set_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
  515. {
  516. int ret;
  517. if (sp->buffer_target_count == cnt) return 0;
  518. mutex_lock(&sp->mutex);
  519. do {
  520. sp->buffer_target_count = cnt;
  521. ret = pvr2_stream_achieve_buffer_count(sp);
  522. } while(0);
  523. mutex_unlock(&sp->mutex);
  524. return ret;
  525. }
  526. struct pvr2_buffer *pvr2_stream_get_idle_buffer(struct pvr2_stream *sp)
  527. {
  528. struct list_head *lp = sp->idle_list.next;
  529. if (lp == &sp->idle_list) return NULL;
  530. return list_entry(lp,struct pvr2_buffer,list_overhead);
  531. }
  532. struct pvr2_buffer *pvr2_stream_get_ready_buffer(struct pvr2_stream *sp)
  533. {
  534. struct list_head *lp = sp->ready_list.next;
  535. if (lp == &sp->ready_list) return NULL;
  536. return list_entry(lp,struct pvr2_buffer,list_overhead);
  537. }
  538. struct pvr2_buffer *pvr2_stream_get_buffer(struct pvr2_stream *sp,int id)
  539. {
  540. if (id < 0) return NULL;
  541. if (id >= sp->buffer_total_count) return NULL;
  542. return sp->buffers[id];
  543. }
  544. int pvr2_stream_get_ready_count(struct pvr2_stream *sp)
  545. {
  546. return sp->r_count;
  547. }
  548. void pvr2_stream_kill(struct pvr2_stream *sp)
  549. {
  550. struct pvr2_buffer *bp;
  551. mutex_lock(&sp->mutex);
  552. do {
  553. pvr2_stream_internal_flush(sp);
  554. while ((bp = pvr2_stream_get_ready_buffer(sp)) != NULL) {
  555. pvr2_buffer_set_idle(bp);
  556. }
  557. if (sp->buffer_total_count != sp->buffer_target_count) {
  558. pvr2_stream_achieve_buffer_count(sp);
  559. }
  560. } while(0);
  561. mutex_unlock(&sp->mutex);
  562. }
  563. int pvr2_buffer_queue(struct pvr2_buffer *bp)
  564. {
  565. #undef SEED_BUFFER
  566. #ifdef SEED_BUFFER
  567. unsigned int idx;
  568. unsigned int val;
  569. #endif
  570. int ret = 0;
  571. struct pvr2_stream *sp;
  572. if (!bp) return -EINVAL;
  573. sp = bp->stream;
  574. mutex_lock(&sp->mutex);
  575. do {
  576. pvr2_buffer_wipe(bp);
  577. if (!sp->dev) {
  578. ret = -EIO;
  579. break;
  580. }
  581. pvr2_buffer_set_queued(bp);
  582. #ifdef SEED_BUFFER
  583. for (idx = 0; idx < (bp->max_count) / 4; idx++) {
  584. val = bp->id << 24;
  585. val |= idx;
  586. ((unsigned int *)(bp->ptr))[idx] = val;
  587. }
  588. #endif
  589. bp->status = -EINPROGRESS;
  590. usb_fill_bulk_urb(bp->purb, // struct urb *urb
  591. sp->dev, // struct usb_device *dev
  592. // endpoint (below)
  593. usb_rcvbulkpipe(sp->dev,sp->endpoint),
  594. bp->ptr, // void *transfer_buffer
  595. bp->max_count, // int buffer_length
  596. buffer_complete,
  597. bp);
  598. usb_submit_urb(bp->purb,GFP_KERNEL);
  599. } while(0);
  600. mutex_unlock(&sp->mutex);
  601. return ret;
  602. }
  603. int pvr2_buffer_set_buffer(struct pvr2_buffer *bp,void *ptr,unsigned int cnt)
  604. {
  605. int ret = 0;
  606. unsigned long irq_flags;
  607. struct pvr2_stream *sp;
  608. if (!bp) return -EINVAL;
  609. sp = bp->stream;
  610. mutex_lock(&sp->mutex);
  611. do {
  612. spin_lock_irqsave(&sp->list_lock,irq_flags);
  613. if (bp->state != pvr2_buffer_state_idle) {
  614. ret = -EPERM;
  615. } else {
  616. bp->ptr = ptr;
  617. bp->stream->i_bcount -= bp->max_count;
  618. bp->max_count = cnt;
  619. bp->stream->i_bcount += bp->max_count;
  620. pvr2_trace(PVR2_TRACE_BUF_FLOW,
  621. "/*---TRACE_FLOW---*/ bufferPool "
  622. " %8s cap cap=%07d cnt=%02d",
  623. pvr2_buffer_state_decode(
  624. pvr2_buffer_state_idle),
  625. bp->stream->i_bcount,bp->stream->i_count);
  626. }
  627. spin_unlock_irqrestore(&sp->list_lock,irq_flags);
  628. } while(0);
  629. mutex_unlock(&sp->mutex);
  630. return ret;
  631. }
  632. unsigned int pvr2_buffer_get_count(struct pvr2_buffer *bp)
  633. {
  634. return bp->used_count;
  635. }
  636. int pvr2_buffer_get_status(struct pvr2_buffer *bp)
  637. {
  638. return bp->status;
  639. }
  640. int pvr2_buffer_get_id(struct pvr2_buffer *bp)
  641. {
  642. return bp->id;
  643. }