tm6000-video.c 42 KB

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