gspca.c 49 KB

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