gspca.c 49 KB

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