cdc-wdm.c 25 KB

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