tm6000-video.c 42 KB

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