gspca.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. /*
  2. * Main USB camera driver
  3. *
  4. * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
  5. *
  6. * Camera button input handling by Márton Németh
  7. * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define GSPCA_VERSION "2.14.0"
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/string.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/io.h>
  34. #include <asm/page.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/ktime.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-ctrls.h>
  39. #include <media/v4l2-fh.h>
  40. #include <media/v4l2-event.h>
  41. #include "gspca.h"
  42. #if IS_ENABLED(CONFIG_INPUT)
  43. #include <linux/input.h>
  44. #include <linux/usb/input.h>
  45. #endif
  46. /* global values */
  47. #define DEF_NURBS 3 /* default number of URBs */
  48. #if DEF_NURBS > MAX_NURBS
  49. #error "DEF_NURBS too big"
  50. #endif
  51. MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
  52. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  53. MODULE_LICENSE("GPL");
  54. MODULE_VERSION(GSPCA_VERSION);
  55. int gspca_debug;
  56. EXPORT_SYMBOL(gspca_debug);
  57. static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
  58. __u32 pixfmt, int w, int h)
  59. {
  60. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  61. PDEBUG(debug, "%s %c%c%c%c %dx%d",
  62. txt,
  63. pixfmt & 0xff,
  64. (pixfmt >> 8) & 0xff,
  65. (pixfmt >> 16) & 0xff,
  66. pixfmt >> 24,
  67. w, h);
  68. } else {
  69. PDEBUG(debug, "%s 0x%08x %dx%d",
  70. txt,
  71. pixfmt,
  72. w, h);
  73. }
  74. }
  75. /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
  76. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  77. #define GSPCA_MEMORY_READ 7
  78. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  79. /*
  80. * VMA operations.
  81. */
  82. static void gspca_vm_open(struct vm_area_struct *vma)
  83. {
  84. struct gspca_frame *frame = vma->vm_private_data;
  85. frame->vma_use_count++;
  86. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  87. }
  88. static void gspca_vm_close(struct vm_area_struct *vma)
  89. {
  90. struct gspca_frame *frame = vma->vm_private_data;
  91. if (--frame->vma_use_count <= 0)
  92. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  93. }
  94. static const struct vm_operations_struct gspca_vm_ops = {
  95. .open = gspca_vm_open,
  96. .close = gspca_vm_close,
  97. };
  98. /*
  99. * Input and interrupt endpoint handling functions
  100. */
  101. #if IS_ENABLED(CONFIG_INPUT)
  102. static void int_irq(struct urb *urb)
  103. {
  104. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  105. int ret;
  106. ret = urb->status;
  107. switch (ret) {
  108. case 0:
  109. if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
  110. urb->transfer_buffer, urb->actual_length) < 0) {
  111. PERR("Unknown packet received");
  112. }
  113. break;
  114. case -ENOENT:
  115. case -ECONNRESET:
  116. case -ENODEV:
  117. case -ESHUTDOWN:
  118. /* Stop is requested either by software or hardware is gone,
  119. * keep the ret value non-zero and don't resubmit later.
  120. */
  121. break;
  122. default:
  123. PERR("URB error %i, resubmitting", urb->status);
  124. urb->status = 0;
  125. ret = 0;
  126. }
  127. if (ret == 0) {
  128. ret = usb_submit_urb(urb, GFP_ATOMIC);
  129. if (ret < 0)
  130. pr_err("Resubmit URB failed with error %i\n", ret);
  131. }
  132. }
  133. static int gspca_input_connect(struct gspca_dev *dev)
  134. {
  135. struct input_dev *input_dev;
  136. int err = 0;
  137. dev->input_dev = NULL;
  138. if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
  139. input_dev = input_allocate_device();
  140. if (!input_dev)
  141. return -ENOMEM;
  142. usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
  143. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  144. input_dev->name = dev->sd_desc->name;
  145. input_dev->phys = dev->phys;
  146. usb_to_input_id(dev->dev, &input_dev->id);
  147. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  148. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  149. input_dev->dev.parent = &dev->dev->dev;
  150. err = input_register_device(input_dev);
  151. if (err) {
  152. pr_err("Input device registration failed with error %i\n",
  153. err);
  154. input_dev->dev.parent = NULL;
  155. input_free_device(input_dev);
  156. } else {
  157. dev->input_dev = input_dev;
  158. }
  159. }
  160. return err;
  161. }
  162. static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
  163. struct usb_endpoint_descriptor *ep)
  164. {
  165. unsigned int buffer_len;
  166. int interval;
  167. struct urb *urb;
  168. struct usb_device *dev;
  169. void *buffer = NULL;
  170. int ret = -EINVAL;
  171. buffer_len = le16_to_cpu(ep->wMaxPacketSize);
  172. interval = ep->bInterval;
  173. PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
  174. "buffer_len=%u, interval=%u",
  175. ep->bEndpointAddress, buffer_len, interval);
  176. dev = gspca_dev->dev;
  177. urb = usb_alloc_urb(0, GFP_KERNEL);
  178. if (!urb) {
  179. ret = -ENOMEM;
  180. goto error;
  181. }
  182. buffer = usb_alloc_coherent(dev, buffer_len,
  183. GFP_KERNEL, &urb->transfer_dma);
  184. if (!buffer) {
  185. ret = -ENOMEM;
  186. goto error_buffer;
  187. }
  188. usb_fill_int_urb(urb, dev,
  189. usb_rcvintpipe(dev, ep->bEndpointAddress),
  190. buffer, buffer_len,
  191. int_irq, (void *)gspca_dev, interval);
  192. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  193. ret = usb_submit_urb(urb, GFP_KERNEL);
  194. if (ret < 0) {
  195. PERR("submit int URB failed with error %i", ret);
  196. goto error_submit;
  197. }
  198. gspca_dev->int_urb = urb;
  199. return ret;
  200. error_submit:
  201. usb_free_coherent(dev,
  202. urb->transfer_buffer_length,
  203. urb->transfer_buffer,
  204. urb->transfer_dma);
  205. error_buffer:
  206. usb_free_urb(urb);
  207. error:
  208. return ret;
  209. }
  210. static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  211. {
  212. struct usb_interface *intf;
  213. struct usb_host_interface *intf_desc;
  214. struct usb_endpoint_descriptor *ep;
  215. int i;
  216. if (gspca_dev->sd_desc->int_pkt_scan) {
  217. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  218. intf_desc = intf->cur_altsetting;
  219. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  220. ep = &intf_desc->endpoint[i].desc;
  221. if (usb_endpoint_dir_in(ep) &&
  222. usb_endpoint_xfer_int(ep)) {
  223. alloc_and_submit_int_urb(gspca_dev, ep);
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  230. {
  231. struct urb *urb;
  232. urb = gspca_dev->int_urb;
  233. if (urb) {
  234. gspca_dev->int_urb = NULL;
  235. usb_kill_urb(urb);
  236. usb_free_coherent(gspca_dev->dev,
  237. urb->transfer_buffer_length,
  238. urb->transfer_buffer,
  239. urb->transfer_dma);
  240. usb_free_urb(urb);
  241. }
  242. }
  243. #else
  244. static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  245. {
  246. }
  247. static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  248. {
  249. }
  250. static inline int gspca_input_connect(struct gspca_dev *dev)
  251. {
  252. return 0;
  253. }
  254. #endif
  255. /*
  256. * fill a video frame from an URB and resubmit
  257. */
  258. static void fill_frame(struct gspca_dev *gspca_dev,
  259. struct urb *urb)
  260. {
  261. u8 *data; /* address of data in the iso message */
  262. int i, len, st;
  263. cam_pkt_op pkt_scan;
  264. if (urb->status != 0) {
  265. if (urb->status == -ESHUTDOWN)
  266. return; /* disconnection */
  267. #ifdef CONFIG_PM
  268. if (gspca_dev->frozen)
  269. return;
  270. #endif
  271. PERR("urb status: %d", urb->status);
  272. urb->status = 0;
  273. goto resubmit;
  274. }
  275. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  276. for (i = 0; i < urb->number_of_packets; i++) {
  277. len = urb->iso_frame_desc[i].actual_length;
  278. /* check the packet status and length */
  279. st = urb->iso_frame_desc[i].status;
  280. if (st) {
  281. pr_err("ISOC data error: [%d] len=%d, status=%d\n",
  282. i, len, st);
  283. gspca_dev->last_packet_type = DISCARD_PACKET;
  284. continue;
  285. }
  286. if (len == 0) {
  287. if (gspca_dev->empty_packet == 0)
  288. gspca_dev->empty_packet = 1;
  289. continue;
  290. }
  291. /* let the packet be analyzed by the subdriver */
  292. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  293. i, urb->iso_frame_desc[i].offset, len);
  294. data = (u8 *) urb->transfer_buffer
  295. + urb->iso_frame_desc[i].offset;
  296. pkt_scan(gspca_dev, data, len);
  297. }
  298. resubmit:
  299. /* resubmit the URB */
  300. st = usb_submit_urb(urb, GFP_ATOMIC);
  301. if (st < 0)
  302. pr_err("usb_submit_urb() ret %d\n", st);
  303. }
  304. /*
  305. * ISOC message interrupt from the USB device
  306. *
  307. * Analyse each packet and call the subdriver for copy to the frame buffer.
  308. */
  309. static void isoc_irq(struct urb *urb)
  310. {
  311. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  312. PDEBUG(D_PACK, "isoc irq");
  313. if (!gspca_dev->streaming)
  314. return;
  315. fill_frame(gspca_dev, urb);
  316. }
  317. /*
  318. * bulk message interrupt from the USB device
  319. */
  320. static void bulk_irq(struct urb *urb)
  321. {
  322. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  323. int st;
  324. PDEBUG(D_PACK, "bulk irq");
  325. if (!gspca_dev->streaming)
  326. return;
  327. switch (urb->status) {
  328. case 0:
  329. break;
  330. case -ESHUTDOWN:
  331. return; /* disconnection */
  332. default:
  333. #ifdef CONFIG_PM
  334. if (gspca_dev->frozen)
  335. return;
  336. #endif
  337. PERR("urb status: %d", urb->status);
  338. urb->status = 0;
  339. goto resubmit;
  340. }
  341. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  342. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  343. urb->transfer_buffer,
  344. urb->actual_length);
  345. resubmit:
  346. /* resubmit the URB */
  347. if (gspca_dev->cam.bulk_nurbs != 0) {
  348. st = usb_submit_urb(urb, GFP_ATOMIC);
  349. if (st < 0)
  350. pr_err("usb_submit_urb() ret %d\n", st);
  351. }
  352. }
  353. /*
  354. * add data to the current frame
  355. *
  356. * This function is called by the subdrivers at interrupt level.
  357. *
  358. * To build a frame, these ones must add
  359. * - one FIRST_PACKET
  360. * - 0 or many INTER_PACKETs
  361. * - one LAST_PACKET
  362. * DISCARD_PACKET invalidates the whole frame.
  363. */
  364. void gspca_frame_add(struct gspca_dev *gspca_dev,
  365. enum gspca_packet_type packet_type,
  366. const u8 *data,
  367. int len)
  368. {
  369. struct gspca_frame *frame;
  370. int i, j;
  371. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  372. if (packet_type == FIRST_PACKET) {
  373. i = atomic_read(&gspca_dev->fr_i);
  374. /* if there are no queued buffer, discard the whole frame */
  375. if (i == atomic_read(&gspca_dev->fr_q)) {
  376. gspca_dev->last_packet_type = DISCARD_PACKET;
  377. gspca_dev->sequence++;
  378. return;
  379. }
  380. j = gspca_dev->fr_queue[i];
  381. frame = &gspca_dev->frame[j];
  382. v4l2_get_timestamp(&frame->v4l2_buf.timestamp);
  383. frame->v4l2_buf.sequence = gspca_dev->sequence++;
  384. gspca_dev->image = frame->data;
  385. gspca_dev->image_len = 0;
  386. } else {
  387. switch (gspca_dev->last_packet_type) {
  388. case DISCARD_PACKET:
  389. if (packet_type == LAST_PACKET) {
  390. gspca_dev->last_packet_type = packet_type;
  391. gspca_dev->image = NULL;
  392. gspca_dev->image_len = 0;
  393. }
  394. return;
  395. case LAST_PACKET:
  396. return;
  397. }
  398. }
  399. /* append the packet to the frame buffer */
  400. if (len > 0) {
  401. if (gspca_dev->image_len + len > gspca_dev->frsz) {
  402. PERR("frame overflow %d > %d",
  403. gspca_dev->image_len + len,
  404. gspca_dev->frsz);
  405. packet_type = DISCARD_PACKET;
  406. } else {
  407. /* !! image is NULL only when last pkt is LAST or DISCARD
  408. if (gspca_dev->image == NULL) {
  409. pr_err("gspca_frame_add() image == NULL\n");
  410. return;
  411. }
  412. */
  413. memcpy(gspca_dev->image + gspca_dev->image_len,
  414. data, len);
  415. gspca_dev->image_len += len;
  416. }
  417. }
  418. gspca_dev->last_packet_type = packet_type;
  419. /* if last packet, invalidate packet concatenation until
  420. * next first packet, wake up the application and advance
  421. * in the queue */
  422. if (packet_type == LAST_PACKET) {
  423. i = atomic_read(&gspca_dev->fr_i);
  424. j = gspca_dev->fr_queue[i];
  425. frame = &gspca_dev->frame[j];
  426. frame->v4l2_buf.bytesused = gspca_dev->image_len;
  427. frame->v4l2_buf.flags = (frame->v4l2_buf.flags
  428. | V4L2_BUF_FLAG_DONE)
  429. & ~V4L2_BUF_FLAG_QUEUED;
  430. i = (i + 1) % GSPCA_MAX_FRAMES;
  431. atomic_set(&gspca_dev->fr_i, i);
  432. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  433. PDEBUG(D_FRAM, "frame complete len:%d",
  434. frame->v4l2_buf.bytesused);
  435. gspca_dev->image = NULL;
  436. gspca_dev->image_len = 0;
  437. }
  438. }
  439. EXPORT_SYMBOL(gspca_frame_add);
  440. static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
  441. enum v4l2_memory memory, unsigned int count)
  442. {
  443. struct gspca_frame *frame;
  444. unsigned int frsz;
  445. int i;
  446. frsz = gspca_dev->pixfmt.sizeimage;
  447. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  448. frsz = PAGE_ALIGN(frsz);
  449. if (count >= GSPCA_MAX_FRAMES)
  450. count = GSPCA_MAX_FRAMES - 1;
  451. gspca_dev->frbuf = vmalloc_32(frsz * count);
  452. if (!gspca_dev->frbuf) {
  453. pr_err("frame alloc failed\n");
  454. return -ENOMEM;
  455. }
  456. gspca_dev->capt_file = file;
  457. gspca_dev->memory = memory;
  458. gspca_dev->frsz = frsz;
  459. gspca_dev->nframes = count;
  460. for (i = 0; i < count; i++) {
  461. frame = &gspca_dev->frame[i];
  462. frame->v4l2_buf.index = i;
  463. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  464. frame->v4l2_buf.flags = 0;
  465. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  466. frame->v4l2_buf.length = frsz;
  467. frame->v4l2_buf.memory = memory;
  468. frame->v4l2_buf.sequence = 0;
  469. frame->data = gspca_dev->frbuf + i * frsz;
  470. frame->v4l2_buf.m.offset = i * frsz;
  471. }
  472. atomic_set(&gspca_dev->fr_q, 0);
  473. atomic_set(&gspca_dev->fr_i, 0);
  474. gspca_dev->fr_o = 0;
  475. return 0;
  476. }
  477. static void frame_free(struct gspca_dev *gspca_dev)
  478. {
  479. int i;
  480. PDEBUG(D_STREAM, "frame free");
  481. if (gspca_dev->frbuf != NULL) {
  482. vfree(gspca_dev->frbuf);
  483. gspca_dev->frbuf = NULL;
  484. for (i = 0; i < gspca_dev->nframes; i++)
  485. gspca_dev->frame[i].data = NULL;
  486. }
  487. gspca_dev->nframes = 0;
  488. gspca_dev->frsz = 0;
  489. gspca_dev->capt_file = NULL;
  490. gspca_dev->memory = GSPCA_MEMORY_NO;
  491. }
  492. static void destroy_urbs(struct gspca_dev *gspca_dev)
  493. {
  494. struct urb *urb;
  495. unsigned int i;
  496. PDEBUG(D_STREAM, "kill transfer");
  497. for (i = 0; i < MAX_NURBS; i++) {
  498. urb = gspca_dev->urb[i];
  499. if (urb == NULL)
  500. break;
  501. gspca_dev->urb[i] = NULL;
  502. usb_kill_urb(urb);
  503. usb_free_coherent(gspca_dev->dev,
  504. urb->transfer_buffer_length,
  505. urb->transfer_buffer,
  506. urb->transfer_dma);
  507. usb_free_urb(urb);
  508. }
  509. }
  510. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  511. {
  512. int ret;
  513. if (gspca_dev->alt == 0)
  514. return 0;
  515. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  516. if (ret < 0)
  517. pr_err("set alt 0 err %d\n", ret);
  518. return ret;
  519. }
  520. /* Note: both the queue and the usb locks should be held when calling this */
  521. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  522. {
  523. gspca_dev->streaming = 0;
  524. gspca_dev->usb_err = 0;
  525. if (gspca_dev->sd_desc->stopN)
  526. gspca_dev->sd_desc->stopN(gspca_dev);
  527. destroy_urbs(gspca_dev);
  528. gspca_input_destroy_urb(gspca_dev);
  529. gspca_set_alt0(gspca_dev);
  530. gspca_input_create_urb(gspca_dev);
  531. if (gspca_dev->sd_desc->stop0)
  532. gspca_dev->sd_desc->stop0(gspca_dev);
  533. PDEBUG(D_STREAM, "stream off OK");
  534. }
  535. /*
  536. * look for an input transfer endpoint in an alternate setting.
  537. *
  538. * If xfer_ep is invalid, return the first valid ep found, otherwise
  539. * look for exactly the ep with address equal to xfer_ep.
  540. */
  541. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  542. int xfer, int xfer_ep)
  543. {
  544. struct usb_host_endpoint *ep;
  545. int i, attr;
  546. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  547. ep = &alt->endpoint[i];
  548. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  549. if (attr == xfer
  550. && ep->desc.wMaxPacketSize != 0
  551. && usb_endpoint_dir_in(&ep->desc)
  552. && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
  553. return ep;
  554. }
  555. return NULL;
  556. }
  557. /* compute the minimum bandwidth for the current transfer */
  558. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  559. {
  560. u32 bandwidth;
  561. /* get the (max) image size */
  562. bandwidth = gspca_dev->pixfmt.sizeimage;
  563. /* if the image is compressed, estimate its mean size */
  564. if (!gspca_dev->cam.needs_full_bandwidth &&
  565. bandwidth < gspca_dev->pixfmt.width *
  566. gspca_dev->pixfmt.height)
  567. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  568. /* estimate the frame rate */
  569. if (gspca_dev->sd_desc->get_streamparm) {
  570. struct v4l2_streamparm parm;
  571. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  572. bandwidth *= parm.parm.capture.timeperframe.denominator;
  573. bandwidth /= parm.parm.capture.timeperframe.numerator;
  574. } else {
  575. /* don't hope more than 15 fps with USB 1.1 and
  576. * image resolution >= 640x480 */
  577. if (gspca_dev->pixfmt.width >= 640
  578. && gspca_dev->dev->speed == USB_SPEED_FULL)
  579. bandwidth *= 15; /* 15 fps */
  580. else
  581. bandwidth *= 30; /* 30 fps */
  582. }
  583. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  584. return bandwidth;
  585. }
  586. /* endpoint table */
  587. #define MAX_ALT 16
  588. struct ep_tb_s {
  589. u32 alt;
  590. u32 bandwidth;
  591. };
  592. /*
  593. * build the table of the endpoints
  594. * and compute the minimum bandwidth for the image transfer
  595. */
  596. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  597. struct usb_interface *intf,
  598. struct ep_tb_s *ep_tb)
  599. {
  600. struct usb_host_endpoint *ep;
  601. int i, j, nbalt, psize, found;
  602. u32 bandwidth, last_bw;
  603. nbalt = intf->num_altsetting;
  604. if (nbalt > MAX_ALT)
  605. nbalt = MAX_ALT; /* fixme: should warn */
  606. /* build the endpoint table */
  607. i = 0;
  608. last_bw = 0;
  609. for (;;) {
  610. ep_tb->bandwidth = 2000 * 2000 * 120;
  611. found = 0;
  612. for (j = 0; j < nbalt; j++) {
  613. ep = alt_xfer(&intf->altsetting[j],
  614. USB_ENDPOINT_XFER_ISOC,
  615. gspca_dev->xfer_ep);
  616. if (ep == NULL)
  617. continue;
  618. if (ep->desc.bInterval == 0) {
  619. pr_err("alt %d iso endp with 0 interval\n", j);
  620. continue;
  621. }
  622. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  623. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  624. bandwidth = psize * 1000;
  625. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  626. || gspca_dev->dev->speed == USB_SPEED_SUPER)
  627. bandwidth *= 8;
  628. bandwidth /= 1 << (ep->desc.bInterval - 1);
  629. if (bandwidth <= last_bw)
  630. continue;
  631. if (bandwidth < ep_tb->bandwidth) {
  632. ep_tb->bandwidth = bandwidth;
  633. ep_tb->alt = j;
  634. found = 1;
  635. }
  636. }
  637. if (!found)
  638. break;
  639. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  640. ep_tb->alt, ep_tb->bandwidth);
  641. last_bw = ep_tb->bandwidth;
  642. i++;
  643. ep_tb++;
  644. }
  645. /*
  646. * If the camera:
  647. * has a usb audio class interface (a built in usb mic); and
  648. * is a usb 1 full speed device; and
  649. * uses the max full speed iso bandwidth; and
  650. * and has more than 1 alt setting
  651. * then skip the highest alt setting to spare bandwidth for the mic
  652. */
  653. if (gspca_dev->audio &&
  654. gspca_dev->dev->speed == USB_SPEED_FULL &&
  655. last_bw >= 1000000 &&
  656. i > 1) {
  657. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  658. i--;
  659. ep_tb--;
  660. }
  661. /* get the requested bandwidth and start at the highest atlsetting */
  662. bandwidth = which_bandwidth(gspca_dev);
  663. ep_tb--;
  664. while (i > 1) {
  665. ep_tb--;
  666. if (ep_tb->bandwidth < bandwidth)
  667. break;
  668. i--;
  669. }
  670. return i;
  671. }
  672. /*
  673. * create the URBs for image transfer
  674. */
  675. static int create_urbs(struct gspca_dev *gspca_dev,
  676. struct usb_host_endpoint *ep)
  677. {
  678. struct urb *urb;
  679. int n, nurbs, i, psize, npkt, bsize;
  680. /* calculate the packet size and the number of packets */
  681. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  682. if (!gspca_dev->cam.bulk) { /* isoc */
  683. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  684. if (gspca_dev->pkt_size == 0)
  685. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  686. else
  687. psize = gspca_dev->pkt_size;
  688. npkt = gspca_dev->cam.npkt;
  689. if (npkt == 0)
  690. npkt = 32; /* default value */
  691. bsize = psize * npkt;
  692. PDEBUG(D_STREAM,
  693. "isoc %d pkts size %d = bsize:%d",
  694. npkt, psize, bsize);
  695. nurbs = DEF_NURBS;
  696. } else { /* bulk */
  697. npkt = 0;
  698. bsize = gspca_dev->cam.bulk_size;
  699. if (bsize == 0)
  700. bsize = psize;
  701. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  702. if (gspca_dev->cam.bulk_nurbs != 0)
  703. nurbs = gspca_dev->cam.bulk_nurbs;
  704. else
  705. nurbs = 1;
  706. }
  707. for (n = 0; n < nurbs; n++) {
  708. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  709. if (!urb) {
  710. pr_err("usb_alloc_urb failed\n");
  711. return -ENOMEM;
  712. }
  713. gspca_dev->urb[n] = urb;
  714. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  715. bsize,
  716. GFP_KERNEL,
  717. &urb->transfer_dma);
  718. if (urb->transfer_buffer == NULL) {
  719. pr_err("usb_alloc_coherent failed\n");
  720. return -ENOMEM;
  721. }
  722. urb->dev = gspca_dev->dev;
  723. urb->context = gspca_dev;
  724. urb->transfer_buffer_length = bsize;
  725. if (npkt != 0) { /* ISOC */
  726. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  727. ep->desc.bEndpointAddress);
  728. urb->transfer_flags = URB_ISO_ASAP
  729. | URB_NO_TRANSFER_DMA_MAP;
  730. urb->interval = 1 << (ep->desc.bInterval - 1);
  731. urb->complete = isoc_irq;
  732. urb->number_of_packets = npkt;
  733. for (i = 0; i < npkt; i++) {
  734. urb->iso_frame_desc[i].length = psize;
  735. urb->iso_frame_desc[i].offset = psize * i;
  736. }
  737. } else { /* bulk */
  738. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  739. ep->desc.bEndpointAddress);
  740. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  741. urb->complete = bulk_irq;
  742. }
  743. }
  744. return 0;
  745. }
  746. /*
  747. * start the USB transfer
  748. */
  749. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  750. {
  751. struct usb_interface *intf;
  752. struct usb_host_endpoint *ep;
  753. struct urb *urb;
  754. struct ep_tb_s ep_tb[MAX_ALT];
  755. int n, ret, xfer, alt, alt_idx;
  756. /* reset the streaming variables */
  757. gspca_dev->image = NULL;
  758. gspca_dev->image_len = 0;
  759. gspca_dev->last_packet_type = DISCARD_PACKET;
  760. gspca_dev->sequence = 0;
  761. gspca_dev->usb_err = 0;
  762. /* do the specific subdriver stuff before endpoint selection */
  763. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  764. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  765. if (gspca_dev->sd_desc->isoc_init) {
  766. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  767. if (ret < 0)
  768. return ret;
  769. }
  770. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  771. : USB_ENDPOINT_XFER_ISOC;
  772. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  773. if (gspca_dev->alt != 0) {
  774. gspca_dev->alt--; /* (previous version compatibility) */
  775. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
  776. gspca_dev->xfer_ep);
  777. if (ep == NULL) {
  778. pr_err("bad altsetting %d\n", gspca_dev->alt);
  779. return -EIO;
  780. }
  781. ep_tb[0].alt = gspca_dev->alt;
  782. alt_idx = 1;
  783. } else {
  784. /* else, compute the minimum bandwidth
  785. * and build the endpoint table */
  786. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  787. if (alt_idx <= 0) {
  788. pr_err("no transfer endpoint found\n");
  789. return -EIO;
  790. }
  791. }
  792. /* set the highest alternate setting and
  793. * loop until urb submit succeeds */
  794. gspca_input_destroy_urb(gspca_dev);
  795. gspca_dev->alt = ep_tb[--alt_idx].alt;
  796. alt = -1;
  797. for (;;) {
  798. if (alt != gspca_dev->alt) {
  799. alt = gspca_dev->alt;
  800. if (intf->num_altsetting > 1) {
  801. ret = usb_set_interface(gspca_dev->dev,
  802. gspca_dev->iface,
  803. alt);
  804. if (ret < 0) {
  805. if (ret == -ENOSPC)
  806. goto retry; /*fixme: ugly*/
  807. pr_err("set alt %d err %d\n", alt, ret);
  808. goto out;
  809. }
  810. }
  811. }
  812. if (!gspca_dev->cam.no_urb_create) {
  813. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  814. ret = create_urbs(gspca_dev,
  815. alt_xfer(&intf->altsetting[alt], xfer,
  816. gspca_dev->xfer_ep));
  817. if (ret < 0) {
  818. destroy_urbs(gspca_dev);
  819. goto out;
  820. }
  821. }
  822. /* clear the bulk endpoint */
  823. if (gspca_dev->cam.bulk)
  824. usb_clear_halt(gspca_dev->dev,
  825. gspca_dev->urb[0]->pipe);
  826. /* start the cam */
  827. ret = gspca_dev->sd_desc->start(gspca_dev);
  828. if (ret < 0) {
  829. destroy_urbs(gspca_dev);
  830. goto out;
  831. }
  832. gspca_dev->streaming = 1;
  833. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  834. /* some bulk transfers are started by the subdriver */
  835. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  836. break;
  837. /* submit the URBs */
  838. for (n = 0; n < MAX_NURBS; n++) {
  839. urb = gspca_dev->urb[n];
  840. if (urb == NULL)
  841. break;
  842. ret = usb_submit_urb(urb, GFP_KERNEL);
  843. if (ret < 0)
  844. break;
  845. }
  846. if (ret >= 0)
  847. break; /* transfer is started */
  848. /* something when wrong
  849. * stop the webcam and free the transfer resources */
  850. gspca_stream_off(gspca_dev);
  851. if (ret != -ENOSPC) {
  852. pr_err("usb_submit_urb alt %d err %d\n",
  853. gspca_dev->alt, ret);
  854. goto out;
  855. }
  856. /* the bandwidth is not wide enough
  857. * negotiate or try a lower alternate setting */
  858. retry:
  859. PERR("alt %d - bandwidth not wide enough, trying again", alt);
  860. msleep(20); /* wait for kill complete */
  861. if (gspca_dev->sd_desc->isoc_nego) {
  862. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  863. if (ret < 0)
  864. goto out;
  865. } else {
  866. if (alt_idx <= 0) {
  867. pr_err("no transfer endpoint found\n");
  868. ret = -EIO;
  869. goto out;
  870. }
  871. gspca_dev->alt = ep_tb[--alt_idx].alt;
  872. }
  873. }
  874. out:
  875. gspca_input_create_urb(gspca_dev);
  876. return ret;
  877. }
  878. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  879. {
  880. int i;
  881. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  882. gspca_dev->curr_mode = i;
  883. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
  884. /* does nothing if ctrl_handler == NULL */
  885. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  886. }
  887. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  888. int width, int height)
  889. {
  890. int i;
  891. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  892. if (width >= gspca_dev->cam.cam_mode[i].width
  893. && height >= gspca_dev->cam.cam_mode[i].height)
  894. break;
  895. }
  896. return i;
  897. }
  898. /*
  899. * search a mode with the right pixel format
  900. */
  901. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  902. int mode,
  903. int pixfmt)
  904. {
  905. int modeU, modeD;
  906. modeU = modeD = mode;
  907. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  908. if (--modeD >= 0) {
  909. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  910. == pixfmt)
  911. return modeD;
  912. }
  913. if (++modeU < gspca_dev->cam.nmodes) {
  914. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  915. == pixfmt)
  916. return modeU;
  917. }
  918. }
  919. return -EINVAL;
  920. }
  921. #ifdef CONFIG_VIDEO_ADV_DEBUG
  922. static int vidioc_g_chip_info(struct file *file, void *priv,
  923. struct v4l2_dbg_chip_info *chip)
  924. {
  925. struct gspca_dev *gspca_dev = video_drvdata(file);
  926. gspca_dev->usb_err = 0;
  927. if (gspca_dev->sd_desc->get_chip_info)
  928. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  929. return chip->match.addr ? -EINVAL : 0;
  930. }
  931. static int vidioc_g_register(struct file *file, void *priv,
  932. struct v4l2_dbg_register *reg)
  933. {
  934. struct gspca_dev *gspca_dev = video_drvdata(file);
  935. gspca_dev->usb_err = 0;
  936. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  937. }
  938. static int vidioc_s_register(struct file *file, void *priv,
  939. const struct v4l2_dbg_register *reg)
  940. {
  941. struct gspca_dev *gspca_dev = video_drvdata(file);
  942. gspca_dev->usb_err = 0;
  943. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  944. }
  945. #endif
  946. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  947. struct v4l2_fmtdesc *fmtdesc)
  948. {
  949. struct gspca_dev *gspca_dev = video_drvdata(file);
  950. int i, j, index;
  951. __u32 fmt_tb[8];
  952. /* give an index to each format */
  953. index = 0;
  954. j = 0;
  955. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  956. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  957. j = 0;
  958. for (;;) {
  959. if (fmt_tb[j] == fmt_tb[index])
  960. break;
  961. j++;
  962. }
  963. if (j == index) {
  964. if (fmtdesc->index == index)
  965. break; /* new format */
  966. index++;
  967. if (index >= ARRAY_SIZE(fmt_tb))
  968. return -EINVAL;
  969. }
  970. }
  971. if (i < 0)
  972. return -EINVAL; /* no more format */
  973. fmtdesc->pixelformat = fmt_tb[index];
  974. if (gspca_dev->cam.cam_mode[i].sizeimage <
  975. gspca_dev->cam.cam_mode[i].width *
  976. gspca_dev->cam.cam_mode[i].height)
  977. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  978. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  979. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  980. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  981. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  982. fmtdesc->description[4] = '\0';
  983. return 0;
  984. }
  985. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  986. struct v4l2_format *fmt)
  987. {
  988. struct gspca_dev *gspca_dev = video_drvdata(file);
  989. fmt->fmt.pix = gspca_dev->pixfmt;
  990. /* some drivers use priv internally, zero it before giving it back to
  991. the core */
  992. fmt->fmt.pix.priv = 0;
  993. return 0;
  994. }
  995. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  996. struct v4l2_format *fmt)
  997. {
  998. int w, h, mode, mode2;
  999. w = fmt->fmt.pix.width;
  1000. h = fmt->fmt.pix.height;
  1001. PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
  1002. fmt->fmt.pix.pixelformat, w, h);
  1003. /* search the closest mode for width and height */
  1004. mode = wxh_to_mode(gspca_dev, w, h);
  1005. /* OK if right palette */
  1006. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1007. != fmt->fmt.pix.pixelformat) {
  1008. /* else, search the closest mode with the same pixel format */
  1009. mode2 = gspca_get_mode(gspca_dev, mode,
  1010. fmt->fmt.pix.pixelformat);
  1011. if (mode2 >= 0)
  1012. mode = mode2;
  1013. }
  1014. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1015. if (gspca_dev->sd_desc->try_fmt) {
  1016. /* pass original resolution to subdriver try_fmt */
  1017. fmt->fmt.pix.width = w;
  1018. fmt->fmt.pix.height = h;
  1019. gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
  1020. }
  1021. /* some drivers use priv internally, zero it before giving it back to
  1022. the core */
  1023. fmt->fmt.pix.priv = 0;
  1024. return mode; /* used when s_fmt */
  1025. }
  1026. static int vidioc_try_fmt_vid_cap(struct file *file,
  1027. void *priv,
  1028. struct v4l2_format *fmt)
  1029. {
  1030. struct gspca_dev *gspca_dev = video_drvdata(file);
  1031. int ret;
  1032. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1033. if (ret < 0)
  1034. return ret;
  1035. return 0;
  1036. }
  1037. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1038. struct v4l2_format *fmt)
  1039. {
  1040. struct gspca_dev *gspca_dev = video_drvdata(file);
  1041. int ret;
  1042. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1043. return -ERESTARTSYS;
  1044. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1045. if (ret < 0)
  1046. goto out;
  1047. if (gspca_dev->nframes != 0
  1048. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1049. ret = -EINVAL;
  1050. goto out;
  1051. }
  1052. if (gspca_dev->streaming) {
  1053. ret = -EBUSY;
  1054. goto out;
  1055. }
  1056. gspca_dev->curr_mode = ret;
  1057. if (gspca_dev->sd_desc->try_fmt)
  1058. /* subdriver try_fmt can modify format parameters */
  1059. gspca_dev->pixfmt = fmt->fmt.pix;
  1060. else
  1061. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret];
  1062. ret = 0;
  1063. out:
  1064. mutex_unlock(&gspca_dev->queue_lock);
  1065. return ret;
  1066. }
  1067. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1068. struct v4l2_frmsizeenum *fsize)
  1069. {
  1070. struct gspca_dev *gspca_dev = video_drvdata(file);
  1071. int i;
  1072. __u32 index = 0;
  1073. if (gspca_dev->sd_desc->enum_framesizes)
  1074. return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
  1075. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1076. if (fsize->pixel_format !=
  1077. gspca_dev->cam.cam_mode[i].pixelformat)
  1078. continue;
  1079. if (fsize->index == index) {
  1080. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1081. fsize->discrete.width =
  1082. gspca_dev->cam.cam_mode[i].width;
  1083. fsize->discrete.height =
  1084. gspca_dev->cam.cam_mode[i].height;
  1085. return 0;
  1086. }
  1087. index++;
  1088. }
  1089. return -EINVAL;
  1090. }
  1091. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1092. struct v4l2_frmivalenum *fival)
  1093. {
  1094. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1095. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1096. __u32 i;
  1097. if (gspca_dev->cam.mode_framerates == NULL ||
  1098. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1099. return -EINVAL;
  1100. if (fival->pixel_format !=
  1101. gspca_dev->cam.cam_mode[mode].pixelformat)
  1102. return -EINVAL;
  1103. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1104. if (fival->index == i) {
  1105. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1106. fival->discrete.numerator = 1;
  1107. fival->discrete.denominator =
  1108. gspca_dev->cam.mode_framerates[mode].rates[i];
  1109. return 0;
  1110. }
  1111. }
  1112. return -EINVAL;
  1113. }
  1114. static void gspca_release(struct v4l2_device *v4l2_device)
  1115. {
  1116. struct gspca_dev *gspca_dev =
  1117. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1118. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1119. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1120. kfree(gspca_dev->usb_buf);
  1121. kfree(gspca_dev);
  1122. }
  1123. static int dev_open(struct file *file)
  1124. {
  1125. struct gspca_dev *gspca_dev = video_drvdata(file);
  1126. int ret;
  1127. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1128. /* protect the subdriver against rmmod */
  1129. if (!try_module_get(gspca_dev->module))
  1130. return -ENODEV;
  1131. ret = v4l2_fh_open(file);
  1132. if (ret)
  1133. module_put(gspca_dev->module);
  1134. return ret;
  1135. }
  1136. static int dev_close(struct file *file)
  1137. {
  1138. struct gspca_dev *gspca_dev = video_drvdata(file);
  1139. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1140. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1141. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1142. return -ERESTARTSYS;
  1143. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1144. mutex_unlock(&gspca_dev->usb_lock);
  1145. return -ERESTARTSYS;
  1146. }
  1147. /* if the file did the capture, free the streaming resources */
  1148. if (gspca_dev->capt_file == file) {
  1149. if (gspca_dev->streaming)
  1150. gspca_stream_off(gspca_dev);
  1151. frame_free(gspca_dev);
  1152. }
  1153. module_put(gspca_dev->module);
  1154. mutex_unlock(&gspca_dev->queue_lock);
  1155. mutex_unlock(&gspca_dev->usb_lock);
  1156. PDEBUG(D_STREAM, "close done");
  1157. return v4l2_fh_release(file);
  1158. }
  1159. static int vidioc_querycap(struct file *file, void *priv,
  1160. struct v4l2_capability *cap)
  1161. {
  1162. struct gspca_dev *gspca_dev = video_drvdata(file);
  1163. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1164. sizeof cap->driver);
  1165. if (gspca_dev->dev->product != NULL) {
  1166. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1167. sizeof cap->card);
  1168. } else {
  1169. snprintf((char *) cap->card, sizeof cap->card,
  1170. "USB Camera (%04x:%04x)",
  1171. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1172. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1173. }
  1174. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1175. sizeof(cap->bus_info));
  1176. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1177. | V4L2_CAP_STREAMING
  1178. | V4L2_CAP_READWRITE;
  1179. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1180. return 0;
  1181. }
  1182. static int vidioc_enum_input(struct file *file, void *priv,
  1183. struct v4l2_input *input)
  1184. {
  1185. struct gspca_dev *gspca_dev = video_drvdata(file);
  1186. if (input->index != 0)
  1187. return -EINVAL;
  1188. input->type = V4L2_INPUT_TYPE_CAMERA;
  1189. input->status = gspca_dev->cam.input_flags;
  1190. strlcpy(input->name, gspca_dev->sd_desc->name,
  1191. sizeof input->name);
  1192. return 0;
  1193. }
  1194. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1195. {
  1196. *i = 0;
  1197. return 0;
  1198. }
  1199. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1200. {
  1201. if (i > 0)
  1202. return -EINVAL;
  1203. return (0);
  1204. }
  1205. static int vidioc_reqbufs(struct file *file, void *priv,
  1206. struct v4l2_requestbuffers *rb)
  1207. {
  1208. struct gspca_dev *gspca_dev = video_drvdata(file);
  1209. int i, ret = 0, streaming;
  1210. i = rb->memory; /* (avoid compilation warning) */
  1211. switch (i) {
  1212. case GSPCA_MEMORY_READ: /* (internal call) */
  1213. case V4L2_MEMORY_MMAP:
  1214. case V4L2_MEMORY_USERPTR:
  1215. break;
  1216. default:
  1217. return -EINVAL;
  1218. }
  1219. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1220. return -ERESTARTSYS;
  1221. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1222. && gspca_dev->memory != GSPCA_MEMORY_READ
  1223. && gspca_dev->memory != rb->memory) {
  1224. ret = -EBUSY;
  1225. goto out;
  1226. }
  1227. /* only one file may do the capture */
  1228. if (gspca_dev->capt_file != NULL
  1229. && gspca_dev->capt_file != file) {
  1230. ret = -EBUSY;
  1231. goto out;
  1232. }
  1233. /* if allocated, the buffers must not be mapped */
  1234. for (i = 0; i < gspca_dev->nframes; i++) {
  1235. if (gspca_dev->frame[i].vma_use_count) {
  1236. ret = -EBUSY;
  1237. goto out;
  1238. }
  1239. }
  1240. /* stop streaming */
  1241. streaming = gspca_dev->streaming;
  1242. if (streaming) {
  1243. gspca_stream_off(gspca_dev);
  1244. /* Don't restart the stream when switching from read
  1245. * to mmap mode */
  1246. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1247. streaming = 0;
  1248. }
  1249. /* free the previous allocated buffers, if any */
  1250. if (gspca_dev->nframes != 0)
  1251. frame_free(gspca_dev);
  1252. if (rb->count == 0) /* unrequest */
  1253. goto out;
  1254. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1255. if (ret == 0) {
  1256. rb->count = gspca_dev->nframes;
  1257. if (streaming)
  1258. ret = gspca_init_transfer(gspca_dev);
  1259. }
  1260. out:
  1261. mutex_unlock(&gspca_dev->queue_lock);
  1262. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1263. return ret;
  1264. }
  1265. static int vidioc_querybuf(struct file *file, void *priv,
  1266. struct v4l2_buffer *v4l2_buf)
  1267. {
  1268. struct gspca_dev *gspca_dev = video_drvdata(file);
  1269. struct gspca_frame *frame;
  1270. if (v4l2_buf->index >= gspca_dev->nframes)
  1271. return -EINVAL;
  1272. frame = &gspca_dev->frame[v4l2_buf->index];
  1273. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1274. return 0;
  1275. }
  1276. static int vidioc_streamon(struct file *file, void *priv,
  1277. enum v4l2_buf_type buf_type)
  1278. {
  1279. struct gspca_dev *gspca_dev = video_drvdata(file);
  1280. int ret;
  1281. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1282. return -EINVAL;
  1283. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1284. return -ERESTARTSYS;
  1285. /* check the capture file */
  1286. if (gspca_dev->capt_file != file) {
  1287. ret = -EBUSY;
  1288. goto out;
  1289. }
  1290. if (gspca_dev->nframes == 0
  1291. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1292. ret = -EINVAL;
  1293. goto out;
  1294. }
  1295. if (!gspca_dev->streaming) {
  1296. ret = gspca_init_transfer(gspca_dev);
  1297. if (ret < 0)
  1298. goto out;
  1299. }
  1300. PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK",
  1301. gspca_dev->pixfmt.pixelformat,
  1302. gspca_dev->pixfmt.width, gspca_dev->pixfmt.height);
  1303. ret = 0;
  1304. out:
  1305. mutex_unlock(&gspca_dev->queue_lock);
  1306. return ret;
  1307. }
  1308. static int vidioc_streamoff(struct file *file, void *priv,
  1309. enum v4l2_buf_type buf_type)
  1310. {
  1311. struct gspca_dev *gspca_dev = video_drvdata(file);
  1312. int i, ret;
  1313. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1314. return -EINVAL;
  1315. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1316. return -ERESTARTSYS;
  1317. if (!gspca_dev->streaming) {
  1318. ret = 0;
  1319. goto out;
  1320. }
  1321. /* check the capture file */
  1322. if (gspca_dev->capt_file != file) {
  1323. ret = -EBUSY;
  1324. goto out;
  1325. }
  1326. /* stop streaming */
  1327. gspca_stream_off(gspca_dev);
  1328. /* In case another thread is waiting in dqbuf */
  1329. wake_up_interruptible(&gspca_dev->wq);
  1330. /* empty the transfer queues */
  1331. for (i = 0; i < gspca_dev->nframes; i++)
  1332. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1333. atomic_set(&gspca_dev->fr_q, 0);
  1334. atomic_set(&gspca_dev->fr_i, 0);
  1335. gspca_dev->fr_o = 0;
  1336. ret = 0;
  1337. out:
  1338. mutex_unlock(&gspca_dev->queue_lock);
  1339. return ret;
  1340. }
  1341. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1342. struct v4l2_jpegcompression *jpegcomp)
  1343. {
  1344. struct gspca_dev *gspca_dev = video_drvdata(file);
  1345. gspca_dev->usb_err = 0;
  1346. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1347. }
  1348. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1349. const struct v4l2_jpegcompression *jpegcomp)
  1350. {
  1351. struct gspca_dev *gspca_dev = video_drvdata(file);
  1352. gspca_dev->usb_err = 0;
  1353. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1354. }
  1355. static int vidioc_g_parm(struct file *filp, void *priv,
  1356. struct v4l2_streamparm *parm)
  1357. {
  1358. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1359. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1360. if (gspca_dev->sd_desc->get_streamparm) {
  1361. gspca_dev->usb_err = 0;
  1362. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1363. return gspca_dev->usb_err;
  1364. }
  1365. return 0;
  1366. }
  1367. static int vidioc_s_parm(struct file *filp, void *priv,
  1368. struct v4l2_streamparm *parm)
  1369. {
  1370. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1371. unsigned int n;
  1372. n = parm->parm.capture.readbuffers;
  1373. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1374. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1375. else
  1376. gspca_dev->nbufread = n;
  1377. if (gspca_dev->sd_desc->set_streamparm) {
  1378. gspca_dev->usb_err = 0;
  1379. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1380. return gspca_dev->usb_err;
  1381. }
  1382. return 0;
  1383. }
  1384. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1385. {
  1386. struct gspca_dev *gspca_dev = video_drvdata(file);
  1387. struct gspca_frame *frame;
  1388. struct page *page;
  1389. unsigned long addr, start, size;
  1390. int i, ret;
  1391. start = vma->vm_start;
  1392. size = vma->vm_end - vma->vm_start;
  1393. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1394. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1395. return -ERESTARTSYS;
  1396. if (gspca_dev->capt_file != file) {
  1397. ret = -EINVAL;
  1398. goto out;
  1399. }
  1400. frame = NULL;
  1401. for (i = 0; i < gspca_dev->nframes; ++i) {
  1402. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1403. PDEBUG(D_STREAM, "mmap bad memory type");
  1404. break;
  1405. }
  1406. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1407. == vma->vm_pgoff) {
  1408. frame = &gspca_dev->frame[i];
  1409. break;
  1410. }
  1411. }
  1412. if (frame == NULL) {
  1413. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1414. ret = -EINVAL;
  1415. goto out;
  1416. }
  1417. if (size != frame->v4l2_buf.length) {
  1418. PDEBUG(D_STREAM, "mmap bad size");
  1419. ret = -EINVAL;
  1420. goto out;
  1421. }
  1422. /*
  1423. * - VM_IO marks the area as being a mmaped region for I/O to a
  1424. * device. It also prevents the region from being core dumped.
  1425. */
  1426. vma->vm_flags |= VM_IO;
  1427. addr = (unsigned long) frame->data;
  1428. while (size > 0) {
  1429. page = vmalloc_to_page((void *) addr);
  1430. ret = vm_insert_page(vma, start, page);
  1431. if (ret < 0)
  1432. goto out;
  1433. start += PAGE_SIZE;
  1434. addr += PAGE_SIZE;
  1435. size -= PAGE_SIZE;
  1436. }
  1437. vma->vm_ops = &gspca_vm_ops;
  1438. vma->vm_private_data = frame;
  1439. gspca_vm_open(vma);
  1440. ret = 0;
  1441. out:
  1442. mutex_unlock(&gspca_dev->queue_lock);
  1443. return ret;
  1444. }
  1445. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1446. enum v4l2_memory memory)
  1447. {
  1448. if (!gspca_dev->present)
  1449. return -ENODEV;
  1450. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1451. !gspca_dev->streaming)
  1452. return -EINVAL;
  1453. /* check if a frame is ready */
  1454. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1455. }
  1456. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1457. enum v4l2_memory memory)
  1458. {
  1459. int ret;
  1460. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1461. return -ERESTARTSYS;
  1462. ret = frame_ready_nolock(gspca_dev, file, memory);
  1463. mutex_unlock(&gspca_dev->queue_lock);
  1464. return ret;
  1465. }
  1466. /*
  1467. * dequeue a video buffer
  1468. *
  1469. * If nonblock_ing is false, block until a buffer is available.
  1470. */
  1471. static int vidioc_dqbuf(struct file *file, void *priv,
  1472. struct v4l2_buffer *v4l2_buf)
  1473. {
  1474. struct gspca_dev *gspca_dev = video_drvdata(file);
  1475. struct gspca_frame *frame;
  1476. int i, j, ret;
  1477. PDEBUG(D_FRAM, "dqbuf");
  1478. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1479. return -ERESTARTSYS;
  1480. for (;;) {
  1481. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1482. if (ret < 0)
  1483. goto out;
  1484. if (ret > 0)
  1485. break;
  1486. mutex_unlock(&gspca_dev->queue_lock);
  1487. if (file->f_flags & O_NONBLOCK)
  1488. return -EAGAIN;
  1489. /* wait till a frame is ready */
  1490. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1491. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1492. msecs_to_jiffies(3000));
  1493. if (ret < 0)
  1494. return ret;
  1495. if (ret == 0)
  1496. return -EIO;
  1497. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1498. return -ERESTARTSYS;
  1499. }
  1500. i = gspca_dev->fr_o;
  1501. j = gspca_dev->fr_queue[i];
  1502. frame = &gspca_dev->frame[j];
  1503. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1504. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1505. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1506. PDEBUG(D_FRAM, "dqbuf %d", j);
  1507. ret = 0;
  1508. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1509. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1510. frame->data,
  1511. frame->v4l2_buf.bytesused)) {
  1512. PERR("dqbuf cp to user failed");
  1513. ret = -EFAULT;
  1514. }
  1515. }
  1516. out:
  1517. mutex_unlock(&gspca_dev->queue_lock);
  1518. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1519. mutex_lock(&gspca_dev->usb_lock);
  1520. gspca_dev->usb_err = 0;
  1521. if (gspca_dev->present)
  1522. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1523. mutex_unlock(&gspca_dev->usb_lock);
  1524. }
  1525. return ret;
  1526. }
  1527. /*
  1528. * queue a video buffer
  1529. *
  1530. * Attempting to queue a buffer that has already been
  1531. * queued will return -EINVAL.
  1532. */
  1533. static int vidioc_qbuf(struct file *file, void *priv,
  1534. struct v4l2_buffer *v4l2_buf)
  1535. {
  1536. struct gspca_dev *gspca_dev = video_drvdata(file);
  1537. struct gspca_frame *frame;
  1538. int i, index, ret;
  1539. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1540. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1541. return -ERESTARTSYS;
  1542. index = v4l2_buf->index;
  1543. if ((unsigned) index >= gspca_dev->nframes) {
  1544. PDEBUG(D_FRAM,
  1545. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1546. ret = -EINVAL;
  1547. goto out;
  1548. }
  1549. if (v4l2_buf->memory != gspca_dev->memory) {
  1550. PDEBUG(D_FRAM, "qbuf bad memory type");
  1551. ret = -EINVAL;
  1552. goto out;
  1553. }
  1554. frame = &gspca_dev->frame[index];
  1555. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1556. PDEBUG(D_FRAM, "qbuf bad state");
  1557. ret = -EINVAL;
  1558. goto out;
  1559. }
  1560. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1561. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1562. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1563. frame->v4l2_buf.length = v4l2_buf->length;
  1564. }
  1565. /* put the buffer in the 'queued' queue */
  1566. i = atomic_read(&gspca_dev->fr_q);
  1567. gspca_dev->fr_queue[i] = index;
  1568. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1569. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1570. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1571. ret = 0;
  1572. out:
  1573. mutex_unlock(&gspca_dev->queue_lock);
  1574. return ret;
  1575. }
  1576. /*
  1577. * allocate the resources for read()
  1578. */
  1579. static int read_alloc(struct gspca_dev *gspca_dev,
  1580. struct file *file)
  1581. {
  1582. struct v4l2_buffer v4l2_buf;
  1583. int i, ret;
  1584. PDEBUG(D_STREAM, "read alloc");
  1585. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1586. return -ERESTARTSYS;
  1587. if (gspca_dev->nframes == 0) {
  1588. struct v4l2_requestbuffers rb;
  1589. memset(&rb, 0, sizeof rb);
  1590. rb.count = gspca_dev->nbufread;
  1591. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1592. rb.memory = GSPCA_MEMORY_READ;
  1593. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1594. if (ret != 0) {
  1595. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1596. goto out;
  1597. }
  1598. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1599. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1600. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1601. for (i = 0; i < gspca_dev->nbufread; i++) {
  1602. v4l2_buf.index = i;
  1603. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1604. if (ret != 0) {
  1605. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1606. goto out;
  1607. }
  1608. }
  1609. }
  1610. /* start streaming */
  1611. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1612. if (ret != 0)
  1613. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1614. out:
  1615. mutex_unlock(&gspca_dev->usb_lock);
  1616. return ret;
  1617. }
  1618. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1619. {
  1620. struct gspca_dev *gspca_dev = video_drvdata(file);
  1621. unsigned long req_events = poll_requested_events(wait);
  1622. int ret = 0;
  1623. PDEBUG(D_FRAM, "poll");
  1624. if (req_events & POLLPRI)
  1625. ret |= v4l2_ctrl_poll(file, wait);
  1626. if (req_events & (POLLIN | POLLRDNORM)) {
  1627. /* if reqbufs is not done, the user would use read() */
  1628. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1629. if (read_alloc(gspca_dev, file) != 0) {
  1630. ret |= POLLERR;
  1631. goto out;
  1632. }
  1633. }
  1634. poll_wait(file, &gspca_dev->wq, wait);
  1635. /* check if an image has been received */
  1636. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1637. ret |= POLLERR;
  1638. goto out;
  1639. }
  1640. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1641. ret |= POLLIN | POLLRDNORM;
  1642. mutex_unlock(&gspca_dev->queue_lock);
  1643. }
  1644. out:
  1645. if (!gspca_dev->present)
  1646. ret |= POLLHUP;
  1647. return ret;
  1648. }
  1649. static ssize_t dev_read(struct file *file, char __user *data,
  1650. size_t count, loff_t *ppos)
  1651. {
  1652. struct gspca_dev *gspca_dev = video_drvdata(file);
  1653. struct gspca_frame *frame;
  1654. struct v4l2_buffer v4l2_buf;
  1655. struct timeval timestamp;
  1656. int n, ret, ret2;
  1657. PDEBUG(D_FRAM, "read (%zd)", count);
  1658. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1659. ret = read_alloc(gspca_dev, file);
  1660. if (ret != 0)
  1661. return ret;
  1662. }
  1663. /* get a frame */
  1664. v4l2_get_timestamp(&timestamp);
  1665. timestamp.tv_sec--;
  1666. n = 2;
  1667. for (;;) {
  1668. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1669. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1670. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1671. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1672. if (ret != 0) {
  1673. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1674. return ret;
  1675. }
  1676. /* if the process slept for more than 1 second,
  1677. * get a newer frame */
  1678. frame = &gspca_dev->frame[v4l2_buf.index];
  1679. if (--n < 0)
  1680. break; /* avoid infinite loop */
  1681. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1682. break;
  1683. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1684. if (ret != 0) {
  1685. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1686. return ret;
  1687. }
  1688. }
  1689. /* copy the frame */
  1690. if (count > frame->v4l2_buf.bytesused)
  1691. count = frame->v4l2_buf.bytesused;
  1692. ret = copy_to_user(data, frame->data, count);
  1693. if (ret != 0) {
  1694. PERR("read cp to user lack %d / %zd", ret, count);
  1695. ret = -EFAULT;
  1696. goto out;
  1697. }
  1698. ret = count;
  1699. out:
  1700. /* in each case, requeue the buffer */
  1701. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1702. if (ret2 != 0)
  1703. return ret2;
  1704. return ret;
  1705. }
  1706. static struct v4l2_file_operations dev_fops = {
  1707. .owner = THIS_MODULE,
  1708. .open = dev_open,
  1709. .release = dev_close,
  1710. .read = dev_read,
  1711. .mmap = dev_mmap,
  1712. .unlocked_ioctl = video_ioctl2,
  1713. .poll = dev_poll,
  1714. };
  1715. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1716. .vidioc_querycap = vidioc_querycap,
  1717. .vidioc_dqbuf = vidioc_dqbuf,
  1718. .vidioc_qbuf = vidioc_qbuf,
  1719. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1720. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1721. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1722. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1723. .vidioc_streamon = vidioc_streamon,
  1724. .vidioc_enum_input = vidioc_enum_input,
  1725. .vidioc_g_input = vidioc_g_input,
  1726. .vidioc_s_input = vidioc_s_input,
  1727. .vidioc_reqbufs = vidioc_reqbufs,
  1728. .vidioc_querybuf = vidioc_querybuf,
  1729. .vidioc_streamoff = vidioc_streamoff,
  1730. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1731. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1732. .vidioc_g_parm = vidioc_g_parm,
  1733. .vidioc_s_parm = vidioc_s_parm,
  1734. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1735. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1736. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1737. .vidioc_g_chip_info = vidioc_g_chip_info,
  1738. .vidioc_g_register = vidioc_g_register,
  1739. .vidioc_s_register = vidioc_s_register,
  1740. #endif
  1741. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1742. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1743. };
  1744. static const struct video_device gspca_template = {
  1745. .name = "gspca main driver",
  1746. .fops = &dev_fops,
  1747. .ioctl_ops = &dev_ioctl_ops,
  1748. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1749. };
  1750. /*
  1751. * probe and create a new gspca device
  1752. *
  1753. * This function must be called by the sub-driver when it is
  1754. * called for probing a new device.
  1755. */
  1756. int gspca_dev_probe2(struct usb_interface *intf,
  1757. const struct usb_device_id *id,
  1758. const struct sd_desc *sd_desc,
  1759. int dev_size,
  1760. struct module *module)
  1761. {
  1762. struct gspca_dev *gspca_dev;
  1763. struct usb_device *dev = interface_to_usbdev(intf);
  1764. int ret;
  1765. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1766. sd_desc->name, id->idVendor, id->idProduct);
  1767. /* create the device */
  1768. if (dev_size < sizeof *gspca_dev)
  1769. dev_size = sizeof *gspca_dev;
  1770. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1771. if (!gspca_dev) {
  1772. pr_err("couldn't kzalloc gspca struct\n");
  1773. return -ENOMEM;
  1774. }
  1775. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1776. if (!gspca_dev->usb_buf) {
  1777. pr_err("out of memory\n");
  1778. ret = -ENOMEM;
  1779. goto out;
  1780. }
  1781. gspca_dev->dev = dev;
  1782. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1783. gspca_dev->xfer_ep = -1;
  1784. /* check if any audio device */
  1785. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1786. int i;
  1787. struct usb_interface *intf2;
  1788. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1789. intf2 = dev->actconfig->interface[i];
  1790. if (intf2 != NULL
  1791. && intf2->altsetting != NULL
  1792. && intf2->altsetting->desc.bInterfaceClass ==
  1793. USB_CLASS_AUDIO) {
  1794. gspca_dev->audio = 1;
  1795. break;
  1796. }
  1797. }
  1798. }
  1799. gspca_dev->v4l2_dev.release = gspca_release;
  1800. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1801. if (ret)
  1802. goto out;
  1803. gspca_dev->sd_desc = sd_desc;
  1804. gspca_dev->nbufread = 2;
  1805. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1806. gspca_dev->vdev = gspca_template;
  1807. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1808. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1809. gspca_dev->module = module;
  1810. gspca_dev->present = 1;
  1811. mutex_init(&gspca_dev->usb_lock);
  1812. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1813. mutex_init(&gspca_dev->queue_lock);
  1814. init_waitqueue_head(&gspca_dev->wq);
  1815. /* configure the subdriver and initialize the USB device */
  1816. ret = sd_desc->config(gspca_dev, id);
  1817. if (ret < 0)
  1818. goto out;
  1819. ret = sd_desc->init(gspca_dev);
  1820. if (ret < 0)
  1821. goto out;
  1822. if (sd_desc->init_controls)
  1823. ret = sd_desc->init_controls(gspca_dev);
  1824. if (ret < 0)
  1825. goto out;
  1826. gspca_set_default_mode(gspca_dev);
  1827. ret = gspca_input_connect(gspca_dev);
  1828. if (ret)
  1829. goto out;
  1830. /*
  1831. * Don't take usb_lock for these ioctls. This improves latency if
  1832. * usb_lock is taken for a long time, e.g. when changing a control
  1833. * value, and a new frame is ready to be dequeued.
  1834. */
  1835. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1836. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1837. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1838. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1839. if (!gspca_dev->sd_desc->get_register)
  1840. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1841. if (!gspca_dev->sd_desc->set_register)
  1842. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1843. #endif
  1844. if (!gspca_dev->sd_desc->get_jcomp)
  1845. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1846. if (!gspca_dev->sd_desc->set_jcomp)
  1847. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1848. /* init video stuff */
  1849. ret = video_register_device(&gspca_dev->vdev,
  1850. VFL_TYPE_GRABBER,
  1851. -1);
  1852. if (ret < 0) {
  1853. pr_err("video_register_device err %d\n", ret);
  1854. goto out;
  1855. }
  1856. usb_set_intfdata(intf, gspca_dev);
  1857. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  1858. gspca_input_create_urb(gspca_dev);
  1859. return 0;
  1860. out:
  1861. #if IS_ENABLED(CONFIG_INPUT)
  1862. if (gspca_dev->input_dev)
  1863. input_unregister_device(gspca_dev->input_dev);
  1864. #endif
  1865. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1866. kfree(gspca_dev->usb_buf);
  1867. kfree(gspca_dev);
  1868. return ret;
  1869. }
  1870. EXPORT_SYMBOL(gspca_dev_probe2);
  1871. /* same function as the previous one, but check the interface */
  1872. int gspca_dev_probe(struct usb_interface *intf,
  1873. const struct usb_device_id *id,
  1874. const struct sd_desc *sd_desc,
  1875. int dev_size,
  1876. struct module *module)
  1877. {
  1878. struct usb_device *dev = interface_to_usbdev(intf);
  1879. /* we don't handle multi-config cameras */
  1880. if (dev->descriptor.bNumConfigurations != 1) {
  1881. pr_err("%04x:%04x too many config\n",
  1882. id->idVendor, id->idProduct);
  1883. return -ENODEV;
  1884. }
  1885. /* the USB video interface must be the first one */
  1886. if (dev->actconfig->desc.bNumInterfaces != 1
  1887. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1888. return -ENODEV;
  1889. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1890. }
  1891. EXPORT_SYMBOL(gspca_dev_probe);
  1892. /*
  1893. * USB disconnection
  1894. *
  1895. * This function must be called by the sub-driver
  1896. * when the device disconnects, after the specific resources are freed.
  1897. */
  1898. void gspca_disconnect(struct usb_interface *intf)
  1899. {
  1900. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1901. #if IS_ENABLED(CONFIG_INPUT)
  1902. struct input_dev *input_dev;
  1903. #endif
  1904. PDEBUG(D_PROBE, "%s disconnect",
  1905. video_device_node_name(&gspca_dev->vdev));
  1906. mutex_lock(&gspca_dev->usb_lock);
  1907. gspca_dev->present = 0;
  1908. destroy_urbs(gspca_dev);
  1909. #if IS_ENABLED(CONFIG_INPUT)
  1910. gspca_input_destroy_urb(gspca_dev);
  1911. input_dev = gspca_dev->input_dev;
  1912. if (input_dev) {
  1913. gspca_dev->input_dev = NULL;
  1914. input_unregister_device(input_dev);
  1915. }
  1916. #endif
  1917. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  1918. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  1919. gspca_dev->sd_desc->stop0(gspca_dev);
  1920. gspca_dev->streaming = 0;
  1921. gspca_dev->dev = NULL;
  1922. wake_up_interruptible(&gspca_dev->wq);
  1923. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1924. video_unregister_device(&gspca_dev->vdev);
  1925. mutex_unlock(&gspca_dev->usb_lock);
  1926. /* (this will call gspca_release() immediately or on last close) */
  1927. v4l2_device_put(&gspca_dev->v4l2_dev);
  1928. }
  1929. EXPORT_SYMBOL(gspca_disconnect);
  1930. #ifdef CONFIG_PM
  1931. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1932. {
  1933. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1934. gspca_input_destroy_urb(gspca_dev);
  1935. if (!gspca_dev->streaming)
  1936. return 0;
  1937. mutex_lock(&gspca_dev->usb_lock);
  1938. gspca_dev->frozen = 1; /* avoid urb error messages */
  1939. gspca_dev->usb_err = 0;
  1940. if (gspca_dev->sd_desc->stopN)
  1941. gspca_dev->sd_desc->stopN(gspca_dev);
  1942. destroy_urbs(gspca_dev);
  1943. gspca_set_alt0(gspca_dev);
  1944. if (gspca_dev->sd_desc->stop0)
  1945. gspca_dev->sd_desc->stop0(gspca_dev);
  1946. mutex_unlock(&gspca_dev->usb_lock);
  1947. return 0;
  1948. }
  1949. EXPORT_SYMBOL(gspca_suspend);
  1950. int gspca_resume(struct usb_interface *intf)
  1951. {
  1952. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1953. int streaming, ret = 0;
  1954. mutex_lock(&gspca_dev->usb_lock);
  1955. gspca_dev->frozen = 0;
  1956. gspca_dev->usb_err = 0;
  1957. gspca_dev->sd_desc->init(gspca_dev);
  1958. /*
  1959. * Most subdrivers send all ctrl values on sd_start and thus
  1960. * only write to the device registers on s_ctrl when streaming ->
  1961. * Clear streaming to avoid setting all ctrls twice.
  1962. */
  1963. streaming = gspca_dev->streaming;
  1964. gspca_dev->streaming = 0;
  1965. if (streaming)
  1966. ret = gspca_init_transfer(gspca_dev);
  1967. else
  1968. gspca_input_create_urb(gspca_dev);
  1969. mutex_unlock(&gspca_dev->usb_lock);
  1970. return ret;
  1971. }
  1972. EXPORT_SYMBOL(gspca_resume);
  1973. #endif
  1974. /* -- module insert / remove -- */
  1975. static int __init gspca_init(void)
  1976. {
  1977. pr_info("v" GSPCA_VERSION " registered\n");
  1978. return 0;
  1979. }
  1980. static void __exit gspca_exit(void)
  1981. {
  1982. }
  1983. module_init(gspca_init);
  1984. module_exit(gspca_exit);
  1985. module_param_named(debug, gspca_debug, int, 0644);
  1986. MODULE_PARM_DESC(debug,
  1987. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");