mdc800.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * USB-Kernel Driver for the Mustek MDC800 Digital Camera
  20. * (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
  21. *
  22. *
  23. * The driver brings the USB functions of the MDC800 to Linux.
  24. * To use the Camera you must support the USB Protocol of the camera
  25. * to the Kernel Node.
  26. * The Driver uses a misc device Node. Create it with :
  27. * mknod /dev/mustek c 180 32
  28. *
  29. * The driver supports only one camera.
  30. *
  31. * Fix: mdc800 used sleep_on and slept with io_lock held.
  32. * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
  33. * a semaphore from a spinlock.
  34. * by Oliver Neukum <oliver@neukum.name>
  35. * (02/12/2001)
  36. *
  37. * Identify version on module load.
  38. * (08/04/2001) gb
  39. *
  40. * version 0.7.5
  41. * Fixed potential SMP races with Spinlocks.
  42. * Thanks to Oliver Neukum <oliver@neukum.name> who
  43. * noticed the race conditions.
  44. * (30/10/2000)
  45. *
  46. * Fixed: Setting urb->dev before submitting urb.
  47. * by Greg KH <greg@kroah.com>
  48. * (13/10/2000)
  49. *
  50. * version 0.7.3
  51. * bugfix : The mdc800->state field gets set to READY after the
  52. * the diconnect function sets it to NOT_CONNECTED. This makes the
  53. * driver running like the camera is connected and causes some
  54. * hang ups.
  55. *
  56. * version 0.7.1
  57. * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
  58. * problems when compiled as Module.
  59. * (04/04/2000)
  60. *
  61. * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
  62. * was updated to use these values.
  63. * (26/03/2000)
  64. *
  65. * The Init und Exit Module Function are updated.
  66. * (01/03/2000)
  67. *
  68. * version 0.7.0
  69. * Rewrite of the driver : The driver now uses URB's. The old stuff
  70. * has been removed.
  71. *
  72. * version 0.6.0
  73. * Rewrite of this driver: The Emulation of the rs232 protocoll
  74. * has been removed from the driver. A special executeCommand function
  75. * for this driver is included to gphoto.
  76. * The driver supports two kind of communication to bulk endpoints.
  77. * Either with the dev->bus->ops->bulk... or with callback function.
  78. * (09/11/1999)
  79. *
  80. * version 0.5.0:
  81. * first Version that gets a version number. Most of the needed
  82. * functions work.
  83. * (20/10/1999)
  84. */
  85. #include <linux/sched.h>
  86. #include <linux/signal.h>
  87. #include <linux/spinlock.h>
  88. #include <linux/errno.h>
  89. #include <linux/random.h>
  90. #include <linux/poll.h>
  91. #include <linux/init.h>
  92. #include <linux/slab.h>
  93. #include <linux/module.h>
  94. #include <linux/wait.h>
  95. #include <linux/mutex.h>
  96. #include <linux/usb.h>
  97. #include <linux/fs.h>
  98. /*
  99. * Version Information
  100. */
  101. #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
  102. #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
  103. #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
  104. /* Vendor and Product Information */
  105. #define MDC800_VENDOR_ID 0x055f
  106. #define MDC800_PRODUCT_ID 0xa800
  107. /* Timeouts (msec) */
  108. #define TO_DOWNLOAD_GET_READY 1500
  109. #define TO_DOWNLOAD_GET_BUSY 1500
  110. #define TO_WRITE_GET_READY 1000
  111. #define TO_DEFAULT_COMMAND 5000
  112. #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
  113. #define TO_GET_READY TO_DEFAULT_COMMAND
  114. /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
  115. #define MDC800_DEVICE_MINOR_BASE 32
  116. /**************************************************************************
  117. Data and structs
  118. ***************************************************************************/
  119. typedef enum {
  120. NOT_CONNECTED, READY, WORKING, DOWNLOAD
  121. } mdc800_state;
  122. /* Data for the driver */
  123. struct mdc800_data
  124. {
  125. struct usb_device * dev; // Device Data
  126. mdc800_state state;
  127. unsigned int endpoint [4];
  128. struct urb * irq_urb;
  129. wait_queue_head_t irq_wait;
  130. int irq_woken;
  131. char* irq_urb_buffer;
  132. int camera_busy; // is camera busy ?
  133. int camera_request_ready; // Status to synchronize with irq
  134. char camera_response [8]; // last Bytes send after busy
  135. struct urb * write_urb;
  136. char* write_urb_buffer;
  137. wait_queue_head_t write_wait;
  138. int written;
  139. struct urb * download_urb;
  140. char* download_urb_buffer;
  141. wait_queue_head_t download_wait;
  142. int downloaded;
  143. int download_left; // Bytes left to download ?
  144. /* Device Data */
  145. char out [64]; // Answer Buffer
  146. int out_ptr; // Index to the first not readen byte
  147. int out_count; // Bytes in the buffer
  148. int open; // Camera device open ?
  149. struct mutex io_lock; // IO -lock
  150. char in [8]; // Command Input Buffer
  151. int in_count;
  152. int pic_index; // Cache for the Imagesize (-1 for nothing cached )
  153. int pic_len;
  154. int minor;
  155. };
  156. /* Specification of the Endpoints */
  157. static struct usb_endpoint_descriptor mdc800_ed [4] =
  158. {
  159. {
  160. .bLength = 0,
  161. .bDescriptorType = 0,
  162. .bEndpointAddress = 0x01,
  163. .bmAttributes = 0x02,
  164. .wMaxPacketSize = __constant_cpu_to_le16(8),
  165. .bInterval = 0,
  166. .bRefresh = 0,
  167. .bSynchAddress = 0,
  168. },
  169. {
  170. .bLength = 0,
  171. .bDescriptorType = 0,
  172. .bEndpointAddress = 0x82,
  173. .bmAttributes = 0x03,
  174. .wMaxPacketSize = __constant_cpu_to_le16(8),
  175. .bInterval = 0,
  176. .bRefresh = 0,
  177. .bSynchAddress = 0,
  178. },
  179. {
  180. .bLength = 0,
  181. .bDescriptorType = 0,
  182. .bEndpointAddress = 0x03,
  183. .bmAttributes = 0x02,
  184. .wMaxPacketSize = __constant_cpu_to_le16(64),
  185. .bInterval = 0,
  186. .bRefresh = 0,
  187. .bSynchAddress = 0,
  188. },
  189. {
  190. .bLength = 0,
  191. .bDescriptorType = 0,
  192. .bEndpointAddress = 0x84,
  193. .bmAttributes = 0x02,
  194. .wMaxPacketSize = __constant_cpu_to_le16(64),
  195. .bInterval = 0,
  196. .bRefresh = 0,
  197. .bSynchAddress = 0,
  198. },
  199. };
  200. /* The Variable used by the driver */
  201. static struct mdc800_data* mdc800;
  202. /***************************************************************************
  203. The USB Part of the driver
  204. ****************************************************************************/
  205. static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
  206. {
  207. return (
  208. ( a->bEndpointAddress == b->bEndpointAddress )
  209. && ( a->bmAttributes == b->bmAttributes )
  210. && ( a->wMaxPacketSize == b->wMaxPacketSize )
  211. );
  212. }
  213. /*
  214. * Checks whether the camera responds busy
  215. */
  216. static int mdc800_isBusy (char* ch)
  217. {
  218. int i=0;
  219. while (i<8)
  220. {
  221. if (ch [i] != (char)0x99)
  222. return 0;
  223. i++;
  224. }
  225. return 1;
  226. }
  227. /*
  228. * Checks whether the Camera is ready
  229. */
  230. static int mdc800_isReady (char *ch)
  231. {
  232. int i=0;
  233. while (i<8)
  234. {
  235. if (ch [i] != (char)0xbb)
  236. return 0;
  237. i++;
  238. }
  239. return 1;
  240. }
  241. /*
  242. * USB IRQ Handler for InputLine
  243. */
  244. static void mdc800_usb_irq (struct urb *urb)
  245. {
  246. int data_received=0, wake_up;
  247. unsigned char* b=urb->transfer_buffer;
  248. struct mdc800_data* mdc800=urb->context;
  249. if (urb->status >= 0)
  250. {
  251. //dbg ("%i %i %i %i %i %i %i %i \n",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
  252. if (mdc800_isBusy (b))
  253. {
  254. if (!mdc800->camera_busy)
  255. {
  256. mdc800->camera_busy=1;
  257. dbg ("gets busy");
  258. }
  259. }
  260. else
  261. {
  262. if (mdc800->camera_busy && mdc800_isReady (b))
  263. {
  264. mdc800->camera_busy=0;
  265. dbg ("gets ready");
  266. }
  267. }
  268. if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
  269. {
  270. /* Store Data in camera_answer field */
  271. dbg ("%i %i %i %i %i %i %i %i ",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
  272. memcpy (mdc800->camera_response,b,8);
  273. data_received=1;
  274. }
  275. }
  276. wake_up= ( mdc800->camera_request_ready > 0 )
  277. &&
  278. (
  279. ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
  280. ||
  281. ((mdc800->camera_request_ready == 2) && data_received)
  282. ||
  283. ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
  284. ||
  285. (urb->status < 0)
  286. );
  287. if (wake_up)
  288. {
  289. mdc800->camera_request_ready=0;
  290. mdc800->irq_woken=1;
  291. wake_up (&mdc800->irq_wait);
  292. }
  293. }
  294. /*
  295. * Waits a while until the irq responds that camera is ready
  296. *
  297. * mode : 0: Wait for camera gets ready
  298. * 1: Wait for receiving data
  299. * 2: Wait for camera gets busy
  300. *
  301. * msec: Time to wait
  302. */
  303. static int mdc800_usb_waitForIRQ (int mode, int msec)
  304. {
  305. mdc800->camera_request_ready=1+mode;
  306. wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
  307. mdc800->irq_woken = 0;
  308. if (mdc800->camera_request_ready>0)
  309. {
  310. mdc800->camera_request_ready=0;
  311. err ("timeout waiting for camera.");
  312. return -1;
  313. }
  314. if (mdc800->state == NOT_CONNECTED)
  315. {
  316. warn ("Camera gets disconnected during waiting for irq.");
  317. mdc800->camera_request_ready=0;
  318. return -2;
  319. }
  320. return 0;
  321. }
  322. /*
  323. * The write_urb callback function
  324. */
  325. static void mdc800_usb_write_notify (struct urb *urb)
  326. {
  327. struct mdc800_data* mdc800=urb->context;
  328. if (urb->status != 0)
  329. {
  330. err ("writing command fails (status=%i)", urb->status);
  331. }
  332. else
  333. {
  334. mdc800->state=READY;
  335. }
  336. mdc800->written = 1;
  337. wake_up (&mdc800->write_wait);
  338. }
  339. /*
  340. * The download_urb callback function
  341. */
  342. static void mdc800_usb_download_notify (struct urb *urb)
  343. {
  344. struct mdc800_data* mdc800=urb->context;
  345. if (urb->status == 0)
  346. {
  347. /* Fill output buffer with these data */
  348. memcpy (mdc800->out, urb->transfer_buffer, 64);
  349. mdc800->out_count=64;
  350. mdc800->out_ptr=0;
  351. mdc800->download_left-=64;
  352. if (mdc800->download_left == 0)
  353. {
  354. mdc800->state=READY;
  355. }
  356. }
  357. else
  358. {
  359. err ("request bytes fails (status:%i)", urb->status);
  360. }
  361. mdc800->downloaded = 1;
  362. wake_up (&mdc800->download_wait);
  363. }
  364. /***************************************************************************
  365. Probing for the Camera
  366. ***************************************************************************/
  367. static struct usb_driver mdc800_usb_driver;
  368. static const struct file_operations mdc800_device_ops;
  369. static struct usb_class_driver mdc800_class = {
  370. .name = "mdc800%d",
  371. .fops = &mdc800_device_ops,
  372. .minor_base = MDC800_DEVICE_MINOR_BASE,
  373. };
  374. /*
  375. * Callback to search the Mustek MDC800 on the USB Bus
  376. */
  377. static int mdc800_usb_probe (struct usb_interface *intf,
  378. const struct usb_device_id *id)
  379. {
  380. int i,j;
  381. struct usb_host_interface *intf_desc;
  382. struct usb_device *dev = interface_to_usbdev (intf);
  383. int irq_interval=0;
  384. int retval;
  385. dbg ("(mdc800_usb_probe) called.");
  386. if (mdc800->dev != NULL)
  387. {
  388. warn ("only one Mustek MDC800 is supported.");
  389. return -ENODEV;
  390. }
  391. if (dev->descriptor.bNumConfigurations != 1)
  392. {
  393. err ("probe fails -> wrong Number of Configuration");
  394. return -ENODEV;
  395. }
  396. intf_desc = intf->cur_altsetting;
  397. if (
  398. ( intf_desc->desc.bInterfaceClass != 0xff )
  399. || ( intf_desc->desc.bInterfaceSubClass != 0 )
  400. || ( intf_desc->desc.bInterfaceProtocol != 0 )
  401. || ( intf_desc->desc.bNumEndpoints != 4)
  402. )
  403. {
  404. err ("probe fails -> wrong Interface");
  405. return -ENODEV;
  406. }
  407. /* Check the Endpoints */
  408. for (i=0; i<4; i++)
  409. {
  410. mdc800->endpoint[i]=-1;
  411. for (j=0; j<4; j++)
  412. {
  413. if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
  414. {
  415. mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
  416. if (i==1)
  417. {
  418. irq_interval=intf_desc->endpoint [j].desc.bInterval;
  419. }
  420. continue;
  421. }
  422. }
  423. if (mdc800->endpoint[i] == -1)
  424. {
  425. err ("probe fails -> Wrong Endpoints.");
  426. return -ENODEV;
  427. }
  428. }
  429. info ("Found Mustek MDC800 on USB.");
  430. mutex_lock(&mdc800->io_lock);
  431. retval = usb_register_dev(intf, &mdc800_class);
  432. if (retval) {
  433. err ("Not able to get a minor for this device.");
  434. return -ENODEV;
  435. }
  436. mdc800->dev=dev;
  437. mdc800->open=0;
  438. /* Setup URB Structs */
  439. usb_fill_int_urb (
  440. mdc800->irq_urb,
  441. mdc800->dev,
  442. usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
  443. mdc800->irq_urb_buffer,
  444. 8,
  445. mdc800_usb_irq,
  446. mdc800,
  447. irq_interval
  448. );
  449. usb_fill_bulk_urb (
  450. mdc800->write_urb,
  451. mdc800->dev,
  452. usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
  453. mdc800->write_urb_buffer,
  454. 8,
  455. mdc800_usb_write_notify,
  456. mdc800
  457. );
  458. usb_fill_bulk_urb (
  459. mdc800->download_urb,
  460. mdc800->dev,
  461. usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
  462. mdc800->download_urb_buffer,
  463. 64,
  464. mdc800_usb_download_notify,
  465. mdc800
  466. );
  467. mdc800->state=READY;
  468. mutex_unlock(&mdc800->io_lock);
  469. usb_set_intfdata(intf, mdc800);
  470. return 0;
  471. }
  472. /*
  473. * Disconnect USB device (maybe the MDC800)
  474. */
  475. static void mdc800_usb_disconnect (struct usb_interface *intf)
  476. {
  477. struct mdc800_data* mdc800 = usb_get_intfdata(intf);
  478. dbg ("(mdc800_usb_disconnect) called");
  479. if (mdc800) {
  480. if (mdc800->state == NOT_CONNECTED)
  481. return;
  482. usb_deregister_dev(intf, &mdc800_class);
  483. /* must be under lock to make sure no URB
  484. is submitted after usb_kill_urb() */
  485. mutex_lock(&mdc800->io_lock);
  486. mdc800->state=NOT_CONNECTED;
  487. usb_kill_urb(mdc800->irq_urb);
  488. usb_kill_urb(mdc800->write_urb);
  489. usb_kill_urb(mdc800->download_urb);
  490. mutex_unlock(&mdc800->io_lock);
  491. mdc800->dev = NULL;
  492. usb_set_intfdata(intf, NULL);
  493. }
  494. info ("Mustek MDC800 disconnected from USB.");
  495. }
  496. /***************************************************************************
  497. The Misc device Part (file_operations)
  498. ****************************************************************************/
  499. /*
  500. * This Function calc the Answersize for a command.
  501. */
  502. static int mdc800_getAnswerSize (char command)
  503. {
  504. switch ((unsigned char) command)
  505. {
  506. case 0x2a:
  507. case 0x49:
  508. case 0x51:
  509. case 0x0d:
  510. case 0x20:
  511. case 0x07:
  512. case 0x01:
  513. case 0x25:
  514. case 0x00:
  515. return 8;
  516. case 0x05:
  517. case 0x3e:
  518. return mdc800->pic_len;
  519. case 0x09:
  520. return 4096;
  521. default:
  522. return 0;
  523. }
  524. }
  525. /*
  526. * Init the device: (1) alloc mem (2) Increase MOD Count ..
  527. */
  528. static int mdc800_device_open (struct inode* inode, struct file *file)
  529. {
  530. int retval=0;
  531. int errn=0;
  532. mutex_lock(&mdc800->io_lock);
  533. if (mdc800->state == NOT_CONNECTED)
  534. {
  535. errn=-EBUSY;
  536. goto error_out;
  537. }
  538. if (mdc800->open)
  539. {
  540. errn=-EBUSY;
  541. goto error_out;
  542. }
  543. mdc800->in_count=0;
  544. mdc800->out_count=0;
  545. mdc800->out_ptr=0;
  546. mdc800->pic_index=0;
  547. mdc800->pic_len=-1;
  548. mdc800->download_left=0;
  549. mdc800->camera_busy=0;
  550. mdc800->camera_request_ready=0;
  551. retval=0;
  552. mdc800->irq_urb->dev = mdc800->dev;
  553. if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
  554. {
  555. err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
  556. errn = -EIO;
  557. goto error_out;
  558. }
  559. mdc800->open=1;
  560. dbg ("Mustek MDC800 device opened.");
  561. error_out:
  562. mutex_unlock(&mdc800->io_lock);
  563. return errn;
  564. }
  565. /*
  566. * Close the Camera and release Memory
  567. */
  568. static int mdc800_device_release (struct inode* inode, struct file *file)
  569. {
  570. int retval=0;
  571. dbg ("Mustek MDC800 device closed.");
  572. mutex_lock(&mdc800->io_lock);
  573. if (mdc800->open && (mdc800->state != NOT_CONNECTED))
  574. {
  575. usb_kill_urb(mdc800->irq_urb);
  576. usb_kill_urb(mdc800->write_urb);
  577. usb_kill_urb(mdc800->download_urb);
  578. mdc800->open=0;
  579. }
  580. else
  581. {
  582. retval=-EIO;
  583. }
  584. mutex_unlock(&mdc800->io_lock);
  585. return retval;
  586. }
  587. /*
  588. * The Device read callback Function
  589. */
  590. static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
  591. {
  592. size_t left=len, sts=len; /* single transfer size */
  593. char __user *ptr = buf;
  594. mutex_lock(&mdc800->io_lock);
  595. if (mdc800->state == NOT_CONNECTED)
  596. {
  597. mutex_unlock(&mdc800->io_lock);
  598. return -EBUSY;
  599. }
  600. if (mdc800->state == WORKING)
  601. {
  602. warn ("Illegal State \"working\" reached during read ?!");
  603. mutex_unlock(&mdc800->io_lock);
  604. return -EBUSY;
  605. }
  606. if (!mdc800->open)
  607. {
  608. mutex_unlock(&mdc800->io_lock);
  609. return -EBUSY;
  610. }
  611. while (left)
  612. {
  613. if (signal_pending (current))
  614. {
  615. mutex_unlock(&mdc800->io_lock);
  616. return -EINTR;
  617. }
  618. sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
  619. if (sts <= 0)
  620. {
  621. /* Too less Data in buffer */
  622. if (mdc800->state == DOWNLOAD)
  623. {
  624. mdc800->out_count=0;
  625. mdc800->out_ptr=0;
  626. /* Download -> Request new bytes */
  627. mdc800->download_urb->dev = mdc800->dev;
  628. if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
  629. {
  630. err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
  631. mutex_unlock(&mdc800->io_lock);
  632. return len-left;
  633. }
  634. wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
  635. TO_DOWNLOAD_GET_READY*HZ/1000);
  636. mdc800->downloaded = 0;
  637. if (mdc800->download_urb->status != 0)
  638. {
  639. err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
  640. mutex_unlock(&mdc800->io_lock);
  641. return len-left;
  642. }
  643. }
  644. else
  645. {
  646. /* No more bytes -> that's an error*/
  647. mutex_unlock(&mdc800->io_lock);
  648. return -EIO;
  649. }
  650. }
  651. else
  652. {
  653. /* Copy Bytes */
  654. if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
  655. sts)) {
  656. mutex_unlock(&mdc800->io_lock);
  657. return -EFAULT;
  658. }
  659. ptr+=sts;
  660. left-=sts;
  661. mdc800->out_ptr+=sts;
  662. }
  663. }
  664. mutex_unlock(&mdc800->io_lock);
  665. return len-left;
  666. }
  667. /*
  668. * The Device write callback Function
  669. * If a 8Byte Command is received, it will be send to the camera.
  670. * After this the driver initiates the request for the answer or
  671. * just waits until the camera becomes ready.
  672. */
  673. static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
  674. {
  675. size_t i=0;
  676. mutex_lock(&mdc800->io_lock);
  677. if (mdc800->state != READY)
  678. {
  679. mutex_unlock(&mdc800->io_lock);
  680. return -EBUSY;
  681. }
  682. if (!mdc800->open )
  683. {
  684. mutex_unlock(&mdc800->io_lock);
  685. return -EBUSY;
  686. }
  687. while (i<len)
  688. {
  689. unsigned char c;
  690. if (signal_pending (current))
  691. {
  692. mutex_unlock(&mdc800->io_lock);
  693. return -EINTR;
  694. }
  695. if(get_user(c, buf+i))
  696. {
  697. mutex_unlock(&mdc800->io_lock);
  698. return -EFAULT;
  699. }
  700. /* check for command start */
  701. if (c == 0x55)
  702. {
  703. mdc800->in_count=0;
  704. mdc800->out_count=0;
  705. mdc800->out_ptr=0;
  706. mdc800->download_left=0;
  707. }
  708. /* save command byte */
  709. if (mdc800->in_count < 8)
  710. {
  711. mdc800->in[mdc800->in_count] = c;
  712. mdc800->in_count++;
  713. }
  714. else
  715. {
  716. mutex_unlock(&mdc800->io_lock);
  717. return -EIO;
  718. }
  719. /* Command Buffer full ? -> send it to camera */
  720. if (mdc800->in_count == 8)
  721. {
  722. int answersize;
  723. if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
  724. {
  725. err ("Camera didn't get ready.\n");
  726. mutex_unlock(&mdc800->io_lock);
  727. return -EIO;
  728. }
  729. answersize=mdc800_getAnswerSize (mdc800->in[1]);
  730. mdc800->state=WORKING;
  731. memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
  732. mdc800->write_urb->dev = mdc800->dev;
  733. if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
  734. {
  735. err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
  736. mutex_unlock(&mdc800->io_lock);
  737. return -EIO;
  738. }
  739. wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
  740. mdc800->written = 0;
  741. if (mdc800->state == WORKING)
  742. {
  743. usb_kill_urb(mdc800->write_urb);
  744. mutex_unlock(&mdc800->io_lock);
  745. return -EIO;
  746. }
  747. switch ((unsigned char) mdc800->in[1])
  748. {
  749. case 0x05: /* Download Image */
  750. case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
  751. if (mdc800->pic_len < 0)
  752. {
  753. err ("call 0x07 before 0x05,0x3e");
  754. mdc800->state=READY;
  755. mutex_unlock(&mdc800->io_lock);
  756. return -EIO;
  757. }
  758. mdc800->pic_len=-1;
  759. case 0x09: /* Download Thumbnail */
  760. mdc800->download_left=answersize+64;
  761. mdc800->state=DOWNLOAD;
  762. mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
  763. break;
  764. default:
  765. if (answersize)
  766. {
  767. if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
  768. {
  769. err ("requesting answer from irq fails");
  770. mutex_unlock(&mdc800->io_lock);
  771. return -EIO;
  772. }
  773. /* Write dummy data, (this is ugly but part of the USB Protocol */
  774. /* if you use endpoint 1 as bulk and not as irq) */
  775. memcpy (mdc800->out, mdc800->camera_response,8);
  776. /* This is the interpreted answer */
  777. memcpy (&mdc800->out[8], mdc800->camera_response,8);
  778. mdc800->out_ptr=0;
  779. mdc800->out_count=16;
  780. /* Cache the Imagesize, if command was getImageSize */
  781. if (mdc800->in [1] == (char) 0x07)
  782. {
  783. mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
  784. dbg ("cached imagesize = %i",mdc800->pic_len);
  785. }
  786. }
  787. else
  788. {
  789. if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
  790. {
  791. err ("Command Timeout.");
  792. mutex_unlock(&mdc800->io_lock);
  793. return -EIO;
  794. }
  795. }
  796. mdc800->state=READY;
  797. break;
  798. }
  799. }
  800. i++;
  801. }
  802. mutex_unlock(&mdc800->io_lock);
  803. return i;
  804. }
  805. /***************************************************************************
  806. Init and Cleanup this driver (Structs and types)
  807. ****************************************************************************/
  808. /* File Operations of this drivers */
  809. static const struct file_operations mdc800_device_ops =
  810. {
  811. .owner = THIS_MODULE,
  812. .read = mdc800_device_read,
  813. .write = mdc800_device_write,
  814. .open = mdc800_device_open,
  815. .release = mdc800_device_release,
  816. };
  817. static struct usb_device_id mdc800_table [] = {
  818. { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
  819. { } /* Terminating entry */
  820. };
  821. MODULE_DEVICE_TABLE (usb, mdc800_table);
  822. /*
  823. * USB Driver Struct for this device
  824. */
  825. static struct usb_driver mdc800_usb_driver =
  826. {
  827. .name = "mdc800",
  828. .probe = mdc800_usb_probe,
  829. .disconnect = mdc800_usb_disconnect,
  830. .id_table = mdc800_table
  831. };
  832. /************************************************************************
  833. Init and Cleanup this driver (Main Functions)
  834. *************************************************************************/
  835. static int __init usb_mdc800_init (void)
  836. {
  837. int retval = -ENODEV;
  838. /* Allocate Memory */
  839. mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
  840. if (!mdc800)
  841. goto cleanup_on_fail;
  842. mdc800->dev = NULL;
  843. mdc800->state=NOT_CONNECTED;
  844. mutex_init (&mdc800->io_lock);
  845. init_waitqueue_head (&mdc800->irq_wait);
  846. init_waitqueue_head (&mdc800->write_wait);
  847. init_waitqueue_head (&mdc800->download_wait);
  848. mdc800->irq_woken = 0;
  849. mdc800->downloaded = 0;
  850. mdc800->written = 0;
  851. mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
  852. if (!mdc800->irq_urb_buffer)
  853. goto cleanup_on_fail;
  854. mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
  855. if (!mdc800->write_urb_buffer)
  856. goto cleanup_on_fail;
  857. mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
  858. if (!mdc800->download_urb_buffer)
  859. goto cleanup_on_fail;
  860. mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
  861. if (!mdc800->irq_urb)
  862. goto cleanup_on_fail;
  863. mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
  864. if (!mdc800->download_urb)
  865. goto cleanup_on_fail;
  866. mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
  867. if (!mdc800->write_urb)
  868. goto cleanup_on_fail;
  869. /* Register the driver */
  870. retval = usb_register(&mdc800_usb_driver);
  871. if (retval)
  872. goto cleanup_on_fail;
  873. info (DRIVER_VERSION ":" DRIVER_DESC);
  874. return 0;
  875. /* Clean driver up, when something fails */
  876. cleanup_on_fail:
  877. if (mdc800 != NULL)
  878. {
  879. err ("can't alloc memory!");
  880. kfree(mdc800->download_urb_buffer);
  881. kfree(mdc800->write_urb_buffer);
  882. kfree(mdc800->irq_urb_buffer);
  883. usb_free_urb(mdc800->write_urb);
  884. usb_free_urb(mdc800->download_urb);
  885. usb_free_urb(mdc800->irq_urb);
  886. kfree (mdc800);
  887. }
  888. mdc800 = NULL;
  889. return retval;
  890. }
  891. static void __exit usb_mdc800_cleanup (void)
  892. {
  893. usb_deregister (&mdc800_usb_driver);
  894. usb_free_urb (mdc800->irq_urb);
  895. usb_free_urb (mdc800->download_urb);
  896. usb_free_urb (mdc800->write_urb);
  897. kfree (mdc800->irq_urb_buffer);
  898. kfree (mdc800->write_urb_buffer);
  899. kfree (mdc800->download_urb_buffer);
  900. kfree (mdc800);
  901. mdc800 = NULL;
  902. }
  903. module_init (usb_mdc800_init);
  904. module_exit (usb_mdc800_cleanup);
  905. MODULE_AUTHOR( DRIVER_AUTHOR );
  906. MODULE_DESCRIPTION( DRIVER_DESC );
  907. MODULE_LICENSE("GPL");