konicawc.c 26 KB

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