cdc-wdm.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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,
  493. /*
  494. * needs both flags. We cannot do with one
  495. * because resetting it would cause a race
  496. * with write() yet we need to signal
  497. * a disconnect
  498. */
  499. !test_bit(WDM_IN_USE, &desc->flags) ||
  500. test_bit(WDM_DISCONNECTING, &desc->flags));
  501. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  502. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  503. return -ENODEV;
  504. if (desc->werr < 0)
  505. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  506. desc->werr);
  507. return usb_translate_errors(desc->werr);
  508. }
  509. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  510. {
  511. struct wdm_device *desc = file->private_data;
  512. unsigned long flags;
  513. unsigned int mask = 0;
  514. spin_lock_irqsave(&desc->iuspin, flags);
  515. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  516. mask = POLLHUP | POLLERR;
  517. spin_unlock_irqrestore(&desc->iuspin, flags);
  518. goto desc_out;
  519. }
  520. if (test_bit(WDM_READ, &desc->flags))
  521. mask = POLLIN | POLLRDNORM;
  522. if (desc->rerr || desc->werr)
  523. mask |= POLLERR;
  524. if (!test_bit(WDM_IN_USE, &desc->flags))
  525. mask |= POLLOUT | POLLWRNORM;
  526. spin_unlock_irqrestore(&desc->iuspin, flags);
  527. poll_wait(file, &desc->wait, wait);
  528. desc_out:
  529. return mask;
  530. }
  531. static int wdm_open(struct inode *inode, struct file *file)
  532. {
  533. int minor = iminor(inode);
  534. int rv = -ENODEV;
  535. struct usb_interface *intf;
  536. struct wdm_device *desc;
  537. mutex_lock(&wdm_mutex);
  538. desc = wdm_find_device_by_minor(minor);
  539. if (!desc)
  540. goto out;
  541. intf = desc->intf;
  542. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  543. goto out;
  544. file->private_data = desc;
  545. rv = usb_autopm_get_interface(desc->intf);
  546. if (rv < 0) {
  547. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  548. goto out;
  549. }
  550. /* using write lock to protect desc->count */
  551. mutex_lock(&desc->wlock);
  552. if (!desc->count++) {
  553. desc->werr = 0;
  554. desc->rerr = 0;
  555. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  556. if (rv < 0) {
  557. desc->count--;
  558. dev_err(&desc->intf->dev,
  559. "Error submitting int urb - %d\n", rv);
  560. }
  561. } else {
  562. rv = 0;
  563. }
  564. mutex_unlock(&desc->wlock);
  565. if (desc->count == 1)
  566. desc->manage_power(intf, 1);
  567. usb_autopm_put_interface(desc->intf);
  568. out:
  569. mutex_unlock(&wdm_mutex);
  570. return rv;
  571. }
  572. static int wdm_release(struct inode *inode, struct file *file)
  573. {
  574. struct wdm_device *desc = file->private_data;
  575. mutex_lock(&wdm_mutex);
  576. /* using write lock to protect desc->count */
  577. mutex_lock(&desc->wlock);
  578. desc->count--;
  579. mutex_unlock(&desc->wlock);
  580. if (!desc->count) {
  581. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  582. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  583. kill_urbs(desc);
  584. desc->manage_power(desc->intf, 0);
  585. } else {
  586. /* must avoid dev_printk here as desc->intf is invalid */
  587. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  588. cleanup(desc);
  589. }
  590. }
  591. mutex_unlock(&wdm_mutex);
  592. return 0;
  593. }
  594. static const struct file_operations wdm_fops = {
  595. .owner = THIS_MODULE,
  596. .read = wdm_read,
  597. .write = wdm_write,
  598. .open = wdm_open,
  599. .flush = wdm_flush,
  600. .release = wdm_release,
  601. .poll = wdm_poll,
  602. .llseek = noop_llseek,
  603. };
  604. static struct usb_class_driver wdm_class = {
  605. .name = "cdc-wdm%d",
  606. .fops = &wdm_fops,
  607. .minor_base = WDM_MINOR_BASE,
  608. };
  609. /* --- error handling --- */
  610. static void wdm_rxwork(struct work_struct *work)
  611. {
  612. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  613. unsigned long flags;
  614. int rv = 0;
  615. int responding;
  616. spin_lock_irqsave(&desc->iuspin, flags);
  617. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  618. spin_unlock_irqrestore(&desc->iuspin, flags);
  619. } else {
  620. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  621. spin_unlock_irqrestore(&desc->iuspin, flags);
  622. if (!responding)
  623. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  624. if (rv < 0 && rv != -EPERM) {
  625. spin_lock_irqsave(&desc->iuspin, flags);
  626. clear_bit(WDM_RESPONDING, &desc->flags);
  627. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  628. schedule_work(&desc->rxwork);
  629. spin_unlock_irqrestore(&desc->iuspin, flags);
  630. }
  631. }
  632. }
  633. /* --- hotplug --- */
  634. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  635. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  636. {
  637. int rv = -ENOMEM;
  638. struct wdm_device *desc;
  639. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  640. if (!desc)
  641. goto out;
  642. INIT_LIST_HEAD(&desc->device_list);
  643. mutex_init(&desc->rlock);
  644. mutex_init(&desc->wlock);
  645. spin_lock_init(&desc->iuspin);
  646. init_waitqueue_head(&desc->wait);
  647. desc->wMaxCommand = bufsize;
  648. /* this will be expanded and needed in hardware endianness */
  649. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  650. desc->intf = intf;
  651. INIT_WORK(&desc->rxwork, wdm_rxwork);
  652. rv = -EINVAL;
  653. if (!usb_endpoint_is_int_in(ep))
  654. goto err;
  655. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  656. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  657. if (!desc->orq)
  658. goto err;
  659. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  660. if (!desc->irq)
  661. goto err;
  662. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  663. if (!desc->validity)
  664. goto err;
  665. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  666. if (!desc->response)
  667. goto err;
  668. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  669. if (!desc->command)
  670. goto err;
  671. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  672. if (!desc->ubuf)
  673. goto err;
  674. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  675. if (!desc->sbuf)
  676. goto err;
  677. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  678. if (!desc->inbuf)
  679. goto err;
  680. usb_fill_int_urb(
  681. desc->validity,
  682. interface_to_usbdev(intf),
  683. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  684. desc->sbuf,
  685. desc->wMaxPacketSize,
  686. wdm_int_callback,
  687. desc,
  688. ep->bInterval
  689. );
  690. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  691. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  692. desc->irq->wValue = 0;
  693. desc->irq->wIndex = desc->inum; /* already converted */
  694. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  695. usb_fill_control_urb(
  696. desc->response,
  697. interface_to_usbdev(intf),
  698. /* using common endpoint 0 */
  699. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  700. (unsigned char *)desc->irq,
  701. desc->inbuf,
  702. desc->wMaxCommand,
  703. wdm_in_callback,
  704. desc
  705. );
  706. desc->manage_power = manage_power;
  707. spin_lock(&wdm_device_list_lock);
  708. list_add(&desc->device_list, &wdm_device_list);
  709. spin_unlock(&wdm_device_list_lock);
  710. rv = usb_register_dev(intf, &wdm_class);
  711. if (rv < 0)
  712. goto err;
  713. else
  714. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  715. out:
  716. return rv;
  717. err:
  718. spin_lock(&wdm_device_list_lock);
  719. list_del(&desc->device_list);
  720. spin_unlock(&wdm_device_list_lock);
  721. cleanup(desc);
  722. return rv;
  723. }
  724. static int wdm_manage_power(struct usb_interface *intf, int on)
  725. {
  726. /* need autopm_get/put here to ensure the usbcore sees the new value */
  727. int rv = usb_autopm_get_interface(intf);
  728. intf->needs_remote_wakeup = on;
  729. if (!rv)
  730. usb_autopm_put_interface(intf);
  731. return 0;
  732. }
  733. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  734. {
  735. int rv = -EINVAL;
  736. struct usb_host_interface *iface;
  737. struct usb_endpoint_descriptor *ep;
  738. struct usb_cdc_dmm_desc *dmhd;
  739. u8 *buffer = intf->altsetting->extra;
  740. int buflen = intf->altsetting->extralen;
  741. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  742. if (!buffer)
  743. goto err;
  744. while (buflen > 0) {
  745. if ((buflen < buffer[0]) || (buffer[0] < 3)) {
  746. dev_err(&intf->dev, "invalid descriptor buffer length\n");
  747. goto err;
  748. }
  749. if (buffer[1] != USB_DT_CS_INTERFACE) {
  750. dev_err(&intf->dev, "skipping garbage\n");
  751. goto next_desc;
  752. }
  753. switch (buffer[2]) {
  754. case USB_CDC_HEADER_TYPE:
  755. break;
  756. case USB_CDC_DMM_TYPE:
  757. dmhd = (struct usb_cdc_dmm_desc *)buffer;
  758. maxcom = le16_to_cpu(dmhd->wMaxCommand);
  759. dev_dbg(&intf->dev,
  760. "Finding maximum buffer length: %d", maxcom);
  761. break;
  762. default:
  763. dev_err(&intf->dev,
  764. "Ignoring extra header, type %d, length %d\n",
  765. buffer[2], buffer[0]);
  766. break;
  767. }
  768. next_desc:
  769. buflen -= buffer[0];
  770. buffer += buffer[0];
  771. }
  772. iface = intf->cur_altsetting;
  773. if (iface->desc.bNumEndpoints != 1)
  774. goto err;
  775. ep = &iface->endpoint[0].desc;
  776. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  777. err:
  778. return rv;
  779. }
  780. /**
  781. * usb_cdc_wdm_register - register a WDM subdriver
  782. * @intf: usb interface the subdriver will associate with
  783. * @ep: interrupt endpoint to monitor for notifications
  784. * @bufsize: maximum message size to support for read/write
  785. *
  786. * Create WDM usb class character device and associate it with intf
  787. * without binding, allowing another driver to manage the interface.
  788. *
  789. * The subdriver will manage the given interrupt endpoint exclusively
  790. * and will issue control requests referring to the given intf. It
  791. * will otherwise avoid interferring, and in particular not do
  792. * usb_set_intfdata/usb_get_intfdata on intf.
  793. *
  794. * The return value is a pointer to the subdriver's struct usb_driver.
  795. * The registering driver is responsible for calling this subdriver's
  796. * disconnect, suspend, resume, pre_reset and post_reset methods from
  797. * its own.
  798. */
  799. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  800. struct usb_endpoint_descriptor *ep,
  801. int bufsize,
  802. int (*manage_power)(struct usb_interface *, int))
  803. {
  804. int rv = -EINVAL;
  805. rv = wdm_create(intf, ep, bufsize, manage_power);
  806. if (rv < 0)
  807. goto err;
  808. return &wdm_driver;
  809. err:
  810. return ERR_PTR(rv);
  811. }
  812. EXPORT_SYMBOL(usb_cdc_wdm_register);
  813. static void wdm_disconnect(struct usb_interface *intf)
  814. {
  815. struct wdm_device *desc;
  816. unsigned long flags;
  817. usb_deregister_dev(intf, &wdm_class);
  818. desc = wdm_find_device(intf);
  819. mutex_lock(&wdm_mutex);
  820. /* the spinlock makes sure no new urbs are generated in the callbacks */
  821. spin_lock_irqsave(&desc->iuspin, flags);
  822. set_bit(WDM_DISCONNECTING, &desc->flags);
  823. set_bit(WDM_READ, &desc->flags);
  824. spin_unlock_irqrestore(&desc->iuspin, flags);
  825. wake_up_all(&desc->wait);
  826. mutex_lock(&desc->rlock);
  827. mutex_lock(&desc->wlock);
  828. kill_urbs(desc);
  829. cancel_work_sync(&desc->rxwork);
  830. mutex_unlock(&desc->wlock);
  831. mutex_unlock(&desc->rlock);
  832. /* the desc->intf pointer used as list key is now invalid */
  833. spin_lock(&wdm_device_list_lock);
  834. list_del(&desc->device_list);
  835. spin_unlock(&wdm_device_list_lock);
  836. if (!desc->count)
  837. cleanup(desc);
  838. mutex_unlock(&wdm_mutex);
  839. }
  840. #ifdef CONFIG_PM
  841. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  842. {
  843. struct wdm_device *desc = wdm_find_device(intf);
  844. int rv = 0;
  845. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  846. /* if this is an autosuspend the caller does the locking */
  847. if (!PMSG_IS_AUTO(message)) {
  848. mutex_lock(&desc->rlock);
  849. mutex_lock(&desc->wlock);
  850. }
  851. spin_lock_irq(&desc->iuspin);
  852. if (PMSG_IS_AUTO(message) &&
  853. (test_bit(WDM_IN_USE, &desc->flags)
  854. || test_bit(WDM_RESPONDING, &desc->flags))) {
  855. spin_unlock_irq(&desc->iuspin);
  856. rv = -EBUSY;
  857. } else {
  858. set_bit(WDM_SUSPENDING, &desc->flags);
  859. spin_unlock_irq(&desc->iuspin);
  860. /* callback submits work - order is essential */
  861. kill_urbs(desc);
  862. cancel_work_sync(&desc->rxwork);
  863. }
  864. if (!PMSG_IS_AUTO(message)) {
  865. mutex_unlock(&desc->wlock);
  866. mutex_unlock(&desc->rlock);
  867. }
  868. return rv;
  869. }
  870. #endif
  871. static int recover_from_urb_loss(struct wdm_device *desc)
  872. {
  873. int rv = 0;
  874. if (desc->count) {
  875. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  876. if (rv < 0)
  877. dev_err(&desc->intf->dev,
  878. "Error resume submitting int urb - %d\n", rv);
  879. }
  880. return rv;
  881. }
  882. #ifdef CONFIG_PM
  883. static int wdm_resume(struct usb_interface *intf)
  884. {
  885. struct wdm_device *desc = wdm_find_device(intf);
  886. int rv;
  887. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  888. clear_bit(WDM_SUSPENDING, &desc->flags);
  889. rv = recover_from_urb_loss(desc);
  890. return rv;
  891. }
  892. #endif
  893. static int wdm_pre_reset(struct usb_interface *intf)
  894. {
  895. struct wdm_device *desc = wdm_find_device(intf);
  896. /*
  897. * we notify everybody using poll of
  898. * an exceptional situation
  899. * must be done before recovery lest a spontaneous
  900. * message from the device is lost
  901. */
  902. spin_lock_irq(&desc->iuspin);
  903. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  904. set_bit(WDM_READ, &desc->flags); /* unblock read */
  905. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  906. desc->rerr = -EINTR;
  907. spin_unlock_irq(&desc->iuspin);
  908. wake_up_all(&desc->wait);
  909. mutex_lock(&desc->rlock);
  910. mutex_lock(&desc->wlock);
  911. kill_urbs(desc);
  912. cancel_work_sync(&desc->rxwork);
  913. return 0;
  914. }
  915. static int wdm_post_reset(struct usb_interface *intf)
  916. {
  917. struct wdm_device *desc = wdm_find_device(intf);
  918. int rv;
  919. clear_bit(WDM_OVERFLOW, &desc->flags);
  920. clear_bit(WDM_RESETTING, &desc->flags);
  921. rv = recover_from_urb_loss(desc);
  922. mutex_unlock(&desc->wlock);
  923. mutex_unlock(&desc->rlock);
  924. return rv;
  925. }
  926. static struct usb_driver wdm_driver = {
  927. .name = "cdc_wdm",
  928. .probe = wdm_probe,
  929. .disconnect = wdm_disconnect,
  930. #ifdef CONFIG_PM
  931. .suspend = wdm_suspend,
  932. .resume = wdm_resume,
  933. .reset_resume = wdm_resume,
  934. #endif
  935. .pre_reset = wdm_pre_reset,
  936. .post_reset = wdm_post_reset,
  937. .id_table = wdm_ids,
  938. .supports_autosuspend = 1,
  939. };
  940. module_usb_driver(wdm_driver);
  941. MODULE_AUTHOR(DRIVER_AUTHOR);
  942. MODULE_DESCRIPTION(DRIVER_DESC);
  943. MODULE_LICENSE("GPL");