tm6000-video.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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 (!dev->urb_buffer && 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_err("cannot alloc isoc_ctl.urb %i\n", i);
  561. tm6000_uninit_isoc(dev);
  562. usb_free_urb(urb);
  563. return -ENOMEM;
  564. }
  565. dev->isoc_ctl.urb[i] = urb;
  566. urb->transfer_dma = dev->urb_dma[i];
  567. dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
  568. usb_fill_bulk_urb(urb, dev->udev, pipe,
  569. dev->isoc_ctl.transfer_buffer[i], sb_size,
  570. tm6000_irq_callback, dma_q);
  571. urb->interval = dev->isoc_in.endp->desc.bInterval;
  572. urb->number_of_packets = max_packets;
  573. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  574. for (j = 0; j < max_packets; j++) {
  575. urb->iso_frame_desc[j].offset = size * j;
  576. urb->iso_frame_desc[j].length = size;
  577. }
  578. }
  579. return 0;
  580. }
  581. static int tm6000_start_thread(struct tm6000_core *dev)
  582. {
  583. struct tm6000_dmaqueue *dma_q = &dev->vidq;
  584. int i;
  585. dma_q->frame = 0;
  586. dma_q->ini_jiffies = jiffies;
  587. init_waitqueue_head(&dma_q->wq);
  588. /* submit urbs and enables IRQ */
  589. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  590. int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  591. if (rc) {
  592. tm6000_err("submit of urb %i failed (error=%i)\n", i,
  593. rc);
  594. tm6000_uninit_isoc(dev);
  595. return rc;
  596. }
  597. }
  598. return 0;
  599. }
  600. /* ------------------------------------------------------------------
  601. * Videobuf operations
  602. * ------------------------------------------------------------------
  603. */
  604. static int
  605. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  606. {
  607. struct tm6000_fh *fh = vq->priv_data;
  608. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  609. if (0 == *count)
  610. *count = TM6000_DEF_BUF;
  611. if (*count < TM6000_MIN_BUF)
  612. *count = TM6000_MIN_BUF;
  613. while (*size * *count > vid_limit * 1024 * 1024)
  614. (*count)--;
  615. return 0;
  616. }
  617. static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
  618. {
  619. struct tm6000_fh *fh = vq->priv_data;
  620. struct tm6000_core *dev = fh->dev;
  621. unsigned long flags;
  622. if (in_interrupt())
  623. BUG();
  624. /* We used to wait for the buffer to finish here, but this didn't work
  625. because, as we were keeping the state as VIDEOBUF_QUEUED,
  626. videobuf_queue_cancel marked it as finished for us.
  627. (Also, it could wedge forever if the hardware was misconfigured.)
  628. This should be safe; by the time we get here, the buffer isn't
  629. queued anymore. If we ever start marking the buffers as
  630. VIDEOBUF_ACTIVE, it won't be, though.
  631. */
  632. spin_lock_irqsave(&dev->slock, flags);
  633. if (dev->isoc_ctl.buf == buf)
  634. dev->isoc_ctl.buf = NULL;
  635. spin_unlock_irqrestore(&dev->slock, flags);
  636. videobuf_vmalloc_free(&buf->vb);
  637. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  638. }
  639. static int
  640. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  641. enum v4l2_field field)
  642. {
  643. struct tm6000_fh *fh = vq->priv_data;
  644. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  645. struct tm6000_core *dev = fh->dev;
  646. int rc = 0;
  647. BUG_ON(NULL == fh->fmt);
  648. /* FIXME: It assumes depth=2 */
  649. /* The only currently supported format is 16 bits/pixel */
  650. buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
  651. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  652. return -EINVAL;
  653. if (buf->fmt != fh->fmt ||
  654. buf->vb.width != fh->width ||
  655. buf->vb.height != fh->height ||
  656. buf->vb.field != field) {
  657. buf->fmt = fh->fmt;
  658. buf->vb.width = fh->width;
  659. buf->vb.height = fh->height;
  660. buf->vb.field = field;
  661. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  662. }
  663. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  664. rc = videobuf_iolock(vq, &buf->vb, NULL);
  665. if (rc != 0)
  666. goto fail;
  667. }
  668. if (!dev->isoc_ctl.num_bufs) {
  669. rc = tm6000_prepare_isoc(dev);
  670. if (rc < 0)
  671. goto fail;
  672. rc = tm6000_start_thread(dev);
  673. if (rc < 0)
  674. goto fail;
  675. }
  676. buf->vb.state = VIDEOBUF_PREPARED;
  677. return 0;
  678. fail:
  679. free_buffer(vq, buf);
  680. return rc;
  681. }
  682. static void
  683. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  684. {
  685. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  686. struct tm6000_fh *fh = vq->priv_data;
  687. struct tm6000_core *dev = fh->dev;
  688. struct tm6000_dmaqueue *vidq = &dev->vidq;
  689. buf->vb.state = VIDEOBUF_QUEUED;
  690. list_add_tail(&buf->vb.queue, &vidq->active);
  691. }
  692. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  693. {
  694. struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
  695. free_buffer(vq, buf);
  696. }
  697. static struct videobuf_queue_ops tm6000_video_qops = {
  698. .buf_setup = buffer_setup,
  699. .buf_prepare = buffer_prepare,
  700. .buf_queue = buffer_queue,
  701. .buf_release = buffer_release,
  702. };
  703. /* ------------------------------------------------------------------
  704. * IOCTL handling
  705. * ------------------------------------------------------------------
  706. */
  707. static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
  708. {
  709. /* Is the current fh handling it? if so, that's OK */
  710. if (dev->resources == fh && dev->is_res_read)
  711. return true;
  712. return false;
  713. }
  714. static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
  715. {
  716. /* Is the current fh handling it? if so, that's OK */
  717. if (dev->resources == fh)
  718. return true;
  719. return false;
  720. }
  721. static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
  722. bool is_res_read)
  723. {
  724. /* Is the current fh handling it? if so, that's OK */
  725. if (dev->resources == fh && dev->is_res_read == is_res_read)
  726. return true;
  727. /* is it free? */
  728. if (dev->resources)
  729. return false;
  730. /* grab it */
  731. dev->resources = fh;
  732. dev->is_res_read = is_res_read;
  733. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
  734. return true;
  735. }
  736. static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
  737. {
  738. /* Is the current fh handling it? if so, that's OK */
  739. if (dev->resources != fh)
  740. return;
  741. dev->resources = NULL;
  742. dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
  743. }
  744. /* ------------------------------------------------------------------
  745. * IOCTL vidioc handling
  746. * ------------------------------------------------------------------
  747. */
  748. static int vidioc_querycap(struct file *file, void *priv,
  749. struct v4l2_capability *cap)
  750. {
  751. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  752. struct video_device *vdev = video_devdata(file);
  753. strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
  754. strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
  755. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  756. if (dev->tuner_type != TUNER_ABSENT)
  757. cap->device_caps |= V4L2_CAP_TUNER;
  758. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  759. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
  760. V4L2_CAP_STREAMING |
  761. V4L2_CAP_READWRITE;
  762. else
  763. cap->device_caps |= V4L2_CAP_RADIO;
  764. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  765. V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  766. return 0;
  767. }
  768. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  769. struct v4l2_fmtdesc *f)
  770. {
  771. if (f->index >= ARRAY_SIZE(format))
  772. return -EINVAL;
  773. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  774. f->pixelformat = format[f->index].fourcc;
  775. return 0;
  776. }
  777. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  778. struct v4l2_format *f)
  779. {
  780. struct tm6000_fh *fh = priv;
  781. f->fmt.pix.width = fh->width;
  782. f->fmt.pix.height = fh->height;
  783. f->fmt.pix.field = fh->vb_vidq.field;
  784. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  785. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  786. f->fmt.pix.bytesperline =
  787. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  788. f->fmt.pix.sizeimage =
  789. f->fmt.pix.height * f->fmt.pix.bytesperline;
  790. return 0;
  791. }
  792. static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
  793. {
  794. unsigned int i;
  795. for (i = 0; i < ARRAY_SIZE(format); i++)
  796. if (format[i].fourcc == fourcc)
  797. return format+i;
  798. return NULL;
  799. }
  800. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  801. struct v4l2_format *f)
  802. {
  803. struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
  804. struct tm6000_fmt *fmt;
  805. enum v4l2_field field;
  806. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  807. if (NULL == fmt) {
  808. dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
  809. " invalid.\n", f->fmt.pix.pixelformat);
  810. return -EINVAL;
  811. }
  812. field = f->fmt.pix.field;
  813. if (field == V4L2_FIELD_ANY)
  814. field = V4L2_FIELD_SEQ_TB;
  815. else if (V4L2_FIELD_INTERLACED != field) {
  816. dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
  817. return -EINVAL;
  818. }
  819. tm6000_get_std_res(dev);
  820. f->fmt.pix.width = dev->width;
  821. f->fmt.pix.height = dev->height;
  822. f->fmt.pix.width &= ~0x01;
  823. f->fmt.pix.field = field;
  824. f->fmt.pix.bytesperline =
  825. (f->fmt.pix.width * fmt->depth) >> 3;
  826. f->fmt.pix.sizeimage =
  827. f->fmt.pix.height * f->fmt.pix.bytesperline;
  828. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  829. return 0;
  830. }
  831. /*FIXME: This seems to be generic enough to be at videodev2 */
  832. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  833. struct v4l2_format *f)
  834. {
  835. struct tm6000_fh *fh = priv;
  836. struct tm6000_core *dev = fh->dev;
  837. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  838. if (ret < 0)
  839. return ret;
  840. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  841. fh->width = f->fmt.pix.width;
  842. fh->height = f->fmt.pix.height;
  843. fh->vb_vidq.field = f->fmt.pix.field;
  844. fh->type = f->type;
  845. dev->fourcc = f->fmt.pix.pixelformat;
  846. tm6000_set_fourcc_format(dev);
  847. return 0;
  848. }
  849. static int vidioc_reqbufs(struct file *file, void *priv,
  850. struct v4l2_requestbuffers *p)
  851. {
  852. struct tm6000_fh *fh = priv;
  853. return videobuf_reqbufs(&fh->vb_vidq, p);
  854. }
  855. static int vidioc_querybuf(struct file *file, void *priv,
  856. struct v4l2_buffer *p)
  857. {
  858. struct tm6000_fh *fh = priv;
  859. return videobuf_querybuf(&fh->vb_vidq, p);
  860. }
  861. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  862. {
  863. struct tm6000_fh *fh = priv;
  864. return videobuf_qbuf(&fh->vb_vidq, p);
  865. }
  866. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  867. {
  868. struct tm6000_fh *fh = priv;
  869. return videobuf_dqbuf(&fh->vb_vidq, p,
  870. file->f_flags & O_NONBLOCK);
  871. }
  872. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  873. {
  874. struct tm6000_fh *fh = priv;
  875. struct tm6000_core *dev = fh->dev;
  876. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  877. return -EINVAL;
  878. if (i != fh->type)
  879. return -EINVAL;
  880. if (!res_get(dev, fh, false))
  881. return -EBUSY;
  882. return videobuf_streamon(&fh->vb_vidq);
  883. }
  884. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  885. {
  886. struct tm6000_fh *fh = priv;
  887. struct tm6000_core *dev = fh->dev;
  888. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  889. return -EINVAL;
  890. if (i != fh->type)
  891. return -EINVAL;
  892. videobuf_streamoff(&fh->vb_vidq);
  893. res_free(dev, fh);
  894. return 0;
  895. }
  896. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  897. {
  898. int rc = 0;
  899. struct tm6000_fh *fh = priv;
  900. struct tm6000_core *dev = fh->dev;
  901. dev->norm = *norm;
  902. rc = tm6000_init_analog_mode(dev);
  903. fh->width = dev->width;
  904. fh->height = dev->height;
  905. if (rc < 0)
  906. return rc;
  907. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, 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->vfd->current_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. 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. 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. 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. if (t->audmode > V4L2_TUNER_MODE_STEREO)
  1085. t->audmode = V4L2_TUNER_MODE_STEREO;
  1086. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1087. return 0;
  1088. }
  1089. /* ------------------------------------------------------------------
  1090. File operations for the device
  1091. ------------------------------------------------------------------*/
  1092. static int __tm6000_open(struct file *file)
  1093. {
  1094. struct video_device *vdev = video_devdata(file);
  1095. struct tm6000_core *dev = video_drvdata(file);
  1096. struct tm6000_fh *fh;
  1097. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1098. int rc;
  1099. int radio = 0;
  1100. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
  1101. video_device_node_name(vdev));
  1102. switch (vdev->vfl_type) {
  1103. case VFL_TYPE_GRABBER:
  1104. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1105. break;
  1106. case VFL_TYPE_VBI:
  1107. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1108. break;
  1109. case VFL_TYPE_RADIO:
  1110. radio = 1;
  1111. break;
  1112. }
  1113. /* If more than one user, mutex should be added */
  1114. dev->users++;
  1115. dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
  1116. video_device_node_name(vdev), v4l2_type_names[type],
  1117. dev->users);
  1118. /* allocate + initialize per filehandle data */
  1119. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1120. if (NULL == fh) {
  1121. dev->users--;
  1122. return -ENOMEM;
  1123. }
  1124. v4l2_fh_init(&fh->fh, vdev);
  1125. file->private_data = fh;
  1126. fh->dev = dev;
  1127. fh->radio = radio;
  1128. dev->radio = radio;
  1129. fh->type = type;
  1130. dev->fourcc = format[0].fourcc;
  1131. fh->fmt = format_by_fourcc(dev->fourcc);
  1132. tm6000_get_std_res(dev);
  1133. fh->width = dev->width;
  1134. fh->height = dev->height;
  1135. dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
  1136. "dev->vidq=0x%08lx\n",
  1137. (unsigned long)fh, (unsigned long)dev,
  1138. (unsigned long)&dev->vidq);
  1139. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1140. "queued=%d\n", list_empty(&dev->vidq.queued));
  1141. dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
  1142. "active=%d\n", list_empty(&dev->vidq.active));
  1143. /* initialize hardware on analog mode */
  1144. rc = tm6000_init_analog_mode(dev);
  1145. if (rc < 0)
  1146. return rc;
  1147. dev->mode = TM6000_MODE_ANALOG;
  1148. if (!fh->radio) {
  1149. videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
  1150. NULL, &dev->slock,
  1151. fh->type,
  1152. V4L2_FIELD_INTERLACED,
  1153. sizeof(struct tm6000_buffer), fh, &dev->lock);
  1154. } else {
  1155. dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
  1156. tm6000_set_audio_rinput(dev);
  1157. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1158. tm6000_prepare_isoc(dev);
  1159. tm6000_start_thread(dev);
  1160. }
  1161. v4l2_fh_add(&fh->fh);
  1162. return 0;
  1163. }
  1164. static int tm6000_open(struct file *file)
  1165. {
  1166. struct video_device *vdev = video_devdata(file);
  1167. int res;
  1168. mutex_lock(vdev->lock);
  1169. res = __tm6000_open(file);
  1170. mutex_unlock(vdev->lock);
  1171. return res;
  1172. }
  1173. static ssize_t
  1174. tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
  1175. {
  1176. struct tm6000_fh *fh = file->private_data;
  1177. struct tm6000_core *dev = fh->dev;
  1178. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1179. int res;
  1180. if (!res_get(fh->dev, fh, true))
  1181. return -EBUSY;
  1182. if (mutex_lock_interruptible(&dev->lock))
  1183. return -ERESTARTSYS;
  1184. res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
  1185. file->f_flags & O_NONBLOCK);
  1186. mutex_unlock(&dev->lock);
  1187. return res;
  1188. }
  1189. return 0;
  1190. }
  1191. static unsigned int
  1192. __tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1193. {
  1194. unsigned long req_events = poll_requested_events(wait);
  1195. struct tm6000_fh *fh = file->private_data;
  1196. struct tm6000_buffer *buf;
  1197. int res = 0;
  1198. if (v4l2_event_pending(&fh->fh))
  1199. res = POLLPRI;
  1200. else if (req_events & POLLPRI)
  1201. poll_wait(file, &fh->fh.wait, wait);
  1202. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1203. return res | POLLERR;
  1204. if (!!is_res_streaming(fh->dev, fh))
  1205. return res | POLLERR;
  1206. if (!is_res_read(fh->dev, fh)) {
  1207. /* streaming capture */
  1208. if (list_empty(&fh->vb_vidq.stream))
  1209. return res | POLLERR;
  1210. buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
  1211. } else if (req_events & (POLLIN | POLLRDNORM)) {
  1212. /* read() capture */
  1213. return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
  1214. }
  1215. poll_wait(file, &buf->vb.done, wait);
  1216. if (buf->vb.state == VIDEOBUF_DONE ||
  1217. buf->vb.state == VIDEOBUF_ERROR)
  1218. return res | POLLIN | POLLRDNORM;
  1219. return res;
  1220. }
  1221. static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
  1222. {
  1223. struct tm6000_fh *fh = file->private_data;
  1224. struct tm6000_core *dev = fh->dev;
  1225. unsigned int res;
  1226. mutex_lock(&dev->lock);
  1227. res = __tm6000_poll(file, wait);
  1228. mutex_unlock(&dev->lock);
  1229. return res;
  1230. }
  1231. static int tm6000_release(struct file *file)
  1232. {
  1233. struct tm6000_fh *fh = file->private_data;
  1234. struct tm6000_core *dev = fh->dev;
  1235. struct video_device *vdev = video_devdata(file);
  1236. dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
  1237. video_device_node_name(vdev), dev->users);
  1238. mutex_lock(&dev->lock);
  1239. dev->users--;
  1240. res_free(dev, fh);
  1241. if (!dev->users) {
  1242. tm6000_uninit_isoc(dev);
  1243. /* Stop interrupt USB pipe */
  1244. tm6000_ir_int_stop(dev);
  1245. usb_reset_configuration(dev->udev);
  1246. if (dev->int_in.endp)
  1247. usb_set_interface(dev->udev,
  1248. dev->isoc_in.bInterfaceNumber, 2);
  1249. else
  1250. usb_set_interface(dev->udev,
  1251. dev->isoc_in.bInterfaceNumber, 0);
  1252. /* Start interrupt USB pipe */
  1253. tm6000_ir_int_start(dev);
  1254. if (!fh->radio)
  1255. videobuf_mmap_free(&fh->vb_vidq);
  1256. }
  1257. v4l2_fh_del(&fh->fh);
  1258. v4l2_fh_exit(&fh->fh);
  1259. kfree(fh);
  1260. mutex_unlock(&dev->lock);
  1261. return 0;
  1262. }
  1263. static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
  1264. {
  1265. struct tm6000_fh *fh = file->private_data;
  1266. struct tm6000_core *dev = fh->dev;
  1267. int res;
  1268. if (mutex_lock_interruptible(&dev->lock))
  1269. return -ERESTARTSYS;
  1270. res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1271. mutex_unlock(&dev->lock);
  1272. return res;
  1273. }
  1274. static struct v4l2_file_operations tm6000_fops = {
  1275. .owner = THIS_MODULE,
  1276. .open = tm6000_open,
  1277. .release = tm6000_release,
  1278. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1279. .read = tm6000_read,
  1280. .poll = tm6000_poll,
  1281. .mmap = tm6000_mmap,
  1282. };
  1283. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1284. .vidioc_querycap = vidioc_querycap,
  1285. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1286. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1287. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1288. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1289. .vidioc_s_std = vidioc_s_std,
  1290. .vidioc_enum_input = vidioc_enum_input,
  1291. .vidioc_g_input = vidioc_g_input,
  1292. .vidioc_s_input = vidioc_s_input,
  1293. .vidioc_g_tuner = vidioc_g_tuner,
  1294. .vidioc_s_tuner = vidioc_s_tuner,
  1295. .vidioc_g_frequency = vidioc_g_frequency,
  1296. .vidioc_s_frequency = vidioc_s_frequency,
  1297. .vidioc_streamon = vidioc_streamon,
  1298. .vidioc_streamoff = vidioc_streamoff,
  1299. .vidioc_reqbufs = vidioc_reqbufs,
  1300. .vidioc_querybuf = vidioc_querybuf,
  1301. .vidioc_qbuf = vidioc_qbuf,
  1302. .vidioc_dqbuf = vidioc_dqbuf,
  1303. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1304. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1305. };
  1306. static struct video_device tm6000_template = {
  1307. .name = "tm6000",
  1308. .fops = &tm6000_fops,
  1309. .ioctl_ops = &video_ioctl_ops,
  1310. .release = video_device_release,
  1311. .tvnorms = TM6000_STD,
  1312. .current_norm = V4L2_STD_NTSC_M,
  1313. };
  1314. static const struct v4l2_file_operations radio_fops = {
  1315. .owner = THIS_MODULE,
  1316. .open = tm6000_open,
  1317. .poll = v4l2_ctrl_poll,
  1318. .release = tm6000_release,
  1319. .unlocked_ioctl = video_ioctl2,
  1320. };
  1321. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1322. .vidioc_querycap = vidioc_querycap,
  1323. .vidioc_g_tuner = radio_g_tuner,
  1324. .vidioc_s_tuner = radio_s_tuner,
  1325. .vidioc_g_frequency = vidioc_g_frequency,
  1326. .vidioc_s_frequency = vidioc_s_frequency,
  1327. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1328. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1329. };
  1330. static struct video_device tm6000_radio_template = {
  1331. .name = "tm6000",
  1332. .fops = &radio_fops,
  1333. .ioctl_ops = &radio_ioctl_ops,
  1334. };
  1335. /* -----------------------------------------------------------------
  1336. * Initialization and module stuff
  1337. * ------------------------------------------------------------------
  1338. */
  1339. static struct video_device *vdev_init(struct tm6000_core *dev,
  1340. const struct video_device
  1341. *template, const char *type_name)
  1342. {
  1343. struct video_device *vfd;
  1344. vfd = video_device_alloc();
  1345. if (NULL == vfd)
  1346. return NULL;
  1347. *vfd = *template;
  1348. vfd->v4l2_dev = &dev->v4l2_dev;
  1349. vfd->release = video_device_release;
  1350. vfd->debug = tm6000_debug;
  1351. vfd->lock = &dev->lock;
  1352. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1353. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1354. video_set_drvdata(vfd, dev);
  1355. return vfd;
  1356. }
  1357. int tm6000_v4l2_register(struct tm6000_core *dev)
  1358. {
  1359. int ret = 0;
  1360. v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
  1361. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
  1362. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1363. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1364. v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
  1365. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1366. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1367. V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
  1368. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1369. V4L2_CID_CONTRAST, 0, 255, 1, 119);
  1370. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1371. V4L2_CID_SATURATION, 0, 255, 1, 112);
  1372. v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
  1373. V4L2_CID_HUE, -128, 127, 1, 0);
  1374. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1375. &dev->radio_ctrl_handler, NULL);
  1376. if (dev->radio_ctrl_handler.error)
  1377. ret = dev->radio_ctrl_handler.error;
  1378. if (!ret && dev->ctrl_handler.error)
  1379. ret = dev->ctrl_handler.error;
  1380. if (ret)
  1381. goto free_ctrl;
  1382. dev->vfd = vdev_init(dev, &tm6000_template, "video");
  1383. if (!dev->vfd) {
  1384. printk(KERN_INFO "%s: can't register video device\n",
  1385. dev->name);
  1386. ret = -ENOMEM;
  1387. goto free_ctrl;
  1388. }
  1389. dev->vfd->ctrl_handler = &dev->ctrl_handler;
  1390. /* init video dma queues */
  1391. INIT_LIST_HEAD(&dev->vidq.active);
  1392. INIT_LIST_HEAD(&dev->vidq.queued);
  1393. ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
  1394. if (ret < 0) {
  1395. printk(KERN_INFO "%s: can't register video device\n",
  1396. dev->name);
  1397. video_device_release(dev->vfd);
  1398. dev->vfd = NULL;
  1399. goto free_ctrl;
  1400. }
  1401. printk(KERN_INFO "%s: registered device %s\n",
  1402. dev->name, video_device_node_name(dev->vfd));
  1403. if (dev->caps.has_radio) {
  1404. dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
  1405. "radio");
  1406. if (!dev->radio_dev) {
  1407. printk(KERN_INFO "%s: can't register radio device\n",
  1408. dev->name);
  1409. ret = -ENXIO;
  1410. goto unreg_video;
  1411. }
  1412. dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
  1413. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1414. radio_nr);
  1415. if (ret < 0) {
  1416. printk(KERN_INFO "%s: can't register radio device\n",
  1417. dev->name);
  1418. video_device_release(dev->radio_dev);
  1419. goto unreg_video;
  1420. }
  1421. printk(KERN_INFO "%s: registered device %s\n",
  1422. dev->name, video_device_node_name(dev->radio_dev));
  1423. }
  1424. printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
  1425. return ret;
  1426. unreg_video:
  1427. video_unregister_device(dev->vfd);
  1428. free_ctrl:
  1429. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1430. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1431. return ret;
  1432. }
  1433. int tm6000_v4l2_unregister(struct tm6000_core *dev)
  1434. {
  1435. video_unregister_device(dev->vfd);
  1436. /* if URB buffers are still allocated free them now */
  1437. tm6000_free_urb_buffers(dev);
  1438. if (dev->radio_dev) {
  1439. if (video_is_registered(dev->radio_dev))
  1440. video_unregister_device(dev->radio_dev);
  1441. else
  1442. video_device_release(dev->radio_dev);
  1443. dev->radio_dev = NULL;
  1444. }
  1445. return 0;
  1446. }
  1447. int tm6000_v4l2_exit(void)
  1448. {
  1449. return 0;
  1450. }
  1451. module_param(video_nr, int, 0);
  1452. MODULE_PARM_DESC(video_nr, "Allow changing video device number");
  1453. module_param_named(debug, tm6000_debug, int, 0444);
  1454. MODULE_PARM_DESC(debug, "activates debug info");
  1455. module_param(vid_limit, int, 0644);
  1456. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  1457. module_param(keep_urb, bool, 0);
  1458. MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");