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 disconnect 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/signal.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. struct device *dev = &mdc800->dev->dev;
  250. int status = urb->status;
  251. if (status >= 0) {
  252. if (mdc800_isBusy (b))
  253. {
  254. if (!mdc800->camera_busy)
  255. {
  256. mdc800->camera_busy=1;
  257. dev_dbg(dev, "gets busy\n");
  258. }
  259. }
  260. else
  261. {
  262. if (mdc800->camera_busy && mdc800_isReady (b))
  263. {
  264. mdc800->camera_busy=0;
  265. dev_dbg(dev, "gets ready\n");
  266. }
  267. }
  268. if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
  269. {
  270. /* Store Data in camera_answer field */
  271. dev_dbg(dev, "%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]);
  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,
  307. msecs_to_jiffies(msec));
  308. mdc800->irq_woken = 0;
  309. if (mdc800->camera_request_ready>0)
  310. {
  311. mdc800->camera_request_ready=0;
  312. dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
  313. return -1;
  314. }
  315. if (mdc800->state == NOT_CONNECTED)
  316. {
  317. printk(KERN_WARNING "mdc800: Camera gets disconnected "
  318. "during waiting for irq.\n");
  319. mdc800->camera_request_ready=0;
  320. return -2;
  321. }
  322. return 0;
  323. }
  324. /*
  325. * The write_urb callback function
  326. */
  327. static void mdc800_usb_write_notify (struct urb *urb)
  328. {
  329. struct mdc800_data* mdc800=urb->context;
  330. int status = urb->status;
  331. if (status != 0)
  332. dev_err(&mdc800->dev->dev,
  333. "writing command fails (status=%i)\n", status);
  334. else
  335. mdc800->state=READY;
  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. int status = urb->status;
  346. if (status == 0) {
  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. } else {
  357. dev_err(&mdc800->dev->dev,
  358. "request bytes fails (status:%i)\n", status);
  359. }
  360. mdc800->downloaded = 1;
  361. wake_up (&mdc800->download_wait);
  362. }
  363. /***************************************************************************
  364. Probing for the Camera
  365. ***************************************************************************/
  366. static struct usb_driver mdc800_usb_driver;
  367. static const struct file_operations mdc800_device_ops;
  368. static struct usb_class_driver mdc800_class = {
  369. .name = "mdc800%d",
  370. .fops = &mdc800_device_ops,
  371. .minor_base = MDC800_DEVICE_MINOR_BASE,
  372. };
  373. /*
  374. * Callback to search the Mustek MDC800 on the USB Bus
  375. */
  376. static int mdc800_usb_probe (struct usb_interface *intf,
  377. const struct usb_device_id *id)
  378. {
  379. int i,j;
  380. struct usb_host_interface *intf_desc;
  381. struct usb_device *dev = interface_to_usbdev (intf);
  382. int irq_interval=0;
  383. int retval;
  384. dev_dbg(&intf->dev, "(%s) called.\n", __func__);
  385. if (mdc800->dev != NULL)
  386. {
  387. dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
  388. return -ENODEV;
  389. }
  390. if (dev->descriptor.bNumConfigurations != 1)
  391. {
  392. dev_err(&intf->dev,
  393. "probe fails -> wrong Number of Configuration\n");
  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. dev_err(&intf->dev, "probe fails -> wrong Interface\n");
  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. }
  421. }
  422. if (mdc800->endpoint[i] == -1)
  423. {
  424. dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
  425. return -ENODEV;
  426. }
  427. }
  428. dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
  429. mutex_lock(&mdc800->io_lock);
  430. retval = usb_register_dev(intf, &mdc800_class);
  431. if (retval) {
  432. dev_err(&intf->dev, "Not able to get a minor for this device.\n");
  433. mutex_unlock(&mdc800->io_lock);
  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. dev_dbg(&intf->dev, "(%s) called\n", __func__);
  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. dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
  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. retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
  554. if (retval) {
  555. dev_err(&mdc800->dev->dev,
  556. "request USB irq fails (submit_retval=%i).\n", retval);
  557. errn = -EIO;
  558. goto error_out;
  559. }
  560. mdc800->open=1;
  561. dev_dbg(&mdc800->dev->dev, "Mustek MDC800 device opened.\n");
  562. error_out:
  563. mutex_unlock(&mdc800->io_lock);
  564. return errn;
  565. }
  566. /*
  567. * Close the Camera and release Memory
  568. */
  569. static int mdc800_device_release (struct inode* inode, struct file *file)
  570. {
  571. int retval=0;
  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,
  639. mdc800->downloaded,
  640. msecs_to_jiffies(TO_DOWNLOAD_GET_READY));
  641. mdc800->downloaded = 0;
  642. if (mdc800->download_urb->status != 0)
  643. {
  644. dev_err(&mdc800->dev->dev,
  645. "request download-bytes fails "
  646. "(status=%i)\n",
  647. mdc800->download_urb->status);
  648. mutex_unlock(&mdc800->io_lock);
  649. return len-left;
  650. }
  651. }
  652. else
  653. {
  654. /* No more bytes -> that's an error*/
  655. mutex_unlock(&mdc800->io_lock);
  656. return -EIO;
  657. }
  658. }
  659. else
  660. {
  661. /* Copy Bytes */
  662. if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
  663. sts)) {
  664. mutex_unlock(&mdc800->io_lock);
  665. return -EFAULT;
  666. }
  667. ptr+=sts;
  668. left-=sts;
  669. mdc800->out_ptr+=sts;
  670. }
  671. }
  672. mutex_unlock(&mdc800->io_lock);
  673. return len-left;
  674. }
  675. /*
  676. * The Device write callback Function
  677. * If a 8Byte Command is received, it will be send to the camera.
  678. * After this the driver initiates the request for the answer or
  679. * just waits until the camera becomes ready.
  680. */
  681. static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
  682. {
  683. size_t i=0;
  684. int retval;
  685. mutex_lock(&mdc800->io_lock);
  686. if (mdc800->state != READY)
  687. {
  688. mutex_unlock(&mdc800->io_lock);
  689. return -EBUSY;
  690. }
  691. if (!mdc800->open )
  692. {
  693. mutex_unlock(&mdc800->io_lock);
  694. return -EBUSY;
  695. }
  696. while (i<len)
  697. {
  698. unsigned char c;
  699. if (signal_pending (current))
  700. {
  701. mutex_unlock(&mdc800->io_lock);
  702. return -EINTR;
  703. }
  704. if(get_user(c, buf+i))
  705. {
  706. mutex_unlock(&mdc800->io_lock);
  707. return -EFAULT;
  708. }
  709. /* check for command start */
  710. if (c == 0x55)
  711. {
  712. mdc800->in_count=0;
  713. mdc800->out_count=0;
  714. mdc800->out_ptr=0;
  715. mdc800->download_left=0;
  716. }
  717. /* save command byte */
  718. if (mdc800->in_count < 8)
  719. {
  720. mdc800->in[mdc800->in_count] = c;
  721. mdc800->in_count++;
  722. }
  723. else
  724. {
  725. mutex_unlock(&mdc800->io_lock);
  726. return -EIO;
  727. }
  728. /* Command Buffer full ? -> send it to camera */
  729. if (mdc800->in_count == 8)
  730. {
  731. int answersize;
  732. if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
  733. {
  734. dev_err(&mdc800->dev->dev,
  735. "Camera didn't get ready.\n");
  736. mutex_unlock(&mdc800->io_lock);
  737. return -EIO;
  738. }
  739. answersize=mdc800_getAnswerSize (mdc800->in[1]);
  740. mdc800->state=WORKING;
  741. memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
  742. mdc800->write_urb->dev = mdc800->dev;
  743. retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
  744. if (retval) {
  745. dev_err(&mdc800->dev->dev,
  746. "submitting write urb fails "
  747. "(retval=%i)\n", retval);
  748. mutex_unlock(&mdc800->io_lock);
  749. return -EIO;
  750. }
  751. wait_event_timeout(mdc800->write_wait, mdc800->written,
  752. msecs_to_jiffies(TO_WRITE_GET_READY));
  753. mdc800->written = 0;
  754. if (mdc800->state == WORKING)
  755. {
  756. usb_kill_urb(mdc800->write_urb);
  757. mutex_unlock(&mdc800->io_lock);
  758. return -EIO;
  759. }
  760. switch ((unsigned char) mdc800->in[1])
  761. {
  762. case 0x05: /* Download Image */
  763. case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
  764. if (mdc800->pic_len < 0)
  765. {
  766. dev_err(&mdc800->dev->dev,
  767. "call 0x07 before "
  768. "0x05,0x3e\n");
  769. mdc800->state=READY;
  770. mutex_unlock(&mdc800->io_lock);
  771. return -EIO;
  772. }
  773. mdc800->pic_len=-1;
  774. case 0x09: /* Download Thumbnail */
  775. mdc800->download_left=answersize+64;
  776. mdc800->state=DOWNLOAD;
  777. mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
  778. break;
  779. default:
  780. if (answersize)
  781. {
  782. if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
  783. {
  784. dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
  785. mutex_unlock(&mdc800->io_lock);
  786. return -EIO;
  787. }
  788. /* Write dummy data, (this is ugly but part of the USB Protocol */
  789. /* if you use endpoint 1 as bulk and not as irq) */
  790. memcpy (mdc800->out, mdc800->camera_response,8);
  791. /* This is the interpreted answer */
  792. memcpy (&mdc800->out[8], mdc800->camera_response,8);
  793. mdc800->out_ptr=0;
  794. mdc800->out_count=16;
  795. /* Cache the Imagesize, if command was getImageSize */
  796. if (mdc800->in [1] == (char) 0x07)
  797. {
  798. mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
  799. dev_dbg(&mdc800->dev->dev, "cached imagesize = %i\n", mdc800->pic_len);
  800. }
  801. }
  802. else
  803. {
  804. if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
  805. {
  806. dev_err(&mdc800->dev->dev, "Command Timeout.\n");
  807. mutex_unlock(&mdc800->io_lock);
  808. return -EIO;
  809. }
  810. }
  811. mdc800->state=READY;
  812. break;
  813. }
  814. }
  815. i++;
  816. }
  817. mutex_unlock(&mdc800->io_lock);
  818. return i;
  819. }
  820. /***************************************************************************
  821. Init and Cleanup this driver (Structs and types)
  822. ****************************************************************************/
  823. /* File Operations of this drivers */
  824. static const struct file_operations mdc800_device_ops =
  825. {
  826. .owner = THIS_MODULE,
  827. .read = mdc800_device_read,
  828. .write = mdc800_device_write,
  829. .open = mdc800_device_open,
  830. .release = mdc800_device_release,
  831. .llseek = noop_llseek,
  832. };
  833. static const struct usb_device_id mdc800_table[] = {
  834. { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
  835. { } /* Terminating entry */
  836. };
  837. MODULE_DEVICE_TABLE (usb, mdc800_table);
  838. /*
  839. * USB Driver Struct for this device
  840. */
  841. static struct usb_driver mdc800_usb_driver =
  842. {
  843. .name = "mdc800",
  844. .probe = mdc800_usb_probe,
  845. .disconnect = mdc800_usb_disconnect,
  846. .id_table = mdc800_table
  847. };
  848. /************************************************************************
  849. Init and Cleanup this driver (Main Functions)
  850. *************************************************************************/
  851. static int __init usb_mdc800_init (void)
  852. {
  853. int retval = -ENODEV;
  854. /* Allocate Memory */
  855. mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
  856. if (!mdc800)
  857. goto cleanup_on_fail;
  858. mdc800->dev = NULL;
  859. mdc800->state=NOT_CONNECTED;
  860. mutex_init (&mdc800->io_lock);
  861. init_waitqueue_head (&mdc800->irq_wait);
  862. init_waitqueue_head (&mdc800->write_wait);
  863. init_waitqueue_head (&mdc800->download_wait);
  864. mdc800->irq_woken = 0;
  865. mdc800->downloaded = 0;
  866. mdc800->written = 0;
  867. mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
  868. if (!mdc800->irq_urb_buffer)
  869. goto cleanup_on_fail;
  870. mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
  871. if (!mdc800->write_urb_buffer)
  872. goto cleanup_on_fail;
  873. mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
  874. if (!mdc800->download_urb_buffer)
  875. goto cleanup_on_fail;
  876. mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
  877. if (!mdc800->irq_urb)
  878. goto cleanup_on_fail;
  879. mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
  880. if (!mdc800->download_urb)
  881. goto cleanup_on_fail;
  882. mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
  883. if (!mdc800->write_urb)
  884. goto cleanup_on_fail;
  885. /* Register the driver */
  886. retval = usb_register(&mdc800_usb_driver);
  887. if (retval)
  888. goto cleanup_on_fail;
  889. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  890. DRIVER_DESC "\n");
  891. return 0;
  892. /* Clean driver up, when something fails */
  893. cleanup_on_fail:
  894. if (mdc800 != NULL)
  895. {
  896. printk(KERN_ERR "mdc800: can't alloc memory!\n");
  897. kfree(mdc800->download_urb_buffer);
  898. kfree(mdc800->write_urb_buffer);
  899. kfree(mdc800->irq_urb_buffer);
  900. usb_free_urb(mdc800->write_urb);
  901. usb_free_urb(mdc800->download_urb);
  902. usb_free_urb(mdc800->irq_urb);
  903. kfree (mdc800);
  904. }
  905. mdc800 = NULL;
  906. return retval;
  907. }
  908. static void __exit usb_mdc800_cleanup (void)
  909. {
  910. usb_deregister (&mdc800_usb_driver);
  911. usb_free_urb (mdc800->irq_urb);
  912. usb_free_urb (mdc800->download_urb);
  913. usb_free_urb (mdc800->write_urb);
  914. kfree (mdc800->irq_urb_buffer);
  915. kfree (mdc800->write_urb_buffer);
  916. kfree (mdc800->download_urb_buffer);
  917. kfree (mdc800);
  918. mdc800 = NULL;
  919. }
  920. module_init (usb_mdc800_init);
  921. module_exit (usb_mdc800_cleanup);
  922. MODULE_AUTHOR( DRIVER_AUTHOR );
  923. MODULE_DESCRIPTION( DRIVER_DESC );
  924. MODULE_LICENSE("GPL");