cdc-wdm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /*
  2. * cdc-wdm.c
  3. *
  4. * This driver supports USB CDC WCM Device Management.
  5. *
  6. * Copyright (c) 2007-2009 Oliver Neukum
  7. *
  8. * Some code taken from cdc-acm.c
  9. *
  10. * Released under the GPLv2.
  11. *
  12. * Many thanks to Carl Nordbeck
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/ioctl.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/bitops.h>
  22. #include <linux/poll.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/cdc.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <linux/usb/cdc-wdm.h>
  28. /*
  29. * Version Information
  30. */
  31. #define DRIVER_VERSION "v0.03"
  32. #define DRIVER_AUTHOR "Oliver Neukum"
  33. #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
  34. static const struct usb_device_id wdm_ids[] = {
  35. {
  36. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  37. USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  38. .bInterfaceClass = USB_CLASS_COMM,
  39. .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
  40. },
  41. { }
  42. };
  43. MODULE_DEVICE_TABLE (usb, wdm_ids);
  44. #define WDM_MINOR_BASE 176
  45. #define WDM_IN_USE 1
  46. #define WDM_DISCONNECTING 2
  47. #define WDM_RESULT 3
  48. #define WDM_READ 4
  49. #define WDM_INT_STALL 5
  50. #define WDM_POLL_RUNNING 6
  51. #define WDM_RESPONDING 7
  52. #define WDM_SUSPENDING 8
  53. #define WDM_RESETTING 9
  54. #define WDM_OVERFLOW 10
  55. #define WDM_MAX 16
  56. /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
  57. #define WDM_DEFAULT_BUFSIZE 256
  58. static DEFINE_MUTEX(wdm_mutex);
  59. static DEFINE_SPINLOCK(wdm_device_list_lock);
  60. static LIST_HEAD(wdm_device_list);
  61. /* --- method tables --- */
  62. struct wdm_device {
  63. u8 *inbuf; /* buffer for response */
  64. u8 *outbuf; /* buffer for command */
  65. u8 *sbuf; /* buffer for status */
  66. u8 *ubuf; /* buffer for copy to user space */
  67. struct urb *command;
  68. struct urb *response;
  69. struct urb *validity;
  70. struct usb_interface *intf;
  71. struct usb_ctrlrequest *orq;
  72. struct usb_ctrlrequest *irq;
  73. spinlock_t iuspin;
  74. unsigned long flags;
  75. u16 bufsize;
  76. u16 wMaxCommand;
  77. u16 wMaxPacketSize;
  78. __le16 inum;
  79. int reslength;
  80. int length;
  81. int read;
  82. int count;
  83. dma_addr_t shandle;
  84. dma_addr_t ihandle;
  85. struct mutex wlock;
  86. struct mutex rlock;
  87. wait_queue_head_t wait;
  88. struct work_struct rxwork;
  89. int werr;
  90. int rerr;
  91. int resp_count;
  92. struct list_head device_list;
  93. int (*manage_power)(struct usb_interface *, int);
  94. };
  95. static struct usb_driver wdm_driver;
  96. /* return intfdata if we own the interface, else look up intf in the list */
  97. static struct wdm_device *wdm_find_device(struct usb_interface *intf)
  98. {
  99. struct wdm_device *desc;
  100. spin_lock(&wdm_device_list_lock);
  101. list_for_each_entry(desc, &wdm_device_list, device_list)
  102. if (desc->intf == intf)
  103. goto found;
  104. desc = NULL;
  105. found:
  106. spin_unlock(&wdm_device_list_lock);
  107. return desc;
  108. }
  109. static struct wdm_device *wdm_find_device_by_minor(int minor)
  110. {
  111. struct wdm_device *desc;
  112. spin_lock(&wdm_device_list_lock);
  113. list_for_each_entry(desc, &wdm_device_list, device_list)
  114. if (desc->intf->minor == minor)
  115. goto found;
  116. desc = NULL;
  117. found:
  118. spin_unlock(&wdm_device_list_lock);
  119. return desc;
  120. }
  121. /* --- callbacks --- */
  122. static void wdm_out_callback(struct urb *urb)
  123. {
  124. struct wdm_device *desc;
  125. desc = urb->context;
  126. spin_lock(&desc->iuspin);
  127. desc->werr = urb->status;
  128. spin_unlock(&desc->iuspin);
  129. kfree(desc->outbuf);
  130. desc->outbuf = NULL;
  131. clear_bit(WDM_IN_USE, &desc->flags);
  132. wake_up(&desc->wait);
  133. }
  134. static void wdm_in_callback(struct urb *urb)
  135. {
  136. struct wdm_device *desc = urb->context;
  137. int status = urb->status;
  138. int length = urb->actual_length;
  139. spin_lock(&desc->iuspin);
  140. clear_bit(WDM_RESPONDING, &desc->flags);
  141. if (status) {
  142. switch (status) {
  143. case -ENOENT:
  144. dev_dbg(&desc->intf->dev,
  145. "nonzero urb status received: -ENOENT");
  146. goto skip_error;
  147. case -ECONNRESET:
  148. dev_dbg(&desc->intf->dev,
  149. "nonzero urb status received: -ECONNRESET");
  150. goto skip_error;
  151. case -ESHUTDOWN:
  152. dev_dbg(&desc->intf->dev,
  153. "nonzero urb status received: -ESHUTDOWN");
  154. goto skip_error;
  155. case -EPIPE:
  156. dev_err(&desc->intf->dev,
  157. "nonzero urb status received: -EPIPE\n");
  158. break;
  159. default:
  160. dev_err(&desc->intf->dev,
  161. "Unexpected error %d\n", status);
  162. break;
  163. }
  164. }
  165. desc->rerr = status;
  166. if (length + desc->length > desc->wMaxCommand) {
  167. /* The buffer would overflow */
  168. set_bit(WDM_OVERFLOW, &desc->flags);
  169. } else {
  170. /* we may already be in overflow */
  171. if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
  172. memmove(desc->ubuf + desc->length, desc->inbuf, length);
  173. desc->length += length;
  174. desc->reslength = length;
  175. }
  176. }
  177. skip_error:
  178. wake_up(&desc->wait);
  179. set_bit(WDM_READ, &desc->flags);
  180. spin_unlock(&desc->iuspin);
  181. }
  182. static void wdm_int_callback(struct urb *urb)
  183. {
  184. int rv = 0;
  185. int responding;
  186. int status = urb->status;
  187. struct wdm_device *desc;
  188. struct usb_cdc_notification *dr;
  189. desc = urb->context;
  190. dr = (struct usb_cdc_notification *)desc->sbuf;
  191. if (status) {
  192. switch (status) {
  193. case -ESHUTDOWN:
  194. case -ENOENT:
  195. case -ECONNRESET:
  196. return; /* unplug */
  197. case -EPIPE:
  198. set_bit(WDM_INT_STALL, &desc->flags);
  199. dev_err(&desc->intf->dev, "Stall on int endpoint\n");
  200. goto sw; /* halt is cleared in work */
  201. default:
  202. dev_err(&desc->intf->dev,
  203. "nonzero urb status received: %d\n", status);
  204. break;
  205. }
  206. }
  207. if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
  208. dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
  209. urb->actual_length);
  210. goto exit;
  211. }
  212. switch (dr->bNotificationType) {
  213. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  214. dev_dbg(&desc->intf->dev,
  215. "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
  216. le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
  217. break;
  218. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  219. dev_dbg(&desc->intf->dev,
  220. "NOTIFY_NETWORK_CONNECTION %s network",
  221. dr->wValue ? "connected to" : "disconnected from");
  222. goto exit;
  223. case USB_CDC_NOTIFY_SPEED_CHANGE:
  224. dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)",
  225. urb->actual_length);
  226. goto exit;
  227. default:
  228. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  229. dev_err(&desc->intf->dev,
  230. "unknown notification %d received: index %d len %d\n",
  231. dr->bNotificationType,
  232. le16_to_cpu(dr->wIndex),
  233. le16_to_cpu(dr->wLength));
  234. goto exit;
  235. }
  236. spin_lock(&desc->iuspin);
  237. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  238. if (!desc->resp_count++ && !responding
  239. && !test_bit(WDM_DISCONNECTING, &desc->flags)
  240. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  241. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  242. dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
  243. __func__, rv);
  244. }
  245. spin_unlock(&desc->iuspin);
  246. if (rv < 0) {
  247. clear_bit(WDM_RESPONDING, &desc->flags);
  248. if (rv == -EPERM)
  249. return;
  250. if (rv == -ENOMEM) {
  251. sw:
  252. rv = schedule_work(&desc->rxwork);
  253. if (rv)
  254. dev_err(&desc->intf->dev,
  255. "Cannot schedule work\n");
  256. }
  257. }
  258. exit:
  259. rv = usb_submit_urb(urb, GFP_ATOMIC);
  260. if (rv)
  261. dev_err(&desc->intf->dev,
  262. "%s - usb_submit_urb failed with result %d\n",
  263. __func__, rv);
  264. }
  265. static void kill_urbs(struct wdm_device *desc)
  266. {
  267. /* the order here is essential */
  268. usb_kill_urb(desc->command);
  269. usb_kill_urb(desc->validity);
  270. usb_kill_urb(desc->response);
  271. }
  272. static void free_urbs(struct wdm_device *desc)
  273. {
  274. usb_free_urb(desc->validity);
  275. usb_free_urb(desc->response);
  276. usb_free_urb(desc->command);
  277. }
  278. static void cleanup(struct wdm_device *desc)
  279. {
  280. kfree(desc->sbuf);
  281. kfree(desc->inbuf);
  282. kfree(desc->orq);
  283. kfree(desc->irq);
  284. kfree(desc->ubuf);
  285. free_urbs(desc);
  286. kfree(desc);
  287. }
  288. static ssize_t wdm_write
  289. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  290. {
  291. u8 *buf;
  292. int rv = -EMSGSIZE, r, we;
  293. struct wdm_device *desc = file->private_data;
  294. struct usb_ctrlrequest *req;
  295. if (count > desc->wMaxCommand)
  296. count = desc->wMaxCommand;
  297. spin_lock_irq(&desc->iuspin);
  298. we = desc->werr;
  299. desc->werr = 0;
  300. spin_unlock_irq(&desc->iuspin);
  301. if (we < 0)
  302. return usb_translate_errors(we);
  303. buf = kmalloc(count, GFP_KERNEL);
  304. if (!buf) {
  305. rv = -ENOMEM;
  306. goto outnl;
  307. }
  308. r = copy_from_user(buf, buffer, count);
  309. if (r > 0) {
  310. rv = -EFAULT;
  311. goto out_free_mem;
  312. }
  313. /* concurrent writes and disconnect */
  314. r = mutex_lock_interruptible(&desc->wlock);
  315. rv = -ERESTARTSYS;
  316. if (r)
  317. goto out_free_mem;
  318. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  319. rv = -ENODEV;
  320. goto out_free_mem_lock;
  321. }
  322. r = usb_autopm_get_interface(desc->intf);
  323. if (r < 0) {
  324. rv = usb_translate_errors(r);
  325. goto out_free_mem_lock;
  326. }
  327. if (!(file->f_flags & O_NONBLOCK))
  328. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  329. &desc->flags));
  330. else
  331. if (test_bit(WDM_IN_USE, &desc->flags))
  332. r = -EAGAIN;
  333. if (test_bit(WDM_RESETTING, &desc->flags))
  334. r = -EIO;
  335. if (r < 0) {
  336. rv = r;
  337. goto out_free_mem_pm;
  338. }
  339. req = desc->orq;
  340. usb_fill_control_urb(
  341. desc->command,
  342. interface_to_usbdev(desc->intf),
  343. /* using common endpoint 0 */
  344. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  345. (unsigned char *)req,
  346. buf,
  347. count,
  348. wdm_out_callback,
  349. desc
  350. );
  351. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  352. USB_RECIP_INTERFACE);
  353. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  354. req->wValue = 0;
  355. req->wIndex = desc->inum; /* already converted */
  356. req->wLength = cpu_to_le16(count);
  357. set_bit(WDM_IN_USE, &desc->flags);
  358. desc->outbuf = buf;
  359. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  360. if (rv < 0) {
  361. desc->outbuf = NULL;
  362. clear_bit(WDM_IN_USE, &desc->flags);
  363. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  364. rv = usb_translate_errors(rv);
  365. goto out_free_mem_pm;
  366. } else {
  367. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
  368. le16_to_cpu(req->wIndex));
  369. }
  370. usb_autopm_put_interface(desc->intf);
  371. mutex_unlock(&desc->wlock);
  372. outnl:
  373. return rv < 0 ? rv : count;
  374. out_free_mem_pm:
  375. usb_autopm_put_interface(desc->intf);
  376. out_free_mem_lock:
  377. mutex_unlock(&desc->wlock);
  378. out_free_mem:
  379. kfree(buf);
  380. return rv;
  381. }
  382. /*
  383. * clear WDM_READ flag and possibly submit the read urb if resp_count
  384. * is non-zero.
  385. *
  386. * Called with desc->iuspin locked
  387. */
  388. static int clear_wdm_read_flag(struct wdm_device *desc)
  389. {
  390. int rv = 0;
  391. clear_bit(WDM_READ, &desc->flags);
  392. /* submit read urb only if the device is waiting for it */
  393. if (!desc->resp_count || !--desc->resp_count)
  394. goto out;
  395. set_bit(WDM_RESPONDING, &desc->flags);
  396. spin_unlock_irq(&desc->iuspin);
  397. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  398. spin_lock_irq(&desc->iuspin);
  399. if (rv) {
  400. dev_err(&desc->intf->dev,
  401. "usb_submit_urb failed with result %d\n", rv);
  402. /* make sure the next notification trigger a submit */
  403. clear_bit(WDM_RESPONDING, &desc->flags);
  404. desc->resp_count = 0;
  405. }
  406. out:
  407. return rv;
  408. }
  409. static ssize_t wdm_read
  410. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  411. {
  412. int rv, cntr;
  413. int i = 0;
  414. struct wdm_device *desc = file->private_data;
  415. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  416. if (rv < 0)
  417. return -ERESTARTSYS;
  418. cntr = ACCESS_ONCE(desc->length);
  419. if (cntr == 0) {
  420. desc->read = 0;
  421. retry:
  422. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  423. rv = -ENODEV;
  424. goto err;
  425. }
  426. if (test_bit(WDM_OVERFLOW, &desc->flags)) {
  427. clear_bit(WDM_OVERFLOW, &desc->flags);
  428. rv = -ENOBUFS;
  429. goto err;
  430. }
  431. i++;
  432. if (file->f_flags & O_NONBLOCK) {
  433. if (!test_bit(WDM_READ, &desc->flags)) {
  434. rv = cntr ? cntr : -EAGAIN;
  435. goto err;
  436. }
  437. rv = 0;
  438. } else {
  439. rv = wait_event_interruptible(desc->wait,
  440. test_bit(WDM_READ, &desc->flags));
  441. }
  442. /* may have happened while we slept */
  443. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  444. rv = -ENODEV;
  445. goto err;
  446. }
  447. if (test_bit(WDM_RESETTING, &desc->flags)) {
  448. rv = -EIO;
  449. goto err;
  450. }
  451. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  452. if (rv < 0) {
  453. rv = -ERESTARTSYS;
  454. goto err;
  455. }
  456. spin_lock_irq(&desc->iuspin);
  457. if (desc->rerr) { /* read completed, error happened */
  458. rv = usb_translate_errors(desc->rerr);
  459. desc->rerr = 0;
  460. spin_unlock_irq(&desc->iuspin);
  461. goto err;
  462. }
  463. /*
  464. * recheck whether we've lost the race
  465. * against the completion handler
  466. */
  467. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  468. spin_unlock_irq(&desc->iuspin);
  469. goto retry;
  470. }
  471. if (!desc->reslength) { /* zero length read */
  472. dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
  473. rv = clear_wdm_read_flag(desc);
  474. spin_unlock_irq(&desc->iuspin);
  475. if (rv < 0)
  476. goto err;
  477. goto retry;
  478. }
  479. cntr = desc->length;
  480. spin_unlock_irq(&desc->iuspin);
  481. }
  482. if (cntr > count)
  483. cntr = count;
  484. rv = copy_to_user(buffer, desc->ubuf, cntr);
  485. if (rv > 0) {
  486. rv = -EFAULT;
  487. goto err;
  488. }
  489. spin_lock_irq(&desc->iuspin);
  490. for (i = 0; i < desc->length - cntr; i++)
  491. desc->ubuf[i] = desc->ubuf[i + cntr];
  492. desc->length -= cntr;
  493. /* in case we had outstanding data */
  494. if (!desc->length)
  495. clear_wdm_read_flag(desc);
  496. spin_unlock_irq(&desc->iuspin);
  497. rv = cntr;
  498. err:
  499. mutex_unlock(&desc->rlock);
  500. return rv;
  501. }
  502. static int wdm_flush(struct file *file, fl_owner_t id)
  503. {
  504. struct wdm_device *desc = file->private_data;
  505. wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
  506. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  507. if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
  508. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  509. desc->werr);
  510. return usb_translate_errors(desc->werr);
  511. }
  512. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  513. {
  514. struct wdm_device *desc = file->private_data;
  515. unsigned long flags;
  516. unsigned int mask = 0;
  517. spin_lock_irqsave(&desc->iuspin, flags);
  518. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  519. mask = POLLHUP | POLLERR;
  520. spin_unlock_irqrestore(&desc->iuspin, flags);
  521. goto desc_out;
  522. }
  523. if (test_bit(WDM_READ, &desc->flags))
  524. mask = POLLIN | POLLRDNORM;
  525. if (desc->rerr || desc->werr)
  526. mask |= POLLERR;
  527. if (!test_bit(WDM_IN_USE, &desc->flags))
  528. mask |= POLLOUT | POLLWRNORM;
  529. spin_unlock_irqrestore(&desc->iuspin, flags);
  530. poll_wait(file, &desc->wait, wait);
  531. desc_out:
  532. return mask;
  533. }
  534. static int wdm_open(struct inode *inode, struct file *file)
  535. {
  536. int minor = iminor(inode);
  537. int rv = -ENODEV;
  538. struct usb_interface *intf;
  539. struct wdm_device *desc;
  540. mutex_lock(&wdm_mutex);
  541. desc = wdm_find_device_by_minor(minor);
  542. if (!desc)
  543. goto out;
  544. intf = desc->intf;
  545. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  546. goto out;
  547. file->private_data = desc;
  548. rv = usb_autopm_get_interface(desc->intf);
  549. if (rv < 0) {
  550. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  551. goto out;
  552. }
  553. /* using write lock to protect desc->count */
  554. mutex_lock(&desc->wlock);
  555. if (!desc->count++) {
  556. desc->werr = 0;
  557. desc->rerr = 0;
  558. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  559. if (rv < 0) {
  560. desc->count--;
  561. dev_err(&desc->intf->dev,
  562. "Error submitting int urb - %d\n", rv);
  563. rv = usb_translate_errors(rv);
  564. }
  565. } else {
  566. rv = 0;
  567. }
  568. mutex_unlock(&desc->wlock);
  569. if (desc->count == 1)
  570. desc->manage_power(intf, 1);
  571. usb_autopm_put_interface(desc->intf);
  572. out:
  573. mutex_unlock(&wdm_mutex);
  574. return rv;
  575. }
  576. static int wdm_release(struct inode *inode, struct file *file)
  577. {
  578. struct wdm_device *desc = file->private_data;
  579. mutex_lock(&wdm_mutex);
  580. /* using write lock to protect desc->count */
  581. mutex_lock(&desc->wlock);
  582. desc->count--;
  583. mutex_unlock(&desc->wlock);
  584. if (!desc->count) {
  585. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  586. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  587. kill_urbs(desc);
  588. spin_lock_irq(&desc->iuspin);
  589. desc->resp_count = 0;
  590. spin_unlock_irq(&desc->iuspin);
  591. desc->manage_power(desc->intf, 0);
  592. } else {
  593. /* must avoid dev_printk here as desc->intf is invalid */
  594. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  595. cleanup(desc);
  596. }
  597. }
  598. mutex_unlock(&wdm_mutex);
  599. return 0;
  600. }
  601. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  602. {
  603. struct wdm_device *desc = file->private_data;
  604. int rv = 0;
  605. switch (cmd) {
  606. case IOCTL_WDM_MAX_COMMAND:
  607. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  608. rv = -EFAULT;
  609. break;
  610. default:
  611. rv = -ENOTTY;
  612. }
  613. return rv;
  614. }
  615. static const struct file_operations wdm_fops = {
  616. .owner = THIS_MODULE,
  617. .read = wdm_read,
  618. .write = wdm_write,
  619. .open = wdm_open,
  620. .flush = wdm_flush,
  621. .release = wdm_release,
  622. .poll = wdm_poll,
  623. .unlocked_ioctl = wdm_ioctl,
  624. .compat_ioctl = wdm_ioctl,
  625. .llseek = noop_llseek,
  626. };
  627. static struct usb_class_driver wdm_class = {
  628. .name = "cdc-wdm%d",
  629. .fops = &wdm_fops,
  630. .minor_base = WDM_MINOR_BASE,
  631. };
  632. /* --- error handling --- */
  633. static void wdm_rxwork(struct work_struct *work)
  634. {
  635. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  636. unsigned long flags;
  637. int rv = 0;
  638. int responding;
  639. spin_lock_irqsave(&desc->iuspin, flags);
  640. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  641. spin_unlock_irqrestore(&desc->iuspin, flags);
  642. } else {
  643. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  644. spin_unlock_irqrestore(&desc->iuspin, flags);
  645. if (!responding)
  646. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  647. if (rv < 0 && rv != -EPERM) {
  648. spin_lock_irqsave(&desc->iuspin, flags);
  649. clear_bit(WDM_RESPONDING, &desc->flags);
  650. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  651. schedule_work(&desc->rxwork);
  652. spin_unlock_irqrestore(&desc->iuspin, flags);
  653. }
  654. }
  655. }
  656. /* --- hotplug --- */
  657. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  658. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  659. {
  660. int rv = -ENOMEM;
  661. struct wdm_device *desc;
  662. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  663. if (!desc)
  664. goto out;
  665. INIT_LIST_HEAD(&desc->device_list);
  666. mutex_init(&desc->rlock);
  667. mutex_init(&desc->wlock);
  668. spin_lock_init(&desc->iuspin);
  669. init_waitqueue_head(&desc->wait);
  670. desc->wMaxCommand = bufsize;
  671. /* this will be expanded and needed in hardware endianness */
  672. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  673. desc->intf = intf;
  674. INIT_WORK(&desc->rxwork, wdm_rxwork);
  675. rv = -EINVAL;
  676. if (!usb_endpoint_is_int_in(ep))
  677. goto err;
  678. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  679. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  680. if (!desc->orq)
  681. goto err;
  682. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  683. if (!desc->irq)
  684. goto err;
  685. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  686. if (!desc->validity)
  687. goto err;
  688. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  689. if (!desc->response)
  690. goto err;
  691. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  692. if (!desc->command)
  693. goto err;
  694. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  695. if (!desc->ubuf)
  696. goto err;
  697. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  698. if (!desc->sbuf)
  699. goto err;
  700. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  701. if (!desc->inbuf)
  702. goto err;
  703. usb_fill_int_urb(
  704. desc->validity,
  705. interface_to_usbdev(intf),
  706. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  707. desc->sbuf,
  708. desc->wMaxPacketSize,
  709. wdm_int_callback,
  710. desc,
  711. ep->bInterval
  712. );
  713. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  714. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  715. desc->irq->wValue = 0;
  716. desc->irq->wIndex = desc->inum; /* already converted */
  717. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  718. usb_fill_control_urb(
  719. desc->response,
  720. interface_to_usbdev(intf),
  721. /* using common endpoint 0 */
  722. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  723. (unsigned char *)desc->irq,
  724. desc->inbuf,
  725. desc->wMaxCommand,
  726. wdm_in_callback,
  727. desc
  728. );
  729. desc->manage_power = manage_power;
  730. spin_lock(&wdm_device_list_lock);
  731. list_add(&desc->device_list, &wdm_device_list);
  732. spin_unlock(&wdm_device_list_lock);
  733. rv = usb_register_dev(intf, &wdm_class);
  734. if (rv < 0)
  735. goto err;
  736. else
  737. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  738. out:
  739. return rv;
  740. err:
  741. spin_lock(&wdm_device_list_lock);
  742. list_del(&desc->device_list);
  743. spin_unlock(&wdm_device_list_lock);
  744. cleanup(desc);
  745. return rv;
  746. }
  747. static int wdm_manage_power(struct usb_interface *intf, int on)
  748. {
  749. /* need autopm_get/put here to ensure the usbcore sees the new value */
  750. int rv = usb_autopm_get_interface(intf);
  751. intf->needs_remote_wakeup = on;
  752. if (!rv)
  753. usb_autopm_put_interface(intf);
  754. return 0;
  755. }
  756. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  757. {
  758. int rv = -EINVAL;
  759. struct usb_host_interface *iface;
  760. struct usb_endpoint_descriptor *ep;
  761. struct usb_cdc_dmm_desc *dmhd;
  762. u8 *buffer = intf->altsetting->extra;
  763. int buflen = intf->altsetting->extralen;
  764. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  765. if (!buffer)
  766. goto err;
  767. while (buflen > 2) {
  768. if (buffer[1] != USB_DT_CS_INTERFACE) {
  769. dev_err(&intf->dev, "skipping garbage\n");
  770. goto next_desc;
  771. }
  772. switch (buffer[2]) {
  773. case USB_CDC_HEADER_TYPE:
  774. break;
  775. case USB_CDC_DMM_TYPE:
  776. dmhd = (struct usb_cdc_dmm_desc *)buffer;
  777. maxcom = le16_to_cpu(dmhd->wMaxCommand);
  778. dev_dbg(&intf->dev,
  779. "Finding maximum buffer length: %d", maxcom);
  780. break;
  781. default:
  782. dev_err(&intf->dev,
  783. "Ignoring extra header, type %d, length %d\n",
  784. buffer[2], buffer[0]);
  785. break;
  786. }
  787. next_desc:
  788. buflen -= buffer[0];
  789. buffer += buffer[0];
  790. }
  791. iface = intf->cur_altsetting;
  792. if (iface->desc.bNumEndpoints != 1)
  793. goto err;
  794. ep = &iface->endpoint[0].desc;
  795. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  796. err:
  797. return rv;
  798. }
  799. /**
  800. * usb_cdc_wdm_register - register a WDM subdriver
  801. * @intf: usb interface the subdriver will associate with
  802. * @ep: interrupt endpoint to monitor for notifications
  803. * @bufsize: maximum message size to support for read/write
  804. *
  805. * Create WDM usb class character device and associate it with intf
  806. * without binding, allowing another driver to manage the interface.
  807. *
  808. * The subdriver will manage the given interrupt endpoint exclusively
  809. * and will issue control requests referring to the given intf. It
  810. * will otherwise avoid interferring, and in particular not do
  811. * usb_set_intfdata/usb_get_intfdata on intf.
  812. *
  813. * The return value is a pointer to the subdriver's struct usb_driver.
  814. * The registering driver is responsible for calling this subdriver's
  815. * disconnect, suspend, resume, pre_reset and post_reset methods from
  816. * its own.
  817. */
  818. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  819. struct usb_endpoint_descriptor *ep,
  820. int bufsize,
  821. int (*manage_power)(struct usb_interface *, int))
  822. {
  823. int rv = -EINVAL;
  824. rv = wdm_create(intf, ep, bufsize, manage_power);
  825. if (rv < 0)
  826. goto err;
  827. return &wdm_driver;
  828. err:
  829. return ERR_PTR(rv);
  830. }
  831. EXPORT_SYMBOL(usb_cdc_wdm_register);
  832. static void wdm_disconnect(struct usb_interface *intf)
  833. {
  834. struct wdm_device *desc;
  835. unsigned long flags;
  836. usb_deregister_dev(intf, &wdm_class);
  837. desc = wdm_find_device(intf);
  838. mutex_lock(&wdm_mutex);
  839. /* the spinlock makes sure no new urbs are generated in the callbacks */
  840. spin_lock_irqsave(&desc->iuspin, flags);
  841. set_bit(WDM_DISCONNECTING, &desc->flags);
  842. set_bit(WDM_READ, &desc->flags);
  843. /* to terminate pending flushes */
  844. clear_bit(WDM_IN_USE, &desc->flags);
  845. spin_unlock_irqrestore(&desc->iuspin, flags);
  846. wake_up_all(&desc->wait);
  847. mutex_lock(&desc->rlock);
  848. mutex_lock(&desc->wlock);
  849. kill_urbs(desc);
  850. cancel_work_sync(&desc->rxwork);
  851. mutex_unlock(&desc->wlock);
  852. mutex_unlock(&desc->rlock);
  853. /* the desc->intf pointer used as list key is now invalid */
  854. spin_lock(&wdm_device_list_lock);
  855. list_del(&desc->device_list);
  856. spin_unlock(&wdm_device_list_lock);
  857. if (!desc->count)
  858. cleanup(desc);
  859. else
  860. dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
  861. mutex_unlock(&wdm_mutex);
  862. }
  863. #ifdef CONFIG_PM
  864. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  865. {
  866. struct wdm_device *desc = wdm_find_device(intf);
  867. int rv = 0;
  868. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  869. /* if this is an autosuspend the caller does the locking */
  870. if (!PMSG_IS_AUTO(message)) {
  871. mutex_lock(&desc->rlock);
  872. mutex_lock(&desc->wlock);
  873. }
  874. spin_lock_irq(&desc->iuspin);
  875. if (PMSG_IS_AUTO(message) &&
  876. (test_bit(WDM_IN_USE, &desc->flags)
  877. || test_bit(WDM_RESPONDING, &desc->flags))) {
  878. spin_unlock_irq(&desc->iuspin);
  879. rv = -EBUSY;
  880. } else {
  881. set_bit(WDM_SUSPENDING, &desc->flags);
  882. spin_unlock_irq(&desc->iuspin);
  883. /* callback submits work - order is essential */
  884. kill_urbs(desc);
  885. cancel_work_sync(&desc->rxwork);
  886. }
  887. if (!PMSG_IS_AUTO(message)) {
  888. mutex_unlock(&desc->wlock);
  889. mutex_unlock(&desc->rlock);
  890. }
  891. return rv;
  892. }
  893. #endif
  894. static int recover_from_urb_loss(struct wdm_device *desc)
  895. {
  896. int rv = 0;
  897. if (desc->count) {
  898. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  899. if (rv < 0)
  900. dev_err(&desc->intf->dev,
  901. "Error resume submitting int urb - %d\n", rv);
  902. }
  903. return rv;
  904. }
  905. #ifdef CONFIG_PM
  906. static int wdm_resume(struct usb_interface *intf)
  907. {
  908. struct wdm_device *desc = wdm_find_device(intf);
  909. int rv;
  910. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  911. clear_bit(WDM_SUSPENDING, &desc->flags);
  912. rv = recover_from_urb_loss(desc);
  913. return rv;
  914. }
  915. #endif
  916. static int wdm_pre_reset(struct usb_interface *intf)
  917. {
  918. struct wdm_device *desc = wdm_find_device(intf);
  919. /*
  920. * we notify everybody using poll of
  921. * an exceptional situation
  922. * must be done before recovery lest a spontaneous
  923. * message from the device is lost
  924. */
  925. spin_lock_irq(&desc->iuspin);
  926. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  927. set_bit(WDM_READ, &desc->flags); /* unblock read */
  928. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  929. desc->rerr = -EINTR;
  930. spin_unlock_irq(&desc->iuspin);
  931. wake_up_all(&desc->wait);
  932. mutex_lock(&desc->rlock);
  933. mutex_lock(&desc->wlock);
  934. kill_urbs(desc);
  935. cancel_work_sync(&desc->rxwork);
  936. return 0;
  937. }
  938. static int wdm_post_reset(struct usb_interface *intf)
  939. {
  940. struct wdm_device *desc = wdm_find_device(intf);
  941. int rv;
  942. clear_bit(WDM_OVERFLOW, &desc->flags);
  943. clear_bit(WDM_RESETTING, &desc->flags);
  944. rv = recover_from_urb_loss(desc);
  945. mutex_unlock(&desc->wlock);
  946. mutex_unlock(&desc->rlock);
  947. return 0;
  948. }
  949. static struct usb_driver wdm_driver = {
  950. .name = "cdc_wdm",
  951. .probe = wdm_probe,
  952. .disconnect = wdm_disconnect,
  953. #ifdef CONFIG_PM
  954. .suspend = wdm_suspend,
  955. .resume = wdm_resume,
  956. .reset_resume = wdm_resume,
  957. #endif
  958. .pre_reset = wdm_pre_reset,
  959. .post_reset = wdm_post_reset,
  960. .id_table = wdm_ids,
  961. .supports_autosuspend = 1,
  962. .disable_hub_initiated_lpm = 1,
  963. };
  964. module_usb_driver(wdm_driver);
  965. MODULE_AUTHOR(DRIVER_AUTHOR);
  966. MODULE_DESCRIPTION(DRIVER_DESC);
  967. MODULE_LICENSE("GPL");