cdc-wdm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. dr->wIndex, 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, dr->wIndex, dr->wLength);
  232. goto exit;
  233. }
  234. spin_lock(&desc->iuspin);
  235. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  236. if (!desc->resp_count++ && !responding
  237. && !test_bit(WDM_DISCONNECTING, &desc->flags)
  238. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  239. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  240. dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
  241. __func__, rv);
  242. }
  243. spin_unlock(&desc->iuspin);
  244. if (rv < 0) {
  245. clear_bit(WDM_RESPONDING, &desc->flags);
  246. if (rv == -EPERM)
  247. return;
  248. if (rv == -ENOMEM) {
  249. sw:
  250. rv = schedule_work(&desc->rxwork);
  251. if (rv)
  252. dev_err(&desc->intf->dev,
  253. "Cannot schedule work\n");
  254. }
  255. }
  256. exit:
  257. rv = usb_submit_urb(urb, GFP_ATOMIC);
  258. if (rv)
  259. dev_err(&desc->intf->dev,
  260. "%s - usb_submit_urb failed with result %d\n",
  261. __func__, rv);
  262. }
  263. static void kill_urbs(struct wdm_device *desc)
  264. {
  265. /* the order here is essential */
  266. usb_kill_urb(desc->command);
  267. usb_kill_urb(desc->validity);
  268. usb_kill_urb(desc->response);
  269. }
  270. static void free_urbs(struct wdm_device *desc)
  271. {
  272. usb_free_urb(desc->validity);
  273. usb_free_urb(desc->response);
  274. usb_free_urb(desc->command);
  275. }
  276. static void cleanup(struct wdm_device *desc)
  277. {
  278. kfree(desc->sbuf);
  279. kfree(desc->inbuf);
  280. kfree(desc->orq);
  281. kfree(desc->irq);
  282. kfree(desc->ubuf);
  283. free_urbs(desc);
  284. kfree(desc);
  285. }
  286. static ssize_t wdm_write
  287. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  288. {
  289. u8 *buf;
  290. int rv = -EMSGSIZE, r, we;
  291. struct wdm_device *desc = file->private_data;
  292. struct usb_ctrlrequest *req;
  293. if (count > desc->wMaxCommand)
  294. count = desc->wMaxCommand;
  295. spin_lock_irq(&desc->iuspin);
  296. we = desc->werr;
  297. desc->werr = 0;
  298. spin_unlock_irq(&desc->iuspin);
  299. if (we < 0)
  300. return -EIO;
  301. buf = kmalloc(count, GFP_KERNEL);
  302. if (!buf) {
  303. rv = -ENOMEM;
  304. goto outnl;
  305. }
  306. r = copy_from_user(buf, buffer, count);
  307. if (r > 0) {
  308. kfree(buf);
  309. rv = -EFAULT;
  310. goto outnl;
  311. }
  312. /* concurrent writes and disconnect */
  313. r = mutex_lock_interruptible(&desc->wlock);
  314. rv = -ERESTARTSYS;
  315. if (r) {
  316. kfree(buf);
  317. goto outnl;
  318. }
  319. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  320. kfree(buf);
  321. rv = -ENODEV;
  322. goto outnp;
  323. }
  324. r = usb_autopm_get_interface(desc->intf);
  325. if (r < 0) {
  326. kfree(buf);
  327. rv = usb_translate_errors(r);
  328. goto outnp;
  329. }
  330. if (!(file->f_flags & O_NONBLOCK))
  331. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  332. &desc->flags));
  333. else
  334. if (test_bit(WDM_IN_USE, &desc->flags))
  335. r = -EAGAIN;
  336. if (test_bit(WDM_RESETTING, &desc->flags))
  337. r = -EIO;
  338. if (r < 0) {
  339. kfree(buf);
  340. rv = r;
  341. goto out;
  342. }
  343. req = desc->orq;
  344. usb_fill_control_urb(
  345. desc->command,
  346. interface_to_usbdev(desc->intf),
  347. /* using common endpoint 0 */
  348. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  349. (unsigned char *)req,
  350. buf,
  351. count,
  352. wdm_out_callback,
  353. desc
  354. );
  355. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  356. USB_RECIP_INTERFACE);
  357. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  358. req->wValue = 0;
  359. req->wIndex = desc->inum;
  360. req->wLength = cpu_to_le16(count);
  361. set_bit(WDM_IN_USE, &desc->flags);
  362. desc->outbuf = buf;
  363. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  364. if (rv < 0) {
  365. kfree(buf);
  366. desc->outbuf = NULL;
  367. clear_bit(WDM_IN_USE, &desc->flags);
  368. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  369. rv = usb_translate_errors(rv);
  370. } else {
  371. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
  372. req->wIndex);
  373. }
  374. out:
  375. usb_autopm_put_interface(desc->intf);
  376. outnp:
  377. mutex_unlock(&desc->wlock);
  378. outnl:
  379. return rv < 0 ? rv : count;
  380. }
  381. /*
  382. * clear WDM_READ flag and possibly submit the read urb if resp_count
  383. * is non-zero.
  384. *
  385. * Called with desc->iuspin locked
  386. */
  387. static int clear_wdm_read_flag(struct wdm_device *desc)
  388. {
  389. int rv = 0;
  390. clear_bit(WDM_READ, &desc->flags);
  391. /* submit read urb only if the device is waiting for it */
  392. if (!desc->resp_count || !--desc->resp_count)
  393. goto out;
  394. set_bit(WDM_RESPONDING, &desc->flags);
  395. spin_unlock_irq(&desc->iuspin);
  396. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  397. spin_lock_irq(&desc->iuspin);
  398. if (rv) {
  399. dev_err(&desc->intf->dev,
  400. "usb_submit_urb failed with result %d\n", rv);
  401. /* make sure the next notification trigger a submit */
  402. clear_bit(WDM_RESPONDING, &desc->flags);
  403. desc->resp_count = 0;
  404. }
  405. out:
  406. return rv;
  407. }
  408. static ssize_t wdm_read
  409. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  410. {
  411. int rv, cntr;
  412. int i = 0;
  413. struct wdm_device *desc = file->private_data;
  414. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  415. if (rv < 0)
  416. return -ERESTARTSYS;
  417. cntr = ACCESS_ONCE(desc->length);
  418. if (cntr == 0) {
  419. desc->read = 0;
  420. retry:
  421. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  422. rv = -ENODEV;
  423. goto err;
  424. }
  425. if (test_bit(WDM_OVERFLOW, &desc->flags)) {
  426. clear_bit(WDM_OVERFLOW, &desc->flags);
  427. rv = -ENOBUFS;
  428. goto err;
  429. }
  430. i++;
  431. if (file->f_flags & O_NONBLOCK) {
  432. if (!test_bit(WDM_READ, &desc->flags)) {
  433. rv = cntr ? cntr : -EAGAIN;
  434. goto err;
  435. }
  436. rv = 0;
  437. } else {
  438. rv = wait_event_interruptible(desc->wait,
  439. test_bit(WDM_READ, &desc->flags));
  440. }
  441. /* may have happened while we slept */
  442. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  443. rv = -ENODEV;
  444. goto err;
  445. }
  446. if (test_bit(WDM_RESETTING, &desc->flags)) {
  447. rv = -EIO;
  448. goto err;
  449. }
  450. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  451. if (rv < 0) {
  452. rv = -ERESTARTSYS;
  453. goto err;
  454. }
  455. spin_lock_irq(&desc->iuspin);
  456. if (desc->rerr) { /* read completed, error happened */
  457. desc->rerr = 0;
  458. spin_unlock_irq(&desc->iuspin);
  459. rv = -EIO;
  460. goto err;
  461. }
  462. /*
  463. * recheck whether we've lost the race
  464. * against the completion handler
  465. */
  466. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  467. spin_unlock_irq(&desc->iuspin);
  468. goto retry;
  469. }
  470. if (!desc->reslength) { /* zero length read */
  471. dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
  472. rv = clear_wdm_read_flag(desc);
  473. spin_unlock_irq(&desc->iuspin);
  474. if (rv < 0)
  475. goto err;
  476. goto retry;
  477. }
  478. cntr = desc->length;
  479. spin_unlock_irq(&desc->iuspin);
  480. }
  481. if (cntr > count)
  482. cntr = count;
  483. rv = copy_to_user(buffer, desc->ubuf, cntr);
  484. if (rv > 0) {
  485. rv = -EFAULT;
  486. goto err;
  487. }
  488. spin_lock_irq(&desc->iuspin);
  489. for (i = 0; i < desc->length - cntr; i++)
  490. desc->ubuf[i] = desc->ubuf[i + cntr];
  491. desc->length -= cntr;
  492. /* in case we had outstanding data */
  493. if (!desc->length)
  494. clear_wdm_read_flag(desc);
  495. spin_unlock_irq(&desc->iuspin);
  496. rv = cntr;
  497. err:
  498. mutex_unlock(&desc->rlock);
  499. return rv;
  500. }
  501. static int wdm_flush(struct file *file, fl_owner_t id)
  502. {
  503. struct wdm_device *desc = file->private_data;
  504. wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
  505. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  506. if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
  507. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  508. desc->werr);
  509. return usb_translate_errors(desc->werr);
  510. }
  511. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  512. {
  513. struct wdm_device *desc = file->private_data;
  514. unsigned long flags;
  515. unsigned int mask = 0;
  516. spin_lock_irqsave(&desc->iuspin, flags);
  517. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  518. mask = POLLHUP | POLLERR;
  519. spin_unlock_irqrestore(&desc->iuspin, flags);
  520. goto desc_out;
  521. }
  522. if (test_bit(WDM_READ, &desc->flags))
  523. mask = POLLIN | POLLRDNORM;
  524. if (desc->rerr || desc->werr)
  525. mask |= POLLERR;
  526. if (!test_bit(WDM_IN_USE, &desc->flags))
  527. mask |= POLLOUT | POLLWRNORM;
  528. spin_unlock_irqrestore(&desc->iuspin, flags);
  529. poll_wait(file, &desc->wait, wait);
  530. desc_out:
  531. return mask;
  532. }
  533. static int wdm_open(struct inode *inode, struct file *file)
  534. {
  535. int minor = iminor(inode);
  536. int rv = -ENODEV;
  537. struct usb_interface *intf;
  538. struct wdm_device *desc;
  539. mutex_lock(&wdm_mutex);
  540. desc = wdm_find_device_by_minor(minor);
  541. if (!desc)
  542. goto out;
  543. intf = desc->intf;
  544. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  545. goto out;
  546. file->private_data = desc;
  547. rv = usb_autopm_get_interface(desc->intf);
  548. if (rv < 0) {
  549. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  550. goto out;
  551. }
  552. /* using write lock to protect desc->count */
  553. mutex_lock(&desc->wlock);
  554. if (!desc->count++) {
  555. desc->werr = 0;
  556. desc->rerr = 0;
  557. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  558. if (rv < 0) {
  559. desc->count--;
  560. dev_err(&desc->intf->dev,
  561. "Error submitting int urb - %d\n", rv);
  562. rv = usb_translate_errors(rv);
  563. }
  564. } else {
  565. rv = 0;
  566. }
  567. mutex_unlock(&desc->wlock);
  568. if (desc->count == 1)
  569. desc->manage_power(intf, 1);
  570. usb_autopm_put_interface(desc->intf);
  571. out:
  572. mutex_unlock(&wdm_mutex);
  573. return rv;
  574. }
  575. static int wdm_release(struct inode *inode, struct file *file)
  576. {
  577. struct wdm_device *desc = file->private_data;
  578. mutex_lock(&wdm_mutex);
  579. /* using write lock to protect desc->count */
  580. mutex_lock(&desc->wlock);
  581. desc->count--;
  582. mutex_unlock(&desc->wlock);
  583. if (!desc->count) {
  584. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  585. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  586. kill_urbs(desc);
  587. spin_lock_irq(&desc->iuspin);
  588. desc->resp_count = 0;
  589. spin_unlock_irq(&desc->iuspin);
  590. desc->manage_power(desc->intf, 0);
  591. } else {
  592. /* must avoid dev_printk here as desc->intf is invalid */
  593. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  594. cleanup(desc);
  595. }
  596. }
  597. mutex_unlock(&wdm_mutex);
  598. return 0;
  599. }
  600. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  601. {
  602. struct wdm_device *desc = file->private_data;
  603. int rv = 0;
  604. switch (cmd) {
  605. case IOCTL_WDM_MAX_COMMAND:
  606. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  607. rv = -EFAULT;
  608. break;
  609. default:
  610. rv = -ENOTTY;
  611. }
  612. return rv;
  613. }
  614. static const struct file_operations wdm_fops = {
  615. .owner = THIS_MODULE,
  616. .read = wdm_read,
  617. .write = wdm_write,
  618. .open = wdm_open,
  619. .flush = wdm_flush,
  620. .release = wdm_release,
  621. .poll = wdm_poll,
  622. .unlocked_ioctl = wdm_ioctl,
  623. .compat_ioctl = wdm_ioctl,
  624. .llseek = noop_llseek,
  625. };
  626. static struct usb_class_driver wdm_class = {
  627. .name = "cdc-wdm%d",
  628. .fops = &wdm_fops,
  629. .minor_base = WDM_MINOR_BASE,
  630. };
  631. /* --- error handling --- */
  632. static void wdm_rxwork(struct work_struct *work)
  633. {
  634. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  635. unsigned long flags;
  636. int rv = 0;
  637. int responding;
  638. spin_lock_irqsave(&desc->iuspin, flags);
  639. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  640. spin_unlock_irqrestore(&desc->iuspin, flags);
  641. } else {
  642. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  643. spin_unlock_irqrestore(&desc->iuspin, flags);
  644. if (!responding)
  645. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  646. if (rv < 0 && rv != -EPERM) {
  647. spin_lock_irqsave(&desc->iuspin, flags);
  648. clear_bit(WDM_RESPONDING, &desc->flags);
  649. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  650. schedule_work(&desc->rxwork);
  651. spin_unlock_irqrestore(&desc->iuspin, flags);
  652. }
  653. }
  654. }
  655. /* --- hotplug --- */
  656. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  657. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  658. {
  659. int rv = -ENOMEM;
  660. struct wdm_device *desc;
  661. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  662. if (!desc)
  663. goto out;
  664. INIT_LIST_HEAD(&desc->device_list);
  665. mutex_init(&desc->rlock);
  666. mutex_init(&desc->wlock);
  667. spin_lock_init(&desc->iuspin);
  668. init_waitqueue_head(&desc->wait);
  669. desc->wMaxCommand = bufsize;
  670. /* this will be expanded and needed in hardware endianness */
  671. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  672. desc->intf = intf;
  673. INIT_WORK(&desc->rxwork, wdm_rxwork);
  674. rv = -EINVAL;
  675. if (!usb_endpoint_is_int_in(ep))
  676. goto err;
  677. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  678. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  679. if (!desc->orq)
  680. goto err;
  681. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  682. if (!desc->irq)
  683. goto err;
  684. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  685. if (!desc->validity)
  686. goto err;
  687. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  688. if (!desc->response)
  689. goto err;
  690. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  691. if (!desc->command)
  692. goto err;
  693. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  694. if (!desc->ubuf)
  695. goto err;
  696. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  697. if (!desc->sbuf)
  698. goto err;
  699. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  700. if (!desc->inbuf)
  701. goto err;
  702. usb_fill_int_urb(
  703. desc->validity,
  704. interface_to_usbdev(intf),
  705. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  706. desc->sbuf,
  707. desc->wMaxPacketSize,
  708. wdm_int_callback,
  709. desc,
  710. ep->bInterval
  711. );
  712. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  713. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  714. desc->irq->wValue = 0;
  715. desc->irq->wIndex = desc->inum;
  716. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  717. usb_fill_control_urb(
  718. desc->response,
  719. interface_to_usbdev(intf),
  720. /* using common endpoint 0 */
  721. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  722. (unsigned char *)desc->irq,
  723. desc->inbuf,
  724. desc->wMaxCommand,
  725. wdm_in_callback,
  726. desc
  727. );
  728. desc->manage_power = manage_power;
  729. spin_lock(&wdm_device_list_lock);
  730. list_add(&desc->device_list, &wdm_device_list);
  731. spin_unlock(&wdm_device_list_lock);
  732. rv = usb_register_dev(intf, &wdm_class);
  733. if (rv < 0)
  734. goto err;
  735. else
  736. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  737. out:
  738. return rv;
  739. err:
  740. spin_lock(&wdm_device_list_lock);
  741. list_del(&desc->device_list);
  742. spin_unlock(&wdm_device_list_lock);
  743. cleanup(desc);
  744. return rv;
  745. }
  746. static int wdm_manage_power(struct usb_interface *intf, int on)
  747. {
  748. /* need autopm_get/put here to ensure the usbcore sees the new value */
  749. int rv = usb_autopm_get_interface(intf);
  750. intf->needs_remote_wakeup = on;
  751. if (!rv)
  752. usb_autopm_put_interface(intf);
  753. return 0;
  754. }
  755. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  756. {
  757. int rv = -EINVAL;
  758. struct usb_host_interface *iface;
  759. struct usb_endpoint_descriptor *ep;
  760. struct usb_cdc_dmm_desc *dmhd;
  761. u8 *buffer = intf->altsetting->extra;
  762. int buflen = intf->altsetting->extralen;
  763. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  764. if (!buffer)
  765. goto err;
  766. while (buflen > 2) {
  767. if (buffer[1] != USB_DT_CS_INTERFACE) {
  768. dev_err(&intf->dev, "skipping garbage\n");
  769. goto next_desc;
  770. }
  771. switch (buffer[2]) {
  772. case USB_CDC_HEADER_TYPE:
  773. break;
  774. case USB_CDC_DMM_TYPE:
  775. dmhd = (struct usb_cdc_dmm_desc *)buffer;
  776. maxcom = le16_to_cpu(dmhd->wMaxCommand);
  777. dev_dbg(&intf->dev,
  778. "Finding maximum buffer length: %d", maxcom);
  779. break;
  780. default:
  781. dev_err(&intf->dev,
  782. "Ignoring extra header, type %d, length %d\n",
  783. buffer[2], buffer[0]);
  784. break;
  785. }
  786. next_desc:
  787. buflen -= buffer[0];
  788. buffer += buffer[0];
  789. }
  790. iface = intf->cur_altsetting;
  791. if (iface->desc.bNumEndpoints != 1)
  792. goto err;
  793. ep = &iface->endpoint[0].desc;
  794. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  795. err:
  796. return rv;
  797. }
  798. /**
  799. * usb_cdc_wdm_register - register a WDM subdriver
  800. * @intf: usb interface the subdriver will associate with
  801. * @ep: interrupt endpoint to monitor for notifications
  802. * @bufsize: maximum message size to support for read/write
  803. *
  804. * Create WDM usb class character device and associate it with intf
  805. * without binding, allowing another driver to manage the interface.
  806. *
  807. * The subdriver will manage the given interrupt endpoint exclusively
  808. * and will issue control requests referring to the given intf. It
  809. * will otherwise avoid interferring, and in particular not do
  810. * usb_set_intfdata/usb_get_intfdata on intf.
  811. *
  812. * The return value is a pointer to the subdriver's struct usb_driver.
  813. * The registering driver is responsible for calling this subdriver's
  814. * disconnect, suspend, resume, pre_reset and post_reset methods from
  815. * its own.
  816. */
  817. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  818. struct usb_endpoint_descriptor *ep,
  819. int bufsize,
  820. int (*manage_power)(struct usb_interface *, int))
  821. {
  822. int rv = -EINVAL;
  823. rv = wdm_create(intf, ep, bufsize, manage_power);
  824. if (rv < 0)
  825. goto err;
  826. return &wdm_driver;
  827. err:
  828. return ERR_PTR(rv);
  829. }
  830. EXPORT_SYMBOL(usb_cdc_wdm_register);
  831. static void wdm_disconnect(struct usb_interface *intf)
  832. {
  833. struct wdm_device *desc;
  834. unsigned long flags;
  835. usb_deregister_dev(intf, &wdm_class);
  836. desc = wdm_find_device(intf);
  837. mutex_lock(&wdm_mutex);
  838. /* the spinlock makes sure no new urbs are generated in the callbacks */
  839. spin_lock_irqsave(&desc->iuspin, flags);
  840. set_bit(WDM_DISCONNECTING, &desc->flags);
  841. set_bit(WDM_READ, &desc->flags);
  842. /* to terminate pending flushes */
  843. clear_bit(WDM_IN_USE, &desc->flags);
  844. spin_unlock_irqrestore(&desc->iuspin, flags);
  845. wake_up_all(&desc->wait);
  846. mutex_lock(&desc->rlock);
  847. mutex_lock(&desc->wlock);
  848. kill_urbs(desc);
  849. cancel_work_sync(&desc->rxwork);
  850. mutex_unlock(&desc->wlock);
  851. mutex_unlock(&desc->rlock);
  852. /* the desc->intf pointer used as list key is now invalid */
  853. spin_lock(&wdm_device_list_lock);
  854. list_del(&desc->device_list);
  855. spin_unlock(&wdm_device_list_lock);
  856. if (!desc->count)
  857. cleanup(desc);
  858. else
  859. dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
  860. mutex_unlock(&wdm_mutex);
  861. }
  862. #ifdef CONFIG_PM
  863. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  864. {
  865. struct wdm_device *desc = wdm_find_device(intf);
  866. int rv = 0;
  867. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  868. /* if this is an autosuspend the caller does the locking */
  869. if (!PMSG_IS_AUTO(message)) {
  870. mutex_lock(&desc->rlock);
  871. mutex_lock(&desc->wlock);
  872. }
  873. spin_lock_irq(&desc->iuspin);
  874. if (PMSG_IS_AUTO(message) &&
  875. (test_bit(WDM_IN_USE, &desc->flags)
  876. || test_bit(WDM_RESPONDING, &desc->flags))) {
  877. spin_unlock_irq(&desc->iuspin);
  878. rv = -EBUSY;
  879. } else {
  880. set_bit(WDM_SUSPENDING, &desc->flags);
  881. spin_unlock_irq(&desc->iuspin);
  882. /* callback submits work - order is essential */
  883. kill_urbs(desc);
  884. cancel_work_sync(&desc->rxwork);
  885. }
  886. if (!PMSG_IS_AUTO(message)) {
  887. mutex_unlock(&desc->wlock);
  888. mutex_unlock(&desc->rlock);
  889. }
  890. return rv;
  891. }
  892. #endif
  893. static int recover_from_urb_loss(struct wdm_device *desc)
  894. {
  895. int rv = 0;
  896. if (desc->count) {
  897. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  898. if (rv < 0)
  899. dev_err(&desc->intf->dev,
  900. "Error resume submitting int urb - %d\n", rv);
  901. }
  902. return rv;
  903. }
  904. #ifdef CONFIG_PM
  905. static int wdm_resume(struct usb_interface *intf)
  906. {
  907. struct wdm_device *desc = wdm_find_device(intf);
  908. int rv;
  909. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  910. clear_bit(WDM_SUSPENDING, &desc->flags);
  911. rv = recover_from_urb_loss(desc);
  912. return rv;
  913. }
  914. #endif
  915. static int wdm_pre_reset(struct usb_interface *intf)
  916. {
  917. struct wdm_device *desc = wdm_find_device(intf);
  918. /*
  919. * we notify everybody using poll of
  920. * an exceptional situation
  921. * must be done before recovery lest a spontaneous
  922. * message from the device is lost
  923. */
  924. spin_lock_irq(&desc->iuspin);
  925. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  926. set_bit(WDM_READ, &desc->flags); /* unblock read */
  927. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  928. desc->rerr = -EINTR;
  929. spin_unlock_irq(&desc->iuspin);
  930. wake_up_all(&desc->wait);
  931. mutex_lock(&desc->rlock);
  932. mutex_lock(&desc->wlock);
  933. kill_urbs(desc);
  934. cancel_work_sync(&desc->rxwork);
  935. return 0;
  936. }
  937. static int wdm_post_reset(struct usb_interface *intf)
  938. {
  939. struct wdm_device *desc = wdm_find_device(intf);
  940. int rv;
  941. clear_bit(WDM_OVERFLOW, &desc->flags);
  942. clear_bit(WDM_RESETTING, &desc->flags);
  943. rv = recover_from_urb_loss(desc);
  944. mutex_unlock(&desc->wlock);
  945. mutex_unlock(&desc->rlock);
  946. return 0;
  947. }
  948. static struct usb_driver wdm_driver = {
  949. .name = "cdc_wdm",
  950. .probe = wdm_probe,
  951. .disconnect = wdm_disconnect,
  952. #ifdef CONFIG_PM
  953. .suspend = wdm_suspend,
  954. .resume = wdm_resume,
  955. .reset_resume = wdm_resume,
  956. #endif
  957. .pre_reset = wdm_pre_reset,
  958. .post_reset = wdm_post_reset,
  959. .id_table = wdm_ids,
  960. .supports_autosuspend = 1,
  961. .disable_hub_initiated_lpm = 1,
  962. };
  963. module_usb_driver(wdm_driver);
  964. MODULE_AUTHOR(DRIVER_AUTHOR);
  965. MODULE_DESCRIPTION(DRIVER_DESC);
  966. MODULE_LICENSE("GPL");