gspca.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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. frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
  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. i = gspca_dev->curr_mode;
  447. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  448. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  449. frsz = PAGE_ALIGN(frsz);
  450. if (count >= GSPCA_MAX_FRAMES)
  451. count = GSPCA_MAX_FRAMES - 1;
  452. gspca_dev->frbuf = vmalloc_32(frsz * count);
  453. if (!gspca_dev->frbuf) {
  454. pr_err("frame alloc failed\n");
  455. return -ENOMEM;
  456. }
  457. gspca_dev->capt_file = file;
  458. gspca_dev->memory = memory;
  459. gspca_dev->frsz = frsz;
  460. gspca_dev->nframes = count;
  461. for (i = 0; i < count; i++) {
  462. frame = &gspca_dev->frame[i];
  463. frame->v4l2_buf.index = i;
  464. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  465. frame->v4l2_buf.flags = 0;
  466. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  467. frame->v4l2_buf.length = frsz;
  468. frame->v4l2_buf.memory = memory;
  469. frame->v4l2_buf.sequence = 0;
  470. frame->data = gspca_dev->frbuf + i * frsz;
  471. frame->v4l2_buf.m.offset = i * frsz;
  472. }
  473. atomic_set(&gspca_dev->fr_q, 0);
  474. atomic_set(&gspca_dev->fr_i, 0);
  475. gspca_dev->fr_o = 0;
  476. return 0;
  477. }
  478. static void frame_free(struct gspca_dev *gspca_dev)
  479. {
  480. int i;
  481. PDEBUG(D_STREAM, "frame free");
  482. if (gspca_dev->frbuf != NULL) {
  483. vfree(gspca_dev->frbuf);
  484. gspca_dev->frbuf = NULL;
  485. for (i = 0; i < gspca_dev->nframes; i++)
  486. gspca_dev->frame[i].data = NULL;
  487. }
  488. gspca_dev->nframes = 0;
  489. gspca_dev->frsz = 0;
  490. gspca_dev->capt_file = NULL;
  491. gspca_dev->memory = GSPCA_MEMORY_NO;
  492. }
  493. static void destroy_urbs(struct gspca_dev *gspca_dev)
  494. {
  495. struct urb *urb;
  496. unsigned int i;
  497. PDEBUG(D_STREAM, "kill transfer");
  498. for (i = 0; i < MAX_NURBS; i++) {
  499. urb = gspca_dev->urb[i];
  500. if (urb == NULL)
  501. break;
  502. gspca_dev->urb[i] = NULL;
  503. usb_kill_urb(urb);
  504. usb_free_coherent(gspca_dev->dev,
  505. urb->transfer_buffer_length,
  506. urb->transfer_buffer,
  507. urb->transfer_dma);
  508. usb_free_urb(urb);
  509. }
  510. }
  511. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  512. {
  513. int ret;
  514. if (gspca_dev->alt == 0)
  515. return 0;
  516. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  517. if (ret < 0)
  518. pr_err("set alt 0 err %d\n", ret);
  519. return ret;
  520. }
  521. /* Note: both the queue and the usb locks should be held when calling this */
  522. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  523. {
  524. gspca_dev->streaming = 0;
  525. gspca_dev->usb_err = 0;
  526. if (gspca_dev->sd_desc->stopN)
  527. gspca_dev->sd_desc->stopN(gspca_dev);
  528. destroy_urbs(gspca_dev);
  529. gspca_input_destroy_urb(gspca_dev);
  530. gspca_set_alt0(gspca_dev);
  531. gspca_input_create_urb(gspca_dev);
  532. if (gspca_dev->sd_desc->stop0)
  533. gspca_dev->sd_desc->stop0(gspca_dev);
  534. PDEBUG(D_STREAM, "stream off OK");
  535. }
  536. /*
  537. * look for an input transfer endpoint in an alternate setting
  538. */
  539. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  540. int xfer)
  541. {
  542. struct usb_host_endpoint *ep;
  543. int i, attr;
  544. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  545. ep = &alt->endpoint[i];
  546. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  547. if (attr == xfer
  548. && ep->desc.wMaxPacketSize != 0
  549. && usb_endpoint_dir_in(&ep->desc))
  550. return ep;
  551. }
  552. return NULL;
  553. }
  554. /* compute the minimum bandwidth for the current transfer */
  555. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  556. {
  557. u32 bandwidth;
  558. int i;
  559. /* get the (max) image size */
  560. i = gspca_dev->curr_mode;
  561. bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
  562. /* if the image is compressed, estimate its mean size */
  563. if (!gspca_dev->cam.needs_full_bandwidth &&
  564. bandwidth < gspca_dev->cam.cam_mode[i].width *
  565. gspca_dev->cam.cam_mode[i].height)
  566. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  567. /* estimate the frame rate */
  568. if (gspca_dev->sd_desc->get_streamparm) {
  569. struct v4l2_streamparm parm;
  570. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  571. bandwidth *= parm.parm.capture.timeperframe.denominator;
  572. bandwidth /= parm.parm.capture.timeperframe.numerator;
  573. } else {
  574. /* don't hope more than 15 fps with USB 1.1 and
  575. * image resolution >= 640x480 */
  576. if (gspca_dev->width >= 640
  577. && gspca_dev->dev->speed == USB_SPEED_FULL)
  578. bandwidth *= 15; /* 15 fps */
  579. else
  580. bandwidth *= 30; /* 30 fps */
  581. }
  582. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  583. return bandwidth;
  584. }
  585. /* endpoint table */
  586. #define MAX_ALT 16
  587. struct ep_tb_s {
  588. u32 alt;
  589. u32 bandwidth;
  590. };
  591. /*
  592. * build the table of the endpoints
  593. * and compute the minimum bandwidth for the image transfer
  594. */
  595. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  596. struct usb_interface *intf,
  597. struct ep_tb_s *ep_tb)
  598. {
  599. struct usb_host_endpoint *ep;
  600. int i, j, nbalt, psize, found;
  601. u32 bandwidth, last_bw;
  602. nbalt = intf->num_altsetting;
  603. if (nbalt > MAX_ALT)
  604. nbalt = MAX_ALT; /* fixme: should warn */
  605. /* build the endpoint table */
  606. i = 0;
  607. last_bw = 0;
  608. for (;;) {
  609. ep_tb->bandwidth = 2000 * 2000 * 120;
  610. found = 0;
  611. for (j = 0; j < nbalt; j++) {
  612. ep = alt_xfer(&intf->altsetting[j],
  613. USB_ENDPOINT_XFER_ISOC);
  614. if (ep == NULL)
  615. continue;
  616. if (ep->desc.bInterval == 0) {
  617. pr_err("alt %d iso endp with 0 interval\n", j);
  618. continue;
  619. }
  620. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  621. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  622. bandwidth = psize * 1000;
  623. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  624. || gspca_dev->dev->speed == USB_SPEED_SUPER)
  625. bandwidth *= 8;
  626. bandwidth /= 1 << (ep->desc.bInterval - 1);
  627. if (bandwidth <= last_bw)
  628. continue;
  629. if (bandwidth < ep_tb->bandwidth) {
  630. ep_tb->bandwidth = bandwidth;
  631. ep_tb->alt = j;
  632. found = 1;
  633. }
  634. }
  635. if (!found)
  636. break;
  637. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  638. ep_tb->alt, ep_tb->bandwidth);
  639. last_bw = ep_tb->bandwidth;
  640. i++;
  641. ep_tb++;
  642. }
  643. /*
  644. * If the camera:
  645. * has a usb audio class interface (a built in usb mic); and
  646. * is a usb 1 full speed device; and
  647. * uses the max full speed iso bandwidth; and
  648. * and has more than 1 alt setting
  649. * then skip the highest alt setting to spare bandwidth for the mic
  650. */
  651. if (gspca_dev->audio &&
  652. gspca_dev->dev->speed == USB_SPEED_FULL &&
  653. last_bw >= 1000000 &&
  654. i > 1) {
  655. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  656. i--;
  657. ep_tb--;
  658. }
  659. /* get the requested bandwidth and start at the highest atlsetting */
  660. bandwidth = which_bandwidth(gspca_dev);
  661. ep_tb--;
  662. while (i > 1) {
  663. ep_tb--;
  664. if (ep_tb->bandwidth < bandwidth)
  665. break;
  666. i--;
  667. }
  668. return i;
  669. }
  670. /*
  671. * create the URBs for image transfer
  672. */
  673. static int create_urbs(struct gspca_dev *gspca_dev,
  674. struct usb_host_endpoint *ep)
  675. {
  676. struct urb *urb;
  677. int n, nurbs, i, psize, npkt, bsize;
  678. /* calculate the packet size and the number of packets */
  679. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  680. if (!gspca_dev->cam.bulk) { /* isoc */
  681. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  682. if (gspca_dev->pkt_size == 0)
  683. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  684. else
  685. psize = gspca_dev->pkt_size;
  686. npkt = gspca_dev->cam.npkt;
  687. if (npkt == 0)
  688. npkt = 32; /* default value */
  689. bsize = psize * npkt;
  690. PDEBUG(D_STREAM,
  691. "isoc %d pkts size %d = bsize:%d",
  692. npkt, psize, bsize);
  693. nurbs = DEF_NURBS;
  694. } else { /* bulk */
  695. npkt = 0;
  696. bsize = gspca_dev->cam.bulk_size;
  697. if (bsize == 0)
  698. bsize = psize;
  699. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  700. if (gspca_dev->cam.bulk_nurbs != 0)
  701. nurbs = gspca_dev->cam.bulk_nurbs;
  702. else
  703. nurbs = 1;
  704. }
  705. for (n = 0; n < nurbs; n++) {
  706. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  707. if (!urb) {
  708. pr_err("usb_alloc_urb failed\n");
  709. return -ENOMEM;
  710. }
  711. gspca_dev->urb[n] = urb;
  712. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  713. bsize,
  714. GFP_KERNEL,
  715. &urb->transfer_dma);
  716. if (urb->transfer_buffer == NULL) {
  717. pr_err("usb_alloc_coherent failed\n");
  718. return -ENOMEM;
  719. }
  720. urb->dev = gspca_dev->dev;
  721. urb->context = gspca_dev;
  722. urb->transfer_buffer_length = bsize;
  723. if (npkt != 0) { /* ISOC */
  724. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  725. ep->desc.bEndpointAddress);
  726. urb->transfer_flags = URB_ISO_ASAP
  727. | URB_NO_TRANSFER_DMA_MAP;
  728. urb->interval = 1 << (ep->desc.bInterval - 1);
  729. urb->complete = isoc_irq;
  730. urb->number_of_packets = npkt;
  731. for (i = 0; i < npkt; i++) {
  732. urb->iso_frame_desc[i].length = psize;
  733. urb->iso_frame_desc[i].offset = psize * i;
  734. }
  735. } else { /* bulk */
  736. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  737. ep->desc.bEndpointAddress);
  738. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  739. urb->complete = bulk_irq;
  740. }
  741. }
  742. return 0;
  743. }
  744. /*
  745. * start the USB transfer
  746. */
  747. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  748. {
  749. struct usb_interface *intf;
  750. struct usb_host_endpoint *ep;
  751. struct urb *urb;
  752. struct ep_tb_s ep_tb[MAX_ALT];
  753. int n, ret, xfer, alt, alt_idx;
  754. /* reset the streaming variables */
  755. gspca_dev->image = NULL;
  756. gspca_dev->image_len = 0;
  757. gspca_dev->last_packet_type = DISCARD_PACKET;
  758. gspca_dev->sequence = 0;
  759. gspca_dev->usb_err = 0;
  760. /* do the specific subdriver stuff before endpoint selection */
  761. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  762. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  763. if (gspca_dev->sd_desc->isoc_init) {
  764. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  765. if (ret < 0)
  766. return ret;
  767. }
  768. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  769. : USB_ENDPOINT_XFER_ISOC;
  770. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  771. if (gspca_dev->alt != 0) {
  772. gspca_dev->alt--; /* (previous version compatibility) */
  773. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
  774. if (ep == NULL) {
  775. pr_err("bad altsetting %d\n", gspca_dev->alt);
  776. return -EIO;
  777. }
  778. ep_tb[0].alt = gspca_dev->alt;
  779. alt_idx = 1;
  780. } else {
  781. /* else, compute the minimum bandwidth
  782. * and build the endpoint table */
  783. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  784. if (alt_idx <= 0) {
  785. pr_err("no transfer endpoint found\n");
  786. return -EIO;
  787. }
  788. }
  789. /* set the highest alternate setting and
  790. * loop until urb submit succeeds */
  791. gspca_input_destroy_urb(gspca_dev);
  792. gspca_dev->alt = ep_tb[--alt_idx].alt;
  793. alt = -1;
  794. for (;;) {
  795. if (alt != gspca_dev->alt) {
  796. alt = gspca_dev->alt;
  797. if (intf->num_altsetting > 1) {
  798. ret = usb_set_interface(gspca_dev->dev,
  799. gspca_dev->iface,
  800. alt);
  801. if (ret < 0) {
  802. if (ret == -ENOSPC)
  803. goto retry; /*fixme: ugly*/
  804. pr_err("set alt %d err %d\n", alt, ret);
  805. goto out;
  806. }
  807. }
  808. }
  809. if (!gspca_dev->cam.no_urb_create) {
  810. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  811. ret = create_urbs(gspca_dev,
  812. alt_xfer(&intf->altsetting[alt], xfer));
  813. if (ret < 0) {
  814. destroy_urbs(gspca_dev);
  815. goto out;
  816. }
  817. }
  818. /* clear the bulk endpoint */
  819. if (gspca_dev->cam.bulk)
  820. usb_clear_halt(gspca_dev->dev,
  821. gspca_dev->urb[0]->pipe);
  822. /* start the cam */
  823. ret = gspca_dev->sd_desc->start(gspca_dev);
  824. if (ret < 0) {
  825. destroy_urbs(gspca_dev);
  826. goto out;
  827. }
  828. gspca_dev->streaming = 1;
  829. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  830. /* some bulk transfers are started by the subdriver */
  831. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  832. break;
  833. /* submit the URBs */
  834. for (n = 0; n < MAX_NURBS; n++) {
  835. urb = gspca_dev->urb[n];
  836. if (urb == NULL)
  837. break;
  838. ret = usb_submit_urb(urb, GFP_KERNEL);
  839. if (ret < 0)
  840. break;
  841. }
  842. if (ret >= 0)
  843. break; /* transfer is started */
  844. /* something when wrong
  845. * stop the webcam and free the transfer resources */
  846. gspca_stream_off(gspca_dev);
  847. if (ret != -ENOSPC) {
  848. pr_err("usb_submit_urb alt %d err %d\n",
  849. gspca_dev->alt, ret);
  850. goto out;
  851. }
  852. /* the bandwidth is not wide enough
  853. * negotiate or try a lower alternate setting */
  854. retry:
  855. PERR("alt %d - bandwidth not wide enough, trying again", alt);
  856. msleep(20); /* wait for kill complete */
  857. if (gspca_dev->sd_desc->isoc_nego) {
  858. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  859. if (ret < 0)
  860. goto out;
  861. } else {
  862. if (alt_idx <= 0) {
  863. pr_err("no transfer endpoint found\n");
  864. ret = -EIO;
  865. goto out;
  866. }
  867. gspca_dev->alt = ep_tb[--alt_idx].alt;
  868. }
  869. }
  870. out:
  871. gspca_input_create_urb(gspca_dev);
  872. return ret;
  873. }
  874. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  875. {
  876. int i;
  877. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  878. gspca_dev->curr_mode = i;
  879. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  880. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  881. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  882. /* does nothing if ctrl_handler == NULL */
  883. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  884. }
  885. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  886. int width, int height)
  887. {
  888. int i;
  889. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  890. if (width >= gspca_dev->cam.cam_mode[i].width
  891. && height >= gspca_dev->cam.cam_mode[i].height)
  892. break;
  893. }
  894. return i;
  895. }
  896. /*
  897. * search a mode with the right pixel format
  898. */
  899. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  900. int mode,
  901. int pixfmt)
  902. {
  903. int modeU, modeD;
  904. modeU = modeD = mode;
  905. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  906. if (--modeD >= 0) {
  907. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  908. == pixfmt)
  909. return modeD;
  910. }
  911. if (++modeU < gspca_dev->cam.nmodes) {
  912. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  913. == pixfmt)
  914. return modeU;
  915. }
  916. }
  917. return -EINVAL;
  918. }
  919. #ifdef CONFIG_VIDEO_ADV_DEBUG
  920. static int vidioc_g_chip_info(struct file *file, void *priv,
  921. struct v4l2_dbg_chip_info *chip)
  922. {
  923. struct gspca_dev *gspca_dev = video_drvdata(file);
  924. gspca_dev->usb_err = 0;
  925. if (gspca_dev->sd_desc->get_chip_info)
  926. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  927. return chip->match.addr ? -EINVAL : 0;
  928. }
  929. static int vidioc_g_register(struct file *file, void *priv,
  930. struct v4l2_dbg_register *reg)
  931. {
  932. struct gspca_dev *gspca_dev = video_drvdata(file);
  933. gspca_dev->usb_err = 0;
  934. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  935. }
  936. static int vidioc_s_register(struct file *file, void *priv,
  937. const struct v4l2_dbg_register *reg)
  938. {
  939. struct gspca_dev *gspca_dev = video_drvdata(file);
  940. gspca_dev->usb_err = 0;
  941. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  942. }
  943. #endif
  944. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  945. struct v4l2_fmtdesc *fmtdesc)
  946. {
  947. struct gspca_dev *gspca_dev = video_drvdata(file);
  948. int i, j, index;
  949. __u32 fmt_tb[8];
  950. /* give an index to each format */
  951. index = 0;
  952. j = 0;
  953. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  954. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  955. j = 0;
  956. for (;;) {
  957. if (fmt_tb[j] == fmt_tb[index])
  958. break;
  959. j++;
  960. }
  961. if (j == index) {
  962. if (fmtdesc->index == index)
  963. break; /* new format */
  964. index++;
  965. if (index >= ARRAY_SIZE(fmt_tb))
  966. return -EINVAL;
  967. }
  968. }
  969. if (i < 0)
  970. return -EINVAL; /* no more format */
  971. fmtdesc->pixelformat = fmt_tb[index];
  972. if (gspca_dev->cam.cam_mode[i].sizeimage <
  973. gspca_dev->cam.cam_mode[i].width *
  974. gspca_dev->cam.cam_mode[i].height)
  975. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  976. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  977. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  978. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  979. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  980. fmtdesc->description[4] = '\0';
  981. return 0;
  982. }
  983. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  984. struct v4l2_format *fmt)
  985. {
  986. struct gspca_dev *gspca_dev = video_drvdata(file);
  987. int mode;
  988. mode = gspca_dev->curr_mode;
  989. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  990. /* some drivers use priv internally, zero it before giving it to
  991. userspace */
  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. /* some drivers use priv internally, zero it before giving it to
  1016. userspace */
  1017. fmt->fmt.pix.priv = 0;
  1018. return mode; /* used when s_fmt */
  1019. }
  1020. static int vidioc_try_fmt_vid_cap(struct file *file,
  1021. void *priv,
  1022. struct v4l2_format *fmt)
  1023. {
  1024. struct gspca_dev *gspca_dev = video_drvdata(file);
  1025. int ret;
  1026. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1027. if (ret < 0)
  1028. return ret;
  1029. return 0;
  1030. }
  1031. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1032. struct v4l2_format *fmt)
  1033. {
  1034. struct gspca_dev *gspca_dev = video_drvdata(file);
  1035. int ret;
  1036. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1037. return -ERESTARTSYS;
  1038. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1039. if (ret < 0)
  1040. goto out;
  1041. if (gspca_dev->nframes != 0
  1042. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1043. ret = -EINVAL;
  1044. goto out;
  1045. }
  1046. if (ret == gspca_dev->curr_mode) {
  1047. ret = 0;
  1048. goto out; /* same mode */
  1049. }
  1050. if (gspca_dev->streaming) {
  1051. ret = -EBUSY;
  1052. goto out;
  1053. }
  1054. gspca_dev->width = fmt->fmt.pix.width;
  1055. gspca_dev->height = fmt->fmt.pix.height;
  1056. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1057. gspca_dev->curr_mode = ret;
  1058. ret = 0;
  1059. out:
  1060. mutex_unlock(&gspca_dev->queue_lock);
  1061. return ret;
  1062. }
  1063. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1064. struct v4l2_frmsizeenum *fsize)
  1065. {
  1066. struct gspca_dev *gspca_dev = video_drvdata(file);
  1067. int i;
  1068. __u32 index = 0;
  1069. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1070. if (fsize->pixel_format !=
  1071. gspca_dev->cam.cam_mode[i].pixelformat)
  1072. continue;
  1073. if (fsize->index == index) {
  1074. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1075. fsize->discrete.width =
  1076. gspca_dev->cam.cam_mode[i].width;
  1077. fsize->discrete.height =
  1078. gspca_dev->cam.cam_mode[i].height;
  1079. return 0;
  1080. }
  1081. index++;
  1082. }
  1083. return -EINVAL;
  1084. }
  1085. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1086. struct v4l2_frmivalenum *fival)
  1087. {
  1088. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1089. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1090. __u32 i;
  1091. if (gspca_dev->cam.mode_framerates == NULL ||
  1092. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1093. return -EINVAL;
  1094. if (fival->pixel_format !=
  1095. gspca_dev->cam.cam_mode[mode].pixelformat)
  1096. return -EINVAL;
  1097. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1098. if (fival->index == i) {
  1099. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1100. fival->discrete.numerator = 1;
  1101. fival->discrete.denominator =
  1102. gspca_dev->cam.mode_framerates[mode].rates[i];
  1103. return 0;
  1104. }
  1105. }
  1106. return -EINVAL;
  1107. }
  1108. static void gspca_release(struct v4l2_device *v4l2_device)
  1109. {
  1110. struct gspca_dev *gspca_dev =
  1111. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1112. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1113. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1114. kfree(gspca_dev->usb_buf);
  1115. kfree(gspca_dev);
  1116. }
  1117. static int dev_open(struct file *file)
  1118. {
  1119. struct gspca_dev *gspca_dev = video_drvdata(file);
  1120. int ret;
  1121. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1122. /* protect the subdriver against rmmod */
  1123. if (!try_module_get(gspca_dev->module))
  1124. return -ENODEV;
  1125. ret = v4l2_fh_open(file);
  1126. if (ret)
  1127. module_put(gspca_dev->module);
  1128. return ret;
  1129. }
  1130. static int dev_close(struct file *file)
  1131. {
  1132. struct gspca_dev *gspca_dev = video_drvdata(file);
  1133. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1134. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1135. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1136. return -ERESTARTSYS;
  1137. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1138. mutex_unlock(&gspca_dev->usb_lock);
  1139. return -ERESTARTSYS;
  1140. }
  1141. /* if the file did the capture, free the streaming resources */
  1142. if (gspca_dev->capt_file == file) {
  1143. if (gspca_dev->streaming)
  1144. gspca_stream_off(gspca_dev);
  1145. frame_free(gspca_dev);
  1146. }
  1147. module_put(gspca_dev->module);
  1148. mutex_unlock(&gspca_dev->queue_lock);
  1149. mutex_unlock(&gspca_dev->usb_lock);
  1150. PDEBUG(D_STREAM, "close done");
  1151. return v4l2_fh_release(file);
  1152. }
  1153. static int vidioc_querycap(struct file *file, void *priv,
  1154. struct v4l2_capability *cap)
  1155. {
  1156. struct gspca_dev *gspca_dev = video_drvdata(file);
  1157. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1158. sizeof cap->driver);
  1159. if (gspca_dev->dev->product != NULL) {
  1160. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1161. sizeof cap->card);
  1162. } else {
  1163. snprintf((char *) cap->card, sizeof cap->card,
  1164. "USB Camera (%04x:%04x)",
  1165. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1166. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1167. }
  1168. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1169. sizeof(cap->bus_info));
  1170. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1171. | V4L2_CAP_STREAMING
  1172. | V4L2_CAP_READWRITE;
  1173. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1174. return 0;
  1175. }
  1176. static int vidioc_enum_input(struct file *file, void *priv,
  1177. struct v4l2_input *input)
  1178. {
  1179. struct gspca_dev *gspca_dev = video_drvdata(file);
  1180. if (input->index != 0)
  1181. return -EINVAL;
  1182. input->type = V4L2_INPUT_TYPE_CAMERA;
  1183. input->status = gspca_dev->cam.input_flags;
  1184. strlcpy(input->name, gspca_dev->sd_desc->name,
  1185. sizeof input->name);
  1186. return 0;
  1187. }
  1188. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1189. {
  1190. *i = 0;
  1191. return 0;
  1192. }
  1193. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1194. {
  1195. if (i > 0)
  1196. return -EINVAL;
  1197. return (0);
  1198. }
  1199. static int vidioc_reqbufs(struct file *file, void *priv,
  1200. struct v4l2_requestbuffers *rb)
  1201. {
  1202. struct gspca_dev *gspca_dev = video_drvdata(file);
  1203. int i, ret = 0, streaming;
  1204. i = rb->memory; /* (avoid compilation warning) */
  1205. switch (i) {
  1206. case GSPCA_MEMORY_READ: /* (internal call) */
  1207. case V4L2_MEMORY_MMAP:
  1208. case V4L2_MEMORY_USERPTR:
  1209. break;
  1210. default:
  1211. return -EINVAL;
  1212. }
  1213. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1214. return -ERESTARTSYS;
  1215. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1216. && gspca_dev->memory != GSPCA_MEMORY_READ
  1217. && gspca_dev->memory != rb->memory) {
  1218. ret = -EBUSY;
  1219. goto out;
  1220. }
  1221. /* only one file may do the capture */
  1222. if (gspca_dev->capt_file != NULL
  1223. && gspca_dev->capt_file != file) {
  1224. ret = -EBUSY;
  1225. goto out;
  1226. }
  1227. /* if allocated, the buffers must not be mapped */
  1228. for (i = 0; i < gspca_dev->nframes; i++) {
  1229. if (gspca_dev->frame[i].vma_use_count) {
  1230. ret = -EBUSY;
  1231. goto out;
  1232. }
  1233. }
  1234. /* stop streaming */
  1235. streaming = gspca_dev->streaming;
  1236. if (streaming) {
  1237. gspca_stream_off(gspca_dev);
  1238. /* Don't restart the stream when switching from read
  1239. * to mmap mode */
  1240. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1241. streaming = 0;
  1242. }
  1243. /* free the previous allocated buffers, if any */
  1244. if (gspca_dev->nframes != 0)
  1245. frame_free(gspca_dev);
  1246. if (rb->count == 0) /* unrequest */
  1247. goto out;
  1248. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1249. if (ret == 0) {
  1250. rb->count = gspca_dev->nframes;
  1251. if (streaming)
  1252. ret = gspca_init_transfer(gspca_dev);
  1253. }
  1254. out:
  1255. mutex_unlock(&gspca_dev->queue_lock);
  1256. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1257. return ret;
  1258. }
  1259. static int vidioc_querybuf(struct file *file, void *priv,
  1260. struct v4l2_buffer *v4l2_buf)
  1261. {
  1262. struct gspca_dev *gspca_dev = video_drvdata(file);
  1263. struct gspca_frame *frame;
  1264. if (v4l2_buf->index >= gspca_dev->nframes)
  1265. return -EINVAL;
  1266. frame = &gspca_dev->frame[v4l2_buf->index];
  1267. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1268. return 0;
  1269. }
  1270. static int vidioc_streamon(struct file *file, void *priv,
  1271. enum v4l2_buf_type buf_type)
  1272. {
  1273. struct gspca_dev *gspca_dev = video_drvdata(file);
  1274. int ret;
  1275. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1276. return -EINVAL;
  1277. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1278. return -ERESTARTSYS;
  1279. /* check the capture file */
  1280. if (gspca_dev->capt_file != file) {
  1281. ret = -EBUSY;
  1282. goto out;
  1283. }
  1284. if (gspca_dev->nframes == 0
  1285. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1286. ret = -EINVAL;
  1287. goto out;
  1288. }
  1289. if (!gspca_dev->streaming) {
  1290. ret = gspca_init_transfer(gspca_dev);
  1291. if (ret < 0)
  1292. goto out;
  1293. }
  1294. PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK", gspca_dev->pixfmt,
  1295. gspca_dev->width, gspca_dev->height);
  1296. ret = 0;
  1297. out:
  1298. mutex_unlock(&gspca_dev->queue_lock);
  1299. return ret;
  1300. }
  1301. static int vidioc_streamoff(struct file *file, void *priv,
  1302. enum v4l2_buf_type buf_type)
  1303. {
  1304. struct gspca_dev *gspca_dev = video_drvdata(file);
  1305. int i, ret;
  1306. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1307. return -EINVAL;
  1308. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1309. return -ERESTARTSYS;
  1310. if (!gspca_dev->streaming) {
  1311. ret = 0;
  1312. goto out;
  1313. }
  1314. /* check the capture file */
  1315. if (gspca_dev->capt_file != file) {
  1316. ret = -EBUSY;
  1317. goto out;
  1318. }
  1319. /* stop streaming */
  1320. gspca_stream_off(gspca_dev);
  1321. /* In case another thread is waiting in dqbuf */
  1322. wake_up_interruptible(&gspca_dev->wq);
  1323. /* empty the transfer queues */
  1324. for (i = 0; i < gspca_dev->nframes; i++)
  1325. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1326. atomic_set(&gspca_dev->fr_q, 0);
  1327. atomic_set(&gspca_dev->fr_i, 0);
  1328. gspca_dev->fr_o = 0;
  1329. ret = 0;
  1330. out:
  1331. mutex_unlock(&gspca_dev->queue_lock);
  1332. return ret;
  1333. }
  1334. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1335. struct v4l2_jpegcompression *jpegcomp)
  1336. {
  1337. struct gspca_dev *gspca_dev = video_drvdata(file);
  1338. gspca_dev->usb_err = 0;
  1339. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1340. }
  1341. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1342. const 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->set_jcomp(gspca_dev, jpegcomp);
  1347. }
  1348. static int vidioc_g_parm(struct file *filp, void *priv,
  1349. struct v4l2_streamparm *parm)
  1350. {
  1351. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1352. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1353. if (gspca_dev->sd_desc->get_streamparm) {
  1354. gspca_dev->usb_err = 0;
  1355. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1356. return gspca_dev->usb_err;
  1357. }
  1358. return 0;
  1359. }
  1360. static int vidioc_s_parm(struct file *filp, void *priv,
  1361. struct v4l2_streamparm *parm)
  1362. {
  1363. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1364. int n;
  1365. n = parm->parm.capture.readbuffers;
  1366. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1367. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1368. else
  1369. gspca_dev->nbufread = n;
  1370. if (gspca_dev->sd_desc->set_streamparm) {
  1371. gspca_dev->usb_err = 0;
  1372. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1373. return gspca_dev->usb_err;
  1374. }
  1375. return 0;
  1376. }
  1377. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1378. {
  1379. struct gspca_dev *gspca_dev = video_drvdata(file);
  1380. struct gspca_frame *frame;
  1381. struct page *page;
  1382. unsigned long addr, start, size;
  1383. int i, ret;
  1384. start = vma->vm_start;
  1385. size = vma->vm_end - vma->vm_start;
  1386. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1387. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1388. return -ERESTARTSYS;
  1389. if (gspca_dev->capt_file != file) {
  1390. ret = -EINVAL;
  1391. goto out;
  1392. }
  1393. frame = NULL;
  1394. for (i = 0; i < gspca_dev->nframes; ++i) {
  1395. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1396. PDEBUG(D_STREAM, "mmap bad memory type");
  1397. break;
  1398. }
  1399. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1400. == vma->vm_pgoff) {
  1401. frame = &gspca_dev->frame[i];
  1402. break;
  1403. }
  1404. }
  1405. if (frame == NULL) {
  1406. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1407. ret = -EINVAL;
  1408. goto out;
  1409. }
  1410. if (size != frame->v4l2_buf.length) {
  1411. PDEBUG(D_STREAM, "mmap bad size");
  1412. ret = -EINVAL;
  1413. goto out;
  1414. }
  1415. /*
  1416. * - VM_IO marks the area as being a mmaped region for I/O to a
  1417. * device. It also prevents the region from being core dumped.
  1418. */
  1419. vma->vm_flags |= VM_IO;
  1420. addr = (unsigned long) frame->data;
  1421. while (size > 0) {
  1422. page = vmalloc_to_page((void *) addr);
  1423. ret = vm_insert_page(vma, start, page);
  1424. if (ret < 0)
  1425. goto out;
  1426. start += PAGE_SIZE;
  1427. addr += PAGE_SIZE;
  1428. size -= PAGE_SIZE;
  1429. }
  1430. vma->vm_ops = &gspca_vm_ops;
  1431. vma->vm_private_data = frame;
  1432. gspca_vm_open(vma);
  1433. ret = 0;
  1434. out:
  1435. mutex_unlock(&gspca_dev->queue_lock);
  1436. return ret;
  1437. }
  1438. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1439. enum v4l2_memory memory)
  1440. {
  1441. if (!gspca_dev->present)
  1442. return -ENODEV;
  1443. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1444. !gspca_dev->streaming)
  1445. return -EINVAL;
  1446. /* check if a frame is ready */
  1447. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1448. }
  1449. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1450. enum v4l2_memory memory)
  1451. {
  1452. int ret;
  1453. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1454. return -ERESTARTSYS;
  1455. ret = frame_ready_nolock(gspca_dev, file, memory);
  1456. mutex_unlock(&gspca_dev->queue_lock);
  1457. return ret;
  1458. }
  1459. /*
  1460. * dequeue a video buffer
  1461. *
  1462. * If nonblock_ing is false, block until a buffer is available.
  1463. */
  1464. static int vidioc_dqbuf(struct file *file, void *priv,
  1465. struct v4l2_buffer *v4l2_buf)
  1466. {
  1467. struct gspca_dev *gspca_dev = video_drvdata(file);
  1468. struct gspca_frame *frame;
  1469. int i, j, ret;
  1470. PDEBUG(D_FRAM, "dqbuf");
  1471. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1472. return -ERESTARTSYS;
  1473. for (;;) {
  1474. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1475. if (ret < 0)
  1476. goto out;
  1477. if (ret > 0)
  1478. break;
  1479. mutex_unlock(&gspca_dev->queue_lock);
  1480. if (file->f_flags & O_NONBLOCK)
  1481. return -EAGAIN;
  1482. /* wait till a frame is ready */
  1483. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1484. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1485. msecs_to_jiffies(3000));
  1486. if (ret < 0)
  1487. return ret;
  1488. if (ret == 0)
  1489. return -EIO;
  1490. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1491. return -ERESTARTSYS;
  1492. }
  1493. i = gspca_dev->fr_o;
  1494. j = gspca_dev->fr_queue[i];
  1495. frame = &gspca_dev->frame[j];
  1496. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1497. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1498. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1499. PDEBUG(D_FRAM, "dqbuf %d", j);
  1500. ret = 0;
  1501. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1502. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1503. frame->data,
  1504. frame->v4l2_buf.bytesused)) {
  1505. PERR("dqbuf cp to user failed");
  1506. ret = -EFAULT;
  1507. }
  1508. }
  1509. out:
  1510. mutex_unlock(&gspca_dev->queue_lock);
  1511. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1512. mutex_lock(&gspca_dev->usb_lock);
  1513. gspca_dev->usb_err = 0;
  1514. if (gspca_dev->present)
  1515. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1516. mutex_unlock(&gspca_dev->usb_lock);
  1517. }
  1518. return ret;
  1519. }
  1520. /*
  1521. * queue a video buffer
  1522. *
  1523. * Attempting to queue a buffer that has already been
  1524. * queued will return -EINVAL.
  1525. */
  1526. static int vidioc_qbuf(struct file *file, void *priv,
  1527. struct v4l2_buffer *v4l2_buf)
  1528. {
  1529. struct gspca_dev *gspca_dev = video_drvdata(file);
  1530. struct gspca_frame *frame;
  1531. int i, index, ret;
  1532. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1533. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1534. return -ERESTARTSYS;
  1535. index = v4l2_buf->index;
  1536. if ((unsigned) index >= gspca_dev->nframes) {
  1537. PDEBUG(D_FRAM,
  1538. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1539. ret = -EINVAL;
  1540. goto out;
  1541. }
  1542. if (v4l2_buf->memory != gspca_dev->memory) {
  1543. PDEBUG(D_FRAM, "qbuf bad memory type");
  1544. ret = -EINVAL;
  1545. goto out;
  1546. }
  1547. frame = &gspca_dev->frame[index];
  1548. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1549. PDEBUG(D_FRAM, "qbuf bad state");
  1550. ret = -EINVAL;
  1551. goto out;
  1552. }
  1553. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1554. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1555. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1556. frame->v4l2_buf.length = v4l2_buf->length;
  1557. }
  1558. /* put the buffer in the 'queued' queue */
  1559. i = atomic_read(&gspca_dev->fr_q);
  1560. gspca_dev->fr_queue[i] = index;
  1561. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1562. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1563. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1564. ret = 0;
  1565. out:
  1566. mutex_unlock(&gspca_dev->queue_lock);
  1567. return ret;
  1568. }
  1569. /*
  1570. * allocate the resources for read()
  1571. */
  1572. static int read_alloc(struct gspca_dev *gspca_dev,
  1573. struct file *file)
  1574. {
  1575. struct v4l2_buffer v4l2_buf;
  1576. int i, ret;
  1577. PDEBUG(D_STREAM, "read alloc");
  1578. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1579. return -ERESTARTSYS;
  1580. if (gspca_dev->nframes == 0) {
  1581. struct v4l2_requestbuffers rb;
  1582. memset(&rb, 0, sizeof rb);
  1583. rb.count = gspca_dev->nbufread;
  1584. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1585. rb.memory = GSPCA_MEMORY_READ;
  1586. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1587. if (ret != 0) {
  1588. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1589. goto out;
  1590. }
  1591. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1592. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1593. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1594. for (i = 0; i < gspca_dev->nbufread; i++) {
  1595. v4l2_buf.index = i;
  1596. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1597. if (ret != 0) {
  1598. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1599. goto out;
  1600. }
  1601. }
  1602. }
  1603. /* start streaming */
  1604. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1605. if (ret != 0)
  1606. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1607. out:
  1608. mutex_unlock(&gspca_dev->usb_lock);
  1609. return ret;
  1610. }
  1611. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1612. {
  1613. struct gspca_dev *gspca_dev = video_drvdata(file);
  1614. unsigned long req_events = poll_requested_events(wait);
  1615. int ret = 0;
  1616. PDEBUG(D_FRAM, "poll");
  1617. if (req_events & POLLPRI)
  1618. ret |= v4l2_ctrl_poll(file, wait);
  1619. if (req_events & (POLLIN | POLLRDNORM)) {
  1620. /* if reqbufs is not done, the user would use read() */
  1621. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1622. if (read_alloc(gspca_dev, file) != 0) {
  1623. ret |= POLLERR;
  1624. goto out;
  1625. }
  1626. }
  1627. poll_wait(file, &gspca_dev->wq, wait);
  1628. /* check if an image has been received */
  1629. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1630. ret |= POLLERR;
  1631. goto out;
  1632. }
  1633. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1634. ret |= POLLIN | POLLRDNORM;
  1635. mutex_unlock(&gspca_dev->queue_lock);
  1636. }
  1637. out:
  1638. if (!gspca_dev->present)
  1639. ret |= POLLHUP;
  1640. return ret;
  1641. }
  1642. static ssize_t dev_read(struct file *file, char __user *data,
  1643. size_t count, loff_t *ppos)
  1644. {
  1645. struct gspca_dev *gspca_dev = video_drvdata(file);
  1646. struct gspca_frame *frame;
  1647. struct v4l2_buffer v4l2_buf;
  1648. struct timeval timestamp;
  1649. int n, ret, ret2;
  1650. PDEBUG(D_FRAM, "read (%zd)", count);
  1651. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1652. ret = read_alloc(gspca_dev, file);
  1653. if (ret != 0)
  1654. return ret;
  1655. }
  1656. /* get a frame */
  1657. timestamp = ktime_to_timeval(ktime_get());
  1658. timestamp.tv_sec--;
  1659. n = 2;
  1660. for (;;) {
  1661. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1662. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1663. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1664. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1665. if (ret != 0) {
  1666. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1667. return ret;
  1668. }
  1669. /* if the process slept for more than 1 second,
  1670. * get a newer frame */
  1671. frame = &gspca_dev->frame[v4l2_buf.index];
  1672. if (--n < 0)
  1673. break; /* avoid infinite loop */
  1674. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1675. break;
  1676. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1677. if (ret != 0) {
  1678. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1679. return ret;
  1680. }
  1681. }
  1682. /* copy the frame */
  1683. if (count > frame->v4l2_buf.bytesused)
  1684. count = frame->v4l2_buf.bytesused;
  1685. ret = copy_to_user(data, frame->data, count);
  1686. if (ret != 0) {
  1687. PERR("read cp to user lack %d / %zd", ret, count);
  1688. ret = -EFAULT;
  1689. goto out;
  1690. }
  1691. ret = count;
  1692. out:
  1693. /* in each case, requeue the buffer */
  1694. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1695. if (ret2 != 0)
  1696. return ret2;
  1697. return ret;
  1698. }
  1699. static struct v4l2_file_operations dev_fops = {
  1700. .owner = THIS_MODULE,
  1701. .open = dev_open,
  1702. .release = dev_close,
  1703. .read = dev_read,
  1704. .mmap = dev_mmap,
  1705. .unlocked_ioctl = video_ioctl2,
  1706. .poll = dev_poll,
  1707. };
  1708. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1709. .vidioc_querycap = vidioc_querycap,
  1710. .vidioc_dqbuf = vidioc_dqbuf,
  1711. .vidioc_qbuf = vidioc_qbuf,
  1712. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1713. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1714. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1715. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1716. .vidioc_streamon = vidioc_streamon,
  1717. .vidioc_enum_input = vidioc_enum_input,
  1718. .vidioc_g_input = vidioc_g_input,
  1719. .vidioc_s_input = vidioc_s_input,
  1720. .vidioc_reqbufs = vidioc_reqbufs,
  1721. .vidioc_querybuf = vidioc_querybuf,
  1722. .vidioc_streamoff = vidioc_streamoff,
  1723. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1724. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1725. .vidioc_g_parm = vidioc_g_parm,
  1726. .vidioc_s_parm = vidioc_s_parm,
  1727. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1728. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1729. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1730. .vidioc_g_chip_info = vidioc_g_chip_info,
  1731. .vidioc_g_register = vidioc_g_register,
  1732. .vidioc_s_register = vidioc_s_register,
  1733. #endif
  1734. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1735. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1736. };
  1737. static const struct video_device gspca_template = {
  1738. .name = "gspca main driver",
  1739. .fops = &dev_fops,
  1740. .ioctl_ops = &dev_ioctl_ops,
  1741. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1742. };
  1743. /*
  1744. * probe and create a new gspca device
  1745. *
  1746. * This function must be called by the sub-driver when it is
  1747. * called for probing a new device.
  1748. */
  1749. int gspca_dev_probe2(struct usb_interface *intf,
  1750. const struct usb_device_id *id,
  1751. const struct sd_desc *sd_desc,
  1752. int dev_size,
  1753. struct module *module)
  1754. {
  1755. struct gspca_dev *gspca_dev;
  1756. struct usb_device *dev = interface_to_usbdev(intf);
  1757. int ret;
  1758. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1759. sd_desc->name, id->idVendor, id->idProduct);
  1760. /* create the device */
  1761. if (dev_size < sizeof *gspca_dev)
  1762. dev_size = sizeof *gspca_dev;
  1763. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1764. if (!gspca_dev) {
  1765. pr_err("couldn't kzalloc gspca struct\n");
  1766. return -ENOMEM;
  1767. }
  1768. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1769. if (!gspca_dev->usb_buf) {
  1770. pr_err("out of memory\n");
  1771. ret = -ENOMEM;
  1772. goto out;
  1773. }
  1774. gspca_dev->dev = dev;
  1775. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1776. /* check if any audio device */
  1777. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1778. int i;
  1779. struct usb_interface *intf2;
  1780. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1781. intf2 = dev->actconfig->interface[i];
  1782. if (intf2 != NULL
  1783. && intf2->altsetting != NULL
  1784. && intf2->altsetting->desc.bInterfaceClass ==
  1785. USB_CLASS_AUDIO) {
  1786. gspca_dev->audio = 1;
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. gspca_dev->v4l2_dev.release = gspca_release;
  1792. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1793. if (ret)
  1794. goto out;
  1795. gspca_dev->sd_desc = sd_desc;
  1796. gspca_dev->nbufread = 2;
  1797. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1798. gspca_dev->vdev = gspca_template;
  1799. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1800. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1801. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1802. gspca_dev->module = module;
  1803. gspca_dev->present = 1;
  1804. mutex_init(&gspca_dev->usb_lock);
  1805. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1806. mutex_init(&gspca_dev->queue_lock);
  1807. init_waitqueue_head(&gspca_dev->wq);
  1808. /* configure the subdriver and initialize the USB device */
  1809. ret = sd_desc->config(gspca_dev, id);
  1810. if (ret < 0)
  1811. goto out;
  1812. ret = sd_desc->init(gspca_dev);
  1813. if (ret < 0)
  1814. goto out;
  1815. if (sd_desc->init_controls)
  1816. ret = sd_desc->init_controls(gspca_dev);
  1817. if (ret < 0)
  1818. goto out;
  1819. gspca_set_default_mode(gspca_dev);
  1820. ret = gspca_input_connect(gspca_dev);
  1821. if (ret)
  1822. goto out;
  1823. /*
  1824. * Don't take usb_lock for these ioctls. This improves latency if
  1825. * usb_lock is taken for a long time, e.g. when changing a control
  1826. * value, and a new frame is ready to be dequeued.
  1827. */
  1828. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1829. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1830. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1831. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1832. if (!gspca_dev->sd_desc->get_register)
  1833. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1834. if (!gspca_dev->sd_desc->set_register)
  1835. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1836. #endif
  1837. if (!gspca_dev->sd_desc->get_jcomp)
  1838. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1839. if (!gspca_dev->sd_desc->set_jcomp)
  1840. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1841. /* init video stuff */
  1842. ret = video_register_device(&gspca_dev->vdev,
  1843. VFL_TYPE_GRABBER,
  1844. -1);
  1845. if (ret < 0) {
  1846. pr_err("video_register_device err %d\n", ret);
  1847. goto out;
  1848. }
  1849. usb_set_intfdata(intf, gspca_dev);
  1850. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  1851. gspca_input_create_urb(gspca_dev);
  1852. return 0;
  1853. out:
  1854. #if IS_ENABLED(CONFIG_INPUT)
  1855. if (gspca_dev->input_dev)
  1856. input_unregister_device(gspca_dev->input_dev);
  1857. #endif
  1858. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1859. kfree(gspca_dev->usb_buf);
  1860. kfree(gspca_dev);
  1861. return ret;
  1862. }
  1863. EXPORT_SYMBOL(gspca_dev_probe2);
  1864. /* same function as the previous one, but check the interface */
  1865. int gspca_dev_probe(struct usb_interface *intf,
  1866. const struct usb_device_id *id,
  1867. const struct sd_desc *sd_desc,
  1868. int dev_size,
  1869. struct module *module)
  1870. {
  1871. struct usb_device *dev = interface_to_usbdev(intf);
  1872. /* we don't handle multi-config cameras */
  1873. if (dev->descriptor.bNumConfigurations != 1) {
  1874. pr_err("%04x:%04x too many config\n",
  1875. id->idVendor, id->idProduct);
  1876. return -ENODEV;
  1877. }
  1878. /* the USB video interface must be the first one */
  1879. if (dev->actconfig->desc.bNumInterfaces != 1
  1880. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1881. return -ENODEV;
  1882. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1883. }
  1884. EXPORT_SYMBOL(gspca_dev_probe);
  1885. /*
  1886. * USB disconnection
  1887. *
  1888. * This function must be called by the sub-driver
  1889. * when the device disconnects, after the specific resources are freed.
  1890. */
  1891. void gspca_disconnect(struct usb_interface *intf)
  1892. {
  1893. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1894. #if IS_ENABLED(CONFIG_INPUT)
  1895. struct input_dev *input_dev;
  1896. #endif
  1897. PDEBUG(D_PROBE, "%s disconnect",
  1898. video_device_node_name(&gspca_dev->vdev));
  1899. mutex_lock(&gspca_dev->usb_lock);
  1900. gspca_dev->present = 0;
  1901. destroy_urbs(gspca_dev);
  1902. #if IS_ENABLED(CONFIG_INPUT)
  1903. gspca_input_destroy_urb(gspca_dev);
  1904. input_dev = gspca_dev->input_dev;
  1905. if (input_dev) {
  1906. gspca_dev->input_dev = NULL;
  1907. input_unregister_device(input_dev);
  1908. }
  1909. #endif
  1910. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  1911. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  1912. gspca_dev->sd_desc->stop0(gspca_dev);
  1913. gspca_dev->streaming = 0;
  1914. gspca_dev->dev = NULL;
  1915. wake_up_interruptible(&gspca_dev->wq);
  1916. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1917. video_unregister_device(&gspca_dev->vdev);
  1918. mutex_unlock(&gspca_dev->usb_lock);
  1919. /* (this will call gspca_release() immediately or on last close) */
  1920. v4l2_device_put(&gspca_dev->v4l2_dev);
  1921. }
  1922. EXPORT_SYMBOL(gspca_disconnect);
  1923. #ifdef CONFIG_PM
  1924. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1925. {
  1926. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1927. gspca_input_destroy_urb(gspca_dev);
  1928. if (!gspca_dev->streaming)
  1929. return 0;
  1930. mutex_lock(&gspca_dev->usb_lock);
  1931. gspca_dev->frozen = 1; /* avoid urb error messages */
  1932. gspca_dev->usb_err = 0;
  1933. if (gspca_dev->sd_desc->stopN)
  1934. gspca_dev->sd_desc->stopN(gspca_dev);
  1935. destroy_urbs(gspca_dev);
  1936. gspca_set_alt0(gspca_dev);
  1937. if (gspca_dev->sd_desc->stop0)
  1938. gspca_dev->sd_desc->stop0(gspca_dev);
  1939. mutex_unlock(&gspca_dev->usb_lock);
  1940. return 0;
  1941. }
  1942. EXPORT_SYMBOL(gspca_suspend);
  1943. int gspca_resume(struct usb_interface *intf)
  1944. {
  1945. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1946. int streaming, ret = 0;
  1947. mutex_lock(&gspca_dev->usb_lock);
  1948. gspca_dev->frozen = 0;
  1949. gspca_dev->usb_err = 0;
  1950. gspca_dev->sd_desc->init(gspca_dev);
  1951. /*
  1952. * Most subdrivers send all ctrl values on sd_start and thus
  1953. * only write to the device registers on s_ctrl when streaming ->
  1954. * Clear streaming to avoid setting all ctrls twice.
  1955. */
  1956. streaming = gspca_dev->streaming;
  1957. gspca_dev->streaming = 0;
  1958. if (streaming)
  1959. ret = gspca_init_transfer(gspca_dev);
  1960. else
  1961. gspca_input_create_urb(gspca_dev);
  1962. mutex_unlock(&gspca_dev->usb_lock);
  1963. return ret;
  1964. }
  1965. EXPORT_SYMBOL(gspca_resume);
  1966. #endif
  1967. /* -- module insert / remove -- */
  1968. static int __init gspca_init(void)
  1969. {
  1970. pr_info("v" GSPCA_VERSION " registered\n");
  1971. return 0;
  1972. }
  1973. static void __exit gspca_exit(void)
  1974. {
  1975. }
  1976. module_init(gspca_init);
  1977. module_exit(gspca_exit);
  1978. module_param_named(debug, gspca_debug, int, 0644);
  1979. MODULE_PARM_DESC(debug,
  1980. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");