gspca.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #define GSPCA_VERSION "2.14.0"
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/io.h>
  30. #include <asm/page.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/ktime.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-ctrls.h>
  35. #include <media/v4l2-fh.h>
  36. #include <media/v4l2-event.h>
  37. #include "gspca.h"
  38. #if IS_ENABLED(CONFIG_INPUT)
  39. #include <linux/input.h>
  40. #include <linux/usb/input.h>
  41. #endif
  42. /* global values */
  43. #define DEF_NURBS 3 /* default number of URBs */
  44. #if DEF_NURBS > MAX_NURBS
  45. #error "DEF_NURBS too big"
  46. #endif
  47. MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
  48. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  49. MODULE_LICENSE("GPL");
  50. MODULE_VERSION(GSPCA_VERSION);
  51. int gspca_debug;
  52. EXPORT_SYMBOL(gspca_debug);
  53. static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
  54. __u32 pixfmt, int w, int h)
  55. {
  56. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  57. gspca_dbg(gspca_dev, debug, "%s %c%c%c%c %dx%d\n",
  58. txt,
  59. pixfmt & 0xff,
  60. (pixfmt >> 8) & 0xff,
  61. (pixfmt >> 16) & 0xff,
  62. pixfmt >> 24,
  63. w, h);
  64. } else {
  65. gspca_dbg(gspca_dev, debug, "%s 0x%08x %dx%d\n",
  66. txt,
  67. pixfmt,
  68. w, h);
  69. }
  70. }
  71. /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
  72. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  73. #define GSPCA_MEMORY_READ 7
  74. /*
  75. * Input and interrupt endpoint handling functions
  76. */
  77. #if IS_ENABLED(CONFIG_INPUT)
  78. static void int_irq(struct urb *urb)
  79. {
  80. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  81. int ret;
  82. ret = urb->status;
  83. switch (ret) {
  84. case 0:
  85. if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
  86. urb->transfer_buffer, urb->actual_length) < 0) {
  87. gspca_err(gspca_dev, "Unknown packet received\n");
  88. }
  89. break;
  90. case -ENOENT:
  91. case -ECONNRESET:
  92. case -ENODEV:
  93. case -ESHUTDOWN:
  94. /* Stop is requested either by software or hardware is gone,
  95. * keep the ret value non-zero and don't resubmit later.
  96. */
  97. break;
  98. default:
  99. gspca_err(gspca_dev, "URB error %i, resubmitting\n",
  100. urb->status);
  101. urb->status = 0;
  102. ret = 0;
  103. }
  104. if (ret == 0) {
  105. ret = usb_submit_urb(urb, GFP_ATOMIC);
  106. if (ret < 0)
  107. pr_err("Resubmit URB failed with error %i\n", ret);
  108. }
  109. }
  110. static int gspca_input_connect(struct gspca_dev *dev)
  111. {
  112. struct input_dev *input_dev;
  113. int err = 0;
  114. dev->input_dev = NULL;
  115. if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
  116. input_dev = input_allocate_device();
  117. if (!input_dev)
  118. return -ENOMEM;
  119. usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
  120. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  121. input_dev->name = dev->sd_desc->name;
  122. input_dev->phys = dev->phys;
  123. usb_to_input_id(dev->dev, &input_dev->id);
  124. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  125. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  126. input_dev->dev.parent = &dev->dev->dev;
  127. err = input_register_device(input_dev);
  128. if (err) {
  129. pr_err("Input device registration failed with error %i\n",
  130. err);
  131. input_dev->dev.parent = NULL;
  132. input_free_device(input_dev);
  133. } else {
  134. dev->input_dev = input_dev;
  135. }
  136. }
  137. return err;
  138. }
  139. static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
  140. struct usb_endpoint_descriptor *ep)
  141. {
  142. unsigned int buffer_len;
  143. int interval;
  144. struct urb *urb;
  145. struct usb_device *dev;
  146. void *buffer = NULL;
  147. int ret = -EINVAL;
  148. buffer_len = le16_to_cpu(ep->wMaxPacketSize);
  149. interval = ep->bInterval;
  150. gspca_dbg(gspca_dev, D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u\n",
  151. ep->bEndpointAddress, buffer_len, interval);
  152. dev = gspca_dev->dev;
  153. urb = usb_alloc_urb(0, GFP_KERNEL);
  154. if (!urb) {
  155. ret = -ENOMEM;
  156. goto error;
  157. }
  158. buffer = usb_alloc_coherent(dev, buffer_len,
  159. GFP_KERNEL, &urb->transfer_dma);
  160. if (!buffer) {
  161. ret = -ENOMEM;
  162. goto error_buffer;
  163. }
  164. usb_fill_int_urb(urb, dev,
  165. usb_rcvintpipe(dev, ep->bEndpointAddress),
  166. buffer, buffer_len,
  167. int_irq, (void *)gspca_dev, interval);
  168. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  169. ret = usb_submit_urb(urb, GFP_KERNEL);
  170. if (ret < 0) {
  171. gspca_err(gspca_dev, "submit int URB failed with error %i\n",
  172. ret);
  173. goto error_submit;
  174. }
  175. gspca_dev->int_urb = urb;
  176. return ret;
  177. error_submit:
  178. usb_free_coherent(dev,
  179. urb->transfer_buffer_length,
  180. urb->transfer_buffer,
  181. urb->transfer_dma);
  182. error_buffer:
  183. usb_free_urb(urb);
  184. error:
  185. return ret;
  186. }
  187. static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  188. {
  189. struct usb_interface *intf;
  190. struct usb_host_interface *intf_desc;
  191. struct usb_endpoint_descriptor *ep;
  192. int i;
  193. if (gspca_dev->sd_desc->int_pkt_scan) {
  194. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  195. intf_desc = intf->cur_altsetting;
  196. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  197. ep = &intf_desc->endpoint[i].desc;
  198. if (usb_endpoint_dir_in(ep) &&
  199. usb_endpoint_xfer_int(ep)) {
  200. alloc_and_submit_int_urb(gspca_dev, ep);
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  207. {
  208. struct urb *urb;
  209. urb = gspca_dev->int_urb;
  210. if (urb) {
  211. gspca_dev->int_urb = NULL;
  212. usb_kill_urb(urb);
  213. usb_free_coherent(gspca_dev->dev,
  214. urb->transfer_buffer_length,
  215. urb->transfer_buffer,
  216. urb->transfer_dma);
  217. usb_free_urb(urb);
  218. }
  219. }
  220. #else
  221. static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  222. {
  223. }
  224. static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  225. {
  226. }
  227. static inline int gspca_input_connect(struct gspca_dev *dev)
  228. {
  229. return 0;
  230. }
  231. #endif
  232. /*
  233. * fill a video frame from an URB and resubmit
  234. */
  235. static void fill_frame(struct gspca_dev *gspca_dev,
  236. struct urb *urb)
  237. {
  238. u8 *data; /* address of data in the iso message */
  239. int i, len, st;
  240. cam_pkt_op pkt_scan;
  241. if (urb->status != 0) {
  242. if (urb->status == -ESHUTDOWN)
  243. return; /* disconnection */
  244. #ifdef CONFIG_PM
  245. if (gspca_dev->frozen)
  246. return;
  247. #endif
  248. gspca_err(gspca_dev, "urb status: %d\n", urb->status);
  249. urb->status = 0;
  250. goto resubmit;
  251. }
  252. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  253. for (i = 0; i < urb->number_of_packets; i++) {
  254. len = urb->iso_frame_desc[i].actual_length;
  255. /* check the packet status and length */
  256. st = urb->iso_frame_desc[i].status;
  257. if (st) {
  258. pr_err("ISOC data error: [%d] len=%d, status=%d\n",
  259. i, len, st);
  260. gspca_dev->last_packet_type = DISCARD_PACKET;
  261. continue;
  262. }
  263. if (len == 0) {
  264. if (gspca_dev->empty_packet == 0)
  265. gspca_dev->empty_packet = 1;
  266. continue;
  267. }
  268. /* let the packet be analyzed by the subdriver */
  269. gspca_dbg(gspca_dev, D_PACK, "packet [%d] o:%d l:%d\n",
  270. i, urb->iso_frame_desc[i].offset, len);
  271. data = (u8 *) urb->transfer_buffer
  272. + urb->iso_frame_desc[i].offset;
  273. pkt_scan(gspca_dev, data, len);
  274. }
  275. resubmit:
  276. /* resubmit the URB */
  277. st = usb_submit_urb(urb, GFP_ATOMIC);
  278. if (st < 0)
  279. pr_err("usb_submit_urb() ret %d\n", st);
  280. }
  281. /*
  282. * ISOC message interrupt from the USB device
  283. *
  284. * Analyse each packet and call the subdriver for copy to the frame buffer.
  285. */
  286. static void isoc_irq(struct urb *urb)
  287. {
  288. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  289. gspca_dbg(gspca_dev, D_PACK, "isoc irq\n");
  290. if (!vb2_start_streaming_called(&gspca_dev->queue))
  291. return;
  292. fill_frame(gspca_dev, urb);
  293. }
  294. /*
  295. * bulk message interrupt from the USB device
  296. */
  297. static void bulk_irq(struct urb *urb)
  298. {
  299. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  300. int st;
  301. gspca_dbg(gspca_dev, D_PACK, "bulk irq\n");
  302. if (!vb2_start_streaming_called(&gspca_dev->queue))
  303. return;
  304. switch (urb->status) {
  305. case 0:
  306. break;
  307. case -ESHUTDOWN:
  308. return; /* disconnection */
  309. default:
  310. #ifdef CONFIG_PM
  311. if (gspca_dev->frozen)
  312. return;
  313. #endif
  314. gspca_err(gspca_dev, "urb status: %d\n", urb->status);
  315. urb->status = 0;
  316. goto resubmit;
  317. }
  318. gspca_dbg(gspca_dev, D_PACK, "packet l:%d\n", urb->actual_length);
  319. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  320. urb->transfer_buffer,
  321. urb->actual_length);
  322. resubmit:
  323. /* resubmit the URB */
  324. if (gspca_dev->cam.bulk_nurbs != 0) {
  325. st = usb_submit_urb(urb, GFP_ATOMIC);
  326. if (st < 0)
  327. pr_err("usb_submit_urb() ret %d\n", st);
  328. }
  329. }
  330. /*
  331. * add data to the current frame
  332. *
  333. * This function is called by the subdrivers at interrupt level.
  334. *
  335. * To build a frame, these ones must add
  336. * - one FIRST_PACKET
  337. * - 0 or many INTER_PACKETs
  338. * - one LAST_PACKET
  339. * DISCARD_PACKET invalidates the whole frame.
  340. */
  341. void gspca_frame_add(struct gspca_dev *gspca_dev,
  342. enum gspca_packet_type packet_type,
  343. const u8 *data,
  344. int len)
  345. {
  346. struct gspca_buffer *buf;
  347. unsigned long flags;
  348. gspca_dbg(gspca_dev, D_PACK, "add t:%d l:%d\n", packet_type, len);
  349. spin_lock_irqsave(&gspca_dev->qlock, flags);
  350. buf = list_first_entry_or_null(&gspca_dev->buf_list,
  351. typeof(*buf), list);
  352. spin_unlock_irqrestore(&gspca_dev->qlock, flags);
  353. if (packet_type == FIRST_PACKET) {
  354. /* if there is no queued buffer, discard the whole frame */
  355. if (!buf) {
  356. gspca_dev->last_packet_type = DISCARD_PACKET;
  357. gspca_dev->sequence++;
  358. return;
  359. }
  360. gspca_dev->image = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  361. gspca_dev->image_len = 0;
  362. } else {
  363. switch (gspca_dev->last_packet_type) {
  364. case DISCARD_PACKET:
  365. if (packet_type == LAST_PACKET) {
  366. gspca_dev->last_packet_type = packet_type;
  367. gspca_dev->image = NULL;
  368. gspca_dev->image_len = 0;
  369. }
  370. return;
  371. case LAST_PACKET:
  372. return;
  373. }
  374. }
  375. /* append the packet to the frame buffer */
  376. if (len > 0) {
  377. if (gspca_dev->image_len + len > gspca_dev->pixfmt.sizeimage) {
  378. gspca_err(gspca_dev, "frame overflow %d > %d\n",
  379. gspca_dev->image_len + len,
  380. gspca_dev->pixfmt.sizeimage);
  381. packet_type = DISCARD_PACKET;
  382. } else {
  383. /* !! image is NULL only when last pkt is LAST or DISCARD
  384. if (gspca_dev->image == NULL) {
  385. pr_err("gspca_frame_add() image == NULL\n");
  386. return;
  387. }
  388. */
  389. memcpy(gspca_dev->image + gspca_dev->image_len,
  390. data, len);
  391. gspca_dev->image_len += len;
  392. }
  393. }
  394. gspca_dev->last_packet_type = packet_type;
  395. /* if last packet, invalidate packet concatenation until
  396. * next first packet, wake up the application and advance
  397. * in the queue */
  398. if (packet_type == LAST_PACKET) {
  399. spin_lock_irqsave(&gspca_dev->qlock, flags);
  400. list_del(&buf->list);
  401. spin_unlock_irqrestore(&gspca_dev->qlock, flags);
  402. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  403. vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
  404. gspca_dev->image_len);
  405. buf->vb.sequence = gspca_dev->sequence++;
  406. buf->vb.field = V4L2_FIELD_NONE;
  407. gspca_dbg(gspca_dev, D_FRAM, "frame complete len:%d\n",
  408. gspca_dev->image_len);
  409. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  410. gspca_dev->image = NULL;
  411. gspca_dev->image_len = 0;
  412. }
  413. }
  414. EXPORT_SYMBOL(gspca_frame_add);
  415. static void destroy_urbs(struct gspca_dev *gspca_dev)
  416. {
  417. struct urb *urb;
  418. unsigned int i;
  419. gspca_dbg(gspca_dev, D_STREAM, "kill transfer\n");
  420. /* Killing all URBs guarantee that no URB completion
  421. * handler is running. Therefore, there shouldn't
  422. * be anyone trying to access gspca_dev->urb[i]
  423. */
  424. for (i = 0; i < MAX_NURBS; i++)
  425. usb_kill_urb(gspca_dev->urb[i]);
  426. gspca_dbg(gspca_dev, D_STREAM, "releasing urbs\n");
  427. for (i = 0; i < MAX_NURBS; i++) {
  428. urb = gspca_dev->urb[i];
  429. if (!urb)
  430. continue;
  431. gspca_dev->urb[i] = NULL;
  432. usb_free_coherent(gspca_dev->dev,
  433. urb->transfer_buffer_length,
  434. urb->transfer_buffer,
  435. urb->transfer_dma);
  436. usb_free_urb(urb);
  437. }
  438. }
  439. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  440. {
  441. int ret;
  442. if (gspca_dev->alt == 0)
  443. return 0;
  444. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  445. if (ret < 0)
  446. pr_err("set alt 0 err %d\n", ret);
  447. return ret;
  448. }
  449. /*
  450. * look for an input transfer endpoint in an alternate setting.
  451. *
  452. * If xfer_ep is invalid, return the first valid ep found, otherwise
  453. * look for exactly the ep with address equal to xfer_ep.
  454. */
  455. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  456. int xfer, int xfer_ep)
  457. {
  458. struct usb_host_endpoint *ep;
  459. int i, attr;
  460. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  461. ep = &alt->endpoint[i];
  462. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  463. if (attr == xfer
  464. && ep->desc.wMaxPacketSize != 0
  465. && usb_endpoint_dir_in(&ep->desc)
  466. && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
  467. return ep;
  468. }
  469. return NULL;
  470. }
  471. /* compute the minimum bandwidth for the current transfer */
  472. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  473. {
  474. u32 bandwidth;
  475. /* get the (max) image size */
  476. bandwidth = gspca_dev->pixfmt.sizeimage;
  477. /* if the image is compressed, estimate its mean size */
  478. if (!gspca_dev->cam.needs_full_bandwidth &&
  479. bandwidth < gspca_dev->pixfmt.width *
  480. gspca_dev->pixfmt.height)
  481. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  482. /* estimate the frame rate */
  483. if (gspca_dev->sd_desc->get_streamparm) {
  484. struct v4l2_streamparm parm;
  485. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  486. bandwidth *= parm.parm.capture.timeperframe.denominator;
  487. bandwidth /= parm.parm.capture.timeperframe.numerator;
  488. } else {
  489. /* don't hope more than 15 fps with USB 1.1 and
  490. * image resolution >= 640x480 */
  491. if (gspca_dev->pixfmt.width >= 640
  492. && gspca_dev->dev->speed == USB_SPEED_FULL)
  493. bandwidth *= 15; /* 15 fps */
  494. else
  495. bandwidth *= 30; /* 30 fps */
  496. }
  497. gspca_dbg(gspca_dev, D_STREAM, "min bandwidth: %d\n", bandwidth);
  498. return bandwidth;
  499. }
  500. /* endpoint table */
  501. #define MAX_ALT 16
  502. struct ep_tb_s {
  503. u32 alt;
  504. u32 bandwidth;
  505. };
  506. /*
  507. * build the table of the endpoints
  508. * and compute the minimum bandwidth for the image transfer
  509. */
  510. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  511. struct usb_interface *intf,
  512. struct ep_tb_s *ep_tb)
  513. {
  514. struct usb_host_endpoint *ep;
  515. int i, j, nbalt, psize, found;
  516. u32 bandwidth, last_bw;
  517. nbalt = intf->num_altsetting;
  518. if (nbalt > MAX_ALT)
  519. nbalt = MAX_ALT; /* fixme: should warn */
  520. /* build the endpoint table */
  521. i = 0;
  522. last_bw = 0;
  523. for (;;) {
  524. ep_tb->bandwidth = 2000 * 2000 * 120;
  525. found = 0;
  526. for (j = 0; j < nbalt; j++) {
  527. ep = alt_xfer(&intf->altsetting[j],
  528. USB_ENDPOINT_XFER_ISOC,
  529. gspca_dev->xfer_ep);
  530. if (ep == NULL)
  531. continue;
  532. if (ep->desc.bInterval == 0) {
  533. pr_err("alt %d iso endp with 0 interval\n", j);
  534. continue;
  535. }
  536. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  537. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  538. bandwidth = psize * 1000;
  539. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  540. || gspca_dev->dev->speed >= USB_SPEED_SUPER)
  541. bandwidth *= 8;
  542. bandwidth /= 1 << (ep->desc.bInterval - 1);
  543. if (bandwidth <= last_bw)
  544. continue;
  545. if (bandwidth < ep_tb->bandwidth) {
  546. ep_tb->bandwidth = bandwidth;
  547. ep_tb->alt = j;
  548. found = 1;
  549. }
  550. }
  551. if (!found)
  552. break;
  553. gspca_dbg(gspca_dev, D_STREAM, "alt %d bandwidth %d\n",
  554. ep_tb->alt, ep_tb->bandwidth);
  555. last_bw = ep_tb->bandwidth;
  556. i++;
  557. ep_tb++;
  558. }
  559. /*
  560. * If the camera:
  561. * has a usb audio class interface (a built in usb mic); and
  562. * is a usb 1 full speed device; and
  563. * uses the max full speed iso bandwidth; and
  564. * and has more than 1 alt setting
  565. * then skip the highest alt setting to spare bandwidth for the mic
  566. */
  567. if (gspca_dev->audio &&
  568. gspca_dev->dev->speed == USB_SPEED_FULL &&
  569. last_bw >= 1000000 &&
  570. i > 1) {
  571. gspca_dbg(gspca_dev, D_STREAM, "dev has usb audio, skipping highest alt\n");
  572. i--;
  573. ep_tb--;
  574. }
  575. /* get the requested bandwidth and start at the highest atlsetting */
  576. bandwidth = which_bandwidth(gspca_dev);
  577. ep_tb--;
  578. while (i > 1) {
  579. ep_tb--;
  580. if (ep_tb->bandwidth < bandwidth)
  581. break;
  582. i--;
  583. }
  584. return i;
  585. }
  586. /*
  587. * create the URBs for image transfer
  588. */
  589. static int create_urbs(struct gspca_dev *gspca_dev,
  590. struct usb_host_endpoint *ep)
  591. {
  592. struct urb *urb;
  593. int n, nurbs, i, psize, npkt, bsize;
  594. /* calculate the packet size and the number of packets */
  595. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  596. if (!gspca_dev->cam.bulk) { /* isoc */
  597. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  598. if (gspca_dev->pkt_size == 0)
  599. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  600. else
  601. psize = gspca_dev->pkt_size;
  602. npkt = gspca_dev->cam.npkt;
  603. if (npkt == 0)
  604. npkt = 32; /* default value */
  605. bsize = psize * npkt;
  606. gspca_dbg(gspca_dev, D_STREAM,
  607. "isoc %d pkts size %d = bsize:%d\n",
  608. npkt, psize, bsize);
  609. nurbs = DEF_NURBS;
  610. } else { /* bulk */
  611. npkt = 0;
  612. bsize = gspca_dev->cam.bulk_size;
  613. if (bsize == 0)
  614. bsize = psize;
  615. gspca_dbg(gspca_dev, D_STREAM, "bulk bsize:%d\n", bsize);
  616. if (gspca_dev->cam.bulk_nurbs != 0)
  617. nurbs = gspca_dev->cam.bulk_nurbs;
  618. else
  619. nurbs = 1;
  620. }
  621. for (n = 0; n < nurbs; n++) {
  622. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  623. if (!urb)
  624. return -ENOMEM;
  625. gspca_dev->urb[n] = urb;
  626. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  627. bsize,
  628. GFP_KERNEL,
  629. &urb->transfer_dma);
  630. if (urb->transfer_buffer == NULL) {
  631. pr_err("usb_alloc_coherent failed\n");
  632. return -ENOMEM;
  633. }
  634. urb->dev = gspca_dev->dev;
  635. urb->context = gspca_dev;
  636. urb->transfer_buffer_length = bsize;
  637. if (npkt != 0) { /* ISOC */
  638. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  639. ep->desc.bEndpointAddress);
  640. urb->transfer_flags = URB_ISO_ASAP
  641. | URB_NO_TRANSFER_DMA_MAP;
  642. urb->interval = 1 << (ep->desc.bInterval - 1);
  643. urb->complete = isoc_irq;
  644. urb->number_of_packets = npkt;
  645. for (i = 0; i < npkt; i++) {
  646. urb->iso_frame_desc[i].length = psize;
  647. urb->iso_frame_desc[i].offset = psize * i;
  648. }
  649. } else { /* bulk */
  650. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  651. ep->desc.bEndpointAddress);
  652. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  653. urb->complete = bulk_irq;
  654. }
  655. }
  656. return 0;
  657. }
  658. /* Note: both the queue and the usb locks should be held when calling this */
  659. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  660. {
  661. gspca_dev->streaming = false;
  662. gspca_dev->usb_err = 0;
  663. if (gspca_dev->sd_desc->stopN)
  664. gspca_dev->sd_desc->stopN(gspca_dev);
  665. destroy_urbs(gspca_dev);
  666. gspca_input_destroy_urb(gspca_dev);
  667. gspca_set_alt0(gspca_dev);
  668. if (gspca_dev->present)
  669. gspca_input_create_urb(gspca_dev);
  670. if (gspca_dev->sd_desc->stop0)
  671. gspca_dev->sd_desc->stop0(gspca_dev);
  672. gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n");
  673. }
  674. /*
  675. * start the USB transfer
  676. */
  677. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  678. {
  679. struct usb_interface *intf;
  680. struct usb_host_endpoint *ep;
  681. struct urb *urb;
  682. struct ep_tb_s ep_tb[MAX_ALT];
  683. int n, ret, xfer, alt, alt_idx;
  684. /* reset the streaming variables */
  685. gspca_dev->image = NULL;
  686. gspca_dev->image_len = 0;
  687. gspca_dev->last_packet_type = DISCARD_PACKET;
  688. gspca_dev->usb_err = 0;
  689. /* do the specific subdriver stuff before endpoint selection */
  690. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  691. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  692. if (gspca_dev->sd_desc->isoc_init) {
  693. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  694. if (ret < 0)
  695. return ret;
  696. }
  697. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  698. : USB_ENDPOINT_XFER_ISOC;
  699. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  700. if (gspca_dev->alt != 0) {
  701. gspca_dev->alt--; /* (previous version compatibility) */
  702. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
  703. gspca_dev->xfer_ep);
  704. if (ep == NULL) {
  705. pr_err("bad altsetting %d\n", gspca_dev->alt);
  706. return -EIO;
  707. }
  708. ep_tb[0].alt = gspca_dev->alt;
  709. alt_idx = 1;
  710. } else {
  711. /* else, compute the minimum bandwidth
  712. * and build the endpoint table */
  713. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  714. if (alt_idx <= 0) {
  715. pr_err("no transfer endpoint found\n");
  716. return -EIO;
  717. }
  718. }
  719. /* set the highest alternate setting and
  720. * loop until urb submit succeeds */
  721. gspca_input_destroy_urb(gspca_dev);
  722. gspca_dev->alt = ep_tb[--alt_idx].alt;
  723. alt = -1;
  724. for (;;) {
  725. if (alt != gspca_dev->alt) {
  726. alt = gspca_dev->alt;
  727. if (intf->num_altsetting > 1) {
  728. ret = usb_set_interface(gspca_dev->dev,
  729. gspca_dev->iface,
  730. alt);
  731. if (ret < 0) {
  732. if (ret == -ENOSPC)
  733. goto retry; /*fixme: ugly*/
  734. pr_err("set alt %d err %d\n", alt, ret);
  735. goto out;
  736. }
  737. }
  738. }
  739. if (!gspca_dev->cam.no_urb_create) {
  740. gspca_dbg(gspca_dev, D_STREAM, "init transfer alt %d\n",
  741. alt);
  742. ret = create_urbs(gspca_dev,
  743. alt_xfer(&intf->altsetting[alt], xfer,
  744. gspca_dev->xfer_ep));
  745. if (ret < 0) {
  746. destroy_urbs(gspca_dev);
  747. goto out;
  748. }
  749. }
  750. /* clear the bulk endpoint */
  751. if (gspca_dev->cam.bulk)
  752. usb_clear_halt(gspca_dev->dev,
  753. gspca_dev->urb[0]->pipe);
  754. /* start the cam */
  755. ret = gspca_dev->sd_desc->start(gspca_dev);
  756. if (ret < 0) {
  757. destroy_urbs(gspca_dev);
  758. goto out;
  759. }
  760. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  761. gspca_dev->streaming = true;
  762. /* some bulk transfers are started by the subdriver */
  763. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  764. break;
  765. /* submit the URBs */
  766. for (n = 0; n < MAX_NURBS; n++) {
  767. urb = gspca_dev->urb[n];
  768. if (urb == NULL)
  769. break;
  770. ret = usb_submit_urb(urb, GFP_KERNEL);
  771. if (ret < 0)
  772. break;
  773. }
  774. if (ret >= 0)
  775. break; /* transfer is started */
  776. /* something when wrong
  777. * stop the webcam and free the transfer resources */
  778. gspca_stream_off(gspca_dev);
  779. if (ret != -ENOSPC) {
  780. pr_err("usb_submit_urb alt %d err %d\n",
  781. gspca_dev->alt, ret);
  782. goto out;
  783. }
  784. /* the bandwidth is not wide enough
  785. * negotiate or try a lower alternate setting */
  786. retry:
  787. gspca_err(gspca_dev, "alt %d - bandwidth not wide enough, trying again\n",
  788. alt);
  789. msleep(20); /* wait for kill complete */
  790. if (gspca_dev->sd_desc->isoc_nego) {
  791. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  792. if (ret < 0)
  793. goto out;
  794. } else {
  795. if (alt_idx <= 0) {
  796. pr_err("no transfer endpoint found\n");
  797. ret = -EIO;
  798. goto out;
  799. }
  800. gspca_dev->alt = ep_tb[--alt_idx].alt;
  801. }
  802. }
  803. out:
  804. gspca_input_create_urb(gspca_dev);
  805. return ret;
  806. }
  807. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  808. {
  809. int i;
  810. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  811. gspca_dev->curr_mode = i;
  812. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
  813. /* does nothing if ctrl_handler == NULL */
  814. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  815. }
  816. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  817. int width, int height)
  818. {
  819. int i;
  820. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  821. if (width == gspca_dev->cam.cam_mode[i].width
  822. && height == gspca_dev->cam.cam_mode[i].height)
  823. return i;
  824. }
  825. return -EINVAL;
  826. }
  827. static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
  828. int width, int height)
  829. {
  830. int i;
  831. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  832. if (width >= gspca_dev->cam.cam_mode[i].width
  833. && height >= gspca_dev->cam.cam_mode[i].height)
  834. break;
  835. }
  836. return i;
  837. }
  838. /*
  839. * search a mode with the right pixel format
  840. */
  841. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  842. int mode,
  843. int pixfmt)
  844. {
  845. int modeU, modeD;
  846. modeU = modeD = mode;
  847. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  848. if (--modeD >= 0) {
  849. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  850. == pixfmt)
  851. return modeD;
  852. }
  853. if (++modeU < gspca_dev->cam.nmodes) {
  854. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  855. == pixfmt)
  856. return modeU;
  857. }
  858. }
  859. return -EINVAL;
  860. }
  861. #ifdef CONFIG_VIDEO_ADV_DEBUG
  862. static int vidioc_g_chip_info(struct file *file, void *priv,
  863. struct v4l2_dbg_chip_info *chip)
  864. {
  865. struct gspca_dev *gspca_dev = video_drvdata(file);
  866. gspca_dev->usb_err = 0;
  867. if (gspca_dev->sd_desc->get_chip_info)
  868. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  869. return chip->match.addr ? -EINVAL : 0;
  870. }
  871. static int vidioc_g_register(struct file *file, void *priv,
  872. struct v4l2_dbg_register *reg)
  873. {
  874. struct gspca_dev *gspca_dev = video_drvdata(file);
  875. gspca_dev->usb_err = 0;
  876. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  877. }
  878. static int vidioc_s_register(struct file *file, void *priv,
  879. const struct v4l2_dbg_register *reg)
  880. {
  881. struct gspca_dev *gspca_dev = video_drvdata(file);
  882. gspca_dev->usb_err = 0;
  883. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  884. }
  885. #endif
  886. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  887. struct v4l2_fmtdesc *fmtdesc)
  888. {
  889. struct gspca_dev *gspca_dev = video_drvdata(file);
  890. int i, j, index;
  891. __u32 fmt_tb[8];
  892. /* give an index to each format */
  893. index = 0;
  894. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  895. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  896. j = 0;
  897. for (;;) {
  898. if (fmt_tb[j] == fmt_tb[index])
  899. break;
  900. j++;
  901. }
  902. if (j == index) {
  903. if (fmtdesc->index == index)
  904. break; /* new format */
  905. index++;
  906. if (index >= ARRAY_SIZE(fmt_tb))
  907. return -EINVAL;
  908. }
  909. }
  910. if (i < 0)
  911. return -EINVAL; /* no more format */
  912. fmtdesc->pixelformat = fmt_tb[index];
  913. if (gspca_dev->cam.cam_mode[i].sizeimage <
  914. gspca_dev->cam.cam_mode[i].width *
  915. gspca_dev->cam.cam_mode[i].height)
  916. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  917. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  918. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  919. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  920. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  921. fmtdesc->description[4] = '\0';
  922. return 0;
  923. }
  924. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  925. struct v4l2_format *fmt)
  926. {
  927. struct gspca_dev *gspca_dev = video_drvdata(file);
  928. fmt->fmt.pix = gspca_dev->pixfmt;
  929. /* some drivers use priv internally, zero it before giving it back to
  930. the core */
  931. fmt->fmt.pix.priv = 0;
  932. return 0;
  933. }
  934. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  935. struct v4l2_format *fmt)
  936. {
  937. int w, h, mode, mode2;
  938. w = fmt->fmt.pix.width;
  939. h = fmt->fmt.pix.height;
  940. PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
  941. fmt->fmt.pix.pixelformat, w, h);
  942. /* search the nearest mode for width and height */
  943. mode = wxh_to_nearest_mode(gspca_dev, w, h);
  944. /* OK if right palette */
  945. if (gspca_dev->cam.cam_mode[mode].pixelformat
  946. != fmt->fmt.pix.pixelformat) {
  947. /* else, search the closest mode with the same pixel format */
  948. mode2 = gspca_get_mode(gspca_dev, mode,
  949. fmt->fmt.pix.pixelformat);
  950. if (mode2 >= 0)
  951. mode = mode2;
  952. }
  953. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  954. if (gspca_dev->sd_desc->try_fmt) {
  955. /* pass original resolution to subdriver try_fmt */
  956. fmt->fmt.pix.width = w;
  957. fmt->fmt.pix.height = h;
  958. gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
  959. }
  960. /* some drivers use priv internally, zero it before giving it back to
  961. the core */
  962. fmt->fmt.pix.priv = 0;
  963. return mode; /* used when s_fmt */
  964. }
  965. static int vidioc_try_fmt_vid_cap(struct file *file,
  966. void *priv,
  967. struct v4l2_format *fmt)
  968. {
  969. struct gspca_dev *gspca_dev = video_drvdata(file);
  970. if (try_fmt_vid_cap(gspca_dev, fmt) < 0)
  971. return -EINVAL;
  972. return 0;
  973. }
  974. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  975. struct v4l2_format *fmt)
  976. {
  977. struct gspca_dev *gspca_dev = video_drvdata(file);
  978. int mode;
  979. if (vb2_is_busy(&gspca_dev->queue))
  980. return -EBUSY;
  981. mode = try_fmt_vid_cap(gspca_dev, fmt);
  982. if (mode < 0)
  983. return -EINVAL;
  984. gspca_dev->curr_mode = mode;
  985. if (gspca_dev->sd_desc->try_fmt)
  986. /* subdriver try_fmt can modify format parameters */
  987. gspca_dev->pixfmt = fmt->fmt.pix;
  988. else
  989. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode];
  990. return 0;
  991. }
  992. static int vidioc_enum_framesizes(struct file *file, void *priv,
  993. struct v4l2_frmsizeenum *fsize)
  994. {
  995. struct gspca_dev *gspca_dev = video_drvdata(file);
  996. int i;
  997. __u32 index = 0;
  998. if (gspca_dev->sd_desc->enum_framesizes)
  999. return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
  1000. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1001. if (fsize->pixel_format !=
  1002. gspca_dev->cam.cam_mode[i].pixelformat)
  1003. continue;
  1004. if (fsize->index == index) {
  1005. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1006. fsize->discrete.width =
  1007. gspca_dev->cam.cam_mode[i].width;
  1008. fsize->discrete.height =
  1009. gspca_dev->cam.cam_mode[i].height;
  1010. return 0;
  1011. }
  1012. index++;
  1013. }
  1014. return -EINVAL;
  1015. }
  1016. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1017. struct v4l2_frmivalenum *fival)
  1018. {
  1019. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1020. int mode;
  1021. __u32 i;
  1022. mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1023. if (mode < 0)
  1024. return -EINVAL;
  1025. if (gspca_dev->cam.mode_framerates == NULL ||
  1026. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1027. return -EINVAL;
  1028. if (fival->pixel_format !=
  1029. gspca_dev->cam.cam_mode[mode].pixelformat)
  1030. return -EINVAL;
  1031. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1032. if (fival->index == i) {
  1033. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1034. fival->discrete.numerator = 1;
  1035. fival->discrete.denominator =
  1036. gspca_dev->cam.mode_framerates[mode].rates[i];
  1037. return 0;
  1038. }
  1039. }
  1040. return -EINVAL;
  1041. }
  1042. static void gspca_release(struct v4l2_device *v4l2_device)
  1043. {
  1044. struct gspca_dev *gspca_dev =
  1045. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1046. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1047. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1048. kfree(gspca_dev->usb_buf);
  1049. kfree(gspca_dev);
  1050. }
  1051. static int vidioc_querycap(struct file *file, void *priv,
  1052. struct v4l2_capability *cap)
  1053. {
  1054. struct gspca_dev *gspca_dev = video_drvdata(file);
  1055. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1056. sizeof cap->driver);
  1057. if (gspca_dev->dev->product != NULL) {
  1058. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1059. sizeof cap->card);
  1060. } else {
  1061. snprintf((char *) cap->card, sizeof cap->card,
  1062. "USB Camera (%04x:%04x)",
  1063. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1064. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1065. }
  1066. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1067. sizeof(cap->bus_info));
  1068. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1069. | V4L2_CAP_STREAMING
  1070. | V4L2_CAP_READWRITE;
  1071. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1072. return 0;
  1073. }
  1074. static int vidioc_enum_input(struct file *file, void *priv,
  1075. struct v4l2_input *input)
  1076. {
  1077. struct gspca_dev *gspca_dev = video_drvdata(file);
  1078. if (input->index != 0)
  1079. return -EINVAL;
  1080. input->type = V4L2_INPUT_TYPE_CAMERA;
  1081. input->status = gspca_dev->cam.input_flags;
  1082. strlcpy(input->name, gspca_dev->sd_desc->name,
  1083. sizeof input->name);
  1084. return 0;
  1085. }
  1086. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1087. {
  1088. *i = 0;
  1089. return 0;
  1090. }
  1091. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1092. {
  1093. if (i > 0)
  1094. return -EINVAL;
  1095. return 0;
  1096. }
  1097. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1098. struct v4l2_jpegcompression *jpegcomp)
  1099. {
  1100. struct gspca_dev *gspca_dev = video_drvdata(file);
  1101. gspca_dev->usb_err = 0;
  1102. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1103. }
  1104. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1105. const struct v4l2_jpegcompression *jpegcomp)
  1106. {
  1107. struct gspca_dev *gspca_dev = video_drvdata(file);
  1108. gspca_dev->usb_err = 0;
  1109. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1110. }
  1111. static int vidioc_g_parm(struct file *filp, void *priv,
  1112. struct v4l2_streamparm *parm)
  1113. {
  1114. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1115. parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
  1116. if (!gspca_dev->sd_desc->get_streamparm)
  1117. return 0;
  1118. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1119. gspca_dev->usb_err = 0;
  1120. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1121. return gspca_dev->usb_err;
  1122. }
  1123. static int vidioc_s_parm(struct file *filp, void *priv,
  1124. struct v4l2_streamparm *parm)
  1125. {
  1126. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1127. parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
  1128. if (!gspca_dev->sd_desc->set_streamparm) {
  1129. parm->parm.capture.capability = 0;
  1130. return 0;
  1131. }
  1132. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1133. gspca_dev->usb_err = 0;
  1134. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1135. return gspca_dev->usb_err;
  1136. }
  1137. static int gspca_queue_setup(struct vb2_queue *vq,
  1138. unsigned int *nbuffers, unsigned int *nplanes,
  1139. unsigned int sizes[], struct device *alloc_devs[])
  1140. {
  1141. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
  1142. if (*nplanes)
  1143. return sizes[0] < gspca_dev->pixfmt.sizeimage ? -EINVAL : 0;
  1144. *nplanes = 1;
  1145. sizes[0] = gspca_dev->pixfmt.sizeimage;
  1146. return 0;
  1147. }
  1148. static int gspca_buffer_prepare(struct vb2_buffer *vb)
  1149. {
  1150. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
  1151. unsigned long size = gspca_dev->pixfmt.sizeimage;
  1152. if (vb2_plane_size(vb, 0) < size) {
  1153. gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n",
  1154. vb2_plane_size(vb, 0), size);
  1155. return -EINVAL;
  1156. }
  1157. return 0;
  1158. }
  1159. static void gspca_buffer_finish(struct vb2_buffer *vb)
  1160. {
  1161. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
  1162. if (!gspca_dev->sd_desc->dq_callback)
  1163. return;
  1164. gspca_dev->usb_err = 0;
  1165. if (gspca_dev->present)
  1166. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1167. }
  1168. static void gspca_buffer_queue(struct vb2_buffer *vb)
  1169. {
  1170. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
  1171. struct gspca_buffer *buf = to_gspca_buffer(vb);
  1172. unsigned long flags;
  1173. spin_lock_irqsave(&gspca_dev->qlock, flags);
  1174. list_add_tail(&buf->list, &gspca_dev->buf_list);
  1175. spin_unlock_irqrestore(&gspca_dev->qlock, flags);
  1176. }
  1177. static void gspca_return_all_buffers(struct gspca_dev *gspca_dev,
  1178. enum vb2_buffer_state state)
  1179. {
  1180. struct gspca_buffer *buf, *node;
  1181. unsigned long flags;
  1182. spin_lock_irqsave(&gspca_dev->qlock, flags);
  1183. list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) {
  1184. vb2_buffer_done(&buf->vb.vb2_buf, state);
  1185. list_del(&buf->list);
  1186. }
  1187. spin_unlock_irqrestore(&gspca_dev->qlock, flags);
  1188. }
  1189. static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count)
  1190. {
  1191. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
  1192. int ret;
  1193. gspca_dev->sequence = 0;
  1194. ret = gspca_init_transfer(gspca_dev);
  1195. if (ret)
  1196. gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED);
  1197. return ret;
  1198. }
  1199. static void gspca_stop_streaming(struct vb2_queue *vq)
  1200. {
  1201. struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
  1202. gspca_stream_off(gspca_dev);
  1203. /* Release all active buffers */
  1204. gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR);
  1205. }
  1206. static const struct vb2_ops gspca_qops = {
  1207. .queue_setup = gspca_queue_setup,
  1208. .buf_prepare = gspca_buffer_prepare,
  1209. .buf_finish = gspca_buffer_finish,
  1210. .buf_queue = gspca_buffer_queue,
  1211. .start_streaming = gspca_start_streaming,
  1212. .stop_streaming = gspca_stop_streaming,
  1213. .wait_prepare = vb2_ops_wait_prepare,
  1214. .wait_finish = vb2_ops_wait_finish,
  1215. };
  1216. static const struct v4l2_file_operations dev_fops = {
  1217. .owner = THIS_MODULE,
  1218. .open = v4l2_fh_open,
  1219. .release = vb2_fop_release,
  1220. .unlocked_ioctl = video_ioctl2,
  1221. .read = vb2_fop_read,
  1222. .mmap = vb2_fop_mmap,
  1223. .poll = vb2_fop_poll,
  1224. };
  1225. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1226. .vidioc_querycap = vidioc_querycap,
  1227. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1228. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1229. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1230. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1231. .vidioc_enum_input = vidioc_enum_input,
  1232. .vidioc_g_input = vidioc_g_input,
  1233. .vidioc_s_input = vidioc_s_input,
  1234. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1235. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1236. .vidioc_g_parm = vidioc_g_parm,
  1237. .vidioc_s_parm = vidioc_s_parm,
  1238. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1239. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1240. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1241. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1242. .vidioc_querybuf = vb2_ioctl_querybuf,
  1243. .vidioc_qbuf = vb2_ioctl_qbuf,
  1244. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1245. .vidioc_expbuf = vb2_ioctl_expbuf,
  1246. .vidioc_streamon = vb2_ioctl_streamon,
  1247. .vidioc_streamoff = vb2_ioctl_streamoff,
  1248. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1249. .vidioc_g_chip_info = vidioc_g_chip_info,
  1250. .vidioc_g_register = vidioc_g_register,
  1251. .vidioc_s_register = vidioc_s_register,
  1252. #endif
  1253. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1254. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1255. };
  1256. static const struct video_device gspca_template = {
  1257. .name = "gspca main driver",
  1258. .fops = &dev_fops,
  1259. .ioctl_ops = &dev_ioctl_ops,
  1260. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1261. };
  1262. /*
  1263. * probe and create a new gspca device
  1264. *
  1265. * This function must be called by the sub-driver when it is
  1266. * called for probing a new device.
  1267. */
  1268. int gspca_dev_probe2(struct usb_interface *intf,
  1269. const struct usb_device_id *id,
  1270. const struct sd_desc *sd_desc,
  1271. int dev_size,
  1272. struct module *module)
  1273. {
  1274. struct gspca_dev *gspca_dev;
  1275. struct usb_device *dev = interface_to_usbdev(intf);
  1276. struct vb2_queue *q;
  1277. int ret;
  1278. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1279. sd_desc->name, id->idVendor, id->idProduct);
  1280. /* create the device */
  1281. if (dev_size < sizeof *gspca_dev)
  1282. dev_size = sizeof *gspca_dev;
  1283. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1284. if (!gspca_dev) {
  1285. pr_err("couldn't kzalloc gspca struct\n");
  1286. return -ENOMEM;
  1287. }
  1288. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1289. if (!gspca_dev->usb_buf) {
  1290. pr_err("out of memory\n");
  1291. ret = -ENOMEM;
  1292. goto out;
  1293. }
  1294. gspca_dev->dev = dev;
  1295. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1296. gspca_dev->xfer_ep = -1;
  1297. /* check if any audio device */
  1298. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1299. int i;
  1300. struct usb_interface *intf2;
  1301. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1302. intf2 = dev->actconfig->interface[i];
  1303. if (intf2 != NULL
  1304. && intf2->altsetting != NULL
  1305. && intf2->altsetting->desc.bInterfaceClass ==
  1306. USB_CLASS_AUDIO) {
  1307. gspca_dev->audio = 1;
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. gspca_dev->v4l2_dev.release = gspca_release;
  1313. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1314. if (ret)
  1315. goto out;
  1316. gspca_dev->present = true;
  1317. gspca_dev->sd_desc = sd_desc;
  1318. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1319. gspca_dev->vdev = gspca_template;
  1320. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1321. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1322. gspca_dev->module = module;
  1323. mutex_init(&gspca_dev->usb_lock);
  1324. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1325. init_waitqueue_head(&gspca_dev->wq);
  1326. /* Initialize the vb2 queue */
  1327. q = &gspca_dev->queue;
  1328. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1329. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1330. q->drv_priv = gspca_dev;
  1331. q->buf_struct_size = sizeof(struct gspca_buffer);
  1332. q->ops = &gspca_qops;
  1333. q->mem_ops = &vb2_vmalloc_memops;
  1334. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1335. q->min_buffers_needed = 2;
  1336. q->lock = &gspca_dev->usb_lock;
  1337. ret = vb2_queue_init(q);
  1338. if (ret)
  1339. goto out;
  1340. gspca_dev->vdev.queue = q;
  1341. INIT_LIST_HEAD(&gspca_dev->buf_list);
  1342. spin_lock_init(&gspca_dev->qlock);
  1343. /* configure the subdriver and initialize the USB device */
  1344. ret = sd_desc->config(gspca_dev, id);
  1345. if (ret < 0)
  1346. goto out;
  1347. ret = sd_desc->init(gspca_dev);
  1348. if (ret < 0)
  1349. goto out;
  1350. if (sd_desc->init_controls)
  1351. ret = sd_desc->init_controls(gspca_dev);
  1352. if (ret < 0)
  1353. goto out;
  1354. gspca_set_default_mode(gspca_dev);
  1355. ret = gspca_input_connect(gspca_dev);
  1356. if (ret)
  1357. goto out;
  1358. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1359. if (!gspca_dev->sd_desc->get_register)
  1360. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1361. if (!gspca_dev->sd_desc->set_register)
  1362. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1363. #endif
  1364. if (!gspca_dev->sd_desc->get_jcomp)
  1365. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1366. if (!gspca_dev->sd_desc->set_jcomp)
  1367. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1368. /* init video stuff */
  1369. ret = video_register_device(&gspca_dev->vdev,
  1370. VFL_TYPE_GRABBER,
  1371. -1);
  1372. if (ret < 0) {
  1373. pr_err("video_register_device err %d\n", ret);
  1374. goto out;
  1375. }
  1376. usb_set_intfdata(intf, gspca_dev);
  1377. gspca_dbg(gspca_dev, D_PROBE, "%s created\n",
  1378. video_device_node_name(&gspca_dev->vdev));
  1379. gspca_input_create_urb(gspca_dev);
  1380. return 0;
  1381. out:
  1382. #if IS_ENABLED(CONFIG_INPUT)
  1383. if (gspca_dev->input_dev)
  1384. input_unregister_device(gspca_dev->input_dev);
  1385. #endif
  1386. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1387. kfree(gspca_dev->usb_buf);
  1388. kfree(gspca_dev);
  1389. return ret;
  1390. }
  1391. EXPORT_SYMBOL(gspca_dev_probe2);
  1392. /* same function as the previous one, but check the interface */
  1393. int gspca_dev_probe(struct usb_interface *intf,
  1394. const struct usb_device_id *id,
  1395. const struct sd_desc *sd_desc,
  1396. int dev_size,
  1397. struct module *module)
  1398. {
  1399. struct usb_device *dev = interface_to_usbdev(intf);
  1400. /* we don't handle multi-config cameras */
  1401. if (dev->descriptor.bNumConfigurations != 1) {
  1402. pr_err("%04x:%04x too many config\n",
  1403. id->idVendor, id->idProduct);
  1404. return -ENODEV;
  1405. }
  1406. /* the USB video interface must be the first one */
  1407. if (dev->actconfig->desc.bNumInterfaces != 1
  1408. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1409. return -ENODEV;
  1410. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1411. }
  1412. EXPORT_SYMBOL(gspca_dev_probe);
  1413. /*
  1414. * USB disconnection
  1415. *
  1416. * This function must be called by the sub-driver
  1417. * when the device disconnects, after the specific resources are freed.
  1418. */
  1419. void gspca_disconnect(struct usb_interface *intf)
  1420. {
  1421. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1422. #if IS_ENABLED(CONFIG_INPUT)
  1423. struct input_dev *input_dev;
  1424. #endif
  1425. gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n",
  1426. video_device_node_name(&gspca_dev->vdev));
  1427. mutex_lock(&gspca_dev->usb_lock);
  1428. gspca_dev->present = false;
  1429. vb2_queue_error(&gspca_dev->queue);
  1430. #if IS_ENABLED(CONFIG_INPUT)
  1431. input_dev = gspca_dev->input_dev;
  1432. if (input_dev) {
  1433. gspca_dev->input_dev = NULL;
  1434. input_unregister_device(input_dev);
  1435. }
  1436. #endif
  1437. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1438. video_unregister_device(&gspca_dev->vdev);
  1439. mutex_unlock(&gspca_dev->usb_lock);
  1440. /* (this will call gspca_release() immediately or on last close) */
  1441. v4l2_device_put(&gspca_dev->v4l2_dev);
  1442. }
  1443. EXPORT_SYMBOL(gspca_disconnect);
  1444. #ifdef CONFIG_PM
  1445. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1446. {
  1447. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1448. gspca_input_destroy_urb(gspca_dev);
  1449. if (!vb2_start_streaming_called(&gspca_dev->queue))
  1450. return 0;
  1451. mutex_lock(&gspca_dev->usb_lock);
  1452. gspca_dev->frozen = 1; /* avoid urb error messages */
  1453. gspca_dev->usb_err = 0;
  1454. if (gspca_dev->sd_desc->stopN)
  1455. gspca_dev->sd_desc->stopN(gspca_dev);
  1456. destroy_urbs(gspca_dev);
  1457. gspca_set_alt0(gspca_dev);
  1458. if (gspca_dev->sd_desc->stop0)
  1459. gspca_dev->sd_desc->stop0(gspca_dev);
  1460. mutex_unlock(&gspca_dev->usb_lock);
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL(gspca_suspend);
  1464. int gspca_resume(struct usb_interface *intf)
  1465. {
  1466. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1467. int streaming, ret = 0;
  1468. mutex_lock(&gspca_dev->usb_lock);
  1469. gspca_dev->frozen = 0;
  1470. gspca_dev->usb_err = 0;
  1471. gspca_dev->sd_desc->init(gspca_dev);
  1472. /*
  1473. * Most subdrivers send all ctrl values on sd_start and thus
  1474. * only write to the device registers on s_ctrl when streaming ->
  1475. * Clear streaming to avoid setting all ctrls twice.
  1476. */
  1477. streaming = vb2_start_streaming_called(&gspca_dev->queue);
  1478. if (streaming)
  1479. ret = gspca_init_transfer(gspca_dev);
  1480. else
  1481. gspca_input_create_urb(gspca_dev);
  1482. mutex_unlock(&gspca_dev->usb_lock);
  1483. return ret;
  1484. }
  1485. EXPORT_SYMBOL(gspca_resume);
  1486. #endif
  1487. /* -- module insert / remove -- */
  1488. static int __init gspca_init(void)
  1489. {
  1490. pr_info("v" GSPCA_VERSION " registered\n");
  1491. return 0;
  1492. }
  1493. static void __exit gspca_exit(void)
  1494. {
  1495. }
  1496. module_init(gspca_init);
  1497. module_exit(gspca_exit);
  1498. module_param_named(debug, gspca_debug, int, 0644);
  1499. MODULE_PARM_DESC(debug,
  1500. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");