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(void *), GFP_KERNEL);
  418. if (!dev->urb_buffer)
  419. return -ENOMEM;
  420. dev->urb_dma = kmalloc_array(num_bufs, sizeof(dma_addr_t *),
  421. GFP_KERNEL);
  422. if (!dev->urb_dma)
  423. return -ENOMEM;
  424. for (i = 0; i < num_bufs; i++) {
  425. dev->urb_buffer[i] = usb_alloc_coherent(
  426. dev->udev, dev->urb_size,
  427. GFP_KERNEL, &dev->urb_dma[i]);
  428. if (!dev->urb_buffer[i]) {
  429. tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
  430. dev->urb_size, i);
  431. return -ENOMEM;
  432. }
  433. memset(dev->urb_buffer[i], 0, dev->urb_size);
  434. }
  435. return 0;
  436. }
  437. /*
  438. * Free URB buffers
  439. */
  440. static int tm6000_free_urb_buffers(struct tm6000_core *dev)
  441. {
  442. int i;
  443. if (!dev->urb_buffer)
  444. return 0;
  445. for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
  446. if (dev->urb_buffer[i]) {
  447. usb_free_coherent(dev->udev,
  448. dev->urb_size,
  449. dev->urb_buffer[i],
  450. dev->urb_dma[i]);
  451. dev->urb_buffer[i] = NULL;
  452. }
  453. }
  454. kfree(dev->urb_buffer);
  455. kfree(dev->urb_dma);
  456. dev->urb_buffer = NULL;
  457. dev->urb_dma = NULL;
  458. return 0;
  459. }
  460. /*
  461. * Stop and Deallocate URBs
  462. */
  463. static void tm6000_uninit_isoc(struct tm6000_core *dev)
  464. {
  465. struct urb *urb;
  466. int i;
  467. dev->isoc_ctl.buf = NULL;
  468. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  469. urb = dev->isoc_ctl.urb[i];
  470. if (urb) {
  471. usb_kill_urb(urb);
  472. usb_unlink_urb(urb);
  473. usb_free_urb(urb);
  474. dev->isoc_ctl.urb[i] = NULL;
  475. }
  476. dev->isoc_ctl.transfer_buffer[i] = NULL;
  477. }
  478. if (!keep_urb)
  479. tm6000_free_urb_buffers(dev);
  480. kfree(dev->isoc_ctl.urb);
  481. kfree(dev->isoc_ctl.transfer_buffer);
  482. dev->isoc_ctl.urb = NULL;
  483. dev->isoc_ctl.transfer_buffer = NULL;
  484. dev->isoc_ctl.num_bufs = 0;
  485. }
  486. /*
  487. * Assign URBs and start IRQ
  488. */
  489. static int tm6000_prepare_isoc(struct tm6000_core *dev)
  490. {
  491. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  492. int i, j, sb_size, pipe, size, max_packets;
  493. int num_bufs = TM6000_NUM_URB_BUF;
  494. struct urb *urb;
  495. /* De-allocates all pending stuff */
  496. tm6000_uninit_isoc(dev);
  497. /* Stop interrupt USB pipe */
  498. tm6000_ir_int_stop(dev);
  499. usb_set_interface(dev->udev,
  500. dev->isoc_in.bInterfaceNumber,
  501. dev->isoc_in.bAlternateSetting);
  502. /* Start interrupt USB pipe */
  503. tm6000_ir_int_start(dev);
  504. pipe = usb_rcvisocpipe(dev->udev,
  505. dev->isoc_in.endp->desc.bEndpointAddress &
  506. USB_ENDPOINT_NUMBER_MASK);
  507. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  508. if (size > dev->isoc_in.maxsize)
  509. size = dev->isoc_in.maxsize;
  510. dev->isoc_ctl.max_pkt_size = size;
  511. max_packets = TM6000_MAX_ISO_PACKETS;
  512. sb_size = max_packets * size;
  513. dev->urb_size = sb_size;
  514. dev->isoc_ctl.num_bufs = num_bufs;
  515. dev->isoc_ctl.urb = kmalloc_array(num_bufs, sizeof(void *),
  516. GFP_KERNEL);
  517. if (!dev->isoc_ctl.urb)
  518. return -ENOMEM;
  519. dev->isoc_ctl.transfer_buffer = kmalloc_array(num_bufs,
  520. sizeof(void *),
  521. GFP_KERNEL);
  522. if (!dev->isoc_ctl.transfer_buffer) {
  523. kfree(dev->isoc_ctl.urb);
  524. return -ENOMEM;
  525. }
  526. dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
  527. max_packets, num_bufs, sb_size,
  528. dev->isoc_in.maxsize, size);
  529. if (tm6000_alloc_urb_buffers(dev) < 0) {
  530. tm6000_err("cannot allocate memory for urb buffers\n");
  531. /* call free, as some buffers might have been allocated */
  532. tm6000_free_urb_buffers(dev);
  533. kfree(dev->isoc_ctl.urb);
  534. kfree(dev->isoc_ctl.transfer_buffer);
  535. return -ENOMEM;
  536. }
  537. /* allocate urbs and transfer buffers */
  538. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  539. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  540. if (!urb) {
  541. tm6000_uninit_isoc(dev);
  542. tm6000_free_urb_buffers(dev);
  543. return -ENOMEM;
  544. }
  545. dev->isoc_ctl.urb[i] = urb;
  546. urb->transfer_dma = dev->urb_dma[i];
  547. dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
  548. usb_fill_bulk_urb(urb, dev->udev, pipe,
  549. dev->isoc_ctl.transfer_buffer[i], sb_size,
  550. tm6000_irq_callback, dma_q);
  551. urb->interval = dev->isoc_in.endp->desc.bInterval;
  552. urb->number_of_packets = max_packets;
  553. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  554. for (j = 0; j < max_packets; j++) {
  555. urb->iso_frame_desc[j].offset = size * j;
  556. urb->iso_frame_desc[j].length = size;
  557. }
  558. }
  559. return 0;
  560. }
  561. static int tm6000_start_thread(struct tm6000_core *dev)
  562. {
  563. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  564. int i;
  565. dma_q->frame = 0;
  566. dma_q->ini_jiffies = jiffies;
  567. init_waitqueue_head(&dma_q->wq);
  568. /* submit urbs and enables IRQ */
  569. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  570. int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  571. if (rc) {
  572. tm6000_err("submit of urb %i failed (error=%i)\n", i,
  573. rc);
  574. tm6000_uninit_isoc(dev);
  575. return rc;
  576. }
  577. }
  578. return 0;
  579. }
  580. /* ------------------------------------------------------------------
  581. * Videobuf operations
  582. * ------------------------------------------------------------------
  583. */
  584. static int
  585. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  586. {
  587. struct tm6000_fh *fh = vq->priv_data;
  588. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  589. if (0 == *count)
  590. *count = TM6000_DEF_BUF;
  591. if (*count < TM6000_MIN_BUF)
  592. *count = TM6000_MIN_BUF;
  593. while (*size * *count > vid_limit * 1024 * 1024)
  594. (*count)--;
  595. return 0;
  596. }
  597. static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
  598. {
  599. struct tm6000_fh *fh = vq->priv_data;
  600. struct tm6000_core *dev = fh->dev;
  601. unsigned long flags;
  602. BUG_ON(in_interrupt());
  603. /* We used to wait for the buffer to finish here, but this didn't work
  604. because, as we were keeping the state as VIDEOBUF_QUEUED,
  605. videobuf_queue_cancel marked it as finished for us.
  606. (Also, it could wedge forever if the hardware was misconfigured.)
  607. This should be safe; by the time we get here, the buffer isn't
  608. queued anymore. If we ever start marking the buffers as
  609. VIDEOBUF_ACTIVE, it won't be, though.
  610. */
  611. spin_lock_irqsave(&dev->slock, flags);
  612. if (dev->isoc_ctl.buf == buf)
  613. dev->isoc_ctl.buf = NULL;
  614. spin_unlock_irqrestore(&dev->slock, flags);
  615. videobuf_vmalloc_free(&buf->vb);
  616. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  617. }
  618. static int
  619. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  620. enum v4l2_field field)
  621. {
  622. struct tm6000_fh *fh = vq->priv_data;
  623. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  624. struct tm6000_core *dev = fh->dev;
  625. int rc = 0;
  626. BUG_ON(NULL == fh->fmt);
  627. /* FIXME: It assumes depth=2 */
  628. /* The only currently supported format is 16 bits/pixel */
  629. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  630. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  631. return -EINVAL;
  632. if (buf->fmt != fh->fmt ||
  633. buf->vb.width != fh->width ||
  634. buf->vb.height != fh->height ||
  635. buf->vb.field != field) {
  636. buf->fmt = fh->fmt;
  637. buf->vb.width = fh->width;
  638. buf->vb.height = fh->height;
  639. buf->vb.field = field;
  640. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  641. }
  642. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  643. rc = videobuf_iolock(vq, &buf->vb, NULL);
  644. if (rc != 0)
  645. goto fail;
  646. }
  647. if (!dev->isoc_ctl.num_bufs) {
  648. rc = tm6000_prepare_isoc(dev);
  649. if (rc < 0)
  650. goto fail;
  651. rc = tm6000_start_thread(dev);
  652. if (rc < 0)
  653. goto fail;
  654. }
  655. buf->vb.state = VIDEOBUF_PREPARED;
  656. return 0;
  657. fail:
  658. free_buffer(vq, buf);
  659. return rc;
  660. }
  661. static void
  662. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  663. {
  664. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  665. struct tm6000_fh *fh = vq->priv_data;
  666. struct tm6000_core *dev = fh->dev;
  667. struct tm6000_dmaqueue *vidq = &dev->vidq;
  668. buf->vb.state = VIDEOBUF_QUEUED;
  669. list_add_tail(&buf->vb.queue, &vidq->active);
  670. }
  671. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  672. {
  673. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  674. free_buffer(vq, buf);
  675. }
  676. static const struct videobuf_queue_ops tm6000_video_qops = {
  677. .buf_setup = buffer_setup,
  678. .buf_prepare = buffer_prepare,
  679. .buf_queue = buffer_queue,
  680. .buf_release = buffer_release,
  681. };
  682. /* ------------------------------------------------------------------
  683. * IOCTL handling
  684. * ------------------------------------------------------------------
  685. */
  686. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  687. {
  688. /* Is the current fh handling it? if so, that's OK */
  689. if (dev->resources == fh && dev->is_res_read)
  690. return true;
  691. return false;
  692. }
  693. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  694. {
  695. /* Is the current fh handling it? if so, that's OK */
  696. if (dev->resources == fh)
  697. return true;
  698. return false;
  699. }
  700. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  701. bool is_res_read)
  702. {
  703. /* Is the current fh handling it? if so, that's OK */
  704. if (dev->resources == fh && dev->is_res_read == is_res_read)
  705. return true;
  706. /* is it free? */
  707. if (dev->resources)
  708. return false;
  709. /* grab it */
  710. dev->resources = fh;
  711. dev->is_res_read = is_res_read;
  712. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  713. return true;
  714. }
  715. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  716. {
  717. /* Is the current fh handling it? if so, that's OK */
  718. if (dev->resources != fh)
  719. return;
  720. dev->resources = NULL;
  721. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  722. }
  723. /* ------------------------------------------------------------------
  724. * IOCTL vidioc handling
  725. * ------------------------------------------------------------------
  726. */
  727. static int vidioc_querycap(struct file *file, void *priv,
  728. struct v4l2_capability *cap)
  729. {
  730. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  731. struct video_device *vdev = video_devdata(file);
  732. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  733. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  734. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  735. if (dev->tuner_type != TUNER_ABSENT)
  736. cap->device_caps |= V4L2_CAP_TUNER;
  737. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  738. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  739. V4L2_CAP_STREAMING |
  740. V4L2_CAP_READWRITE;
  741. else
  742. cap->device_caps |= V4L2_CAP_RADIO;
  743. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  744. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  745. return 0;
  746. }
  747. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  748. struct v4l2_fmtdesc *f)
  749. {
  750. if (f->index >= ARRAY_SIZE(format))
  751. return -EINVAL;
  752. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  753. f->pixelformat = format[f->index].fourcc;
  754. return 0;
  755. }
  756. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  757. struct v4l2_format *f)
  758. {
  759. struct tm6000_fh *fh = priv;
  760. f->fmt.pix.width = fh->width;
  761. f->fmt.pix.height = fh->height;
  762. f->fmt.pix.field = fh->vb_vidq.field;
  763. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  764. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  765. f->fmt.pix.bytesperline =
  766. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  767. f->fmt.pix.sizeimage =
  768. f->fmt.pix.height * f->fmt.pix.bytesperline;
  769. return 0;
  770. }
  771. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  772. {
  773. unsigned int i;
  774. for (i = 0; i < ARRAY_SIZE(format); i++)
  775. if (format[i].fourcc == fourcc)
  776. return format+i;
  777. return NULL;
  778. }
  779. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  780. struct v4l2_format *f)
  781. {
  782. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  783. struct tm6000_fmt *fmt;
  784. enum v4l2_field field;
  785. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  786. if (NULL == fmt) {
  787. dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
  788. f->fmt.pix.pixelformat);
  789. return -EINVAL;
  790. }
  791. field = f->fmt.pix.field;
  792. field = V4L2_FIELD_INTERLACED;
  793. tm6000_get_std_res(dev);
  794. f->fmt.pix.width = dev->width;
  795. f->fmt.pix.height = dev->height;
  796. f->fmt.pix.width &= ~0x01;
  797. f->fmt.pix.field = field;
  798. f->fmt.pix.bytesperline =
  799. (f->fmt.pix.width * fmt->depth) >> 3;
  800. f->fmt.pix.sizeimage =
  801. f->fmt.pix.height * f->fmt.pix.bytesperline;
  802. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  803. return 0;
  804. }
  805. /*FIXME: This seems to be generic enough to be at videodev2 */
  806. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  807. struct v4l2_format *f)
  808. {
  809. struct tm6000_fh *fh = priv;
  810. struct tm6000_core *dev = fh->dev;
  811. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  812. if (ret < 0)
  813. return ret;
  814. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  815. fh->width = f->fmt.pix.width;
  816. fh->height = f->fmt.pix.height;
  817. fh->vb_vidq.field = f->fmt.pix.field;
  818. fh->type = f->type;
  819. dev->fourcc = f->fmt.pix.pixelformat;
  820. tm6000_set_fourcc_format(dev);
  821. return 0;
  822. }
  823. static int vidioc_reqbufs(struct file *file, void *priv,
  824. struct v4l2_requestbuffers *p)
  825. {
  826. struct tm6000_fh *fh = priv;
  827. return videobuf_reqbufs(&fh->vb_vidq, p);
  828. }
  829. static int vidioc_querybuf(struct file *file, void *priv,
  830. struct v4l2_buffer *p)
  831. {
  832. struct tm6000_fh *fh = priv;
  833. return videobuf_querybuf(&fh->vb_vidq, p);
  834. }
  835. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  836. {
  837. struct tm6000_fh *fh = priv;
  838. return videobuf_qbuf(&fh->vb_vidq, p);
  839. }
  840. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  841. {
  842. struct tm6000_fh *fh = priv;
  843. return videobuf_dqbuf(&fh->vb_vidq, p,
  844. file->f_flags & O_NONBLOCK);
  845. }
  846. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  847. {
  848. struct tm6000_fh *fh = priv;
  849. struct tm6000_core *dev = fh->dev;
  850. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  851. return -EINVAL;
  852. if (i != fh->type)
  853. return -EINVAL;
  854. if (!res_get(dev, fh, false))
  855. return -EBUSY;
  856. return videobuf_streamon(&fh->vb_vidq);
  857. }
  858. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  859. {
  860. struct tm6000_fh *fh = priv;
  861. struct tm6000_core *dev = fh->dev;
  862. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  863. return -EINVAL;
  864. if (i != fh->type)
  865. return -EINVAL;
  866. videobuf_streamoff(&fh->vb_vidq);
  867. res_free(dev, fh);
  868. return 0;
  869. }
  870. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  871. {
  872. int rc = 0;
  873. struct tm6000_fh *fh = priv;
  874. struct tm6000_core *dev = fh->dev;
  875. dev->norm = norm;
  876. rc = tm6000_init_analog_mode(dev);
  877. fh->width = dev->width;
  878. fh->height = dev->height;
  879. if (rc < 0)
  880. return rc;
  881. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
  882. return 0;
  883. }
  884. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  885. {
  886. struct tm6000_fh *fh = priv;
  887. struct tm6000_core *dev = fh->dev;
  888. *norm = dev->norm;
  889. return 0;
  890. }
  891. static const char *iname[] = {
  892. [TM6000_INPUT_TV] = "Television",
  893. [TM6000_INPUT_COMPOSITE1] = "Composite 1",
  894. [TM6000_INPUT_COMPOSITE2] = "Composite 2",
  895. [TM6000_INPUT_SVIDEO] = "S-Video",
  896. };
  897. static int vidioc_enum_input(struct file *file, void *priv,
  898. struct v4l2_input *i)
  899. {
  900. struct tm6000_fh *fh = priv;
  901. struct tm6000_core *dev = fh->dev;
  902. unsigned int n;
  903. n = i->index;
  904. if (n >= 3)
  905. return -EINVAL;
  906. if (!dev->vinput[n].type)
  907. return -EINVAL;
  908. i->index = n;
  909. if (dev->vinput[n].type == TM6000_INPUT_TV)
  910. i->type = V4L2_INPUT_TYPE_TUNER;
  911. else
  912. i->type = V4L2_INPUT_TYPE_CAMERA;
  913. strcpy(i->name, iname[dev->vinput[n].type]);
  914. i->std = TM6000_STD;
  915. return 0;
  916. }
  917. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  918. {
  919. struct tm6000_fh *fh = priv;
  920. struct tm6000_core *dev = fh->dev;
  921. *i = dev->input;
  922. return 0;
  923. }
  924. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  925. {
  926. struct tm6000_fh *fh = priv;
  927. struct tm6000_core *dev = fh->dev;
  928. int rc = 0;
  929. if (i >= 3)
  930. return -EINVAL;
  931. if (!dev->vinput[i].type)
  932. return -EINVAL;
  933. dev->input = i;
  934. rc = vidioc_s_std(file, priv, dev->norm);
  935. return rc;
  936. }
  937. /* --- controls ---------------------------------------------- */
  938. static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
  939. {
  940. struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
  941. u8 val = ctrl->val;
  942. switch (ctrl->id) {
  943. case V4L2_CID_CONTRAST:
  944. tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
  945. return 0;
  946. case V4L2_CID_BRIGHTNESS:
  947. tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
  948. return 0;
  949. case V4L2_CID_SATURATION:
  950. tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
  951. return 0;
  952. case V4L2_CID_HUE:
  953. tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
  954. return 0;
  955. }
  956. return -EINVAL;
  957. }
  958. static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
  959. .s_ctrl = tm6000_s_ctrl,
  960. };
  961. static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  962. {
  963. struct tm6000_core *dev = container_of(ctrl->handler,
  964. struct tm6000_core, radio_ctrl_handler);
  965. u8 val = ctrl->val;
  966. switch (ctrl->id) {
  967. case V4L2_CID_AUDIO_MUTE:
  968. dev->ctl_mute = val;
  969. tm6000_tvaudio_set_mute(dev, val);
  970. return 0;
  971. case V4L2_CID_AUDIO_VOLUME:
  972. dev->ctl_volume = val;
  973. tm6000_set_volume(dev, val);
  974. return 0;
  975. }
  976. return -EINVAL;
  977. }
  978. static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
  979. .s_ctrl = tm6000_radio_s_ctrl,
  980. };
  981. static int vidioc_g_tuner(struct file *file, void *priv,
  982. struct v4l2_tuner *t)
  983. {
  984. struct tm6000_fh *fh = priv;
  985. struct tm6000_core *dev = fh->dev;
  986. if (UNSET == dev->tuner_type)
  987. return -ENOTTY;
  988. if (0 != t->index)
  989. return -EINVAL;
  990. strcpy(t->name, "Television");
  991. t->type = V4L2_TUNER_ANALOG_TV;
  992. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  993. t->rangehigh = 0xffffffffUL;
  994. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  995. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  996. t->audmode = dev->amode;
  997. return 0;
  998. }
  999. static int vidioc_s_tuner(struct file *file, void *priv,
  1000. const 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. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1009. dev->amode = V4L2_TUNER_MODE_STEREO;
  1010. else
  1011. dev->amode = t->audmode;
  1012. dprintk(dev, 3, "audio mode: %x\n", t->audmode);
  1013. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1014. return 0;
  1015. }
  1016. static int vidioc_g_frequency(struct file *file, void *priv,
  1017. struct v4l2_frequency *f)
  1018. {
  1019. struct tm6000_fh *fh = priv;
  1020. struct tm6000_core *dev = fh->dev;
  1021. if (UNSET == dev->tuner_type)
  1022. return -ENOTTY;
  1023. if (f->tuner)
  1024. return -EINVAL;
  1025. f->frequency = dev->freq;
  1026. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1027. return 0;
  1028. }
  1029. static int vidioc_s_frequency(struct file *file, void *priv,
  1030. const struct v4l2_frequency *f)
  1031. {
  1032. struct tm6000_fh *fh = priv;
  1033. struct tm6000_core *dev = fh->dev;
  1034. if (UNSET == dev->tuner_type)
  1035. return -ENOTTY;
  1036. if (f->tuner != 0)
  1037. return -EINVAL;
  1038. dev->freq = f->frequency;
  1039. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1040. return 0;
  1041. }
  1042. static int radio_g_tuner(struct file *file, void *priv,
  1043. struct v4l2_tuner *t)
  1044. {
  1045. struct tm6000_fh *fh = file->private_data;
  1046. struct tm6000_core *dev = fh->dev;
  1047. if (0 != t->index)
  1048. return -EINVAL;
  1049. memset(t, 0, sizeof(*t));
  1050. strcpy(t->name, "Radio");
  1051. t->type = V4L2_TUNER_RADIO;
  1052. t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1053. t->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1054. t->audmode = V4L2_TUNER_MODE_STEREO;
  1055. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1056. return 0;
  1057. }
  1058. static int radio_s_tuner(struct file *file, void *priv,
  1059. const struct v4l2_tuner *t)
  1060. {
  1061. struct tm6000_fh *fh = file->private_data;
  1062. struct tm6000_core *dev = fh->dev;
  1063. if (0 != t->index)
  1064. return -EINVAL;
  1065. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1066. return 0;
  1067. }
  1068. /* ------------------------------------------------------------------
  1069. File operations for the device
  1070. ------------------------------------------------------------------*/
  1071. static int __tm6000_open(struct file *file)
  1072. {
  1073. struct video_device *vdev = video_devdata(file);
  1074. struct tm6000_core *dev = video_drvdata(file);
  1075. struct tm6000_fh *fh;
  1076. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1077. int rc;
  1078. int radio = 0;
  1079. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1080. video_device_node_name(vdev));
  1081. switch (vdev->vfl_type) {
  1082. case VFL_TYPE_GRABBER:
  1083. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1084. break;
  1085. case VFL_TYPE_VBI:
  1086. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1087. break;
  1088. case VFL_TYPE_RADIO:
  1089. radio = 1;
  1090. break;
  1091. default:
  1092. return -EINVAL;
  1093. }
  1094. /* If more than one user, mutex should be added */
  1095. dev->users++;
  1096. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1097. video_device_node_name(vdev), v4l2_type_names[type],
  1098. dev->users);
  1099. /* allocate + initialize per filehandle data */
  1100. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1101. if (NULL == fh) {
  1102. dev->users--;
  1103. return -ENOMEM;
  1104. }
  1105. v4l2_fh_init(&fh->fh, vdev);
  1106. file->private_data = fh;
  1107. fh->dev = dev;
  1108. fh->radio = radio;
  1109. dev->radio = radio;
  1110. fh->type = type;
  1111. dev->fourcc = format[0].fourcc;
  1112. fh->fmt = format_by_fourcc(dev->fourcc);
  1113. tm6000_get_std_res(dev);
  1114. fh->width = dev->width;
  1115. fh->height = dev->height;
  1116. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=%p, dev=%p, dev->vidq=%p\n",
  1117. fh, dev, &dev->vidq);
  1118. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
  1119. list_empty(&dev->vidq.queued));
  1120. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
  1121. list_empty(&dev->vidq.active));
  1122. /* initialize hardware on analog mode */
  1123. rc = tm6000_init_analog_mode(dev);
  1124. if (rc < 0) {
  1125. v4l2_fh_exit(&fh->fh);
  1126. kfree(fh);
  1127. return rc;
  1128. }
  1129. dev->mode = TM6000_MODE_ANALOG;
  1130. if (!fh->radio) {
  1131. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1132. NULL, &dev->slock,
  1133. fh->type,
  1134. V4L2_FIELD_INTERLACED,
  1135. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1136. } else {
  1137. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1138. tm6000_set_audio_rinput(dev);
  1139. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1140. tm6000_prepare_isoc(dev);
  1141. tm6000_start_thread(dev);
  1142. }
  1143. v4l2_fh_add(&fh->fh);
  1144. return 0;
  1145. }
  1146. static int tm6000_open(struct file *file)
  1147. {
  1148. struct video_device *vdev = video_devdata(file);
  1149. int res;
  1150. mutex_lock(vdev->lock);
  1151. res = __tm6000_open(file);
  1152. mutex_unlock(vdev->lock);
  1153. return res;
  1154. }
  1155. static ssize_t
  1156. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1157. {
  1158. struct tm6000_fh *fh = file->private_data;
  1159. struct tm6000_core *dev = fh->dev;
  1160. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1161. int res;
  1162. if (!res_get(fh->dev, fh, true))
  1163. return -EBUSY;
  1164. if (mutex_lock_interruptible(&dev->lock))
  1165. return -ERESTARTSYS;
  1166. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1167. file->f_flags & O_NONBLOCK);
  1168. mutex_unlock(&dev->lock);
  1169. return res;
  1170. }
  1171. return 0;
  1172. }
  1173. static __poll_t
  1174. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1175. {
  1176. __poll_t req_events = poll_requested_events(wait);
  1177. struct tm6000_fh *fh = file->private_data;
  1178. struct tm6000_buffer *buf;
  1179. __poll_t res = 0;
  1180. if (v4l2_event_pending(&fh->fh))
  1181. res = EPOLLPRI;
  1182. else if (req_events & EPOLLPRI)
  1183. poll_wait(file, &fh->fh.wait, wait);
  1184. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1185. return res | EPOLLERR;
  1186. if (!!is_res_streaming(fh->dev, fh))
  1187. return res | EPOLLERR;
  1188. if (!is_res_read(fh->dev, fh)) {
  1189. /* streaming capture */
  1190. if (list_empty(&fh->vb_vidq.stream))
  1191. return res | EPOLLERR;
  1192. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1193. poll_wait(file, &buf->vb.done, wait);
  1194. if (buf->vb.state == VIDEOBUF_DONE ||
  1195. buf->vb.state == VIDEOBUF_ERROR)
  1196. return res | EPOLLIN | EPOLLRDNORM;
  1197. } else if (req_events & (EPOLLIN | EPOLLRDNORM)) {
  1198. /* read() capture */
  1199. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1200. }
  1201. return res;
  1202. }
  1203. static __poll_t tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1204. {
  1205. struct tm6000_fh *fh = file->private_data;
  1206. struct tm6000_core *dev = fh->dev;
  1207. __poll_t res;
  1208. mutex_lock(&dev->lock);
  1209. res = __tm6000_poll(file, wait);
  1210. mutex_unlock(&dev->lock);
  1211. return res;
  1212. }
  1213. static int tm6000_release(struct file *file)
  1214. {
  1215. struct tm6000_fh *fh = file->private_data;
  1216. struct tm6000_core *dev = fh->dev;
  1217. struct video_device *vdev = video_devdata(file);
  1218. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1219. video_device_node_name(vdev), dev->users);
  1220. mutex_lock(&dev->lock);
  1221. dev->users--;
  1222. res_free(dev, fh);
  1223. if (!dev->users) {
  1224. tm6000_uninit_isoc(dev);
  1225. /* Stop interrupt USB pipe */
  1226. tm6000_ir_int_stop(dev);
  1227. usb_reset_configuration(dev->udev);
  1228. if (dev->int_in.endp)
  1229. usb_set_interface(dev->udev,
  1230. dev->isoc_in.bInterfaceNumber, 2);
  1231. else
  1232. usb_set_interface(dev->udev,
  1233. dev->isoc_in.bInterfaceNumber, 0);
  1234. /* Start interrupt USB pipe */
  1235. tm6000_ir_int_start(dev);
  1236. if (!fh->radio)
  1237. videobuf_mmap_free(&fh->vb_vidq);
  1238. }
  1239. v4l2_fh_del(&fh->fh);
  1240. v4l2_fh_exit(&fh->fh);
  1241. kfree(fh);
  1242. mutex_unlock(&dev->lock);
  1243. return 0;
  1244. }
  1245. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1246. {
  1247. struct tm6000_fh *fh = file->private_data;
  1248. struct tm6000_core *dev = fh->dev;
  1249. int res;
  1250. if (mutex_lock_interruptible(&dev->lock))
  1251. return -ERESTARTSYS;
  1252. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1253. mutex_unlock(&dev->lock);
  1254. return res;
  1255. }
  1256. static const struct v4l2_file_operations tm6000_fops = {
  1257. .owner = THIS_MODULE,
  1258. .open = tm6000_open,
  1259. .release = tm6000_release,
  1260. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1261. .read = tm6000_read,
  1262. .poll = tm6000_poll,
  1263. .mmap = tm6000_mmap,
  1264. };
  1265. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1266. .vidioc_querycap = vidioc_querycap,
  1267. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1268. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1269. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1270. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1271. .vidioc_s_std = vidioc_s_std,
  1272. .vidioc_g_std = vidioc_g_std,
  1273. .vidioc_enum_input = vidioc_enum_input,
  1274. .vidioc_g_input = vidioc_g_input,
  1275. .vidioc_s_input = vidioc_s_input,
  1276. .vidioc_g_tuner = vidioc_g_tuner,
  1277. .vidioc_s_tuner = vidioc_s_tuner,
  1278. .vidioc_g_frequency = vidioc_g_frequency,
  1279. .vidioc_s_frequency = vidioc_s_frequency,
  1280. .vidioc_streamon = vidioc_streamon,
  1281. .vidioc_streamoff = vidioc_streamoff,
  1282. .vidioc_reqbufs = vidioc_reqbufs,
  1283. .vidioc_querybuf = vidioc_querybuf,
  1284. .vidioc_qbuf = vidioc_qbuf,
  1285. .vidioc_dqbuf = vidioc_dqbuf,
  1286. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1287. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1288. };
  1289. static struct video_device tm6000_template = {
  1290. .name = "tm6000",
  1291. .fops = &tm6000_fops,
  1292. .ioctl_ops = &video_ioctl_ops,
  1293. .release = video_device_release_empty,
  1294. .tvnorms = TM6000_STD,
  1295. };
  1296. static const struct v4l2_file_operations radio_fops = {
  1297. .owner = THIS_MODULE,
  1298. .open = tm6000_open,
  1299. .poll = v4l2_ctrl_poll,
  1300. .release = tm6000_release,
  1301. .unlocked_ioctl = video_ioctl2,
  1302. };
  1303. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1304. .vidioc_querycap = vidioc_querycap,
  1305. .vidioc_g_tuner = radio_g_tuner,
  1306. .vidioc_s_tuner = radio_s_tuner,
  1307. .vidioc_g_frequency = vidioc_g_frequency,
  1308. .vidioc_s_frequency = vidioc_s_frequency,
  1309. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1310. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1311. };
  1312. static struct video_device tm6000_radio_template = {
  1313. .name = "tm6000",
  1314. .fops = &radio_fops,
  1315. .ioctl_ops = &radio_ioctl_ops,
  1316. };
  1317. /* -----------------------------------------------------------------
  1318. * Initialization and module stuff
  1319. * ------------------------------------------------------------------
  1320. */
  1321. static void vdev_init(struct tm6000_core *dev,
  1322. struct video_device *vfd,
  1323. const struct video_device
  1324. *template, const char *type_name)
  1325. {
  1326. *vfd = *template;
  1327. vfd->v4l2_dev = &dev->v4l2_dev;
  1328. vfd->release = video_device_release_empty;
  1329. vfd->lock = &dev->lock;
  1330. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1331. video_set_drvdata(vfd, dev);
  1332. }
  1333. int tm6000_v4l2_register(struct tm6000_core *dev)
  1334. {
  1335. int ret = 0;
  1336. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1337. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1338. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1339. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1340. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1341. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1342. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1343. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1344. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1345. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1346. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1347. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1348. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1349. V4L2_CID_HUE, -128, 127, 1, 0);
  1350. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1351. &dev->radio_ctrl_handler, NULL);
  1352. if (dev->radio_ctrl_handler.error)
  1353. ret = dev->radio_ctrl_handler.error;
  1354. if (!ret && dev->ctrl_handler.error)
  1355. ret = dev->ctrl_handler.error;
  1356. if (ret)
  1357. goto free_ctrl;
  1358. vdev_init(dev, &dev->vfd, &tm6000_template, "video");
  1359. dev->vfd.ctrl_handler = &dev->ctrl_handler;
  1360. /* init video dma queues */
  1361. INIT_LIST_HEAD(&dev->vidq.active);
  1362. INIT_LIST_HEAD(&dev->vidq.queued);
  1363. ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1364. if (ret < 0) {
  1365. printk(KERN_INFO "%s: can't register video device\n",
  1366. dev->name);
  1367. goto free_ctrl;
  1368. }
  1369. printk(KERN_INFO "%s: registered device %s\n",
  1370. dev->name, video_device_node_name(&dev->vfd));
  1371. if (dev->caps.has_radio) {
  1372. vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
  1373. "radio");
  1374. dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
  1375. ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1376. radio_nr);
  1377. if (ret < 0) {
  1378. printk(KERN_INFO "%s: can't register radio device\n",
  1379. dev->name);
  1380. goto unreg_video;
  1381. }
  1382. printk(KERN_INFO "%s: registered device %s\n",
  1383. dev->name, video_device_node_name(&dev->radio_dev));
  1384. }
  1385. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1386. return ret;
  1387. unreg_video:
  1388. video_unregister_device(&dev->vfd);
  1389. free_ctrl:
  1390. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1391. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1392. return ret;
  1393. }
  1394. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1395. {
  1396. video_unregister_device(&dev->vfd);
  1397. /* if URB buffers are still allocated free them now */
  1398. tm6000_free_urb_buffers(dev);
  1399. video_unregister_device(&dev->radio_dev);
  1400. return 0;
  1401. }
  1402. int tm6000_v4l2_exit(void)
  1403. {
  1404. return 0;
  1405. }
  1406. module_param(video_nr, int, 0);
  1407. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1408. module_param_named(debug, tm6000_debug, int, 0444);
  1409. MODULE_PARM_DESC(debug, "activates debug info");
  1410. module_param(vid_limit, int, 0644);
  1411. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1412. module_param(keep_urb, bool, 0);
  1413. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");