cdc-wdm.c 24 KB

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