cdc-wdm.c 25 KB

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