gspca.c 56 KB

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