konicawc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * konicawc.c - konica webcam driver
  3. *
  4. * Author: Simon Evans <spse@secret.org.uk>
  5. *
  6. * Copyright (C) 2002 Simon Evans
  7. *
  8. * Licence: GPL
  9. *
  10. * Driver for USB webcams based on Konica chipset. This
  11. * chipset is used in Intel YC76 camera.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/usb/input.h>
  18. #include "usbvideo.h"
  19. #define MAX_BRIGHTNESS 108
  20. #define MAX_CONTRAST 108
  21. #define MAX_SATURATION 108
  22. #define MAX_SHARPNESS 108
  23. #define MAX_WHITEBAL 372
  24. #define MAX_SPEED 6
  25. #define MAX_CAMERAS 1
  26. #define DRIVER_VERSION "v1.4"
  27. #define DRIVER_DESC "Konica Webcam driver"
  28. enum ctrl_req {
  29. SetWhitebal = 0x01,
  30. SetBrightness = 0x02,
  31. SetSharpness = 0x03,
  32. SetContrast = 0x04,
  33. SetSaturation = 0x05,
  34. };
  35. enum frame_sizes {
  36. SIZE_160X120 = 0,
  37. SIZE_160X136 = 1,
  38. SIZE_176X144 = 2,
  39. SIZE_320X240 = 3,
  40. };
  41. #define MAX_FRAME_SIZE SIZE_320X240
  42. static struct usbvideo *cams;
  43. #ifdef CONFIG_USB_DEBUG
  44. static int debug;
  45. #define DEBUG(n, format, arg...) \
  46. if (n <= debug) { \
  47. printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
  48. }
  49. #else
  50. #define DEBUG(n, arg...)
  51. static const int debug;
  52. #endif
  53. /* Some default values for initial camera settings,
  54. can be set by modprobe */
  55. static int size;
  56. static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
  57. static int brightness = MAX_BRIGHTNESS/2;
  58. static int contrast = MAX_CONTRAST/2;
  59. static int saturation = MAX_SATURATION/2;
  60. static int sharpness = MAX_SHARPNESS/2;
  61. static int whitebal = 3*(MAX_WHITEBAL/4);
  62. static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
  63. /* These FPS speeds are from the windows config box. They are
  64. * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  65. * real fps.
  66. */
  67. static const int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
  68. { 24, 40, 48, 60, 72, 80, 100 },
  69. { 18, 30, 36, 45, 54, 60, 75 },
  70. { 6, 10, 12, 15, 18, 21, 25 } };
  71. struct cam_size {
  72. u16 width;
  73. u16 height;
  74. u8 cmd;
  75. };
  76. static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
  77. { 160, 136, 0xa },
  78. { 176, 144, 0x4 },
  79. { 320, 240, 0x5 } };
  80. struct konicawc {
  81. u8 brightness; /* camera uses 0 - 9, x11 for real value */
  82. u8 contrast; /* as above */
  83. u8 saturation; /* as above */
  84. u8 sharpness; /* as above */
  85. u8 white_bal; /* 0 - 33, x11 for real value */
  86. u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
  87. u8 size; /* Frame Size */
  88. int height;
  89. int width;
  90. struct urb *sts_urb[USBVIDEO_NUMSBUF];
  91. u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
  92. struct urb *last_data_urb;
  93. int lastframe;
  94. int cur_frame_size; /* number of bytes in current frame size */
  95. int maxline; /* number of lines per frame */
  96. int yplanesz; /* Number of bytes in the Y plane */
  97. unsigned int buttonsts:1;
  98. #ifdef CONFIG_INPUT
  99. struct input_dev *input;
  100. char input_physname[64];
  101. #endif
  102. };
  103. #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
  104. #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
  105. #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
  106. static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  107. {
  108. int retval = usb_control_msg(uvd->dev,
  109. dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
  110. request, 0x40 | dir, value, index, buf, len, 1000);
  111. return retval < 0 ? retval : 0;
  112. }
  113. static inline void konicawc_camera_on(struct uvd *uvd)
  114. {
  115. DEBUG(0, "camera on");
  116. konicawc_set_misc(uvd, 0x2, 1, 0x0b);
  117. }
  118. static inline void konicawc_camera_off(struct uvd *uvd)
  119. {
  120. DEBUG(0, "camera off");
  121. konicawc_set_misc(uvd, 0x2, 0, 0x0b);
  122. }
  123. static void konicawc_set_camera_size(struct uvd *uvd)
  124. {
  125. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  126. konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
  127. cam->width = camera_sizes[cam->size].width;
  128. cam->height = camera_sizes[cam->size].height;
  129. cam->yplanesz = cam->height * cam->width;
  130. cam->cur_frame_size = (cam->yplanesz * 3) / 2;
  131. cam->maxline = cam->yplanesz / 256;
  132. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  133. }
  134. static int konicawc_setup_on_open(struct uvd *uvd)
  135. {
  136. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  137. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  138. cam->brightness * 11);
  139. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  140. DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
  141. cam->white_bal * 11);
  142. konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
  143. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  144. cam->contrast * 11);
  145. konicawc_set_value(uvd, cam->contrast, SetContrast);
  146. DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
  147. cam->saturation * 11);
  148. konicawc_set_value(uvd, cam->saturation, SetSaturation);
  149. DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
  150. cam->sharpness * 11);
  151. konicawc_set_value(uvd, cam->sharpness, SetSharpness);
  152. konicawc_set_camera_size(uvd);
  153. cam->lastframe = -2;
  154. cam->buttonsts = 0;
  155. return 0;
  156. }
  157. static void konicawc_adjust_picture(struct uvd *uvd)
  158. {
  159. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  160. konicawc_camera_off(uvd);
  161. DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
  162. uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
  163. if(cam->brightness != uvd->vpic.brightness / 11) {
  164. cam->brightness = uvd->vpic.brightness / 11;
  165. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  166. cam->brightness * 11);
  167. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  168. }
  169. DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
  170. uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
  171. if(cam->contrast != uvd->vpic.contrast / 11) {
  172. cam->contrast = uvd->vpic.contrast / 11;
  173. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  174. cam->contrast * 11);
  175. konicawc_set_value(uvd, cam->contrast, SetContrast);
  176. }
  177. konicawc_camera_on(uvd);
  178. }
  179. #ifdef CONFIG_INPUT
  180. static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
  181. {
  182. struct input_dev *input_dev;
  183. int error;
  184. usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
  185. strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
  186. cam->input = input_dev = input_allocate_device();
  187. if (!input_dev) {
  188. dev_warn(&dev->dev,
  189. "Not enough memory for camera's input device\n");
  190. return;
  191. }
  192. input_dev->name = "Konicawc snapshot button";
  193. input_dev->phys = cam->input_physname;
  194. usb_to_input_id(dev, &input_dev->id);
  195. input_dev->dev.parent = &dev->dev;
  196. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  197. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  198. error = input_register_device(cam->input);
  199. if (error) {
  200. dev_warn(&dev->dev,
  201. "Failed to register camera's input device, err: %d\n",
  202. error);
  203. input_free_device(cam->input);
  204. cam->input = NULL;
  205. }
  206. }
  207. static void konicawc_unregister_input(struct konicawc *cam)
  208. {
  209. if (cam->input) {
  210. input_unregister_device(cam->input);
  211. cam->input = NULL;
  212. }
  213. }
  214. static void konicawc_report_buttonstat(struct konicawc *cam)
  215. {
  216. if (cam->input) {
  217. input_report_key(cam->input, KEY_CAMERA, cam->buttonsts);
  218. input_sync(cam->input);
  219. }
  220. }
  221. #else
  222. static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
  223. static inline void konicawc_unregister_input(struct konicawc *cam) { }
  224. static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
  225. #endif /* CONFIG_INPUT */
  226. static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
  227. {
  228. char *cdata;
  229. int i, totlen = 0;
  230. unsigned char *status = stsurb->transfer_buffer;
  231. int keep = 0, discard = 0, bad = 0;
  232. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  233. for (i = 0; i < dataurb->number_of_packets; i++) {
  234. int button = cam->buttonsts;
  235. unsigned char sts;
  236. int n = dataurb->iso_frame_desc[i].actual_length;
  237. int st = dataurb->iso_frame_desc[i].status;
  238. cdata = dataurb->transfer_buffer +
  239. dataurb->iso_frame_desc[i].offset;
  240. /* Detect and ignore errored packets */
  241. if (st < 0) {
  242. DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
  243. i, n, st);
  244. uvd->stats.iso_err_count++;
  245. continue;
  246. }
  247. /* Detect and ignore empty packets */
  248. if (n <= 0) {
  249. uvd->stats.iso_skip_count++;
  250. continue;
  251. }
  252. /* See what the status data said about the packet */
  253. sts = *(status+stsurb->iso_frame_desc[i].offset);
  254. /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
  255. * otherwise:
  256. * bit 0 0: keep packet
  257. * 1: drop packet (padding data)
  258. *
  259. * bit 4 0 button not clicked
  260. * 1 button clicked
  261. * button is used to `take a picture' (in software)
  262. */
  263. if(sts < 0x80) {
  264. button = !!(sts & 0x40);
  265. sts &= ~0x40;
  266. }
  267. /* work out the button status, but don't do
  268. anything with it for now */
  269. if(button != cam->buttonsts) {
  270. DEBUG(2, "button: %sclicked", button ? "" : "un");
  271. cam->buttonsts = button;
  272. konicawc_report_buttonstat(cam);
  273. }
  274. if(sts == 0x01) { /* drop frame */
  275. discard++;
  276. continue;
  277. }
  278. if((sts > 0x01) && (sts < 0x80)) {
  279. dev_info(&uvd->dev->dev, "unknown status %2.2x\n",
  280. sts);
  281. bad++;
  282. continue;
  283. }
  284. if(!sts && cam->lastframe == -2) {
  285. DEBUG(2, "dropping frame looking for image start");
  286. continue;
  287. }
  288. keep++;
  289. if(sts & 0x80) { /* frame start */
  290. unsigned char marker[] = { 0, 0xff, 0, 0x00 };
  291. if(cam->lastframe == -2) {
  292. DEBUG(2, "found initial image");
  293. cam->lastframe = -1;
  294. }
  295. marker[3] = sts & 0x7F;
  296. RingQueue_Enqueue(&uvd->dp, marker, 4);
  297. totlen += 4;
  298. }
  299. totlen += n; /* Little local accounting */
  300. RingQueue_Enqueue(&uvd->dp, cdata, n);
  301. }
  302. DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
  303. keep, discard, bad, totlen);
  304. return totlen;
  305. }
  306. static void resubmit_urb(struct uvd *uvd, struct urb *urb)
  307. {
  308. int i, ret;
  309. for (i = 0; i < FRAMES_PER_DESC; i++) {
  310. urb->iso_frame_desc[i].status = 0;
  311. }
  312. urb->dev = uvd->dev;
  313. urb->status = 0;
  314. ret = usb_submit_urb(urb, GFP_ATOMIC);
  315. DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
  316. if(ret)
  317. err("usb_submit_urb error (%d)", ret);
  318. }
  319. static void konicawc_isoc_irq(struct urb *urb)
  320. {
  321. struct uvd *uvd = urb->context;
  322. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  323. /* We don't want to do anything if we are about to be removed! */
  324. if (!CAMERA_IS_OPERATIONAL(uvd))
  325. return;
  326. if (!uvd->streaming) {
  327. DEBUG(1, "Not streaming, but interrupt!");
  328. return;
  329. }
  330. DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
  331. uvd->stats.urb_count++;
  332. if (urb->transfer_buffer_length > 32) {
  333. cam->last_data_urb = urb;
  334. return;
  335. }
  336. /* Copy the data received into ring queue */
  337. if(cam->last_data_urb) {
  338. int len = 0;
  339. if(urb->start_frame != cam->last_data_urb->start_frame)
  340. err("Lost sync on frames");
  341. else if (!urb->status && !cam->last_data_urb->status)
  342. len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
  343. resubmit_urb(uvd, cam->last_data_urb);
  344. resubmit_urb(uvd, urb);
  345. cam->last_data_urb = NULL;
  346. uvd->stats.urb_length = len;
  347. uvd->stats.data_count += len;
  348. if(len)
  349. RingQueue_WakeUpInterruptible(&uvd->dp);
  350. return;
  351. }
  352. return;
  353. }
  354. static int konicawc_start_data(struct uvd *uvd)
  355. {
  356. struct usb_device *dev = uvd->dev;
  357. int i, errFlag;
  358. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  359. int pktsz;
  360. struct usb_interface *intf;
  361. struct usb_host_interface *interface = NULL;
  362. intf = usb_ifnum_to_if(dev, uvd->iface);
  363. if (intf)
  364. interface = usb_altnum_to_altsetting(intf,
  365. spd_to_iface[cam->speed]);
  366. if (!interface)
  367. return -ENXIO;
  368. pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
  369. DEBUG(1, "pktsz = %d", pktsz);
  370. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  371. err("Camera is not operational");
  372. return -EFAULT;
  373. }
  374. uvd->curframe = -1;
  375. konicawc_camera_on(uvd);
  376. /* Alternate interface 1 is is the biggest frame size */
  377. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  378. if (i < 0) {
  379. err("usb_set_interface error");
  380. uvd->last_error = i;
  381. return -EBUSY;
  382. }
  383. /* We double buffer the Iso lists */
  384. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  385. int j, k;
  386. struct urb *urb = uvd->sbuf[i].urb;
  387. urb->dev = dev;
  388. urb->context = uvd;
  389. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  390. urb->interval = 1;
  391. urb->transfer_flags = URB_ISO_ASAP;
  392. urb->transfer_buffer = uvd->sbuf[i].data;
  393. urb->complete = konicawc_isoc_irq;
  394. urb->number_of_packets = FRAMES_PER_DESC;
  395. urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
  396. for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
  397. urb->iso_frame_desc[j].offset = k;
  398. urb->iso_frame_desc[j].length = pktsz;
  399. }
  400. urb = cam->sts_urb[i];
  401. urb->dev = dev;
  402. urb->context = uvd;
  403. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
  404. urb->interval = 1;
  405. urb->transfer_flags = URB_ISO_ASAP;
  406. urb->transfer_buffer = cam->sts_buf[i];
  407. urb->complete = konicawc_isoc_irq;
  408. urb->number_of_packets = FRAMES_PER_DESC;
  409. urb->transfer_buffer_length = FRAMES_PER_DESC;
  410. for (j=0; j < FRAMES_PER_DESC; j++) {
  411. urb->iso_frame_desc[j].offset = j;
  412. urb->iso_frame_desc[j].length = 1;
  413. }
  414. }
  415. cam->last_data_urb = NULL;
  416. /* Submit all URBs */
  417. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  418. errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
  419. if (errFlag)
  420. err("usb_submit_isoc(%d) ret %d", i, errFlag);
  421. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  422. if (errFlag)
  423. err ("usb_submit_isoc(%d) ret %d", i, errFlag);
  424. }
  425. uvd->streaming = 1;
  426. DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
  427. return 0;
  428. }
  429. static void konicawc_stop_data(struct uvd *uvd)
  430. {
  431. int i, j;
  432. struct konicawc *cam;
  433. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  434. return;
  435. konicawc_camera_off(uvd);
  436. uvd->streaming = 0;
  437. cam = (struct konicawc *)uvd->user_data;
  438. cam->last_data_urb = NULL;
  439. /* Unschedule all of the iso td's */
  440. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  441. usb_kill_urb(uvd->sbuf[i].urb);
  442. usb_kill_urb(cam->sts_urb[i]);
  443. }
  444. if (!uvd->remove_pending) {
  445. /* Set packet size to 0 */
  446. j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
  447. if (j < 0) {
  448. err("usb_set_interface() error %d.", j);
  449. uvd->last_error = j;
  450. }
  451. }
  452. }
  453. static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
  454. {
  455. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  456. int maxline = cam->maxline;
  457. int yplanesz = cam->yplanesz;
  458. assert(frame != NULL);
  459. DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
  460. DEBUG(3, "Frame state = %d", frame->scanstate);
  461. if(frame->scanstate == ScanState_Scanning) {
  462. int drop = 0;
  463. int curframe;
  464. int fdrops = 0;
  465. DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
  466. while(RingQueue_GetLength(&uvd->dp) >= 4) {
  467. if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
  468. (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
  469. (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
  470. (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
  471. curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
  472. if(cam->lastframe >= 0) {
  473. fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
  474. fdrops--;
  475. if(fdrops) {
  476. dev_info(&uvd->dev->dev,
  477. "Dropped %d frames "
  478. "(%d -> %d)\n",
  479. fdrops,
  480. cam->lastframe,
  481. curframe);
  482. }
  483. }
  484. cam->lastframe = curframe;
  485. frame->curline = 0;
  486. frame->scanstate = ScanState_Lines;
  487. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
  488. break;
  489. }
  490. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
  491. drop++;
  492. }
  493. if(drop)
  494. DEBUG(2, "dropped %d bytes looking for new frame", drop);
  495. }
  496. if(frame->scanstate == ScanState_Scanning)
  497. return;
  498. /* Try to move data from queue into frame buffer
  499. * We get data in blocks of 384 bytes made up of:
  500. * 256 Y, 64 U, 64 V.
  501. * This needs to be written out as a Y plane, a U plane and a V plane.
  502. */
  503. while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
  504. /* Y */
  505. RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
  506. /* U */
  507. RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
  508. /* V */
  509. RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
  510. frame->seqRead_Length += 384;
  511. frame->curline++;
  512. }
  513. /* See if we filled the frame */
  514. if (frame->curline == maxline) {
  515. DEBUG(5, "got whole frame");
  516. frame->frameState = FrameState_Done_Hold;
  517. frame->curline = 0;
  518. uvd->curframe = -1;
  519. uvd->stats.frame_num++;
  520. }
  521. }
  522. static int konicawc_find_fps(int size, int fps)
  523. {
  524. int i;
  525. fps *= 3;
  526. DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
  527. if(fps <= spd_to_fps[size][0])
  528. return 0;
  529. if(fps >= spd_to_fps[size][MAX_SPEED])
  530. return MAX_SPEED;
  531. for(i = 0; i < MAX_SPEED; i++) {
  532. if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
  533. DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
  534. if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
  535. return i;
  536. else
  537. return i+1;
  538. }
  539. }
  540. return MAX_SPEED+1;
  541. }
  542. static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
  543. {
  544. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  545. int newspeed = cam->speed;
  546. int newsize;
  547. int x = vw->width;
  548. int y = vw->height;
  549. int fps = vw->flags;
  550. if(x > 0 && y > 0) {
  551. DEBUG(2, "trying to find size %d,%d", x, y);
  552. for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
  553. if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
  554. break;
  555. }
  556. } else {
  557. newsize = cam->size;
  558. }
  559. if(newsize > MAX_FRAME_SIZE) {
  560. DEBUG(1, "couldn't find size %d,%d", x, y);
  561. return -EINVAL;
  562. }
  563. if(fps > 0) {
  564. DEBUG(1, "trying to set fps to %d", fps);
  565. newspeed = konicawc_find_fps(newsize, fps);
  566. DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
  567. }
  568. if(newspeed > MAX_SPEED)
  569. return -EINVAL;
  570. DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
  571. if((newsize == cam->size) && (newspeed == cam->speed)) {
  572. DEBUG(1, "Nothing to do");
  573. return 0;
  574. }
  575. DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
  576. camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
  577. konicawc_stop_data(uvd);
  578. uvd->ifaceAltActive = spd_to_iface[newspeed];
  579. DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
  580. cam->speed = newspeed;
  581. if(cam->size != newsize) {
  582. cam->size = newsize;
  583. konicawc_set_camera_size(uvd);
  584. }
  585. /* Flush the input queue and clear any current frame in progress */
  586. RingQueue_Flush(&uvd->dp);
  587. cam->lastframe = -2;
  588. if(uvd->curframe != -1) {
  589. uvd->frame[uvd->curframe].curline = 0;
  590. uvd->frame[uvd->curframe].seqRead_Length = 0;
  591. uvd->frame[uvd->curframe].seqRead_Index = 0;
  592. }
  593. konicawc_start_data(uvd);
  594. return 0;
  595. }
  596. static int konicawc_calculate_fps(struct uvd *uvd)
  597. {
  598. struct konicawc *cam = uvd->user_data;
  599. return spd_to_fps[cam->size][cam->speed]/3;
  600. }
  601. static void konicawc_configure_video(struct uvd *uvd)
  602. {
  603. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  604. u8 buf[2];
  605. memset(&uvd->vpic, 0, sizeof(uvd->vpic));
  606. memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
  607. RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
  608. RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
  609. RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
  610. RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
  611. RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
  612. cam->brightness = brightness / 11;
  613. cam->contrast = contrast / 11;
  614. cam->saturation = saturation / 11;
  615. cam->sharpness = sharpness / 11;
  616. cam->white_bal = whitebal / 11;
  617. uvd->vpic.colour = 108;
  618. uvd->vpic.hue = 108;
  619. uvd->vpic.brightness = brightness;
  620. uvd->vpic.contrast = contrast;
  621. uvd->vpic.whiteness = whitebal;
  622. uvd->vpic.depth = 6;
  623. uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
  624. memset(&uvd->vcap, 0, sizeof(uvd->vcap));
  625. strcpy(uvd->vcap.name, "Konica Webcam");
  626. uvd->vcap.type = VID_TYPE_CAPTURE;
  627. uvd->vcap.channels = 1;
  628. uvd->vcap.audios = 0;
  629. uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
  630. uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
  631. uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
  632. uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
  633. memset(&uvd->vchan, 0, sizeof(uvd->vchan));
  634. uvd->vchan.flags = 0 ;
  635. uvd->vchan.tuners = 0;
  636. uvd->vchan.channel = 0;
  637. uvd->vchan.type = VIDEO_TYPE_CAMERA;
  638. strcpy(uvd->vchan.name, "Camera");
  639. /* Talk to device */
  640. DEBUG(1, "device init");
  641. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  642. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  643. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  644. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  645. if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
  646. DEBUG(2, "2,0,d failed");
  647. DEBUG(1, "setting initial values");
  648. }
  649. static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  650. {
  651. struct usb_device *dev = interface_to_usbdev(intf);
  652. struct uvd *uvd = NULL;
  653. int ix, i, nas;
  654. int actInterface=-1, inactInterface=-1, maxPS=0;
  655. unsigned char video_ep = 0;
  656. DEBUG(1, "konicawc_probe(%p)", intf);
  657. /* We don't handle multi-config cameras */
  658. if (dev->descriptor.bNumConfigurations != 1)
  659. return -ENODEV;
  660. dev_info(&intf->dev, "Konica Webcam (rev. 0x%04x)\n",
  661. le16_to_cpu(dev->descriptor.bcdDevice));
  662. RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
  663. /* Validate found interface: must have one ISO endpoint */
  664. nas = intf->num_altsetting;
  665. if (nas != 8) {
  666. err("Incorrect number of alternate settings (%d) for this camera!", nas);
  667. return -ENODEV;
  668. }
  669. /* Validate all alternate settings */
  670. for (ix=0; ix < nas; ix++) {
  671. const struct usb_host_interface *interface;
  672. const struct usb_endpoint_descriptor *endpoint;
  673. interface = &intf->altsetting[ix];
  674. i = interface->desc.bAlternateSetting;
  675. if (interface->desc.bNumEndpoints != 2) {
  676. err("Interface %d. has %u. endpoints!",
  677. interface->desc.bInterfaceNumber,
  678. (unsigned)(interface->desc.bNumEndpoints));
  679. return -ENODEV;
  680. }
  681. endpoint = &interface->endpoint[1].desc;
  682. DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
  683. endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
  684. if (video_ep == 0)
  685. video_ep = endpoint->bEndpointAddress;
  686. else if (video_ep != endpoint->bEndpointAddress) {
  687. err("Alternate settings have different endpoint addresses!");
  688. return -ENODEV;
  689. }
  690. if (!usb_endpoint_xfer_isoc(endpoint)) {
  691. err("Interface %d. has non-ISO endpoint!",
  692. interface->desc.bInterfaceNumber);
  693. return -ENODEV;
  694. }
  695. if (usb_endpoint_dir_out(endpoint)) {
  696. err("Interface %d. has ISO OUT endpoint!",
  697. interface->desc.bInterfaceNumber);
  698. return -ENODEV;
  699. }
  700. if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
  701. if (inactInterface < 0)
  702. inactInterface = i;
  703. else {
  704. err("More than one inactive alt. setting!");
  705. return -ENODEV;
  706. }
  707. } else {
  708. if (i == spd_to_iface[speed]) {
  709. /* This one is the requested one */
  710. actInterface = i;
  711. }
  712. }
  713. if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
  714. maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
  715. }
  716. if(actInterface == -1) {
  717. err("Cant find required endpoint");
  718. return -ENODEV;
  719. }
  720. DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
  721. uvd = usbvideo_AllocateDevice(cams);
  722. if (uvd != NULL) {
  723. struct konicawc *cam = (struct konicawc *)(uvd->user_data);
  724. /* Here uvd is a fully allocated uvd object */
  725. for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
  726. cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  727. if(cam->sts_urb[i] == NULL) {
  728. while(i--) {
  729. usb_free_urb(cam->sts_urb[i]);
  730. }
  731. err("can't allocate urbs");
  732. return -ENOMEM;
  733. }
  734. }
  735. cam->speed = speed;
  736. RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
  737. cam->width = camera_sizes[size].width;
  738. cam->height = camera_sizes[size].height;
  739. cam->size = size;
  740. uvd->flags = 0;
  741. uvd->debug = debug;
  742. uvd->dev = dev;
  743. uvd->iface = intf->altsetting->desc.bInterfaceNumber;
  744. uvd->ifaceAltInactive = inactInterface;
  745. uvd->ifaceAltActive = actInterface;
  746. uvd->video_endp = video_ep;
  747. uvd->iso_packet_len = maxPS;
  748. uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
  749. uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
  750. uvd->canvas = VIDEOSIZE(320, 240);
  751. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  752. /* Initialize konicawc specific data */
  753. konicawc_configure_video(uvd);
  754. i = usbvideo_RegisterVideoDevice(uvd);
  755. uvd->max_frame_size = (320 * 240 * 3)/2;
  756. if (i != 0) {
  757. err("usbvideo_RegisterVideoDevice() failed.");
  758. uvd = NULL;
  759. }
  760. konicawc_register_input(cam, dev);
  761. }
  762. if (uvd) {
  763. usb_set_intfdata (intf, uvd);
  764. return 0;
  765. }
  766. return -EIO;
  767. }
  768. static void konicawc_free_uvd(struct uvd *uvd)
  769. {
  770. int i;
  771. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  772. konicawc_unregister_input(cam);
  773. for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
  774. usb_free_urb(cam->sts_urb[i]);
  775. cam->sts_urb[i] = NULL;
  776. }
  777. }
  778. static struct usb_device_id id_table[] = {
  779. { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
  780. { } /* Terminating entry */
  781. };
  782. static int __init konicawc_init(void)
  783. {
  784. struct usbvideo_cb cbTbl;
  785. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  786. DRIVER_DESC "\n");
  787. memset(&cbTbl, 0, sizeof(cbTbl));
  788. cbTbl.probe = konicawc_probe;
  789. cbTbl.setupOnOpen = konicawc_setup_on_open;
  790. cbTbl.processData = konicawc_process_isoc;
  791. cbTbl.getFPS = konicawc_calculate_fps;
  792. cbTbl.setVideoMode = konicawc_set_video_mode;
  793. cbTbl.startDataPump = konicawc_start_data;
  794. cbTbl.stopDataPump = konicawc_stop_data;
  795. cbTbl.adjustPicture = konicawc_adjust_picture;
  796. cbTbl.userFree = konicawc_free_uvd;
  797. return usbvideo_register(
  798. &cams,
  799. MAX_CAMERAS,
  800. sizeof(struct konicawc),
  801. "konicawc",
  802. &cbTbl,
  803. THIS_MODULE,
  804. id_table);
  805. }
  806. static void __exit konicawc_cleanup(void)
  807. {
  808. usbvideo_Deregister(&cams);
  809. }
  810. MODULE_DEVICE_TABLE(usb, id_table);
  811. MODULE_LICENSE("GPL");
  812. MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
  813. MODULE_DESCRIPTION(DRIVER_DESC);
  814. module_param(speed, int, 0);
  815. MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
  816. module_param(size, int, 0);
  817. MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
  818. module_param(brightness, int, 0);
  819. MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
  820. module_param(contrast, int, 0);
  821. MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
  822. module_param(saturation, int, 0);
  823. MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
  824. module_param(sharpness, int, 0);
  825. MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
  826. module_param(whitebal, int, 0);
  827. MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
  828. #ifdef CONFIG_USB_DEBUG
  829. module_param(debug, int, S_IRUGO | S_IWUSR);
  830. MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
  831. #endif
  832. module_init(konicawc_init);
  833. module_exit(konicawc_cleanup);