mdc800.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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.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");