gspca.c 49 KB

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