tm6000-video.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. /*
  2. * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
  5. *
  6. * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
  7. * - Fixed module load/unload
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation version 2
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/ioport.h>
  26. #include <linux/init.h>
  27. #include <linux/sched.h>
  28. #include <linux/random.h>
  29. #include <linux/usb.h>
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/tuner.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kthread.h>
  36. #include <linux/highmem.h>
  37. #include <linux/freezer.h>
  38. #include "tm6000-regs.h"
  39. #include "tm6000.h"
  40. #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
  41. /* Limits minimum and default number of buffers */
  42. #define TM6000_MIN_BUF 4
  43. #define TM6000_DEF_BUF 8
  44. #define TM6000_NUM_URB_BUF 8
  45. #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
  46. /* Declare static vars that will be used as parameters */
  47. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  48. static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
  49. static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
  50. static bool keep_urb; /* keep urb buffers allocated */
  51. /* Debug level */
  52. int tm6000_debug;
  53. EXPORT_SYMBOL_GPL(tm6000_debug);
  54. static struct tm6000_fmt format[] = {
  55. {
  56. .name = "4:2:2, packed, YVY2",
  57. .fourcc = V4L2_PIX_FMT_YUYV,
  58. .depth = 16,
  59. }, {
  60. .name = "4:2:2, packed, UYVY",
  61. .fourcc = V4L2_PIX_FMT_UYVY,
  62. .depth = 16,
  63. }, {
  64. .name = "A/V + VBI mux packet",
  65. .fourcc = V4L2_PIX_FMT_TM6000,
  66. .depth = 16,
  67. }
  68. };
  69. /* ------------------------------------------------------------------
  70. * DMA and thread functions
  71. * ------------------------------------------------------------------
  72. */
  73. #define norm_maxw(a) 720
  74. #define norm_maxh(a) 576
  75. #define norm_minw(a) norm_maxw(a)
  76. #define norm_minh(a) norm_maxh(a)
  77. /*
  78. * video-buf generic routine to get the next available buffer
  79. */
  80. static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
  81. struct tm6000_buffer **buf)
  82. {
  83. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  84. if (list_empty(&dma_q->active)) {
  85. dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
  86. *buf = NULL;
  87. return;
  88. }
  89. *buf = list_entry(dma_q->active.next,
  90. struct tm6000_buffer, vb.queue);
  91. }
  92. /*
  93. * Announces that a buffer were filled and request the next
  94. */
  95. static inline void buffer_filled(struct tm6000_core *dev,
  96. struct tm6000_dmaqueue *dma_q,
  97. struct tm6000_buffer *buf)
  98. {
  99. /* Advice that buffer was filled */
  100. dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
  101. buf->vb.state = VIDEOBUF_DONE;
  102. buf->vb.field_count++;
  103. v4l2_get_timestamp(&buf->vb.ts);
  104. list_del(&buf->vb.queue);
  105. wake_up(&buf->vb.done);
  106. }
  107. /*
  108. * Identify the tm5600/6000 buffer header type and properly handles
  109. */
  110. static int copy_streams(u8 *data, unsigned long len,
  111. struct urb *urb)
  112. {
  113. struct tm6000_dmaqueue *dma_q = urb->context;
  114. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  115. u8 *ptr = data, *endp = data+len;
  116. unsigned long header = 0;
  117. int rc = 0;
  118. unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
  119. struct tm6000_buffer *vbuf = NULL;
  120. char *voutp = NULL;
  121. unsigned int linewidth;
  122. if (!dev->radio) {
  123. /* get video buffer */
  124. get_next_buf(dma_q, &vbuf);
  125. if (!vbuf)
  126. return rc;
  127. voutp = videobuf_to_vmalloc(&vbuf->vb);
  128. if (!voutp)
  129. return 0;
  130. }
  131. for (ptr = data; ptr < endp;) {
  132. if (!dev->isoc_ctl.cmd) {
  133. /* Header */
  134. if (dev->isoc_ctl.tmp_buf_len > 0) {
  135. /* from last urb or packet */
  136. header = dev->isoc_ctl.tmp_buf;
  137. if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
  138. memcpy((u8 *)&header +
  139. dev->isoc_ctl.tmp_buf_len,
  140. ptr,
  141. 4 - dev->isoc_ctl.tmp_buf_len);
  142. ptr += 4 - dev->isoc_ctl.tmp_buf_len;
  143. }
  144. dev->isoc_ctl.tmp_buf_len = 0;
  145. } else {
  146. if (ptr + 3 >= endp) {
  147. /* have incomplete header */
  148. dev->isoc_ctl.tmp_buf_len = endp - ptr;
  149. memcpy(&dev->isoc_ctl.tmp_buf, ptr,
  150. dev->isoc_ctl.tmp_buf_len);
  151. return rc;
  152. }
  153. /* Seek for sync */
  154. for (; ptr < endp - 3; ptr++) {
  155. if (*(ptr + 3) == 0x47)
  156. break;
  157. }
  158. /* Get message header */
  159. header = *(unsigned long *)ptr;
  160. ptr += 4;
  161. }
  162. /* split the header fields */
  163. size = ((header & 0x7e) << 1);
  164. if (size > 0)
  165. size -= 4;
  166. block = (header >> 7) & 0xf;
  167. field = (header >> 11) & 0x1;
  168. line = (header >> 12) & 0x1ff;
  169. cmd = (header >> 21) & 0x7;
  170. /* Validates haeder fields */
  171. if (size > TM6000_URB_MSG_LEN)
  172. size = TM6000_URB_MSG_LEN;
  173. pktsize = TM6000_URB_MSG_LEN;
  174. /*
  175. * calculate position in buffer and change the buffer
  176. */
  177. switch (cmd) {
  178. case TM6000_URB_MSG_VIDEO:
  179. if (!dev->radio) {
  180. if ((dev->isoc_ctl.vfield != field) &&
  181. (field == 1)) {
  182. /*
  183. * Announces that a new buffer
  184. * were filled
  185. */
  186. buffer_filled(dev, dma_q, vbuf);
  187. dprintk(dev, V4L2_DEBUG_ISOC,
  188. "new buffer filled\n");
  189. get_next_buf(dma_q, &vbuf);
  190. if (!vbuf)
  191. return rc;
  192. voutp = videobuf_to_vmalloc(&vbuf->vb);
  193. if (!voutp)
  194. return rc;
  195. memset(voutp, 0, vbuf->vb.size);
  196. }
  197. linewidth = vbuf->vb.width << 1;
  198. pos = ((line << 1) - field - 1) *
  199. linewidth + block * TM6000_URB_MSG_LEN;
  200. /* Don't allow to write out of the buffer */
  201. if (pos + size > vbuf->vb.size)
  202. cmd = TM6000_URB_MSG_ERR;
  203. dev->isoc_ctl.vfield = field;
  204. }
  205. break;
  206. case TM6000_URB_MSG_VBI:
  207. break;
  208. case TM6000_URB_MSG_AUDIO:
  209. case TM6000_URB_MSG_PTS:
  210. size = pktsize; /* Size is always 180 bytes */
  211. break;
  212. }
  213. } else {
  214. /* Continue the last copy */
  215. cmd = dev->isoc_ctl.cmd;
  216. size = dev->isoc_ctl.size;
  217. pos = dev->isoc_ctl.pos;
  218. pktsize = dev->isoc_ctl.pktsize;
  219. field = dev->isoc_ctl.field;
  220. }
  221. cpysize = (endp - ptr > size) ? size : endp - ptr;
  222. if (cpysize) {
  223. /* copy data in different buffers */
  224. switch (cmd) {
  225. case TM6000_URB_MSG_VIDEO:
  226. /* Fills video buffer */
  227. if (vbuf)
  228. memcpy(&voutp[pos], ptr, cpysize);
  229. break;
  230. case TM6000_URB_MSG_AUDIO: {
  231. int i;
  232. for (i = 0; i < cpysize; i += 2)
  233. swab16s((u16 *)(ptr + i));
  234. tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
  235. break;
  236. }
  237. case TM6000_URB_MSG_VBI:
  238. /* Need some code to copy vbi buffer */
  239. break;
  240. case TM6000_URB_MSG_PTS: {
  241. /* Need some code to copy pts */
  242. u32 pts;
  243. pts = *(u32 *)ptr;
  244. dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
  245. field, pts);
  246. break;
  247. }
  248. }
  249. }
  250. if (ptr + pktsize > endp) {
  251. /*
  252. * End of URB packet, but cmd processing is not
  253. * complete. Preserve the state for a next packet
  254. */
  255. dev->isoc_ctl.pos = pos + cpysize;
  256. dev->isoc_ctl.size = size - cpysize;
  257. dev->isoc_ctl.cmd = cmd;
  258. dev->isoc_ctl.field = field;
  259. dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
  260. ptr += endp - ptr;
  261. } else {
  262. dev->isoc_ctl.cmd = 0;
  263. ptr += pktsize;
  264. }
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Identify the tm5600/6000 buffer header type and properly handles
  270. */
  271. static int copy_multiplexed(u8 *ptr, unsigned long len,
  272. struct urb *urb)
  273. {
  274. struct tm6000_dmaqueue *dma_q = urb->context;
  275. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  276. unsigned int pos = dev->isoc_ctl.pos, cpysize;
  277. int rc = 1;
  278. struct tm6000_buffer *buf;
  279. char *outp = NULL;
  280. get_next_buf(dma_q, &buf);
  281. if (buf)
  282. outp = videobuf_to_vmalloc(&buf->vb);
  283. if (!outp)
  284. return 0;
  285. while (len > 0) {
  286. cpysize = min(len, buf->vb.size-pos);
  287. memcpy(&outp[pos], ptr, cpysize);
  288. pos += cpysize;
  289. ptr += cpysize;
  290. len -= cpysize;
  291. if (pos >= buf->vb.size) {
  292. pos = 0;
  293. /* Announces that a new buffer were filled */
  294. buffer_filled(dev, dma_q, buf);
  295. dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
  296. get_next_buf(dma_q, &buf);
  297. if (!buf)
  298. break;
  299. outp = videobuf_to_vmalloc(&(buf->vb));
  300. if (!outp)
  301. return rc;
  302. pos = 0;
  303. }
  304. }
  305. dev->isoc_ctl.pos = pos;
  306. return rc;
  307. }
  308. static inline void print_err_status(struct tm6000_core *dev,
  309. int packet, int status)
  310. {
  311. char *errmsg = "Unknown";
  312. switch (status) {
  313. case -ENOENT:
  314. errmsg = "unlinked synchronuously";
  315. break;
  316. case -ECONNRESET:
  317. errmsg = "unlinked asynchronuously";
  318. break;
  319. case -ENOSR:
  320. errmsg = "Buffer error (overrun)";
  321. break;
  322. case -EPIPE:
  323. errmsg = "Stalled (device not responding)";
  324. break;
  325. case -EOVERFLOW:
  326. errmsg = "Babble (bad cable?)";
  327. break;
  328. case -EPROTO:
  329. errmsg = "Bit-stuff error (bad cable?)";
  330. break;
  331. case -EILSEQ:
  332. errmsg = "CRC/Timeout (could be anything)";
  333. break;
  334. case -ETIME:
  335. errmsg = "Device does not respond";
  336. break;
  337. }
  338. if (packet < 0) {
  339. dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
  340. status, errmsg);
  341. } else {
  342. dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
  343. packet, status, errmsg);
  344. }
  345. }
  346. /*
  347. * Controls the isoc copy of each urb packet
  348. */
  349. static inline int tm6000_isoc_copy(struct urb *urb)
  350. {
  351. struct tm6000_dmaqueue *dma_q = urb->context;
  352. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  353. int i, len = 0, rc = 1, status;
  354. char *p;
  355. if (urb->status < 0) {
  356. print_err_status(dev, -1, urb->status);
  357. return 0;
  358. }
  359. for (i = 0; i < urb->number_of_packets; i++) {
  360. status = urb->iso_frame_desc[i].status;
  361. if (status < 0) {
  362. print_err_status(dev, i, status);
  363. continue;
  364. }
  365. len = urb->iso_frame_desc[i].actual_length;
  366. if (len > 0) {
  367. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  368. if (!urb->iso_frame_desc[i].status) {
  369. if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
  370. rc = copy_multiplexed(p, len, urb);
  371. if (rc <= 0)
  372. return rc;
  373. } else {
  374. copy_streams(p, len, urb);
  375. }
  376. }
  377. }
  378. }
  379. return rc;
  380. }
  381. /* ------------------------------------------------------------------
  382. * URB control
  383. * ------------------------------------------------------------------
  384. */
  385. /*
  386. * IRQ callback, called by URB callback
  387. */
  388. static void tm6000_irq_callback(struct urb *urb)
  389. {
  390. struct tm6000_dmaqueue *dma_q = urb->context;
  391. struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  392. int i;
  393. switch (urb->status) {
  394. case 0:
  395. case -ETIMEDOUT:
  396. break;
  397. case -ECONNRESET:
  398. case -ENOENT:
  399. case -ESHUTDOWN:
  400. return;
  401. default:
  402. tm6000_err("urb completion error %d.\n", urb->status);
  403. break;
  404. }
  405. spin_lock(&dev->slock);
  406. tm6000_isoc_copy(urb);
  407. spin_unlock(&dev->slock);
  408. /* Reset urb buffers */
  409. for (i = 0; i < urb->number_of_packets; i++) {
  410. urb->iso_frame_desc[i].status = 0;
  411. urb->iso_frame_desc[i].actual_length = 0;
  412. }
  413. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  414. if (urb->status)
  415. tm6000_err("urb resubmit failed (error=%i)\n",
  416. urb->status);
  417. }
  418. /*
  419. * Allocate URB buffers
  420. */
  421. static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
  422. {
  423. int num_bufs = TM6000_NUM_URB_BUF;
  424. int i;
  425. if (dev->urb_buffer != NULL)
  426. return 0;
  427. dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  428. if (!dev->urb_buffer) {
  429. tm6000_err("cannot allocate memory for urb buffers\n");
  430. return -ENOMEM;
  431. }
  432. dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
  433. if (!dev->urb_dma) {
  434. tm6000_err("cannot allocate memory for urb dma pointers\n");
  435. return -ENOMEM;
  436. }
  437. for (i = 0; i < num_bufs; i++) {
  438. dev->urb_buffer[i] = usb_alloc_coherent(
  439. dev->udev, dev->urb_size,
  440. GFP_KERNEL, &dev->urb_dma[i]);
  441. if (!dev->urb_buffer[i]) {
  442. tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
  443. dev->urb_size, i);
  444. return -ENOMEM;
  445. }
  446. memset(dev->urb_buffer[i], 0, dev->urb_size);
  447. }
  448. return 0;
  449. }
  450. /*
  451. * Free URB buffers
  452. */
  453. static int tm6000_free_urb_buffers(struct tm6000_core *dev)
  454. {
  455. int i;
  456. if (dev->urb_buffer == NULL)
  457. return 0;
  458. for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
  459. if (dev->urb_buffer[i]) {
  460. usb_free_coherent(dev->udev,
  461. dev->urb_size,
  462. dev->urb_buffer[i],
  463. dev->urb_dma[i]);
  464. dev->urb_buffer[i] = NULL;
  465. }
  466. }
  467. kfree(dev->urb_buffer);
  468. kfree(dev->urb_dma);
  469. dev->urb_buffer = NULL;
  470. dev->urb_dma = NULL;
  471. return 0;
  472. }
  473. /*
  474. * Stop and Deallocate URBs
  475. */
  476. static void tm6000_uninit_isoc(struct tm6000_core *dev)
  477. {
  478. struct urb *urb;
  479. int i;
  480. dev->isoc_ctl.buf = NULL;
  481. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  482. urb = dev->isoc_ctl.urb[i];
  483. if (urb) {
  484. usb_kill_urb(urb);
  485. usb_unlink_urb(urb);
  486. usb_free_urb(urb);
  487. dev->isoc_ctl.urb[i] = NULL;
  488. }
  489. dev->isoc_ctl.transfer_buffer[i] = NULL;
  490. }
  491. if (!keep_urb)
  492. tm6000_free_urb_buffers(dev);
  493. kfree(dev->isoc_ctl.urb);
  494. kfree(dev->isoc_ctl.transfer_buffer);
  495. dev->isoc_ctl.urb = NULL;
  496. dev->isoc_ctl.transfer_buffer = NULL;
  497. dev->isoc_ctl.num_bufs = 0;
  498. }
  499. /*
  500. * Assign URBs and start IRQ
  501. */
  502. static int tm6000_prepare_isoc(struct tm6000_core *dev)
  503. {
  504. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  505. int i, j, sb_size, pipe, size, max_packets;
  506. int num_bufs = TM6000_NUM_URB_BUF;
  507. struct urb *urb;
  508. /* De-allocates all pending stuff */
  509. tm6000_uninit_isoc(dev);
  510. /* Stop interrupt USB pipe */
  511. tm6000_ir_int_stop(dev);
  512. usb_set_interface(dev->udev,
  513. dev->isoc_in.bInterfaceNumber,
  514. dev->isoc_in.bAlternateSetting);
  515. /* Start interrupt USB pipe */
  516. tm6000_ir_int_start(dev);
  517. pipe = usb_rcvisocpipe(dev->udev,
  518. dev->isoc_in.endp->desc.bEndpointAddress &
  519. USB_ENDPOINT_NUMBER_MASK);
  520. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  521. if (size > dev->isoc_in.maxsize)
  522. size = dev->isoc_in.maxsize;
  523. dev->isoc_ctl.max_pkt_size = size;
  524. max_packets = TM6000_MAX_ISO_PACKETS;
  525. sb_size = max_packets * size;
  526. dev->urb_size = sb_size;
  527. dev->isoc_ctl.num_bufs = num_bufs;
  528. dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  529. if (!dev->isoc_ctl.urb) {
  530. tm6000_err("cannot alloc memory for usb buffers\n");
  531. return -ENOMEM;
  532. }
  533. dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
  534. GFP_KERNEL);
  535. if (!dev->isoc_ctl.transfer_buffer) {
  536. tm6000_err("cannot allocate memory for usbtransfer\n");
  537. kfree(dev->isoc_ctl.urb);
  538. return -ENOMEM;
  539. }
  540. dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
  541. max_packets, num_bufs, sb_size,
  542. dev->isoc_in.maxsize, size);
  543. if (tm6000_alloc_urb_buffers(dev) < 0) {
  544. tm6000_err("cannot allocate memory for urb buffers\n");
  545. /* call free, as some buffers might have been allocated */
  546. tm6000_free_urb_buffers(dev);
  547. kfree(dev->isoc_ctl.urb);
  548. kfree(dev->isoc_ctl.transfer_buffer);
  549. return -ENOMEM;
  550. }
  551. /* allocate urbs and transfer buffers */
  552. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  553. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  554. if (!urb) {
  555. tm6000_uninit_isoc(dev);
  556. tm6000_free_urb_buffers(dev);
  557. return -ENOMEM;
  558. }
  559. dev->isoc_ctl.urb[i] = urb;
  560. urb->transfer_dma = dev->urb_dma[i];
  561. dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
  562. usb_fill_bulk_urb(urb, dev->udev, pipe,
  563. dev->isoc_ctl.transfer_buffer[i], sb_size,
  564. tm6000_irq_callback, dma_q);
  565. urb->interval = dev->isoc_in.endp->desc.bInterval;
  566. urb->number_of_packets = max_packets;
  567. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  568. for (j = 0; j < max_packets; j++) {
  569. urb->iso_frame_desc[j].offset = size * j;
  570. urb->iso_frame_desc[j].length = size;
  571. }
  572. }
  573. return 0;
  574. }
  575. static int tm6000_start_thread(struct tm6000_core *dev)
  576. {
  577. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  578. int i;
  579. dma_q->frame = 0;
  580. dma_q->ini_jiffies = jiffies;
  581. init_waitqueue_head(&dma_q->wq);
  582. /* submit urbs and enables IRQ */
  583. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  584. int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  585. if (rc) {
  586. tm6000_err("submit of urb %i failed (error=%i)\n", i,
  587. rc);
  588. tm6000_uninit_isoc(dev);
  589. return rc;
  590. }
  591. }
  592. return 0;
  593. }
  594. /* ------------------------------------------------------------------
  595. * Videobuf operations
  596. * ------------------------------------------------------------------
  597. */
  598. static int
  599. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  600. {
  601. struct tm6000_fh *fh = vq->priv_data;
  602. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  603. if (0 == *count)
  604. *count = TM6000_DEF_BUF;
  605. if (*count < TM6000_MIN_BUF)
  606. *count = TM6000_MIN_BUF;
  607. while (*size * *count > vid_limit * 1024 * 1024)
  608. (*count)--;
  609. return 0;
  610. }
  611. static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
  612. {
  613. struct tm6000_fh *fh = vq->priv_data;
  614. struct tm6000_core *dev = fh->dev;
  615. unsigned long flags;
  616. BUG_ON(in_interrupt());
  617. /* We used to wait for the buffer to finish here, but this didn't work
  618. because, as we were keeping the state as VIDEOBUF_QUEUED,
  619. videobuf_queue_cancel marked it as finished for us.
  620. (Also, it could wedge forever if the hardware was misconfigured.)
  621. This should be safe; by the time we get here, the buffer isn't
  622. queued anymore. If we ever start marking the buffers as
  623. VIDEOBUF_ACTIVE, it won't be, though.
  624. */
  625. spin_lock_irqsave(&dev->slock, flags);
  626. if (dev->isoc_ctl.buf == buf)
  627. dev->isoc_ctl.buf = NULL;
  628. spin_unlock_irqrestore(&dev->slock, flags);
  629. videobuf_vmalloc_free(&buf->vb);
  630. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  631. }
  632. static int
  633. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  634. enum v4l2_field field)
  635. {
  636. struct tm6000_fh *fh = vq->priv_data;
  637. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  638. struct tm6000_core *dev = fh->dev;
  639. int rc = 0;
  640. BUG_ON(NULL == fh->fmt);
  641. /* FIXME: It assumes depth=2 */
  642. /* The only currently supported format is 16 bits/pixel */
  643. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  644. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  645. return -EINVAL;
  646. if (buf->fmt != fh->fmt ||
  647. buf->vb.width != fh->width ||
  648. buf->vb.height != fh->height ||
  649. buf->vb.field != field) {
  650. buf->fmt = fh->fmt;
  651. buf->vb.width = fh->width;
  652. buf->vb.height = fh->height;
  653. buf->vb.field = field;
  654. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  655. }
  656. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  657. rc = videobuf_iolock(vq, &buf->vb, NULL);
  658. if (rc != 0)
  659. goto fail;
  660. }
  661. if (!dev->isoc_ctl.num_bufs) {
  662. rc = tm6000_prepare_isoc(dev);
  663. if (rc < 0)
  664. goto fail;
  665. rc = tm6000_start_thread(dev);
  666. if (rc < 0)
  667. goto fail;
  668. }
  669. buf->vb.state = VIDEOBUF_PREPARED;
  670. return 0;
  671. fail:
  672. free_buffer(vq, buf);
  673. return rc;
  674. }
  675. static void
  676. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  677. {
  678. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  679. struct tm6000_fh *fh = vq->priv_data;
  680. struct tm6000_core *dev = fh->dev;
  681. struct tm6000_dmaqueue *vidq = &dev->vidq;
  682. buf->vb.state = VIDEOBUF_QUEUED;
  683. list_add_tail(&buf->vb.queue, &vidq->active);
  684. }
  685. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  686. {
  687. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  688. free_buffer(vq, buf);
  689. }
  690. static struct videobuf_queue_ops tm6000_video_qops = {
  691. .buf_setup = buffer_setup,
  692. .buf_prepare = buffer_prepare,
  693. .buf_queue = buffer_queue,
  694. .buf_release = buffer_release,
  695. };
  696. /* ------------------------------------------------------------------
  697. * IOCTL handling
  698. * ------------------------------------------------------------------
  699. */
  700. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  701. {
  702. /* Is the current fh handling it? if so, that's OK */
  703. if (dev->resources == fh && dev->is_res_read)
  704. return true;
  705. return false;
  706. }
  707. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  708. {
  709. /* Is the current fh handling it? if so, that's OK */
  710. if (dev->resources == fh)
  711. return true;
  712. return false;
  713. }
  714. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  715. bool is_res_read)
  716. {
  717. /* Is the current fh handling it? if so, that's OK */
  718. if (dev->resources == fh && dev->is_res_read == is_res_read)
  719. return true;
  720. /* is it free? */
  721. if (dev->resources)
  722. return false;
  723. /* grab it */
  724. dev->resources = fh;
  725. dev->is_res_read = is_res_read;
  726. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  727. return true;
  728. }
  729. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  730. {
  731. /* Is the current fh handling it? if so, that's OK */
  732. if (dev->resources != fh)
  733. return;
  734. dev->resources = NULL;
  735. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  736. }
  737. /* ------------------------------------------------------------------
  738. * IOCTL vidioc handling
  739. * ------------------------------------------------------------------
  740. */
  741. static int vidioc_querycap(struct file *file, void *priv,
  742. struct v4l2_capability *cap)
  743. {
  744. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  745. struct video_device *vdev = video_devdata(file);
  746. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  747. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  748. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  749. if (dev->tuner_type != TUNER_ABSENT)
  750. cap->device_caps |= V4L2_CAP_TUNER;
  751. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  752. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  753. V4L2_CAP_STREAMING |
  754. V4L2_CAP_READWRITE;
  755. else
  756. cap->device_caps |= V4L2_CAP_RADIO;
  757. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  758. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  759. return 0;
  760. }
  761. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  762. struct v4l2_fmtdesc *f)
  763. {
  764. if (f->index >= ARRAY_SIZE(format))
  765. return -EINVAL;
  766. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  767. f->pixelformat = format[f->index].fourcc;
  768. return 0;
  769. }
  770. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  771. struct v4l2_format *f)
  772. {
  773. struct tm6000_fh *fh = priv;
  774. f->fmt.pix.width = fh->width;
  775. f->fmt.pix.height = fh->height;
  776. f->fmt.pix.field = fh->vb_vidq.field;
  777. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  778. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  779. f->fmt.pix.bytesperline =
  780. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  781. f->fmt.pix.sizeimage =
  782. f->fmt.pix.height * f->fmt.pix.bytesperline;
  783. return 0;
  784. }
  785. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  786. {
  787. unsigned int i;
  788. for (i = 0; i < ARRAY_SIZE(format); i++)
  789. if (format[i].fourcc == fourcc)
  790. return format+i;
  791. return NULL;
  792. }
  793. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  794. struct v4l2_format *f)
  795. {
  796. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  797. struct tm6000_fmt *fmt;
  798. enum v4l2_field field;
  799. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  800. if (NULL == fmt) {
  801. dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
  802. f->fmt.pix.pixelformat);
  803. return -EINVAL;
  804. }
  805. field = f->fmt.pix.field;
  806. field = V4L2_FIELD_INTERLACED;
  807. tm6000_get_std_res(dev);
  808. f->fmt.pix.width = dev->width;
  809. f->fmt.pix.height = dev->height;
  810. f->fmt.pix.width &= ~0x01;
  811. f->fmt.pix.field = field;
  812. f->fmt.pix.bytesperline =
  813. (f->fmt.pix.width * fmt->depth) >> 3;
  814. f->fmt.pix.sizeimage =
  815. f->fmt.pix.height * f->fmt.pix.bytesperline;
  816. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  817. return 0;
  818. }
  819. /*FIXME: This seems to be generic enough to be at videodev2 */
  820. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  821. struct v4l2_format *f)
  822. {
  823. struct tm6000_fh *fh = priv;
  824. struct tm6000_core *dev = fh->dev;
  825. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  826. if (ret < 0)
  827. return ret;
  828. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  829. fh->width = f->fmt.pix.width;
  830. fh->height = f->fmt.pix.height;
  831. fh->vb_vidq.field = f->fmt.pix.field;
  832. fh->type = f->type;
  833. dev->fourcc = f->fmt.pix.pixelformat;
  834. tm6000_set_fourcc_format(dev);
  835. return 0;
  836. }
  837. static int vidioc_reqbufs(struct file *file, void *priv,
  838. struct v4l2_requestbuffers *p)
  839. {
  840. struct tm6000_fh *fh = priv;
  841. return videobuf_reqbufs(&fh->vb_vidq, p);
  842. }
  843. static int vidioc_querybuf(struct file *file, void *priv,
  844. struct v4l2_buffer *p)
  845. {
  846. struct tm6000_fh *fh = priv;
  847. return videobuf_querybuf(&fh->vb_vidq, p);
  848. }
  849. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  850. {
  851. struct tm6000_fh *fh = priv;
  852. return videobuf_qbuf(&fh->vb_vidq, p);
  853. }
  854. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  855. {
  856. struct tm6000_fh *fh = priv;
  857. return videobuf_dqbuf(&fh->vb_vidq, p,
  858. file->f_flags & O_NONBLOCK);
  859. }
  860. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  861. {
  862. struct tm6000_fh *fh = priv;
  863. struct tm6000_core *dev = fh->dev;
  864. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  865. return -EINVAL;
  866. if (i != fh->type)
  867. return -EINVAL;
  868. if (!res_get(dev, fh, false))
  869. return -EBUSY;
  870. return videobuf_streamon(&fh->vb_vidq);
  871. }
  872. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  873. {
  874. struct tm6000_fh *fh = priv;
  875. struct tm6000_core *dev = fh->dev;
  876. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  877. return -EINVAL;
  878. if (i != fh->type)
  879. return -EINVAL;
  880. videobuf_streamoff(&fh->vb_vidq);
  881. res_free(dev, fh);
  882. return 0;
  883. }
  884. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  885. {
  886. int rc = 0;
  887. struct tm6000_fh *fh = priv;
  888. struct tm6000_core *dev = fh->dev;
  889. dev->norm = norm;
  890. rc = tm6000_init_analog_mode(dev);
  891. fh->width = dev->width;
  892. fh->height = dev->height;
  893. if (rc < 0)
  894. return rc;
  895. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
  896. return 0;
  897. }
  898. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  899. {
  900. struct tm6000_fh *fh = priv;
  901. struct tm6000_core *dev = fh->dev;
  902. *norm = dev->norm;
  903. return 0;
  904. }
  905. static const char *iname[] = {
  906. [TM6000_INPUT_TV] = "Television",
  907. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  908. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  909. [TM6000_INPUT_SVIDEO] = "S-Video",
  910. };
  911. static int vidioc_enum_input(struct file *file, void *priv,
  912. struct v4l2_input *i)
  913. {
  914. struct tm6000_fh *fh = priv;
  915. struct tm6000_core *dev = fh->dev;
  916. unsigned int n;
  917. n = i->index;
  918. if (n >= 3)
  919. return -EINVAL;
  920. if (!dev->vinput[n].type)
  921. return -EINVAL;
  922. i->index = n;
  923. if (dev->vinput[n].type == TM6000_INPUT_TV)
  924. i->type = V4L2_INPUT_TYPE_TUNER;
  925. else
  926. i->type = V4L2_INPUT_TYPE_CAMERA;
  927. strcpy(i->name, iname[dev->vinput[n].type]);
  928. i->std = TM6000_STD;
  929. return 0;
  930. }
  931. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  932. {
  933. struct tm6000_fh *fh = priv;
  934. struct tm6000_core *dev = fh->dev;
  935. *i = dev->input;
  936. return 0;
  937. }
  938. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  939. {
  940. struct tm6000_fh *fh = priv;
  941. struct tm6000_core *dev = fh->dev;
  942. int rc = 0;
  943. if (i >= 3)
  944. return -EINVAL;
  945. if (!dev->vinput[i].type)
  946. return -EINVAL;
  947. dev->input = i;
  948. rc = vidioc_s_std(file, priv, dev->norm);
  949. return rc;
  950. }
  951. /* --- controls ---------------------------------------------- */
  952. static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
  953. {
  954. struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
  955. u8 val = ctrl->val;
  956. switch (ctrl->id) {
  957. case V4L2_CID_CONTRAST:
  958. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  959. return 0;
  960. case V4L2_CID_BRIGHTNESS:
  961. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  962. return 0;
  963. case V4L2_CID_SATURATION:
  964. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  965. return 0;
  966. case V4L2_CID_HUE:
  967. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  968. return 0;
  969. }
  970. return -EINVAL;
  971. }
  972. static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
  973. .s_ctrl = tm6000_s_ctrl,
  974. };
  975. static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  976. {
  977. struct tm6000_core *dev = container_of(ctrl->handler,
  978. struct tm6000_core, radio_ctrl_handler);
  979. u8 val = ctrl->val;
  980. switch (ctrl->id) {
  981. case V4L2_CID_AUDIO_MUTE:
  982. dev->ctl_mute = val;
  983. tm6000_tvaudio_set_mute(dev, val);
  984. return 0;
  985. case V4L2_CID_AUDIO_VOLUME:
  986. dev->ctl_volume = val;
  987. tm6000_set_volume(dev, val);
  988. return 0;
  989. }
  990. return -EINVAL;
  991. }
  992. static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
  993. .s_ctrl = tm6000_radio_s_ctrl,
  994. };
  995. static int vidioc_g_tuner(struct file *file, void *priv,
  996. struct v4l2_tuner *t)
  997. {
  998. struct tm6000_fh *fh = priv;
  999. struct tm6000_core *dev = fh->dev;
  1000. if (UNSET == dev->tuner_type)
  1001. return -ENOTTY;
  1002. if (0 != t->index)
  1003. return -EINVAL;
  1004. strcpy(t->name, "Television");
  1005. t->type = V4L2_TUNER_ANALOG_TV;
  1006. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  1007. t->rangehigh = 0xffffffffUL;
  1008. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1009. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1010. t->audmode = dev->amode;
  1011. return 0;
  1012. }
  1013. static int vidioc_s_tuner(struct file *file, void *priv,
  1014. const struct v4l2_tuner *t)
  1015. {
  1016. struct tm6000_fh *fh = priv;
  1017. struct tm6000_core *dev = fh->dev;
  1018. if (UNSET == dev->tuner_type)
  1019. return -ENOTTY;
  1020. if (0 != t->index)
  1021. return -EINVAL;
  1022. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1023. dev->amode = V4L2_TUNER_MODE_STEREO;
  1024. else
  1025. dev->amode = t->audmode;
  1026. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1027. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1028. return 0;
  1029. }
  1030. static int vidioc_g_frequency(struct file *file, void *priv,
  1031. struct v4l2_frequency *f)
  1032. {
  1033. struct tm6000_fh *fh = priv;
  1034. struct tm6000_core *dev = fh->dev;
  1035. if (UNSET == dev->tuner_type)
  1036. return -ENOTTY;
  1037. if (f->tuner)
  1038. return -EINVAL;
  1039. f->frequency = dev->freq;
  1040. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1041. return 0;
  1042. }
  1043. static int vidioc_s_frequency(struct file *file, void *priv,
  1044. const struct v4l2_frequency *f)
  1045. {
  1046. struct tm6000_fh *fh = priv;
  1047. struct tm6000_core *dev = fh->dev;
  1048. if (UNSET == dev->tuner_type)
  1049. return -ENOTTY;
  1050. if (f->tuner != 0)
  1051. return -EINVAL;
  1052. dev->freq = f->frequency;
  1053. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1054. return 0;
  1055. }
  1056. static int radio_g_tuner(struct file *file, void *priv,
  1057. struct v4l2_tuner *t)
  1058. {
  1059. struct tm6000_fh *fh = file->private_data;
  1060. struct tm6000_core *dev = fh->dev;
  1061. if (0 != t->index)
  1062. return -EINVAL;
  1063. memset(t, 0, sizeof(*t));
  1064. strcpy(t->name, "Radio");
  1065. t->type = V4L2_TUNER_RADIO;
  1066. t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1067. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1068. t->audmode = V4L2_TUNER_MODE_STEREO;
  1069. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1070. return 0;
  1071. }
  1072. static int radio_s_tuner(struct file *file, void *priv,
  1073. const struct v4l2_tuner *t)
  1074. {
  1075. struct tm6000_fh *fh = file->private_data;
  1076. struct tm6000_core *dev = fh->dev;
  1077. if (0 != t->index)
  1078. return -EINVAL;
  1079. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1080. return 0;
  1081. }
  1082. /* ------------------------------------------------------------------
  1083. File operations for the device
  1084. ------------------------------------------------------------------*/
  1085. static int __tm6000_open(struct file *file)
  1086. {
  1087. struct video_device *vdev = video_devdata(file);
  1088. struct tm6000_core *dev = video_drvdata(file);
  1089. struct tm6000_fh *fh;
  1090. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1091. int rc;
  1092. int radio = 0;
  1093. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1094. video_device_node_name(vdev));
  1095. switch (vdev->vfl_type) {
  1096. case VFL_TYPE_GRABBER:
  1097. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1098. break;
  1099. case VFL_TYPE_VBI:
  1100. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1101. break;
  1102. case VFL_TYPE_RADIO:
  1103. radio = 1;
  1104. break;
  1105. }
  1106. /* If more than one user, mutex should be added */
  1107. dev->users++;
  1108. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1109. video_device_node_name(vdev), v4l2_type_names[type],
  1110. dev->users);
  1111. /* allocate + initialize per filehandle data */
  1112. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1113. if (NULL == fh) {
  1114. dev->users--;
  1115. return -ENOMEM;
  1116. }
  1117. v4l2_fh_init(&fh->fh, vdev);
  1118. file->private_data = fh;
  1119. fh->dev = dev;
  1120. fh->radio = radio;
  1121. dev->radio = radio;
  1122. fh->type = type;
  1123. dev->fourcc = format[0].fourcc;
  1124. fh->fmt = format_by_fourcc(dev->fourcc);
  1125. tm6000_get_std_res(dev);
  1126. fh->width = dev->width;
  1127. fh->height = dev->height;
  1128. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
  1129. (unsigned long)fh, (unsigned long)dev,
  1130. (unsigned long)&dev->vidq);
  1131. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
  1132. list_empty(&dev->vidq.queued));
  1133. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
  1134. list_empty(&dev->vidq.active));
  1135. /* initialize hardware on analog mode */
  1136. rc = tm6000_init_analog_mode(dev);
  1137. if (rc < 0) {
  1138. v4l2_fh_exit(&fh->fh);
  1139. kfree(fh);
  1140. return rc;
  1141. }
  1142. dev->mode = TM6000_MODE_ANALOG;
  1143. if (!fh->radio) {
  1144. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1145. NULL, &dev->slock,
  1146. fh->type,
  1147. V4L2_FIELD_INTERLACED,
  1148. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1149. } else {
  1150. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1151. tm6000_set_audio_rinput(dev);
  1152. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1153. tm6000_prepare_isoc(dev);
  1154. tm6000_start_thread(dev);
  1155. }
  1156. v4l2_fh_add(&fh->fh);
  1157. return 0;
  1158. }
  1159. static int tm6000_open(struct file *file)
  1160. {
  1161. struct video_device *vdev = video_devdata(file);
  1162. int res;
  1163. mutex_lock(vdev->lock);
  1164. res = __tm6000_open(file);
  1165. mutex_unlock(vdev->lock);
  1166. return res;
  1167. }
  1168. static ssize_t
  1169. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1170. {
  1171. struct tm6000_fh *fh = file->private_data;
  1172. struct tm6000_core *dev = fh->dev;
  1173. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1174. int res;
  1175. if (!res_get(fh->dev, fh, true))
  1176. return -EBUSY;
  1177. if (mutex_lock_interruptible(&dev->lock))
  1178. return -ERESTARTSYS;
  1179. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1180. file->f_flags & O_NONBLOCK);
  1181. mutex_unlock(&dev->lock);
  1182. return res;
  1183. }
  1184. return 0;
  1185. }
  1186. static unsigned int
  1187. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1188. {
  1189. unsigned long req_events = poll_requested_events(wait);
  1190. struct tm6000_fh *fh = file->private_data;
  1191. struct tm6000_buffer *buf;
  1192. int res = 0;
  1193. if (v4l2_event_pending(&fh->fh))
  1194. res = POLLPRI;
  1195. else if (req_events & POLLPRI)
  1196. poll_wait(file, &fh->fh.wait, wait);
  1197. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1198. return res | POLLERR;
  1199. if (!!is_res_streaming(fh->dev, fh))
  1200. return res | POLLERR;
  1201. if (!is_res_read(fh->dev, fh)) {
  1202. /* streaming capture */
  1203. if (list_empty(&fh->vb_vidq.stream))
  1204. return res | POLLERR;
  1205. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1206. poll_wait(file, &buf->vb.done, wait);
  1207. if (buf->vb.state == VIDEOBUF_DONE ||
  1208. buf->vb.state == VIDEOBUF_ERROR)
  1209. return res | POLLIN | POLLRDNORM;
  1210. } else if (req_events & (POLLIN | POLLRDNORM)) {
  1211. /* read() capture */
  1212. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1213. }
  1214. return res;
  1215. }
  1216. static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1217. {
  1218. struct tm6000_fh *fh = file->private_data;
  1219. struct tm6000_core *dev = fh->dev;
  1220. unsigned int res;
  1221. mutex_lock(&dev->lock);
  1222. res = __tm6000_poll(file, wait);
  1223. mutex_unlock(&dev->lock);
  1224. return res;
  1225. }
  1226. static int tm6000_release(struct file *file)
  1227. {
  1228. struct tm6000_fh *fh = file->private_data;
  1229. struct tm6000_core *dev = fh->dev;
  1230. struct video_device *vdev = video_devdata(file);
  1231. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1232. video_device_node_name(vdev), dev->users);
  1233. mutex_lock(&dev->lock);
  1234. dev->users--;
  1235. res_free(dev, fh);
  1236. if (!dev->users) {
  1237. tm6000_uninit_isoc(dev);
  1238. /* Stop interrupt USB pipe */
  1239. tm6000_ir_int_stop(dev);
  1240. usb_reset_configuration(dev->udev);
  1241. if (dev->int_in.endp)
  1242. usb_set_interface(dev->udev,
  1243. dev->isoc_in.bInterfaceNumber, 2);
  1244. else
  1245. usb_set_interface(dev->udev,
  1246. dev->isoc_in.bInterfaceNumber, 0);
  1247. /* Start interrupt USB pipe */
  1248. tm6000_ir_int_start(dev);
  1249. if (!fh->radio)
  1250. videobuf_mmap_free(&fh->vb_vidq);
  1251. }
  1252. v4l2_fh_del(&fh->fh);
  1253. v4l2_fh_exit(&fh->fh);
  1254. kfree(fh);
  1255. mutex_unlock(&dev->lock);
  1256. return 0;
  1257. }
  1258. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1259. {
  1260. struct tm6000_fh *fh = file->private_data;
  1261. struct tm6000_core *dev = fh->dev;
  1262. int res;
  1263. if (mutex_lock_interruptible(&dev->lock))
  1264. return -ERESTARTSYS;
  1265. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1266. mutex_unlock(&dev->lock);
  1267. return res;
  1268. }
  1269. static struct v4l2_file_operations tm6000_fops = {
  1270. .owner = THIS_MODULE,
  1271. .open = tm6000_open,
  1272. .release = tm6000_release,
  1273. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1274. .read = tm6000_read,
  1275. .poll = tm6000_poll,
  1276. .mmap = tm6000_mmap,
  1277. };
  1278. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1279. .vidioc_querycap = vidioc_querycap,
  1280. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1281. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1282. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1283. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1284. .vidioc_s_std = vidioc_s_std,
  1285. .vidioc_g_std = vidioc_g_std,
  1286. .vidioc_enum_input = vidioc_enum_input,
  1287. .vidioc_g_input = vidioc_g_input,
  1288. .vidioc_s_input = vidioc_s_input,
  1289. .vidioc_g_tuner = vidioc_g_tuner,
  1290. .vidioc_s_tuner = vidioc_s_tuner,
  1291. .vidioc_g_frequency = vidioc_g_frequency,
  1292. .vidioc_s_frequency = vidioc_s_frequency,
  1293. .vidioc_streamon = vidioc_streamon,
  1294. .vidioc_streamoff = vidioc_streamoff,
  1295. .vidioc_reqbufs = vidioc_reqbufs,
  1296. .vidioc_querybuf = vidioc_querybuf,
  1297. .vidioc_qbuf = vidioc_qbuf,
  1298. .vidioc_dqbuf = vidioc_dqbuf,
  1299. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1300. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1301. };
  1302. static struct video_device tm6000_template = {
  1303. .name = "tm6000",
  1304. .fops = &tm6000_fops,
  1305. .ioctl_ops = &video_ioctl_ops,
  1306. .release = video_device_release_empty,
  1307. .tvnorms = TM6000_STD,
  1308. };
  1309. static const struct v4l2_file_operations radio_fops = {
  1310. .owner = THIS_MODULE,
  1311. .open = tm6000_open,
  1312. .poll = v4l2_ctrl_poll,
  1313. .release = tm6000_release,
  1314. .unlocked_ioctl = video_ioctl2,
  1315. };
  1316. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1317. .vidioc_querycap = vidioc_querycap,
  1318. .vidioc_g_tuner = radio_g_tuner,
  1319. .vidioc_s_tuner = radio_s_tuner,
  1320. .vidioc_g_frequency = vidioc_g_frequency,
  1321. .vidioc_s_frequency = vidioc_s_frequency,
  1322. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1323. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1324. };
  1325. static struct video_device tm6000_radio_template = {
  1326. .name = "tm6000",
  1327. .fops = &radio_fops,
  1328. .ioctl_ops = &radio_ioctl_ops,
  1329. };
  1330. /* -----------------------------------------------------------------
  1331. * Initialization and module stuff
  1332. * ------------------------------------------------------------------
  1333. */
  1334. static void vdev_init(struct tm6000_core *dev,
  1335. struct video_device *vfd,
  1336. const struct video_device
  1337. *template, const char *type_name)
  1338. {
  1339. *vfd = *template;
  1340. vfd->v4l2_dev = &dev->v4l2_dev;
  1341. vfd->release = video_device_release_empty;
  1342. vfd->lock = &dev->lock;
  1343. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1344. video_set_drvdata(vfd, dev);
  1345. }
  1346. int tm6000_v4l2_register(struct tm6000_core *dev)
  1347. {
  1348. int ret = 0;
  1349. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1350. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1351. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1352. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1353. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1354. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1355. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1356. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1357. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1358. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1359. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1360. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1361. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1362. V4L2_CID_HUE, -128, 127, 1, 0);
  1363. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1364. &dev->radio_ctrl_handler, NULL);
  1365. if (dev->radio_ctrl_handler.error)
  1366. ret = dev->radio_ctrl_handler.error;
  1367. if (!ret && dev->ctrl_handler.error)
  1368. ret = dev->ctrl_handler.error;
  1369. if (ret)
  1370. goto free_ctrl;
  1371. vdev_init(dev, &dev->vfd, &tm6000_template, "video");
  1372. dev->vfd.ctrl_handler = &dev->ctrl_handler;
  1373. /* init video dma queues */
  1374. INIT_LIST_HEAD(&dev->vidq.active);
  1375. INIT_LIST_HEAD(&dev->vidq.queued);
  1376. ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1377. if (ret < 0) {
  1378. printk(KERN_INFO "%s: can't register video device\n",
  1379. dev->name);
  1380. goto free_ctrl;
  1381. }
  1382. printk(KERN_INFO "%s: registered device %s\n",
  1383. dev->name, video_device_node_name(&dev->vfd));
  1384. if (dev->caps.has_radio) {
  1385. vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
  1386. "radio");
  1387. dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
  1388. ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1389. radio_nr);
  1390. if (ret < 0) {
  1391. printk(KERN_INFO "%s: can't register radio device\n",
  1392. dev->name);
  1393. goto unreg_video;
  1394. }
  1395. printk(KERN_INFO "%s: registered device %s\n",
  1396. dev->name, video_device_node_name(&dev->radio_dev));
  1397. }
  1398. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1399. return ret;
  1400. unreg_video:
  1401. video_unregister_device(&dev->vfd);
  1402. free_ctrl:
  1403. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1404. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1405. return ret;
  1406. }
  1407. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1408. {
  1409. video_unregister_device(&dev->vfd);
  1410. /* if URB buffers are still allocated free them now */
  1411. tm6000_free_urb_buffers(dev);
  1412. video_unregister_device(&dev->radio_dev);
  1413. return 0;
  1414. }
  1415. int tm6000_v4l2_exit(void)
  1416. {
  1417. return 0;
  1418. }
  1419. module_param(video_nr, int, 0);
  1420. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1421. module_param_named(debug, tm6000_debug, int, 0444);
  1422. MODULE_PARM_DESC(debug, "activates debug info");
  1423. module_param(vid_limit, int, 0644);
  1424. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1425. module_param(keep_urb, bool, 0);
  1426. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");