tm6000-video.c 41 KB

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