gspca.c 51 KB

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