mdc800.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 = 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 = 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 = 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 = 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. int status = urb->status;
  250. if (status >= 0) {
  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. (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. dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
  312. return -1;
  313. }
  314. if (mdc800->state == NOT_CONNECTED)
  315. {
  316. printk(KERN_WARNING "mdc800: Camera gets disconnected "
  317. "during waiting for irq.\n");
  318. mdc800->camera_request_ready=0;
  319. return -2;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * The write_urb callback function
  325. */
  326. static void mdc800_usb_write_notify (struct urb *urb)
  327. {
  328. struct mdc800_data* mdc800=urb->context;
  329. int status = urb->status;
  330. if (status != 0)
  331. dev_err(&mdc800->dev->dev,
  332. "writing command fails (status=%i)\n", status);
  333. else
  334. mdc800->state=READY;
  335. mdc800->written = 1;
  336. wake_up (&mdc800->write_wait);
  337. }
  338. /*
  339. * The download_urb callback function
  340. */
  341. static void mdc800_usb_download_notify (struct urb *urb)
  342. {
  343. struct mdc800_data* mdc800=urb->context;
  344. int status = urb->status;
  345. if (status == 0) {
  346. /* Fill output buffer with these data */
  347. memcpy (mdc800->out, urb->transfer_buffer, 64);
  348. mdc800->out_count=64;
  349. mdc800->out_ptr=0;
  350. mdc800->download_left-=64;
  351. if (mdc800->download_left == 0)
  352. {
  353. mdc800->state=READY;
  354. }
  355. } else {
  356. dev_err(&mdc800->dev->dev,
  357. "request bytes fails (status:%i)\n", status);
  358. }
  359. mdc800->downloaded = 1;
  360. wake_up (&mdc800->download_wait);
  361. }
  362. /***************************************************************************
  363. Probing for the Camera
  364. ***************************************************************************/
  365. static struct usb_driver mdc800_usb_driver;
  366. static const struct file_operations mdc800_device_ops;
  367. static struct usb_class_driver mdc800_class = {
  368. .name = "mdc800%d",
  369. .fops = &mdc800_device_ops,
  370. .minor_base = MDC800_DEVICE_MINOR_BASE,
  371. };
  372. /*
  373. * Callback to search the Mustek MDC800 on the USB Bus
  374. */
  375. static int mdc800_usb_probe (struct usb_interface *intf,
  376. const struct usb_device_id *id)
  377. {
  378. int i,j;
  379. struct usb_host_interface *intf_desc;
  380. struct usb_device *dev = interface_to_usbdev (intf);
  381. int irq_interval=0;
  382. int retval;
  383. dbg ("(mdc800_usb_probe) called.");
  384. if (mdc800->dev != NULL)
  385. {
  386. dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
  387. return -ENODEV;
  388. }
  389. if (dev->descriptor.bNumConfigurations != 1)
  390. {
  391. dev_err(&intf->dev,
  392. "probe fails -> wrong Number of Configuration\n");
  393. return -ENODEV;
  394. }
  395. intf_desc = intf->cur_altsetting;
  396. if (
  397. ( intf_desc->desc.bInterfaceClass != 0xff )
  398. || ( intf_desc->desc.bInterfaceSubClass != 0 )
  399. || ( intf_desc->desc.bInterfaceProtocol != 0 )
  400. || ( intf_desc->desc.bNumEndpoints != 4)
  401. )
  402. {
  403. dev_err(&intf->dev, "probe fails -> wrong Interface\n");
  404. return -ENODEV;
  405. }
  406. /* Check the Endpoints */
  407. for (i=0; i<4; i++)
  408. {
  409. mdc800->endpoint[i]=-1;
  410. for (j=0; j<4; j++)
  411. {
  412. if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
  413. {
  414. mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
  415. if (i==1)
  416. {
  417. irq_interval=intf_desc->endpoint [j].desc.bInterval;
  418. }
  419. }
  420. }
  421. if (mdc800->endpoint[i] == -1)
  422. {
  423. dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
  424. return -ENODEV;
  425. }
  426. }
  427. dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
  428. mutex_lock(&mdc800->io_lock);
  429. retval = usb_register_dev(intf, &mdc800_class);
  430. if (retval) {
  431. dev_err(&intf->dev, "Not able to get a minor for this device.\n");
  432. mutex_unlock(&mdc800->io_lock);
  433. return -ENODEV;
  434. }
  435. mdc800->dev=dev;
  436. mdc800->open=0;
  437. /* Setup URB Structs */
  438. usb_fill_int_urb (
  439. mdc800->irq_urb,
  440. mdc800->dev,
  441. usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
  442. mdc800->irq_urb_buffer,
  443. 8,
  444. mdc800_usb_irq,
  445. mdc800,
  446. irq_interval
  447. );
  448. usb_fill_bulk_urb (
  449. mdc800->write_urb,
  450. mdc800->dev,
  451. usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
  452. mdc800->write_urb_buffer,
  453. 8,
  454. mdc800_usb_write_notify,
  455. mdc800
  456. );
  457. usb_fill_bulk_urb (
  458. mdc800->download_urb,
  459. mdc800->dev,
  460. usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
  461. mdc800->download_urb_buffer,
  462. 64,
  463. mdc800_usb_download_notify,
  464. mdc800
  465. );
  466. mdc800->state=READY;
  467. mutex_unlock(&mdc800->io_lock);
  468. usb_set_intfdata(intf, mdc800);
  469. return 0;
  470. }
  471. /*
  472. * Disconnect USB device (maybe the MDC800)
  473. */
  474. static void mdc800_usb_disconnect (struct usb_interface *intf)
  475. {
  476. struct mdc800_data* mdc800 = usb_get_intfdata(intf);
  477. dbg ("(mdc800_usb_disconnect) called");
  478. if (mdc800) {
  479. if (mdc800->state == NOT_CONNECTED)
  480. return;
  481. usb_deregister_dev(intf, &mdc800_class);
  482. /* must be under lock to make sure no URB
  483. is submitted after usb_kill_urb() */
  484. mutex_lock(&mdc800->io_lock);
  485. mdc800->state=NOT_CONNECTED;
  486. usb_kill_urb(mdc800->irq_urb);
  487. usb_kill_urb(mdc800->write_urb);
  488. usb_kill_urb(mdc800->download_urb);
  489. mutex_unlock(&mdc800->io_lock);
  490. mdc800->dev = NULL;
  491. usb_set_intfdata(intf, NULL);
  492. }
  493. dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
  494. }
  495. /***************************************************************************
  496. The Misc device Part (file_operations)
  497. ****************************************************************************/
  498. /*
  499. * This Function calc the Answersize for a command.
  500. */
  501. static int mdc800_getAnswerSize (char command)
  502. {
  503. switch ((unsigned char) command)
  504. {
  505. case 0x2a:
  506. case 0x49:
  507. case 0x51:
  508. case 0x0d:
  509. case 0x20:
  510. case 0x07:
  511. case 0x01:
  512. case 0x25:
  513. case 0x00:
  514. return 8;
  515. case 0x05:
  516. case 0x3e:
  517. return mdc800->pic_len;
  518. case 0x09:
  519. return 4096;
  520. default:
  521. return 0;
  522. }
  523. }
  524. /*
  525. * Init the device: (1) alloc mem (2) Increase MOD Count ..
  526. */
  527. static int mdc800_device_open (struct inode* inode, struct file *file)
  528. {
  529. int retval=0;
  530. int errn=0;
  531. mutex_lock(&mdc800->io_lock);
  532. if (mdc800->state == NOT_CONNECTED)
  533. {
  534. errn=-EBUSY;
  535. goto error_out;
  536. }
  537. if (mdc800->open)
  538. {
  539. errn=-EBUSY;
  540. goto error_out;
  541. }
  542. mdc800->in_count=0;
  543. mdc800->out_count=0;
  544. mdc800->out_ptr=0;
  545. mdc800->pic_index=0;
  546. mdc800->pic_len=-1;
  547. mdc800->download_left=0;
  548. mdc800->camera_busy=0;
  549. mdc800->camera_request_ready=0;
  550. retval=0;
  551. mdc800->irq_urb->dev = mdc800->dev;
  552. retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
  553. if (retval) {
  554. dev_err(&mdc800->dev->dev,
  555. "request USB irq fails (submit_retval=%i).\n", retval);
  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. int retval;
  595. mutex_lock(&mdc800->io_lock);
  596. if (mdc800->state == NOT_CONNECTED)
  597. {
  598. mutex_unlock(&mdc800->io_lock);
  599. return -EBUSY;
  600. }
  601. if (mdc800->state == WORKING)
  602. {
  603. printk(KERN_WARNING "mdc800: Illegal State \"working\""
  604. "reached during read ?!\n");
  605. mutex_unlock(&mdc800->io_lock);
  606. return -EBUSY;
  607. }
  608. if (!mdc800->open)
  609. {
  610. mutex_unlock(&mdc800->io_lock);
  611. return -EBUSY;
  612. }
  613. while (left)
  614. {
  615. if (signal_pending (current))
  616. {
  617. mutex_unlock(&mdc800->io_lock);
  618. return -EINTR;
  619. }
  620. sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
  621. if (sts <= 0)
  622. {
  623. /* Too less Data in buffer */
  624. if (mdc800->state == DOWNLOAD)
  625. {
  626. mdc800->out_count=0;
  627. mdc800->out_ptr=0;
  628. /* Download -> Request new bytes */
  629. mdc800->download_urb->dev = mdc800->dev;
  630. retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
  631. if (retval) {
  632. dev_err(&mdc800->dev->dev,
  633. "Can't submit download urb "
  634. "(retval=%i)\n", retval);
  635. mutex_unlock(&mdc800->io_lock);
  636. return len-left;
  637. }
  638. wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
  639. TO_DOWNLOAD_GET_READY*HZ/1000);
  640. mdc800->downloaded = 0;
  641. if (mdc800->download_urb->status != 0)
  642. {
  643. dev_err(&mdc800->dev->dev,
  644. "request download-bytes fails "
  645. "(status=%i)\n",
  646. mdc800->download_urb->status);
  647. mutex_unlock(&mdc800->io_lock);
  648. return len-left;
  649. }
  650. }
  651. else
  652. {
  653. /* No more bytes -> that's an error*/
  654. mutex_unlock(&mdc800->io_lock);
  655. return -EIO;
  656. }
  657. }
  658. else
  659. {
  660. /* Copy Bytes */
  661. if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
  662. sts)) {
  663. mutex_unlock(&mdc800->io_lock);
  664. return -EFAULT;
  665. }
  666. ptr+=sts;
  667. left-=sts;
  668. mdc800->out_ptr+=sts;
  669. }
  670. }
  671. mutex_unlock(&mdc800->io_lock);
  672. return len-left;
  673. }
  674. /*
  675. * The Device write callback Function
  676. * If a 8Byte Command is received, it will be send to the camera.
  677. * After this the driver initiates the request for the answer or
  678. * just waits until the camera becomes ready.
  679. */
  680. static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
  681. {
  682. size_t i=0;
  683. int retval;
  684. mutex_lock(&mdc800->io_lock);
  685. if (mdc800->state != READY)
  686. {
  687. mutex_unlock(&mdc800->io_lock);
  688. return -EBUSY;
  689. }
  690. if (!mdc800->open )
  691. {
  692. mutex_unlock(&mdc800->io_lock);
  693. return -EBUSY;
  694. }
  695. while (i<len)
  696. {
  697. unsigned char c;
  698. if (signal_pending (current))
  699. {
  700. mutex_unlock(&mdc800->io_lock);
  701. return -EINTR;
  702. }
  703. if(get_user(c, buf+i))
  704. {
  705. mutex_unlock(&mdc800->io_lock);
  706. return -EFAULT;
  707. }
  708. /* check for command start */
  709. if (c == 0x55)
  710. {
  711. mdc800->in_count=0;
  712. mdc800->out_count=0;
  713. mdc800->out_ptr=0;
  714. mdc800->download_left=0;
  715. }
  716. /* save command byte */
  717. if (mdc800->in_count < 8)
  718. {
  719. mdc800->in[mdc800->in_count] = c;
  720. mdc800->in_count++;
  721. }
  722. else
  723. {
  724. mutex_unlock(&mdc800->io_lock);
  725. return -EIO;
  726. }
  727. /* Command Buffer full ? -> send it to camera */
  728. if (mdc800->in_count == 8)
  729. {
  730. int answersize;
  731. if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
  732. {
  733. dev_err(&mdc800->dev->dev,
  734. "Camera didn't get ready.\n");
  735. mutex_unlock(&mdc800->io_lock);
  736. return -EIO;
  737. }
  738. answersize=mdc800_getAnswerSize (mdc800->in[1]);
  739. mdc800->state=WORKING;
  740. memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
  741. mdc800->write_urb->dev = mdc800->dev;
  742. retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
  743. if (retval) {
  744. dev_err(&mdc800->dev->dev,
  745. "submitting write urb fails "
  746. "(retval=%i)\n", retval);
  747. mutex_unlock(&mdc800->io_lock);
  748. return -EIO;
  749. }
  750. wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
  751. mdc800->written = 0;
  752. if (mdc800->state == WORKING)
  753. {
  754. usb_kill_urb(mdc800->write_urb);
  755. mutex_unlock(&mdc800->io_lock);
  756. return -EIO;
  757. }
  758. switch ((unsigned char) mdc800->in[1])
  759. {
  760. case 0x05: /* Download Image */
  761. case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
  762. if (mdc800->pic_len < 0)
  763. {
  764. dev_err(&mdc800->dev->dev,
  765. "call 0x07 before "
  766. "0x05,0x3e\n");
  767. mdc800->state=READY;
  768. mutex_unlock(&mdc800->io_lock);
  769. return -EIO;
  770. }
  771. mdc800->pic_len=-1;
  772. case 0x09: /* Download Thumbnail */
  773. mdc800->download_left=answersize+64;
  774. mdc800->state=DOWNLOAD;
  775. mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
  776. break;
  777. default:
  778. if (answersize)
  779. {
  780. if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
  781. {
  782. dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
  783. mutex_unlock(&mdc800->io_lock);
  784. return -EIO;
  785. }
  786. /* Write dummy data, (this is ugly but part of the USB Protocol */
  787. /* if you use endpoint 1 as bulk and not as irq) */
  788. memcpy (mdc800->out, mdc800->camera_response,8);
  789. /* This is the interpreted answer */
  790. memcpy (&mdc800->out[8], mdc800->camera_response,8);
  791. mdc800->out_ptr=0;
  792. mdc800->out_count=16;
  793. /* Cache the Imagesize, if command was getImageSize */
  794. if (mdc800->in [1] == (char) 0x07)
  795. {
  796. mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
  797. dbg ("cached imagesize = %i",mdc800->pic_len);
  798. }
  799. }
  800. else
  801. {
  802. if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
  803. {
  804. dev_err(&mdc800->dev->dev, "Command Timeout.\n");
  805. mutex_unlock(&mdc800->io_lock);
  806. return -EIO;
  807. }
  808. }
  809. mdc800->state=READY;
  810. break;
  811. }
  812. }
  813. i++;
  814. }
  815. mutex_unlock(&mdc800->io_lock);
  816. return i;
  817. }
  818. /***************************************************************************
  819. Init and Cleanup this driver (Structs and types)
  820. ****************************************************************************/
  821. /* File Operations of this drivers */
  822. static const struct file_operations mdc800_device_ops =
  823. {
  824. .owner = THIS_MODULE,
  825. .read = mdc800_device_read,
  826. .write = mdc800_device_write,
  827. .open = mdc800_device_open,
  828. .release = mdc800_device_release,
  829. .llseek = noop_llseek,
  830. };
  831. static const struct usb_device_id mdc800_table[] = {
  832. { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
  833. { } /* Terminating entry */
  834. };
  835. MODULE_DEVICE_TABLE (usb, mdc800_table);
  836. /*
  837. * USB Driver Struct for this device
  838. */
  839. static struct usb_driver mdc800_usb_driver =
  840. {
  841. .name = "mdc800",
  842. .probe = mdc800_usb_probe,
  843. .disconnect = mdc800_usb_disconnect,
  844. .id_table = mdc800_table
  845. };
  846. /************************************************************************
  847. Init and Cleanup this driver (Main Functions)
  848. *************************************************************************/
  849. static int __init usb_mdc800_init (void)
  850. {
  851. int retval = -ENODEV;
  852. /* Allocate Memory */
  853. mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
  854. if (!mdc800)
  855. goto cleanup_on_fail;
  856. mdc800->dev = NULL;
  857. mdc800->state=NOT_CONNECTED;
  858. mutex_init (&mdc800->io_lock);
  859. init_waitqueue_head (&mdc800->irq_wait);
  860. init_waitqueue_head (&mdc800->write_wait);
  861. init_waitqueue_head (&mdc800->download_wait);
  862. mdc800->irq_woken = 0;
  863. mdc800->downloaded = 0;
  864. mdc800->written = 0;
  865. mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
  866. if (!mdc800->irq_urb_buffer)
  867. goto cleanup_on_fail;
  868. mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
  869. if (!mdc800->write_urb_buffer)
  870. goto cleanup_on_fail;
  871. mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
  872. if (!mdc800->download_urb_buffer)
  873. goto cleanup_on_fail;
  874. mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
  875. if (!mdc800->irq_urb)
  876. goto cleanup_on_fail;
  877. mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
  878. if (!mdc800->download_urb)
  879. goto cleanup_on_fail;
  880. mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
  881. if (!mdc800->write_urb)
  882. goto cleanup_on_fail;
  883. /* Register the driver */
  884. retval = usb_register(&mdc800_usb_driver);
  885. if (retval)
  886. goto cleanup_on_fail;
  887. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  888. DRIVER_DESC "\n");
  889. return 0;
  890. /* Clean driver up, when something fails */
  891. cleanup_on_fail:
  892. if (mdc800 != NULL)
  893. {
  894. printk(KERN_ERR "mdc800: can't alloc memory!\n");
  895. kfree(mdc800->download_urb_buffer);
  896. kfree(mdc800->write_urb_buffer);
  897. kfree(mdc800->irq_urb_buffer);
  898. usb_free_urb(mdc800->write_urb);
  899. usb_free_urb(mdc800->download_urb);
  900. usb_free_urb(mdc800->irq_urb);
  901. kfree (mdc800);
  902. }
  903. mdc800 = NULL;
  904. return retval;
  905. }
  906. static void __exit usb_mdc800_cleanup (void)
  907. {
  908. usb_deregister (&mdc800_usb_driver);
  909. usb_free_urb (mdc800->irq_urb);
  910. usb_free_urb (mdc800->download_urb);
  911. usb_free_urb (mdc800->write_urb);
  912. kfree (mdc800->irq_urb_buffer);
  913. kfree (mdc800->write_urb_buffer);
  914. kfree (mdc800->download_urb_buffer);
  915. kfree (mdc800);
  916. mdc800 = NULL;
  917. }
  918. module_init (usb_mdc800_init);
  919. module_exit (usb_mdc800_cleanup);
  920. MODULE_AUTHOR( DRIVER_AUTHOR );
  921. MODULE_DESCRIPTION( DRIVER_DESC );
  922. MODULE_LICENSE("GPL");