gspca.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  1. /*
  2. * Main USB camera driver
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define MODULE_NAME "gspca"
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/io.h>
  30. #include <asm/page.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/jiffies.h>
  33. #include "gspca.h"
  34. /* global values */
  35. #define DEF_NURBS 2 /* default number of URBs (mmap) */
  36. #define USR_NURBS 5 /* default number of URBs (userptr) */
  37. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  38. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  39. MODULE_LICENSE("GPL");
  40. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 2)
  41. static const char version[] = "2.1.2";
  42. static int video_nr = -1;
  43. static int comp_fac = 30; /* Buffer size ratio when compressed in % */
  44. #ifdef VIDEO_ADV_DEBUG
  45. int gspca_debug = D_ERR | D_PROBE;
  46. EXPORT_SYMBOL(gspca_debug);
  47. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  48. {
  49. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  50. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  51. txt,
  52. pixfmt & 0xff,
  53. (pixfmt >> 8) & 0xff,
  54. (pixfmt >> 16) & 0xff,
  55. pixfmt >> 24,
  56. w, h);
  57. } else {
  58. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  59. txt,
  60. pixfmt,
  61. w, h);
  62. }
  63. }
  64. #else
  65. #define PDEBUG_MODE(txt, pixfmt, w, h)
  66. #endif
  67. /* specific memory types - !! should different from V4L2_MEMORY_xxx */
  68. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  69. #define GSPCA_MEMORY_READ 7
  70. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  71. /*
  72. * VMA operations.
  73. */
  74. static void gspca_vm_open(struct vm_area_struct *vma)
  75. {
  76. struct gspca_frame *frame = vma->vm_private_data;
  77. frame->vma_use_count++;
  78. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  79. }
  80. static void gspca_vm_close(struct vm_area_struct *vma)
  81. {
  82. struct gspca_frame *frame = vma->vm_private_data;
  83. if (--frame->vma_use_count <= 0)
  84. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  85. }
  86. static struct vm_operations_struct gspca_vm_ops = {
  87. .open = gspca_vm_open,
  88. .close = gspca_vm_close,
  89. };
  90. /*
  91. * fill a video frame from an URB and resubmit
  92. */
  93. static void fill_frame(struct gspca_dev *gspca_dev,
  94. struct urb *urb)
  95. {
  96. struct gspca_frame *frame;
  97. __u8 *data; /* address of data in the iso message */
  98. int i, j, len, st;
  99. cam_pkt_op pkt_scan;
  100. if (urb->status != 0) {
  101. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  102. return; /* disconnection ? */
  103. }
  104. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  105. for (i = 0; i < urb->number_of_packets; i++) {
  106. /* check the availability of the frame buffer */
  107. j = gspca_dev->fr_i;
  108. j = gspca_dev->fr_queue[j];
  109. frame = &gspca_dev->frame[j];
  110. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  111. != V4L2_BUF_FLAG_QUEUED) {
  112. gspca_dev->last_packet_type = DISCARD_PACKET;
  113. break;
  114. }
  115. /* check the packet status and length */
  116. len = urb->iso_frame_desc[i].actual_length;
  117. if (len == 0)
  118. continue;
  119. st = urb->iso_frame_desc[i].status;
  120. if (st) {
  121. PDEBUG(D_ERR,
  122. "ISOC data error: [%d] len=%d, status=%d",
  123. i, len, st);
  124. gspca_dev->last_packet_type = DISCARD_PACKET;
  125. continue;
  126. }
  127. /* let the packet be analyzed by the subdriver */
  128. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  129. i, urb->iso_frame_desc[i].offset, len);
  130. data = (__u8 *) urb->transfer_buffer
  131. + urb->iso_frame_desc[i].offset;
  132. pkt_scan(gspca_dev, frame, data, len);
  133. }
  134. /* resubmit the URB */
  135. /*fixme: don't do that when userptr and too many URBs sent*/
  136. urb->status = 0;
  137. st = usb_submit_urb(urb, GFP_ATOMIC);
  138. if (st < 0)
  139. PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
  140. }
  141. /*
  142. * ISOC message interrupt from the USB device
  143. *
  144. * Analyse each packet and call the subdriver for copy
  145. * to the frame buffer.
  146. *
  147. * There are 2 functions:
  148. * - the first one (isoc_irq_mmap) is used when the application
  149. * buffers are mapped. The frame detection and copy is done
  150. * at interrupt level.
  151. * - the second one (isoc_irq_user) is used when the application
  152. * buffers are in user space (userptr). The frame detection
  153. * and copy is done by the application.
  154. */
  155. static void isoc_irq_mmap(struct urb *urb
  156. )
  157. {
  158. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  159. PDEBUG(D_PACK, "isoc irq mmap");
  160. if (!gspca_dev->streaming)
  161. return;
  162. fill_frame(gspca_dev, urb);
  163. }
  164. static void isoc_irq_user(struct urb *urb
  165. )
  166. {
  167. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  168. int i;
  169. PDEBUG(D_PACK, "isoc irq user");
  170. if (!gspca_dev->streaming)
  171. return;
  172. i = gspca_dev->urb_in % gspca_dev->nurbs;
  173. if (urb != gspca_dev->urb[i]) {
  174. PDEBUG(D_ERR|D_PACK, "urb out of sequence");
  175. return; /* should never occur */
  176. }
  177. gspca_dev->urb_in++;
  178. atomic_inc(&gspca_dev->nevent); /* new event */
  179. wake_up_interruptible(&gspca_dev->wq);
  180. /*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
  181. }
  182. /*
  183. * treat the isoc messages
  184. *
  185. * This routine is called by the application (case userptr).
  186. */
  187. static void isoc_transfer(struct gspca_dev *gspca_dev)
  188. {
  189. struct urb *urb;
  190. int i;
  191. for (;;) {
  192. i = gspca_dev->urb_out;
  193. PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
  194. if (i == gspca_dev->urb_in) /* isoc message to read */
  195. break; /* no (more) message */
  196. atomic_dec(&gspca_dev->nevent);
  197. /*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
  198. gspca_dev->urb_out = i + 1; /* message treated */
  199. urb = gspca_dev->urb[i % gspca_dev->nurbs];
  200. fill_frame(gspca_dev, urb);
  201. }
  202. }
  203. /*
  204. * add data to the current frame
  205. *
  206. * This function is called by the subdrivers at interrupt level
  207. * or user level.
  208. * To build a frame, these ones must add
  209. * - one FIRST_PACKET
  210. * - 0 or many INTER_PACKETs
  211. * - one LAST_PACKET
  212. * DISCARD_PACKET invalidates the whole frame.
  213. * On LAST_PACKET, a new frame is returned.
  214. */
  215. struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
  216. int packet_type,
  217. struct gspca_frame *frame,
  218. __u8 *data,
  219. int len)
  220. {
  221. int i, j;
  222. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  223. /* when start of a new frame, if the current frame buffer
  224. * is not queued, discard the whole frame */
  225. if (packet_type == FIRST_PACKET) {
  226. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  227. != V4L2_BUF_FLAG_QUEUED) {
  228. gspca_dev->last_packet_type = DISCARD_PACKET;
  229. return frame;
  230. }
  231. frame->data_end = frame->data;
  232. jiffies_to_timeval(get_jiffies_64(),
  233. &frame->v4l2_buf.timestamp);
  234. frame->v4l2_buf.sequence = ++gspca_dev->sequence;
  235. } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
  236. return frame;
  237. }
  238. /* append the packet to the frame buffer */
  239. if (len > 0) {
  240. if (frame->data_end - frame->data + len
  241. > frame->v4l2_buf.length) {
  242. PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
  243. frame->data_end - frame->data + len,
  244. frame->v4l2_buf.length);
  245. packet_type = DISCARD_PACKET;
  246. } else {
  247. if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR) {
  248. memcpy(frame->data_end, data, len);
  249. } else {
  250. if (copy_to_user(frame->data_end,
  251. data, len) != 0) {
  252. PDEBUG(D_ERR|D_PACK,
  253. "copy to user failed");
  254. packet_type = DISCARD_PACKET;
  255. }
  256. }
  257. frame->data_end += len;
  258. }
  259. }
  260. gspca_dev->last_packet_type = packet_type;
  261. /* if last packet, wake the application and advance in the queue */
  262. if (packet_type == LAST_PACKET) {
  263. frame->v4l2_buf.bytesused = frame->data_end - frame->data;
  264. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  265. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
  266. atomic_inc(&gspca_dev->nevent);
  267. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  268. i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
  269. gspca_dev->fr_i = i;
  270. PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
  271. frame->v4l2_buf.bytesused,
  272. gspca_dev->fr_q,
  273. i,
  274. gspca_dev->fr_o);
  275. j = gspca_dev->fr_queue[i];
  276. frame = &gspca_dev->frame[j];
  277. }
  278. return frame;
  279. }
  280. EXPORT_SYMBOL(gspca_frame_add);
  281. static int gspca_is_compressed(__u32 format)
  282. {
  283. switch (format) {
  284. case V4L2_PIX_FMT_MJPEG:
  285. case V4L2_PIX_FMT_JPEG:
  286. case V4L2_PIX_FMT_SPCA561:
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. static void *rvmalloc(unsigned long size)
  292. {
  293. void *mem;
  294. unsigned long adr;
  295. /* size = PAGE_ALIGN(size); (already done) */
  296. mem = vmalloc_32(size);
  297. if (mem != 0) {
  298. memset(mem, 0, size);
  299. adr = (unsigned long) mem;
  300. while ((long) size > 0) {
  301. SetPageReserved(vmalloc_to_page((void *) adr));
  302. adr += PAGE_SIZE;
  303. size -= PAGE_SIZE;
  304. }
  305. }
  306. return mem;
  307. }
  308. static void rvfree(void *mem, unsigned long size)
  309. {
  310. unsigned long adr;
  311. if (!mem)
  312. return;
  313. adr = (unsigned long) mem;
  314. while ((long) size > 0) {
  315. ClearPageReserved(vmalloc_to_page((void *) adr));
  316. adr += PAGE_SIZE;
  317. size -= PAGE_SIZE;
  318. }
  319. vfree(mem);
  320. }
  321. static __u32 get_v4l2_depth(__u32 pixfmt)
  322. {
  323. switch (pixfmt) {
  324. /* case V4L2_PIX_FMT_BGR32:
  325. case V4L2_PIX_FMT_RGB32:
  326. return 32; */
  327. case V4L2_PIX_FMT_RGB24: /* 'RGB3' */
  328. case V4L2_PIX_FMT_BGR24:
  329. return 24;
  330. /* case V4L2_PIX_FMT_RGB565: * 'RGBP' */
  331. case V4L2_PIX_FMT_YUYV: /* 'YUYV' packed 4.2.2 */
  332. case V4L2_PIX_FMT_YYUV: /* 'YYUV' */
  333. return 16;
  334. case V4L2_PIX_FMT_YUV420: /* 'YU12' planar 4.2.0 */
  335. case V4L2_PIX_FMT_SPCA501: /* 'S501' YUYV per line */
  336. return 12;
  337. case V4L2_PIX_FMT_MJPEG:
  338. case V4L2_PIX_FMT_JPEG:
  339. case V4L2_PIX_FMT_SBGGR8: /* 'BA81' Bayer */
  340. case V4L2_PIX_FMT_SN9C10X: /* 'S910' SN9C10x compression */
  341. case V4L2_PIX_FMT_SPCA561: /* 'S561' compressed BGGR bayer */
  342. return 8;
  343. }
  344. PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
  345. pixfmt & 0xff,
  346. (pixfmt >> 8) & 0xff,
  347. (pixfmt >> 16) & 0xff,
  348. pixfmt >> 24);
  349. return 24;
  350. }
  351. static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
  352. {
  353. unsigned int size;
  354. size = gspca_dev->width * gspca_dev->height
  355. * get_v4l2_depth(gspca_dev->pixfmt) / 8;
  356. if (!size)
  357. return -ENOMEM;
  358. return size;
  359. }
  360. static int frame_alloc(struct gspca_dev *gspca_dev,
  361. unsigned int count)
  362. {
  363. struct gspca_frame *frame;
  364. unsigned int frsz;
  365. int i;
  366. frsz = gspca_get_buff_size(gspca_dev);
  367. if (frsz < 0)
  368. return frsz;
  369. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  370. if (count > GSPCA_MAX_FRAMES)
  371. count = GSPCA_MAX_FRAMES;
  372. /* if compressed (JPEG), reduce the buffer size */
  373. if (gspca_is_compressed(gspca_dev->pixfmt))
  374. frsz = (frsz * comp_fac) / 100 + 600; /* (+ JPEG header sz) */
  375. frsz = PAGE_ALIGN(frsz);
  376. PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
  377. gspca_dev->frsz = frsz;
  378. if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
  379. gspca_dev->frbuf = rvmalloc(frsz * count);
  380. if (!gspca_dev->frbuf) {
  381. err("frame alloc failed");
  382. return -ENOMEM;
  383. }
  384. }
  385. gspca_dev->nframes = count;
  386. for (i = 0; i < count; i++) {
  387. frame = &gspca_dev->frame[i];
  388. frame->v4l2_buf.index = i;
  389. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  390. frame->v4l2_buf.flags = 0;
  391. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  392. frame->v4l2_buf.length = frsz;
  393. frame->v4l2_buf.memory = gspca_dev->memory;
  394. frame->v4l2_buf.sequence = 0;
  395. if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
  396. frame->data = frame->data_end =
  397. gspca_dev->frbuf + i * frsz;
  398. frame->v4l2_buf.m.offset = i * frsz;
  399. }
  400. }
  401. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  402. gspca_dev->last_packet_type = DISCARD_PACKET;
  403. gspca_dev->sequence = 0;
  404. atomic_set(&gspca_dev->nevent, 0);
  405. return 0;
  406. }
  407. static void frame_free(struct gspca_dev *gspca_dev)
  408. {
  409. int i;
  410. PDEBUG(D_STREAM, "frame free");
  411. if (gspca_dev->frbuf != 0) {
  412. rvfree(gspca_dev->frbuf,
  413. gspca_dev->nframes * gspca_dev->frsz);
  414. gspca_dev->frbuf = NULL;
  415. for (i = 0; i < gspca_dev->nframes; i++)
  416. gspca_dev->frame[i].data = NULL;
  417. }
  418. gspca_dev->nframes = 0;
  419. }
  420. static void destroy_urbs(struct gspca_dev *gspca_dev)
  421. {
  422. struct urb *urb;
  423. unsigned int i;
  424. PDEBUG(D_STREAM, "kill transfer");
  425. for (i = 0; i < MAX_NURBS; ++i) {
  426. urb = gspca_dev->urb[i];
  427. if (urb == NULL)
  428. break;
  429. gspca_dev->urb[i] = NULL;
  430. usb_kill_urb(urb);
  431. if (urb->transfer_buffer != 0)
  432. usb_buffer_free(gspca_dev->dev,
  433. urb->transfer_buffer_length,
  434. urb->transfer_buffer,
  435. urb->transfer_dma);
  436. usb_free_urb(urb);
  437. }
  438. }
  439. /*
  440. * search an input isochronous endpoint in an alternate setting
  441. */
  442. static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
  443. __u8 epaddr)
  444. {
  445. struct usb_host_endpoint *ep;
  446. int i, attr;
  447. epaddr |= USB_DIR_IN;
  448. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  449. ep = &alt->endpoint[i];
  450. if (ep->desc.bEndpointAddress == epaddr) {
  451. attr = ep->desc.bmAttributes
  452. & USB_ENDPOINT_XFERTYPE_MASK;
  453. if (attr == USB_ENDPOINT_XFER_ISOC)
  454. return ep;
  455. break;
  456. }
  457. }
  458. return NULL;
  459. }
  460. /*
  461. * search an input isochronous endpoint
  462. *
  463. * The endpoint is defined by the subdriver.
  464. * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
  465. * This routine may be called many times when the bandwidth is too small
  466. * (the bandwidth is checked on urb submit).
  467. */
  468. struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
  469. {
  470. struct usb_interface *intf;
  471. struct usb_host_endpoint *ep;
  472. int i, ret;
  473. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  474. ep = NULL;
  475. i = gspca_dev->alt; /* previous alt setting */
  476. while (--i > 0) { /* alt 0 is unusable */
  477. ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
  478. if (ep)
  479. break;
  480. }
  481. if (ep == NULL) {
  482. err("no ISOC endpoint found");
  483. return NULL;
  484. }
  485. PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
  486. i, ep->desc.bEndpointAddress);
  487. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
  488. if (ret < 0) {
  489. err("set interface err %d", ret);
  490. return NULL;
  491. }
  492. gspca_dev->alt = i; /* memorize the current alt setting */
  493. return ep;
  494. }
  495. /*
  496. * create the isochronous URBs
  497. */
  498. static int create_urbs(struct gspca_dev *gspca_dev,
  499. struct usb_host_endpoint *ep)
  500. {
  501. struct urb *urb;
  502. int n, nurbs, i, psize, npkt, bsize;
  503. usb_complete_t usb_complete;
  504. /* calculate the packet size and the number of packets */
  505. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  506. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  507. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  508. npkt = ISO_MAX_SIZE / psize;
  509. if (npkt > ISO_MAX_PKT)
  510. npkt = ISO_MAX_PKT;
  511. bsize = psize * npkt;
  512. PDEBUG(D_STREAM,
  513. "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
  514. /*fixme:don't submit all URBs when userptr*/
  515. if (gspca_dev->memory != V4L2_MEMORY_USERPTR) {
  516. usb_complete = isoc_irq_mmap;
  517. nurbs = DEF_NURBS;
  518. } else {
  519. usb_complete = isoc_irq_user;
  520. nurbs = USR_NURBS;
  521. }
  522. gspca_dev->nurbs = nurbs;
  523. for (n = 0; n < nurbs; n++) {
  524. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  525. if (!urb) {
  526. err("usb_alloc_urb failed");
  527. return -ENOMEM;
  528. }
  529. urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
  530. bsize,
  531. GFP_KERNEL,
  532. &urb->transfer_dma);
  533. if (urb->transfer_buffer == NULL) {
  534. usb_free_urb(urb);
  535. destroy_urbs(gspca_dev);
  536. err("usb_buffer_urb failed");
  537. return -ENOMEM;
  538. }
  539. gspca_dev->urb[n] = urb;
  540. urb->dev = gspca_dev->dev;
  541. urb->context = gspca_dev;
  542. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  543. ep->desc.bEndpointAddress);
  544. urb->transfer_flags = URB_ISO_ASAP
  545. | URB_NO_TRANSFER_DMA_MAP;
  546. urb->interval = ep->desc.bInterval;
  547. urb->complete = usb_complete;
  548. urb->number_of_packets = npkt;
  549. urb->transfer_buffer_length = bsize;
  550. for (i = 0; i < npkt; i++) {
  551. urb->iso_frame_desc[i].length = psize;
  552. urb->iso_frame_desc[i].offset = psize * i;
  553. }
  554. }
  555. gspca_dev->urb_in = gspca_dev->urb_out = 0;
  556. return 0;
  557. }
  558. /*
  559. * start the USB transfer
  560. */
  561. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  562. {
  563. struct usb_host_endpoint *ep;
  564. int n, ret;
  565. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  566. return -ERESTARTSYS;
  567. /* set the higher alternate setting and
  568. * loop until urb submit succeeds */
  569. gspca_dev->alt = gspca_dev->nbalt;
  570. for (;;) {
  571. PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
  572. ep = get_isoc_ep(gspca_dev);
  573. if (ep == NULL) {
  574. ret = -EIO;
  575. goto out;
  576. }
  577. ret = create_urbs(gspca_dev, ep);
  578. if (ret < 0)
  579. goto out;
  580. /* start the cam */
  581. gspca_dev->sd_desc->start(gspca_dev);
  582. gspca_dev->streaming = 1;
  583. atomic_set(&gspca_dev->nevent, 0);
  584. /* submit the URBs */
  585. for (n = 0; n < gspca_dev->nurbs; n++) {
  586. ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
  587. if (ret < 0) {
  588. PDEBUG(D_ERR|D_STREAM,
  589. "usb_submit_urb [%d] err %d", n, ret);
  590. gspca_dev->streaming = 0;
  591. destroy_urbs(gspca_dev);
  592. if (ret == -ENOSPC)
  593. break; /* try the previous alt */
  594. goto out;
  595. }
  596. }
  597. if (ret >= 0)
  598. break;
  599. }
  600. out:
  601. mutex_unlock(&gspca_dev->usb_lock);
  602. return ret;
  603. }
  604. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  605. {
  606. int ret;
  607. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  608. if (ret < 0)
  609. PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
  610. return ret;
  611. }
  612. /* Note both the queue and the usb lock should be hold when calling this */
  613. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  614. {
  615. gspca_dev->streaming = 0;
  616. atomic_set(&gspca_dev->nevent, 0);
  617. if (gspca_dev->present) {
  618. gspca_dev->sd_desc->stopN(gspca_dev);
  619. destroy_urbs(gspca_dev);
  620. gspca_set_alt0(gspca_dev);
  621. gspca_dev->sd_desc->stop0(gspca_dev);
  622. PDEBUG(D_STREAM, "stream off OK");
  623. } else {
  624. destroy_urbs(gspca_dev);
  625. atomic_inc(&gspca_dev->nevent);
  626. wake_up_interruptible(&gspca_dev->wq);
  627. PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
  628. }
  629. }
  630. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  631. {
  632. int i;
  633. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  634. gspca_dev->curr_mode = i;
  635. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  636. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  637. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
  638. }
  639. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  640. int width, int height)
  641. {
  642. int i;
  643. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  644. if (width >= gspca_dev->cam.cam_mode[i].width
  645. && height >= gspca_dev->cam.cam_mode[i].height)
  646. break;
  647. }
  648. return i;
  649. }
  650. /*
  651. * search a mode with the right pixel format
  652. */
  653. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  654. int mode,
  655. int pixfmt)
  656. {
  657. int modeU, modeD;
  658. modeU = modeD = mode;
  659. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  660. if (--modeD >= 0) {
  661. if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt)
  662. return modeD;
  663. }
  664. if (++modeU < gspca_dev->cam.nmodes) {
  665. if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt)
  666. return modeU;
  667. }
  668. }
  669. return -EINVAL;
  670. }
  671. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  672. struct v4l2_fmtdesc *fmtdesc)
  673. {
  674. struct gspca_dev *gspca_dev = priv;
  675. int i, j, index;
  676. __u32 fmt_tb[8];
  677. /* give an index to each format */
  678. index = 0;
  679. j = 0;
  680. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  681. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt;
  682. j = 0;
  683. for (;;) {
  684. if (fmt_tb[j] == fmt_tb[index])
  685. break;
  686. j++;
  687. }
  688. if (j == index) {
  689. if (fmtdesc->index == index)
  690. break; /* new format */
  691. index++;
  692. if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
  693. return -EINVAL;
  694. }
  695. }
  696. if (i < 0)
  697. return -EINVAL; /* no more format */
  698. fmtdesc->pixelformat = fmt_tb[index];
  699. if (gspca_is_compressed(fmt_tb[index]))
  700. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  701. fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  702. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  703. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  704. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  705. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  706. fmtdesc->description[4] = '\0';
  707. return 0;
  708. }
  709. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  710. struct v4l2_format *fmt)
  711. {
  712. struct gspca_dev *gspca_dev = priv;
  713. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  714. return -EINVAL;
  715. fmt->fmt.pix.width = gspca_dev->width;
  716. fmt->fmt.pix.height = gspca_dev->height;
  717. fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
  718. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  719. fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
  720. * fmt->fmt.pix.width / 8;
  721. fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline
  722. * fmt->fmt.pix.height;
  723. /* (should be in the subdriver) */
  724. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  725. fmt->fmt.pix.priv = 0;
  726. return 0;
  727. }
  728. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  729. struct v4l2_format *fmt)
  730. {
  731. int w, h, mode, mode2, frsz;
  732. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  733. return -EINVAL;
  734. w = fmt->fmt.pix.width;
  735. h = fmt->fmt.pix.height;
  736. /* (luvcview problem) */
  737. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  738. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
  739. #ifdef VIDEO_ADV_DEBUG
  740. if (gspca_debug & D_CONF)
  741. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  742. #endif
  743. /* search the closest mode for width and height */
  744. mode = wxh_to_mode(gspca_dev, w, h);
  745. /* OK if right palette */
  746. if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) {
  747. /* else, search the closest mode with the same pixel format */
  748. mode2 = gspca_get_mode(gspca_dev, mode,
  749. fmt->fmt.pix.pixelformat);
  750. if (mode2 >= 0) {
  751. mode = mode2;
  752. } else {
  753. /* no chance, return this mode */
  754. fmt->fmt.pix.pixelformat =
  755. gspca_dev->cam.cam_mode[mode].pixfmt;
  756. #ifdef VIDEO_ADV_DEBUG
  757. if (gspca_debug & D_CONF) {
  758. PDEBUG_MODE("new format",
  759. fmt->fmt.pix.pixelformat,
  760. gspca_dev->cam.cam_mode[mode].width,
  761. gspca_dev->cam.cam_mode[mode].height);
  762. }
  763. #endif
  764. }
  765. }
  766. fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width;
  767. fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height;
  768. fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
  769. * fmt->fmt.pix.width / 8;
  770. frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height;
  771. if (gspca_is_compressed(fmt->fmt.pix.pixelformat))
  772. frsz = (frsz * comp_fac) / 100;
  773. fmt->fmt.pix.sizeimage = frsz;
  774. return mode; /* used when s_fmt */
  775. }
  776. static int vidioc_try_fmt_vid_cap(struct file *file,
  777. void *priv,
  778. struct v4l2_format *fmt)
  779. {
  780. struct gspca_dev *gspca_dev = priv;
  781. int ret;
  782. ret = try_fmt_vid_cap(gspca_dev, fmt);
  783. if (ret < 0)
  784. return ret;
  785. return 0;
  786. }
  787. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  788. struct v4l2_format *fmt)
  789. {
  790. struct gspca_dev *gspca_dev = priv;
  791. int ret;
  792. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  793. /* if v4l1 got JPEG */
  794. if (fmt->fmt.pix.pixelformat == 0
  795. && gspca_dev->streaming) {
  796. fmt->fmt.pix.width = gspca_dev->width;
  797. fmt->fmt.pix.height = gspca_dev->height;
  798. fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
  799. return 0;
  800. }
  801. #endif
  802. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  803. return -ERESTARTSYS;
  804. ret = try_fmt_vid_cap(gspca_dev, fmt);
  805. if (ret < 0)
  806. goto out;
  807. if (gspca_dev->nframes != 0
  808. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  809. ret = -EINVAL;
  810. goto out;
  811. }
  812. if (ret == gspca_dev->curr_mode) {
  813. ret = 0;
  814. goto out; /* same mode */
  815. }
  816. if (gspca_dev->streaming) {
  817. ret = -EBUSY;
  818. goto out;
  819. }
  820. gspca_dev->width = fmt->fmt.pix.width;
  821. gspca_dev->height = fmt->fmt.pix.height;
  822. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  823. gspca_dev->curr_mode = ret;
  824. ret = 0;
  825. out:
  826. mutex_unlock(&gspca_dev->queue_lock);
  827. return ret;
  828. }
  829. static int dev_open(struct inode *inode, struct file *file)
  830. {
  831. struct gspca_dev *gspca_dev;
  832. int ret;
  833. PDEBUG(D_STREAM, "%s open", current->comm);
  834. gspca_dev = (struct gspca_dev *) video_devdata(file);
  835. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  836. return -ERESTARTSYS;
  837. if (!gspca_dev->present) {
  838. ret = -ENODEV;
  839. goto out;
  840. }
  841. /* if not done yet, initialize the sensor */
  842. if (gspca_dev->users == 0) {
  843. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  844. ret = -ERESTARTSYS;
  845. goto out;
  846. }
  847. ret = gspca_dev->sd_desc->open(gspca_dev);
  848. mutex_unlock(&gspca_dev->usb_lock);
  849. if (ret != 0) {
  850. PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
  851. goto out;
  852. }
  853. } else if (gspca_dev->users > 4) { /* (arbitrary value) */
  854. ret = -EBUSY;
  855. goto out;
  856. }
  857. gspca_dev->users++;
  858. file->private_data = gspca_dev;
  859. #ifdef VIDEO_ADV_DEBUG
  860. /* activate the v4l2 debug */
  861. if (gspca_debug & D_V4L2)
  862. gspca_dev->vdev.debug |= 3;
  863. else
  864. gspca_dev->vdev.debug &= ~3;
  865. #endif
  866. out:
  867. mutex_unlock(&gspca_dev->queue_lock);
  868. if (ret != 0)
  869. PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
  870. else
  871. PDEBUG(D_STREAM, "open done");
  872. return ret;
  873. }
  874. static int dev_close(struct inode *inode, struct file *file)
  875. {
  876. struct gspca_dev *gspca_dev = file->private_data;
  877. PDEBUG(D_STREAM, "%s close", current->comm);
  878. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  879. return -ERESTARTSYS;
  880. gspca_dev->users--;
  881. /* if the file did capture, free the streaming resources */
  882. if (gspca_dev->capt_file == file) {
  883. mutex_lock(&gspca_dev->usb_lock);
  884. if (gspca_dev->streaming)
  885. gspca_stream_off(gspca_dev);
  886. gspca_dev->sd_desc->close(gspca_dev);
  887. mutex_unlock(&gspca_dev->usb_lock);
  888. frame_free(gspca_dev);
  889. gspca_dev->capt_file = 0;
  890. gspca_dev->memory = GSPCA_MEMORY_NO;
  891. }
  892. file->private_data = NULL;
  893. mutex_unlock(&gspca_dev->queue_lock);
  894. PDEBUG(D_STREAM, "close done");
  895. return 0;
  896. }
  897. static int vidioc_querycap(struct file *file, void *priv,
  898. struct v4l2_capability *cap)
  899. {
  900. struct gspca_dev *gspca_dev = priv;
  901. memset(cap, 0, sizeof *cap);
  902. strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
  903. strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
  904. strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
  905. sizeof cap->bus_info);
  906. cap->version = DRIVER_VERSION_NUMBER;
  907. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  908. | V4L2_CAP_STREAMING
  909. | V4L2_CAP_READWRITE;
  910. return 0;
  911. }
  912. /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
  913. static int vidioc_queryctrl(struct file *file, void *priv,
  914. struct v4l2_queryctrl *q_ctrl)
  915. {
  916. struct gspca_dev *gspca_dev = priv;
  917. int i;
  918. u32 id;
  919. id = q_ctrl->id;
  920. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  921. id &= V4L2_CTRL_ID_MASK;
  922. id++;
  923. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  924. if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  925. memcpy(q_ctrl,
  926. &gspca_dev->sd_desc->ctrls[i].qctrl,
  927. sizeof *q_ctrl);
  928. return 0;
  929. }
  930. }
  931. return -EINVAL;
  932. }
  933. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  934. if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  935. memcpy(q_ctrl,
  936. &gspca_dev->sd_desc->ctrls[i].qctrl,
  937. sizeof *q_ctrl);
  938. return 0;
  939. }
  940. }
  941. if (id >= V4L2_CID_BASE
  942. && id <= V4L2_CID_LASTP1) {
  943. q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  944. return 0;
  945. }
  946. return -EINVAL;
  947. }
  948. static int vidioc_s_ctrl(struct file *file, void *priv,
  949. struct v4l2_control *ctrl)
  950. {
  951. struct gspca_dev *gspca_dev = priv;
  952. struct ctrl *ctrls;
  953. int i, ret;
  954. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  955. i < gspca_dev->sd_desc->nctrls;
  956. i++, ctrls++) {
  957. if (ctrl->id != ctrls->qctrl.id)
  958. continue;
  959. if (ctrl->value < ctrls->qctrl.minimum
  960. && ctrl->value > ctrls->qctrl.maximum)
  961. return -ERANGE;
  962. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  963. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  964. return -ERESTARTSYS;
  965. ret = ctrls->set(gspca_dev, ctrl->value);
  966. mutex_unlock(&gspca_dev->usb_lock);
  967. return ret;
  968. }
  969. return -EINVAL;
  970. }
  971. static int vidioc_g_ctrl(struct file *file, void *priv,
  972. struct v4l2_control *ctrl)
  973. {
  974. struct gspca_dev *gspca_dev = priv;
  975. struct ctrl *ctrls;
  976. int i, ret;
  977. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  978. i < gspca_dev->sd_desc->nctrls;
  979. i++, ctrls++) {
  980. if (ctrl->id != ctrls->qctrl.id)
  981. continue;
  982. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  983. return -ERESTARTSYS;
  984. ret = ctrls->get(gspca_dev, &ctrl->value);
  985. mutex_unlock(&gspca_dev->usb_lock);
  986. return ret;
  987. }
  988. return -EINVAL;
  989. }
  990. static int vidioc_querymenu(struct file *file, void *priv,
  991. struct v4l2_querymenu *qmenu)
  992. {
  993. struct gspca_dev *gspca_dev = priv;
  994. if (!gspca_dev->sd_desc->querymenu)
  995. return -EINVAL;
  996. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  997. }
  998. static int vidioc_enum_input(struct file *file, void *priv,
  999. struct v4l2_input *input)
  1000. {
  1001. struct gspca_dev *gspca_dev = priv;
  1002. if (input->index != 0)
  1003. return -EINVAL;
  1004. memset(input, 0, sizeof *input);
  1005. input->type = V4L2_INPUT_TYPE_CAMERA;
  1006. strncpy(input->name, gspca_dev->sd_desc->name,
  1007. sizeof input->name);
  1008. return 0;
  1009. }
  1010. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1011. {
  1012. *i = 0;
  1013. return 0;
  1014. }
  1015. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1016. {
  1017. if (i > 0)
  1018. return -EINVAL;
  1019. return (0);
  1020. }
  1021. static int vidioc_reqbufs(struct file *file, void *priv,
  1022. struct v4l2_requestbuffers *rb)
  1023. {
  1024. struct gspca_dev *gspca_dev = priv;
  1025. int i, ret = 0;
  1026. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1027. return -EINVAL;
  1028. switch (rb->memory) {
  1029. case V4L2_MEMORY_MMAP:
  1030. case V4L2_MEMORY_USERPTR:
  1031. break;
  1032. default:
  1033. return -EINVAL;
  1034. }
  1035. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1036. return -ERESTARTSYS;
  1037. for (i = 0; i < gspca_dev->nframes; i++) {
  1038. if (gspca_dev->frame[i].vma_use_count) {
  1039. ret = -EBUSY;
  1040. goto out;
  1041. }
  1042. }
  1043. /* only one file may do capture */
  1044. if ((gspca_dev->capt_file != 0 && gspca_dev->capt_file != file)
  1045. || gspca_dev->streaming) {
  1046. ret = -EBUSY;
  1047. goto out;
  1048. }
  1049. if (rb->count == 0) { /* unrequest? */
  1050. frame_free(gspca_dev);
  1051. gspca_dev->capt_file = 0;
  1052. } else {
  1053. gspca_dev->memory = rb->memory;
  1054. ret = frame_alloc(gspca_dev, rb->count);
  1055. if (ret == 0) {
  1056. rb->count = gspca_dev->nframes;
  1057. gspca_dev->capt_file = file;
  1058. }
  1059. }
  1060. out:
  1061. mutex_unlock(&gspca_dev->queue_lock);
  1062. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1063. return ret;
  1064. }
  1065. static int vidioc_querybuf(struct file *file, void *priv,
  1066. struct v4l2_buffer *v4l2_buf)
  1067. {
  1068. struct gspca_dev *gspca_dev = priv;
  1069. struct gspca_frame *frame;
  1070. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1071. || v4l2_buf->index < 0
  1072. || v4l2_buf->index >= gspca_dev->nframes)
  1073. return -EINVAL;
  1074. frame = &gspca_dev->frame[v4l2_buf->index];
  1075. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1076. return 0;
  1077. }
  1078. static int vidioc_streamon(struct file *file, void *priv,
  1079. enum v4l2_buf_type buf_type)
  1080. {
  1081. struct gspca_dev *gspca_dev = priv;
  1082. int ret;
  1083. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1084. return -EINVAL;
  1085. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1086. return -ERESTARTSYS;
  1087. if (!gspca_dev->present) {
  1088. ret = -ENODEV;
  1089. goto out;
  1090. }
  1091. if (gspca_dev->nframes == 0) {
  1092. ret = -EINVAL;
  1093. goto out;
  1094. }
  1095. if (gspca_dev->capt_file != file) {
  1096. ret = -EINVAL;
  1097. goto out;
  1098. }
  1099. if (!gspca_dev->streaming) {
  1100. ret = gspca_init_transfer(gspca_dev);
  1101. if (ret < 0)
  1102. goto out;
  1103. }
  1104. #ifdef VIDEO_ADV_DEBUG
  1105. if (gspca_debug & D_STREAM) {
  1106. PDEBUG_MODE("stream on OK",
  1107. gspca_dev->pixfmt,
  1108. gspca_dev->width,
  1109. gspca_dev->height);
  1110. }
  1111. #endif
  1112. ret = 0;
  1113. out:
  1114. mutex_unlock(&gspca_dev->queue_lock);
  1115. return ret;
  1116. }
  1117. static int vidioc_streamoff(struct file *file, void *priv,
  1118. enum v4l2_buf_type buf_type)
  1119. {
  1120. struct gspca_dev *gspca_dev = priv;
  1121. int ret;
  1122. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1123. return -EINVAL;
  1124. if (!gspca_dev->streaming)
  1125. return 0;
  1126. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1127. return -ERESTARTSYS;
  1128. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  1129. ret = -ERESTARTSYS;
  1130. goto out;
  1131. }
  1132. if (gspca_dev->capt_file != file) {
  1133. ret = -EINVAL;
  1134. goto out2;
  1135. }
  1136. gspca_stream_off(gspca_dev);
  1137. ret = 0;
  1138. out2:
  1139. mutex_unlock(&gspca_dev->usb_lock);
  1140. out:
  1141. mutex_unlock(&gspca_dev->queue_lock);
  1142. return ret;
  1143. }
  1144. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1145. struct v4l2_jpegcompression *jpegcomp)
  1146. {
  1147. struct gspca_dev *gspca_dev = priv;
  1148. int ret;
  1149. if (!gspca_dev->sd_desc->get_jcomp)
  1150. return -EINVAL;
  1151. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1152. return -ERESTARTSYS;
  1153. ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1154. mutex_unlock(&gspca_dev->usb_lock);
  1155. return ret;
  1156. }
  1157. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1158. struct v4l2_jpegcompression *jpegcomp)
  1159. {
  1160. struct gspca_dev *gspca_dev = priv;
  1161. int ret;
  1162. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1163. return -ERESTARTSYS;
  1164. if (!gspca_dev->sd_desc->set_jcomp)
  1165. return -EINVAL;
  1166. ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1167. mutex_unlock(&gspca_dev->usb_lock);
  1168. return ret;
  1169. }
  1170. static int vidioc_g_parm(struct file *filp, void *priv,
  1171. struct v4l2_streamparm *parm)
  1172. {
  1173. struct gspca_dev *gspca_dev = priv;
  1174. memset(parm, 0, sizeof *parm);
  1175. parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1176. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1177. return 0;
  1178. }
  1179. static int vidioc_s_parm(struct file *filp, void *priv,
  1180. struct v4l2_streamparm *parm)
  1181. {
  1182. struct gspca_dev *gspca_dev = priv;
  1183. int n;
  1184. n = parm->parm.capture.readbuffers;
  1185. if (n == 0 || n > GSPCA_MAX_FRAMES)
  1186. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1187. else
  1188. gspca_dev->nbufread = n;
  1189. return 0;
  1190. }
  1191. static int vidioc_s_std(struct file *filp, void *priv,
  1192. v4l2_std_id *parm)
  1193. {
  1194. return 0;
  1195. }
  1196. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1197. static int vidiocgmbuf(struct file *file, void *priv,
  1198. struct video_mbuf *mbuf)
  1199. {
  1200. struct gspca_dev *gspca_dev = file->private_data;
  1201. int i;
  1202. PDEBUG(D_STREAM, "cgmbuf");
  1203. if (gspca_dev->nframes == 0) {
  1204. int ret;
  1205. {
  1206. struct v4l2_format fmt;
  1207. memset(&fmt, 0, sizeof fmt);
  1208. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1209. i = gspca_dev->cam.nmodes - 1; /* highest mode */
  1210. fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
  1211. fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
  1212. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
  1213. ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
  1214. if (ret != 0)
  1215. return ret;
  1216. }
  1217. {
  1218. struct v4l2_requestbuffers rb;
  1219. memset(&rb, 0, sizeof rb);
  1220. rb.count = 4;
  1221. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1222. rb.memory = V4L2_MEMORY_MMAP;
  1223. ret = vidioc_reqbufs(file, priv, &rb);
  1224. if (ret != 0)
  1225. return ret;
  1226. }
  1227. }
  1228. mbuf->frames = gspca_dev->nframes;
  1229. mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
  1230. for (i = 0; i < mbuf->frames; i++)
  1231. mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
  1232. return 0;
  1233. }
  1234. #endif
  1235. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1236. {
  1237. struct gspca_dev *gspca_dev = file->private_data;
  1238. struct gspca_frame *frame = 0;
  1239. struct page *page;
  1240. unsigned long addr, start, size;
  1241. int i, ret;
  1242. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1243. int compat = 0;
  1244. #endif
  1245. start = vma->vm_start;
  1246. size = vma->vm_end - vma->vm_start;
  1247. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1248. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1249. return -ERESTARTSYS;
  1250. if (!gspca_dev->present) {
  1251. ret = -ENODEV;
  1252. goto out;
  1253. }
  1254. if (gspca_dev->capt_file != file) {
  1255. ret = -EINVAL;
  1256. goto out;
  1257. }
  1258. for (i = 0; i < gspca_dev->nframes; ++i) {
  1259. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1260. PDEBUG(D_STREAM, "mmap bad memory type");
  1261. break;
  1262. }
  1263. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1264. == vma->vm_pgoff) {
  1265. frame = &gspca_dev->frame[i];
  1266. break;
  1267. }
  1268. }
  1269. if (frame == 0) {
  1270. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1271. ret = -EINVAL;
  1272. goto out;
  1273. }
  1274. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1275. if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes)
  1276. compat = 1;
  1277. else
  1278. #endif
  1279. if (size != frame->v4l2_buf.length) {
  1280. PDEBUG(D_STREAM, "mmap bad size");
  1281. ret = -EINVAL;
  1282. goto out;
  1283. }
  1284. /*
  1285. * - VM_IO marks the area as being a mmaped region for I/O to a
  1286. * device. It also prevents the region from being core dumped.
  1287. */
  1288. vma->vm_flags |= VM_IO;
  1289. addr = (unsigned long) frame->data;
  1290. while (size > 0) {
  1291. page = vmalloc_to_page((void *) addr);
  1292. ret = vm_insert_page(vma, start, page);
  1293. if (ret < 0)
  1294. goto out;
  1295. start += PAGE_SIZE;
  1296. addr += PAGE_SIZE;
  1297. size -= PAGE_SIZE;
  1298. }
  1299. vma->vm_ops = &gspca_vm_ops;
  1300. vma->vm_private_data = frame;
  1301. gspca_vm_open(vma);
  1302. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1303. if (compat) {
  1304. /*fixme: ugly*/
  1305. for (i = 1; i < gspca_dev->nframes; ++i)
  1306. gspca_dev->frame[i].v4l2_buf.flags |=
  1307. V4L2_BUF_FLAG_MAPPED;
  1308. }
  1309. #endif
  1310. ret = 0;
  1311. out:
  1312. mutex_unlock(&gspca_dev->queue_lock);
  1313. return ret;
  1314. }
  1315. /*
  1316. * wait for a video frame
  1317. *
  1318. * If a frame is ready, its index is returned.
  1319. */
  1320. static int frame_wait(struct gspca_dev *gspca_dev,
  1321. int nonblock_ing)
  1322. {
  1323. struct gspca_frame *frame;
  1324. int i, j, ret;
  1325. /* if userptr, treat the awaiting URBs */
  1326. if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
  1327. isoc_transfer(gspca_dev);
  1328. /* check if a frame is ready */
  1329. i = gspca_dev->fr_o;
  1330. j = gspca_dev->fr_queue[i];
  1331. frame = &gspca_dev->frame[j];
  1332. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
  1333. atomic_dec(&gspca_dev->nevent);
  1334. goto ok;
  1335. }
  1336. if (nonblock_ing) /* no frame yet */
  1337. return -EAGAIN;
  1338. /* wait till a frame is ready */
  1339. for (;;) {
  1340. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1341. atomic_read(&gspca_dev->nevent) > 0,
  1342. msecs_to_jiffies(3000));
  1343. if (ret <= 0) {
  1344. if (ret < 0)
  1345. return ret; /* interrupt */
  1346. return -EIO; /* timeout */
  1347. }
  1348. atomic_dec(&gspca_dev->nevent);
  1349. if (!gspca_dev->streaming || !gspca_dev->present)
  1350. return -EIO;
  1351. if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
  1352. isoc_transfer(gspca_dev);
  1353. i = gspca_dev->fr_o;
  1354. j = gspca_dev->fr_queue[i];
  1355. frame = &gspca_dev->frame[j];
  1356. if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1357. break;
  1358. }
  1359. ok:
  1360. gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
  1361. PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
  1362. gspca_dev->fr_q,
  1363. gspca_dev->fr_i,
  1364. gspca_dev->fr_o);
  1365. if (gspca_dev->sd_desc->dq_callback)
  1366. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1367. return j;
  1368. }
  1369. /*
  1370. * dequeue a video buffer
  1371. *
  1372. * If nonblock_ing is false, block until a buffer is available.
  1373. */
  1374. static int vidioc_dqbuf(struct file *file, void *priv,
  1375. struct v4l2_buffer *v4l2_buf)
  1376. {
  1377. struct gspca_dev *gspca_dev = priv;
  1378. struct gspca_frame *frame;
  1379. int i, ret;
  1380. PDEBUG(D_FRAM, "dqbuf");
  1381. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1382. || (v4l2_buf->memory != V4L2_MEMORY_MMAP
  1383. && v4l2_buf->memory != V4L2_MEMORY_USERPTR))
  1384. return -EINVAL;
  1385. if (!gspca_dev->streaming)
  1386. return -EINVAL;
  1387. if (gspca_dev->capt_file != file) {
  1388. ret = -EINVAL;
  1389. goto out;
  1390. }
  1391. /* only one read */
  1392. if (mutex_lock_interruptible(&gspca_dev->read_lock))
  1393. return -ERESTARTSYS;
  1394. ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
  1395. if (ret < 0)
  1396. goto out;
  1397. i = ret; /* frame index */
  1398. frame = &gspca_dev->frame[i];
  1399. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1400. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1401. PDEBUG(D_FRAM, "dqbuf %d", i);
  1402. ret = 0;
  1403. out:
  1404. mutex_unlock(&gspca_dev->read_lock);
  1405. return ret;
  1406. }
  1407. /*
  1408. * queue a video buffer
  1409. *
  1410. * Attempting to queue a buffer that has already been
  1411. * queued will return -EINVAL.
  1412. */
  1413. static int vidioc_qbuf(struct file *file, void *priv,
  1414. struct v4l2_buffer *v4l2_buf)
  1415. {
  1416. struct gspca_dev *gspca_dev = priv;
  1417. struct gspca_frame *frame;
  1418. int i, index, ret;
  1419. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1420. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1421. return -EINVAL;
  1422. index = v4l2_buf->index;
  1423. if ((unsigned) index >= gspca_dev->nframes) {
  1424. PDEBUG(D_FRAM,
  1425. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1426. return -EINVAL;
  1427. }
  1428. frame = &gspca_dev->frame[index];
  1429. if (v4l2_buf->memory != frame->v4l2_buf.memory) {
  1430. PDEBUG(D_FRAM, "qbuf bad memory type");
  1431. return -EINVAL;
  1432. }
  1433. if (gspca_dev->capt_file != file)
  1434. return -EINVAL;
  1435. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1436. return -ERESTARTSYS;
  1437. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1438. PDEBUG(D_FRAM, "qbuf bad state");
  1439. ret = -EINVAL;
  1440. goto out;
  1441. }
  1442. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1443. /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
  1444. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1445. frame->data = frame->data_end =
  1446. (__u8 *) v4l2_buf->m.userptr;
  1447. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1448. frame->v4l2_buf.length = v4l2_buf->length;
  1449. }
  1450. /* put the buffer in the 'queued' queue */
  1451. i = gspca_dev->fr_q;
  1452. gspca_dev->fr_queue[i] = index;
  1453. gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
  1454. PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
  1455. gspca_dev->fr_q,
  1456. gspca_dev->fr_i,
  1457. gspca_dev->fr_o);
  1458. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1459. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1460. ret = 0;
  1461. out:
  1462. mutex_unlock(&gspca_dev->queue_lock);
  1463. return ret;
  1464. }
  1465. /*
  1466. * allocate the resources for read()
  1467. */
  1468. static int read_alloc(struct gspca_dev *gspca_dev,
  1469. struct file *file)
  1470. {
  1471. struct v4l2_buffer v4l2_buf;
  1472. int i, ret;
  1473. PDEBUG(D_STREAM, "read alloc");
  1474. if (gspca_dev->nframes == 0) {
  1475. struct v4l2_requestbuffers rb;
  1476. memset(&rb, 0, sizeof rb);
  1477. rb.count = gspca_dev->nbufread;
  1478. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1479. rb.memory = V4L2_MEMORY_MMAP;
  1480. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1481. if (ret != 0) {
  1482. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1483. return ret;
  1484. }
  1485. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1486. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1487. v4l2_buf.memory = V4L2_MEMORY_MMAP;
  1488. for (i = 0; i < gspca_dev->nbufread; i++) {
  1489. v4l2_buf.index = i;
  1490. /*fixme: ugly!*/
  1491. gspca_dev->frame[i].v4l2_buf.flags |=
  1492. V4L2_BUF_FLAG_MAPPED;
  1493. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1494. if (ret != 0) {
  1495. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1496. return ret;
  1497. }
  1498. }
  1499. gspca_dev->memory = GSPCA_MEMORY_READ;
  1500. }
  1501. /* start streaming */
  1502. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1503. if (ret != 0)
  1504. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1505. return ret;
  1506. }
  1507. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1508. {
  1509. struct gspca_dev *gspca_dev = file->private_data;
  1510. int i, ret;
  1511. PDEBUG(D_FRAM, "poll");
  1512. poll_wait(file, &gspca_dev->wq, wait);
  1513. if (!gspca_dev->present)
  1514. return POLLERR;
  1515. /* if not streaming, the user would use read() */
  1516. if (!gspca_dev->streaming) {
  1517. if (gspca_dev->memory != GSPCA_MEMORY_NO) {
  1518. ret = POLLERR; /* not the 1st time */
  1519. goto out;
  1520. }
  1521. ret = read_alloc(gspca_dev, file);
  1522. if (ret != 0) {
  1523. ret = POLLERR;
  1524. goto out;
  1525. }
  1526. }
  1527. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
  1528. return POLLERR;
  1529. if (!gspca_dev->present) {
  1530. ret = POLLERR;
  1531. goto out;
  1532. }
  1533. /* if userptr, treat the awaiting URBs */
  1534. if (gspca_dev->memory == V4L2_MEMORY_USERPTR
  1535. && gspca_dev->capt_file == file)
  1536. isoc_transfer(gspca_dev);
  1537. i = gspca_dev->fr_o;
  1538. i = gspca_dev->fr_queue[i];
  1539. if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1540. ret = POLLIN | POLLRDNORM; /* something to read */
  1541. else
  1542. ret = 0;
  1543. out:
  1544. mutex_unlock(&gspca_dev->queue_lock);
  1545. return ret;
  1546. }
  1547. static ssize_t dev_read(struct file *file, char __user *data,
  1548. size_t count, loff_t *ppos)
  1549. {
  1550. struct gspca_dev *gspca_dev = file->private_data;
  1551. struct gspca_frame *frame;
  1552. struct v4l2_buffer v4l2_buf;
  1553. struct timeval timestamp;
  1554. int n, ret, ret2;
  1555. PDEBUG(D_FRAM, "read (%d)", count);
  1556. if (!gspca_dev->present)
  1557. return -ENODEV;
  1558. switch (gspca_dev->memory) {
  1559. case GSPCA_MEMORY_NO: /* first time */
  1560. ret = read_alloc(gspca_dev, file);
  1561. if (ret != 0)
  1562. return ret;
  1563. break;
  1564. case GSPCA_MEMORY_READ:
  1565. if (gspca_dev->capt_file == file)
  1566. break;
  1567. /* fall thru */
  1568. default:
  1569. return -EINVAL;
  1570. }
  1571. /* get a frame */
  1572. jiffies_to_timeval(get_jiffies_64(), &timestamp);
  1573. timestamp.tv_sec--;
  1574. n = 2;
  1575. for (;;) {
  1576. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1577. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1578. v4l2_buf.memory = V4L2_MEMORY_MMAP;
  1579. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1580. if (ret != 0) {
  1581. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1582. return ret;
  1583. }
  1584. /* if the process slept for more than 1 second,
  1585. * get anewer frame */
  1586. frame = &gspca_dev->frame[v4l2_buf.index];
  1587. if (--n < 0)
  1588. break; /* avoid infinite loop */
  1589. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1590. break;
  1591. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1592. if (ret != 0) {
  1593. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1594. return ret;
  1595. }
  1596. }
  1597. /* copy the frame */
  1598. if (count < frame->v4l2_buf.bytesused) {
  1599. PDEBUG(D_STREAM, "read bad count: %d < %d",
  1600. count, frame->v4l2_buf.bytesused);
  1601. /*fixme: special errno?*/
  1602. ret = -EINVAL;
  1603. goto out;
  1604. }
  1605. count = frame->v4l2_buf.bytesused;
  1606. ret = copy_to_user(data, frame->data, count);
  1607. if (ret != 0) {
  1608. PDEBUG(D_ERR|D_STREAM,
  1609. "read cp to user lack %d / %d", ret, count);
  1610. ret = -EFAULT;
  1611. goto out;
  1612. }
  1613. ret = count;
  1614. out:
  1615. /* in each case, requeue the buffer */
  1616. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1617. if (ret2 != 0)
  1618. return ret2;
  1619. return ret;
  1620. }
  1621. static void dev_release(struct video_device *vfd)
  1622. {
  1623. /* nothing */
  1624. }
  1625. static struct file_operations dev_fops = {
  1626. .owner = THIS_MODULE,
  1627. .open = dev_open,
  1628. .release = dev_close,
  1629. .read = dev_read,
  1630. .mmap = dev_mmap,
  1631. .ioctl = video_ioctl2,
  1632. #ifdef CONFIG_COMPAT
  1633. .compat_ioctl = v4l_compat_ioctl32,
  1634. #endif
  1635. .llseek = no_llseek,
  1636. .poll = dev_poll,
  1637. };
  1638. static struct video_device gspca_template = {
  1639. .name = "gspca main driver",
  1640. .type = VID_TYPE_CAPTURE,
  1641. .fops = &dev_fops,
  1642. .release = dev_release, /* mandatory */
  1643. .minor = -1,
  1644. .vidioc_querycap = vidioc_querycap,
  1645. .vidioc_dqbuf = vidioc_dqbuf,
  1646. .vidioc_qbuf = vidioc_qbuf,
  1647. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1648. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1649. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1650. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1651. .vidioc_streamon = vidioc_streamon,
  1652. .vidioc_queryctrl = vidioc_queryctrl,
  1653. .vidioc_g_ctrl = vidioc_g_ctrl,
  1654. .vidioc_s_ctrl = vidioc_s_ctrl,
  1655. .vidioc_querymenu = vidioc_querymenu,
  1656. .vidioc_enum_input = vidioc_enum_input,
  1657. .vidioc_g_input = vidioc_g_input,
  1658. .vidioc_s_input = vidioc_s_input,
  1659. .vidioc_reqbufs = vidioc_reqbufs,
  1660. .vidioc_querybuf = vidioc_querybuf,
  1661. .vidioc_streamoff = vidioc_streamoff,
  1662. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1663. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1664. .vidioc_g_parm = vidioc_g_parm,
  1665. .vidioc_s_parm = vidioc_s_parm,
  1666. .vidioc_s_std = vidioc_s_std,
  1667. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1668. .vidiocgmbuf = vidiocgmbuf,
  1669. #endif
  1670. };
  1671. /*
  1672. * probe and create a new gspca device
  1673. *
  1674. * This function must be called by the sub-driver when it is
  1675. * called for probing a new device.
  1676. */
  1677. int gspca_dev_probe(struct usb_interface *intf,
  1678. const struct usb_device_id *id,
  1679. const struct sd_desc *sd_desc,
  1680. int dev_size,
  1681. struct module *module)
  1682. {
  1683. struct usb_interface_descriptor *interface;
  1684. struct gspca_dev *gspca_dev;
  1685. struct usb_device *dev = interface_to_usbdev(intf);
  1686. int ret;
  1687. PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
  1688. /* we don't handle multi-config cameras */
  1689. if (dev->descriptor.bNumConfigurations != 1)
  1690. return -ENODEV;
  1691. interface = &intf->cur_altsetting->desc;
  1692. if (interface->bInterfaceNumber > 0)
  1693. return -ENODEV;
  1694. /* create the device */
  1695. if (dev_size < sizeof *gspca_dev)
  1696. dev_size = sizeof *gspca_dev;
  1697. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1698. if (gspca_dev == NULL) {
  1699. err("couldn't kzalloc gspca struct");
  1700. return -EIO;
  1701. }
  1702. gspca_dev->dev = dev;
  1703. gspca_dev->iface = interface->bInterfaceNumber;
  1704. gspca_dev->nbalt = intf->num_altsetting;
  1705. gspca_dev->sd_desc = sd_desc;
  1706. /* gspca_dev->users = 0; (done by kzalloc) */
  1707. gspca_dev->nbufread = 2;
  1708. /* configure the subdriver */
  1709. ret = gspca_dev->sd_desc->config(gspca_dev, id);
  1710. if (ret < 0)
  1711. goto out;
  1712. ret = gspca_set_alt0(gspca_dev);
  1713. if (ret < 0)
  1714. goto out;
  1715. gspca_set_default_mode(gspca_dev);
  1716. mutex_init(&gspca_dev->usb_lock);
  1717. mutex_init(&gspca_dev->read_lock);
  1718. mutex_init(&gspca_dev->queue_lock);
  1719. init_waitqueue_head(&gspca_dev->wq);
  1720. /* init video stuff */
  1721. memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
  1722. gspca_dev->vdev.dev = &dev->dev;
  1723. memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
  1724. gspca_dev->vdev.fops = &gspca_dev->fops;
  1725. gspca_dev->fops.owner = module; /* module protection */
  1726. ret = video_register_device(&gspca_dev->vdev,
  1727. VFL_TYPE_GRABBER,
  1728. video_nr);
  1729. if (ret < 0) {
  1730. err("video_register_device err %d", ret);
  1731. goto out;
  1732. }
  1733. gspca_dev->present = 1;
  1734. usb_set_intfdata(intf, gspca_dev);
  1735. PDEBUG(D_PROBE, "probe ok");
  1736. return 0;
  1737. out:
  1738. kfree(gspca_dev);
  1739. return ret;
  1740. }
  1741. EXPORT_SYMBOL(gspca_dev_probe);
  1742. /*
  1743. * USB disconnection
  1744. *
  1745. * This function must be called by the sub-driver
  1746. * when the device disconnects, after the specific resources are freed.
  1747. */
  1748. void gspca_disconnect(struct usb_interface *intf)
  1749. {
  1750. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1751. if (!gspca_dev)
  1752. return;
  1753. gspca_dev->present = 0;
  1754. mutex_lock(&gspca_dev->queue_lock);
  1755. mutex_lock(&gspca_dev->usb_lock);
  1756. gspca_dev->streaming = 0;
  1757. destroy_urbs(gspca_dev);
  1758. mutex_unlock(&gspca_dev->usb_lock);
  1759. mutex_unlock(&gspca_dev->queue_lock);
  1760. while (gspca_dev->users != 0) { /* wait until fully closed */
  1761. atomic_inc(&gspca_dev->nevent);
  1762. wake_up_interruptible(&gspca_dev->wq); /* wake processes */
  1763. schedule();
  1764. }
  1765. /* We don't want people trying to open up the device */
  1766. video_unregister_device(&gspca_dev->vdev);
  1767. /* Free the memory */
  1768. kfree(gspca_dev);
  1769. PDEBUG(D_PROBE, "disconnect complete");
  1770. }
  1771. EXPORT_SYMBOL(gspca_disconnect);
  1772. /* -- module insert / remove -- */
  1773. static int __init gspca_init(void)
  1774. {
  1775. info("main v%s registered", version);
  1776. return 0;
  1777. }
  1778. static void __exit gspca_exit(void)
  1779. {
  1780. info("main deregistered");
  1781. }
  1782. module_init(gspca_init);
  1783. module_exit(gspca_exit);
  1784. #ifdef VIDEO_ADV_DEBUG
  1785. module_param_named(debug, gspca_debug, int, 0644);
  1786. MODULE_PARM_DESC(debug,
  1787. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  1788. " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
  1789. " 0x0100: v4l2");
  1790. #endif
  1791. module_param(comp_fac, int, 0644);
  1792. MODULE_PARM_DESC(comp_fac,
  1793. "Buffer size ratio when compressed in percent");