gspca.c 47 KB

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