mdc800.c 23 KB

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