tm6000-video.c 42 KB

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