pvrusb2-ioread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  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
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include "pvrusb2-ioread.h"
  22. #include "pvrusb2-debug.h"
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include <linux/mutex.h>
  27. #include <asm/uaccess.h>
  28. #define BUFFER_COUNT 32
  29. #define BUFFER_SIZE PAGE_ALIGN(0x4000)
  30. struct pvr2_ioread {
  31. struct pvr2_stream *stream;
  32. char *buffer_storage[BUFFER_COUNT];
  33. char *sync_key_ptr;
  34. unsigned int sync_key_len;
  35. unsigned int sync_buf_offs;
  36. unsigned int sync_state;
  37. unsigned int sync_trashed_count;
  38. int enabled; // Streaming is on
  39. int spigot_open; // OK to pass data to client
  40. int stream_running; // Passing data to client now
  41. /* State relevant to current buffer being read */
  42. struct pvr2_buffer *c_buf;
  43. char *c_data_ptr;
  44. unsigned int c_data_len;
  45. unsigned int c_data_offs;
  46. struct mutex mutex;
  47. };
  48. static int pvr2_ioread_init(struct pvr2_ioread *cp)
  49. {
  50. unsigned int idx;
  51. cp->stream = NULL;
  52. mutex_init(&cp->mutex);
  53. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  54. cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
  55. if (!(cp->buffer_storage[idx])) break;
  56. }
  57. if (idx < BUFFER_COUNT) {
  58. // An allocation appears to have failed
  59. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  60. if (!(cp->buffer_storage[idx])) continue;
  61. kfree(cp->buffer_storage[idx]);
  62. }
  63. return -ENOMEM;
  64. }
  65. return 0;
  66. }
  67. static void pvr2_ioread_done(struct pvr2_ioread *cp)
  68. {
  69. unsigned int idx;
  70. pvr2_ioread_setup(cp,NULL);
  71. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  72. if (!(cp->buffer_storage[idx])) continue;
  73. kfree(cp->buffer_storage[idx]);
  74. }
  75. }
  76. struct pvr2_ioread *pvr2_ioread_create(void)
  77. {
  78. struct pvr2_ioread *cp;
  79. cp = kmalloc(sizeof(*cp),GFP_KERNEL);
  80. if (!cp) return NULL;
  81. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
  82. memset(cp,0,sizeof(*cp));
  83. if (pvr2_ioread_init(cp) < 0) {
  84. kfree(cp);
  85. return NULL;
  86. }
  87. return cp;
  88. }
  89. void pvr2_ioread_destroy(struct pvr2_ioread *cp)
  90. {
  91. if (!cp) return;
  92. pvr2_ioread_done(cp);
  93. pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
  94. if (cp->sync_key_ptr) {
  95. kfree(cp->sync_key_ptr);
  96. cp->sync_key_ptr = NULL;
  97. }
  98. kfree(cp);
  99. }
  100. void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
  101. const char *sync_key_ptr,
  102. unsigned int sync_key_len)
  103. {
  104. if (!cp) return;
  105. if (!sync_key_ptr) sync_key_len = 0;
  106. if ((sync_key_len == cp->sync_key_len) &&
  107. ((!sync_key_len) ||
  108. (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
  109. if (sync_key_len != cp->sync_key_len) {
  110. if (cp->sync_key_ptr) {
  111. kfree(cp->sync_key_ptr);
  112. cp->sync_key_ptr = NULL;
  113. }
  114. cp->sync_key_len = 0;
  115. if (sync_key_len) {
  116. cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
  117. if (cp->sync_key_ptr) {
  118. cp->sync_key_len = sync_key_len;
  119. }
  120. }
  121. }
  122. if (!cp->sync_key_len) return;
  123. memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
  124. }
  125. static void pvr2_ioread_stop(struct pvr2_ioread *cp)
  126. {
  127. if (!(cp->enabled)) return;
  128. pvr2_trace(PVR2_TRACE_START_STOP,
  129. "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
  130. pvr2_stream_kill(cp->stream);
  131. cp->c_buf = NULL;
  132. cp->c_data_ptr = NULL;
  133. cp->c_data_len = 0;
  134. cp->c_data_offs = 0;
  135. cp->enabled = 0;
  136. cp->stream_running = 0;
  137. cp->spigot_open = 0;
  138. if (cp->sync_state) {
  139. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  140. "/*---TRACE_READ---*/ sync_state <== 0");
  141. cp->sync_state = 0;
  142. }
  143. }
  144. static int pvr2_ioread_start(struct pvr2_ioread *cp)
  145. {
  146. int stat;
  147. struct pvr2_buffer *bp;
  148. if (cp->enabled) return 0;
  149. if (!(cp->stream)) return 0;
  150. pvr2_trace(PVR2_TRACE_START_STOP,
  151. "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
  152. while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != 0) {
  153. stat = pvr2_buffer_queue(bp);
  154. if (stat < 0) {
  155. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  156. "/*---TRACE_READ---*/"
  157. " pvr2_ioread_start id=%p"
  158. " error=%d",
  159. cp,stat);
  160. pvr2_ioread_stop(cp);
  161. return stat;
  162. }
  163. }
  164. cp->enabled = !0;
  165. cp->c_buf = NULL;
  166. cp->c_data_ptr = NULL;
  167. cp->c_data_len = 0;
  168. cp->c_data_offs = 0;
  169. cp->stream_running = 0;
  170. if (cp->sync_key_len) {
  171. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  172. "/*---TRACE_READ---*/ sync_state <== 1");
  173. cp->sync_state = 1;
  174. cp->sync_trashed_count = 0;
  175. cp->sync_buf_offs = 0;
  176. }
  177. cp->spigot_open = 0;
  178. return 0;
  179. }
  180. struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
  181. {
  182. return cp->stream;
  183. }
  184. int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
  185. {
  186. int ret;
  187. unsigned int idx;
  188. struct pvr2_buffer *bp;
  189. mutex_lock(&cp->mutex); do {
  190. if (cp->stream) {
  191. pvr2_trace(PVR2_TRACE_START_STOP,
  192. "/*---TRACE_READ---*/"
  193. " pvr2_ioread_setup (tear-down) id=%p",cp);
  194. pvr2_ioread_stop(cp);
  195. pvr2_stream_kill(cp->stream);
  196. pvr2_stream_set_buffer_count(cp->stream,0);
  197. cp->stream = NULL;
  198. }
  199. if (sp) {
  200. pvr2_trace(PVR2_TRACE_START_STOP,
  201. "/*---TRACE_READ---*/"
  202. " pvr2_ioread_setup (setup) id=%p",cp);
  203. pvr2_stream_kill(sp);
  204. ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
  205. if (ret < 0) return ret;
  206. for (idx = 0; idx < BUFFER_COUNT; idx++) {
  207. bp = pvr2_stream_get_buffer(sp,idx);
  208. pvr2_buffer_set_buffer(bp,
  209. cp->buffer_storage[idx],
  210. BUFFER_SIZE);
  211. }
  212. cp->stream = sp;
  213. }
  214. } while (0); mutex_unlock(&cp->mutex);
  215. return 0;
  216. }
  217. int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
  218. {
  219. int ret = 0;
  220. if ((!fl) == (!(cp->enabled))) return ret;
  221. mutex_lock(&cp->mutex); do {
  222. if (fl) {
  223. ret = pvr2_ioread_start(cp);
  224. } else {
  225. pvr2_ioread_stop(cp);
  226. }
  227. } while (0); mutex_unlock(&cp->mutex);
  228. return ret;
  229. }
  230. static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
  231. {
  232. int stat;
  233. while (cp->c_data_len <= cp->c_data_offs) {
  234. if (cp->c_buf) {
  235. // Flush out current buffer first.
  236. stat = pvr2_buffer_queue(cp->c_buf);
  237. if (stat < 0) {
  238. // Streaming error...
  239. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  240. "/*---TRACE_READ---*/"
  241. " pvr2_ioread_read id=%p"
  242. " queue_error=%d",
  243. cp,stat);
  244. pvr2_ioread_stop(cp);
  245. return 0;
  246. }
  247. cp->c_buf = NULL;
  248. cp->c_data_ptr = NULL;
  249. cp->c_data_len = 0;
  250. cp->c_data_offs = 0;
  251. }
  252. // Now get a freshly filled buffer.
  253. cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
  254. if (!cp->c_buf) break; // Nothing ready; done.
  255. cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
  256. if (!cp->c_data_len) {
  257. // Nothing transferred. Was there an error?
  258. stat = pvr2_buffer_get_status(cp->c_buf);
  259. if (stat < 0) {
  260. // Streaming error...
  261. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  262. "/*---TRACE_READ---*/"
  263. " pvr2_ioread_read id=%p"
  264. " buffer_error=%d",
  265. cp,stat);
  266. pvr2_ioread_stop(cp);
  267. // Give up.
  268. return 0;
  269. }
  270. // Start over...
  271. continue;
  272. }
  273. cp->c_data_offs = 0;
  274. cp->c_data_ptr = cp->buffer_storage[
  275. pvr2_buffer_get_id(cp->c_buf)];
  276. }
  277. return !0;
  278. }
  279. static void pvr2_ioread_filter(struct pvr2_ioread *cp)
  280. {
  281. unsigned int idx;
  282. if (!cp->enabled) return;
  283. if (cp->sync_state != 1) return;
  284. // Search the stream for our synchronization key. This is made
  285. // complicated by the fact that in order to be honest with
  286. // ourselves here we must search across buffer boundaries...
  287. mutex_lock(&cp->mutex); while (1) {
  288. // Ensure we have a buffer
  289. if (!pvr2_ioread_get_buffer(cp)) break;
  290. if (!cp->c_data_len) break;
  291. // Now walk the buffer contents until we match the key or
  292. // run out of buffer data.
  293. for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
  294. if (cp->sync_buf_offs >= cp->sync_key_len) break;
  295. if (cp->c_data_ptr[idx] ==
  296. cp->sync_key_ptr[cp->sync_buf_offs]) {
  297. // Found the next key byte
  298. (cp->sync_buf_offs)++;
  299. } else {
  300. // Whoops, mismatched. Start key over...
  301. cp->sync_buf_offs = 0;
  302. }
  303. }
  304. // Consume what we've walked through
  305. cp->c_data_offs += idx;
  306. cp->sync_trashed_count += idx;
  307. // If we've found the key, then update state and get out.
  308. if (cp->sync_buf_offs >= cp->sync_key_len) {
  309. cp->sync_trashed_count -= cp->sync_key_len;
  310. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  311. "/*---TRACE_READ---*/"
  312. " sync_state <== 2 (skipped %u bytes)",
  313. cp->sync_trashed_count);
  314. cp->sync_state = 2;
  315. cp->sync_buf_offs = 0;
  316. break;
  317. }
  318. if (cp->c_data_offs < cp->c_data_len) {
  319. // Sanity check - should NEVER get here
  320. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  321. "ERROR: pvr2_ioread filter sync problem"
  322. " len=%u offs=%u",
  323. cp->c_data_len,cp->c_data_offs);
  324. // Get out so we don't get stuck in an infinite
  325. // loop.
  326. break;
  327. }
  328. continue; // (for clarity)
  329. } mutex_unlock(&cp->mutex);
  330. }
  331. int pvr2_ioread_avail(struct pvr2_ioread *cp)
  332. {
  333. int ret;
  334. if (!(cp->enabled)) {
  335. // Stream is not enabled; so this is an I/O error
  336. return -EIO;
  337. }
  338. if (cp->sync_state == 1) {
  339. pvr2_ioread_filter(cp);
  340. if (cp->sync_state == 1) return -EAGAIN;
  341. }
  342. ret = 0;
  343. if (cp->stream_running) {
  344. if (!pvr2_stream_get_ready_count(cp->stream)) {
  345. // No data available at all right now.
  346. ret = -EAGAIN;
  347. }
  348. } else {
  349. if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
  350. // Haven't buffered up enough yet; try again later
  351. ret = -EAGAIN;
  352. }
  353. }
  354. if ((!(cp->spigot_open)) != (!(ret == 0))) {
  355. cp->spigot_open = (ret == 0);
  356. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  357. "/*---TRACE_READ---*/ data is %s",
  358. cp->spigot_open ? "available" : "pending");
  359. }
  360. return ret;
  361. }
  362. int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
  363. {
  364. unsigned int copied_cnt;
  365. unsigned int bcnt;
  366. const char *src;
  367. int stat;
  368. int ret = 0;
  369. unsigned int req_cnt = cnt;
  370. if (!cnt) {
  371. pvr2_trace(PVR2_TRACE_TRAP,
  372. "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
  373. " ZERO Request? Returning zero.",cp);
  374. return 0;
  375. }
  376. stat = pvr2_ioread_avail(cp);
  377. if (stat < 0) return stat;
  378. cp->stream_running = !0;
  379. mutex_lock(&cp->mutex); do {
  380. // Suck data out of the buffers and copy to the user
  381. copied_cnt = 0;
  382. if (!buf) cnt = 0;
  383. while (1) {
  384. if (!pvr2_ioread_get_buffer(cp)) {
  385. ret = -EIO;
  386. break;
  387. }
  388. if (!cnt) break;
  389. if (cp->sync_state == 2) {
  390. // We're repeating the sync key data into
  391. // the stream.
  392. src = cp->sync_key_ptr + cp->sync_buf_offs;
  393. bcnt = cp->sync_key_len - cp->sync_buf_offs;
  394. } else {
  395. // Normal buffer copy
  396. src = cp->c_data_ptr + cp->c_data_offs;
  397. bcnt = cp->c_data_len - cp->c_data_offs;
  398. }
  399. if (!bcnt) break;
  400. // Don't run past user's buffer
  401. if (bcnt > cnt) bcnt = cnt;
  402. if (copy_to_user(buf,src,bcnt)) {
  403. // User supplied a bad pointer?
  404. // Give up - this *will* cause data
  405. // to be lost.
  406. ret = -EFAULT;
  407. break;
  408. }
  409. cnt -= bcnt;
  410. buf += bcnt;
  411. copied_cnt += bcnt;
  412. if (cp->sync_state == 2) {
  413. // Update offset inside sync key that we're
  414. // repeating back out.
  415. cp->sync_buf_offs += bcnt;
  416. if (cp->sync_buf_offs >= cp->sync_key_len) {
  417. // Consumed entire key; switch mode
  418. // to normal.
  419. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  420. "/*---TRACE_READ---*/"
  421. " sync_state <== 0");
  422. cp->sync_state = 0;
  423. }
  424. } else {
  425. // Update buffer offset.
  426. cp->c_data_offs += bcnt;
  427. }
  428. }
  429. } while (0); mutex_unlock(&cp->mutex);
  430. if (!ret) {
  431. if (copied_cnt) {
  432. // If anything was copied, return that count
  433. ret = copied_cnt;
  434. } else {
  435. // Nothing copied; suggest to caller that another
  436. // attempt should be tried again later
  437. ret = -EAGAIN;
  438. }
  439. }
  440. pvr2_trace(PVR2_TRACE_DATA_FLOW,
  441. "/*---TRACE_READ---*/ pvr2_ioread_read"
  442. " id=%p request=%d result=%d",
  443. cp,req_cnt,ret);
  444. return ret;
  445. }
  446. /*
  447. Stuff for Emacs to see, in order to encourage consistent editing style:
  448. *** Local Variables: ***
  449. *** mode: c ***
  450. *** fill-column: 75 ***
  451. *** tab-width: 8 ***
  452. *** c-basic-offset: 8 ***
  453. *** End: ***
  454. */