konicawc.c 26 KB

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