pvrusb2-ioread.c 12 KB

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