cdc-acm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
  6. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  10. *
  11. * USB Abstract Control Model driver for USB modems and ISDN adapters
  12. *
  13. * Sponsored by SuSE
  14. *
  15. * ChangeLog:
  16. * v0.9 - thorough cleaning, URBification, almost a rewrite
  17. * v0.10 - some more cleanups
  18. * v0.11 - fixed flow control, read error doesn't stop reads
  19. * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
  20. * v0.13 - added termios, added hangup
  21. * v0.14 - sized down struct acm
  22. * v0.15 - fixed flow control again - characters could be lost
  23. * v0.16 - added code for modems with swapped data and control interfaces
  24. * v0.17 - added new style probing
  25. * v0.18 - fixed new style probing for devices with more configurations
  26. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  27. * v0.20 - switched to probing on interface (rather than device) class
  28. * v0.21 - revert to probing on device for devices with multiple configs
  29. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  30. * config we want, sysadmin changes bConfigurationValue in sysfs.
  31. * v0.23 - use softirq for rx processing, as needed by tty layer
  32. * v0.24 - change probe method to evaluate CDC union descriptor
  33. * v0.25 - downstream tasks paralelized to maximize throughput
  34. * v0.26 - multiple write urbs, writesize increased
  35. */
  36. /*
  37. * This program is free software; you can redistribute it and/or modify
  38. * it under the terms of the GNU General Public License as published by
  39. * the Free Software Foundation; either version 2 of the License, or
  40. * (at your option) any later version.
  41. *
  42. * This program is distributed in the hope that it will be useful,
  43. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. * GNU General Public License for more details.
  46. *
  47. * You should have received a copy of the GNU General Public License
  48. * along with this program; if not, write to the Free Software
  49. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  50. */
  51. #undef DEBUG
  52. #undef VERBOSE_DEBUG
  53. #include <linux/kernel.h>
  54. #include <linux/errno.h>
  55. #include <linux/init.h>
  56. #include <linux/slab.h>
  57. #include <linux/tty.h>
  58. #include <linux/tty_driver.h>
  59. #include <linux/tty_flip.h>
  60. #include <linux/module.h>
  61. #include <linux/mutex.h>
  62. #include <asm/uaccess.h>
  63. #include <linux/usb.h>
  64. #include <linux/usb/cdc.h>
  65. #include <asm/byteorder.h>
  66. #include <asm/unaligned.h>
  67. #include <linux/list.h>
  68. #include "cdc-acm.h"
  69. #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
  70. /*
  71. * Version Information
  72. */
  73. #define DRIVER_VERSION "v0.26"
  74. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  75. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  76. static struct usb_driver acm_driver;
  77. static struct tty_driver *acm_tty_driver;
  78. static struct acm *acm_table[ACM_TTY_MINORS];
  79. static DEFINE_MUTEX(open_mutex);
  80. #define ACM_READY(acm) (acm && acm->dev && acm->used)
  81. #ifdef VERBOSE_DEBUG
  82. #define verbose 1
  83. #else
  84. #define verbose 0
  85. #endif
  86. /*
  87. * Functions for ACM control messages.
  88. */
  89. static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
  90. {
  91. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  92. request, USB_RT_ACM, value,
  93. acm->control->altsetting[0].desc.bInterfaceNumber,
  94. buf, len, 5000);
  95. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
  96. return retval < 0 ? retval : 0;
  97. }
  98. /* devices aren't required to support these requests.
  99. * the cdc acm descriptor tells whether they do...
  100. */
  101. #define acm_set_control(acm, control) \
  102. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  103. #define acm_set_line(acm, line) \
  104. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  105. #define acm_send_break(acm, ms) \
  106. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  107. /*
  108. * Write buffer management.
  109. * All of these assume proper locks taken by the caller.
  110. */
  111. static int acm_wb_alloc(struct acm *acm)
  112. {
  113. int i, wbn;
  114. struct acm_wb *wb;
  115. wbn = 0;
  116. i = 0;
  117. for (;;) {
  118. wb = &acm->wb[wbn];
  119. if (!wb->use) {
  120. wb->use = 1;
  121. return wbn;
  122. }
  123. wbn = (wbn + 1) % ACM_NW;
  124. if (++i >= ACM_NW)
  125. return -1;
  126. }
  127. }
  128. static int acm_wb_is_avail(struct acm *acm)
  129. {
  130. int i, n;
  131. unsigned long flags;
  132. n = ACM_NW;
  133. spin_lock_irqsave(&acm->write_lock, flags);
  134. for (i = 0; i < ACM_NW; i++) {
  135. n -= acm->wb[i].use;
  136. }
  137. spin_unlock_irqrestore(&acm->write_lock, flags);
  138. return n;
  139. }
  140. /*
  141. * Finish write. Caller must hold acm->write_lock
  142. */
  143. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  144. {
  145. wb->use = 0;
  146. acm->transmitting--;
  147. }
  148. /*
  149. * Poke write.
  150. *
  151. * the caller is responsible for locking
  152. */
  153. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  154. {
  155. int rc;
  156. acm->transmitting++;
  157. wb->urb->transfer_buffer = wb->buf;
  158. wb->urb->transfer_dma = wb->dmah;
  159. wb->urb->transfer_buffer_length = wb->len;
  160. wb->urb->dev = acm->dev;
  161. if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
  162. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  163. acm_write_done(acm, wb);
  164. }
  165. return rc;
  166. }
  167. static int acm_write_start(struct acm *acm, int wbn)
  168. {
  169. unsigned long flags;
  170. struct acm_wb *wb = &acm->wb[wbn];
  171. int rc;
  172. spin_lock_irqsave(&acm->write_lock, flags);
  173. if (!acm->dev) {
  174. wb->use = 0;
  175. spin_unlock_irqrestore(&acm->write_lock, flags);
  176. return -ENODEV;
  177. }
  178. dbg("%s susp_count: %d", __func__, acm->susp_count);
  179. if (acm->susp_count) {
  180. acm->delayed_wb = wb;
  181. schedule_work(&acm->waker);
  182. spin_unlock_irqrestore(&acm->write_lock, flags);
  183. return 0; /* A white lie */
  184. }
  185. usb_mark_last_busy(acm->dev);
  186. rc = acm_start_wb(acm, wb);
  187. spin_unlock_irqrestore(&acm->write_lock, flags);
  188. return rc;
  189. }
  190. /*
  191. * attributes exported through sysfs
  192. */
  193. static ssize_t show_caps
  194. (struct device *dev, struct device_attribute *attr, char *buf)
  195. {
  196. struct usb_interface *intf = to_usb_interface(dev);
  197. struct acm *acm = usb_get_intfdata(intf);
  198. return sprintf(buf, "%d", acm->ctrl_caps);
  199. }
  200. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  201. static ssize_t show_country_codes
  202. (struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct usb_interface *intf = to_usb_interface(dev);
  205. struct acm *acm = usb_get_intfdata(intf);
  206. memcpy(buf, acm->country_codes, acm->country_code_size);
  207. return acm->country_code_size;
  208. }
  209. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  210. static ssize_t show_country_rel_date
  211. (struct device *dev, struct device_attribute *attr, char *buf)
  212. {
  213. struct usb_interface *intf = to_usb_interface(dev);
  214. struct acm *acm = usb_get_intfdata(intf);
  215. return sprintf(buf, "%d", acm->country_rel_date);
  216. }
  217. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  218. /*
  219. * Interrupt handlers for various ACM device responses
  220. */
  221. /* control interface reports status changes with "interrupt" transfers */
  222. static void acm_ctrl_irq(struct urb *urb)
  223. {
  224. struct acm *acm = urb->context;
  225. struct usb_cdc_notification *dr = urb->transfer_buffer;
  226. unsigned char *data;
  227. int newctrl;
  228. int retval;
  229. int status = urb->status;
  230. switch (status) {
  231. case 0:
  232. /* success */
  233. break;
  234. case -ECONNRESET:
  235. case -ENOENT:
  236. case -ESHUTDOWN:
  237. /* this urb is terminated, clean up */
  238. dbg("%s - urb shutting down with status: %d", __func__, status);
  239. return;
  240. default:
  241. dbg("%s - nonzero urb status received: %d", __func__, status);
  242. goto exit;
  243. }
  244. if (!ACM_READY(acm))
  245. goto exit;
  246. data = (unsigned char *)(dr + 1);
  247. switch (dr->bNotificationType) {
  248. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  249. dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
  250. break;
  251. case USB_CDC_NOTIFY_SERIAL_STATE:
  252. newctrl = get_unaligned_le16(data);
  253. if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  254. dbg("calling hangup");
  255. tty_hangup(acm->tty);
  256. }
  257. acm->ctrlin = newctrl;
  258. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  259. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  260. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  261. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  262. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  263. break;
  264. default:
  265. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  266. dr->bNotificationType, dr->wIndex,
  267. dr->wLength, data[0], data[1]);
  268. break;
  269. }
  270. exit:
  271. usb_mark_last_busy(acm->dev);
  272. retval = usb_submit_urb (urb, GFP_ATOMIC);
  273. if (retval)
  274. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
  275. "result %d", __func__, retval);
  276. }
  277. /* data interface returns incoming bytes, or we got unthrottled */
  278. static void acm_read_bulk(struct urb *urb)
  279. {
  280. struct acm_rb *buf;
  281. struct acm_ru *rcv = urb->context;
  282. struct acm *acm = rcv->instance;
  283. int status = urb->status;
  284. dbg("Entering acm_read_bulk with status %d", status);
  285. if (!ACM_READY(acm)) {
  286. dev_dbg(&acm->data->dev, "Aborting, acm not ready");
  287. return;
  288. }
  289. usb_mark_last_busy(acm->dev);
  290. if (status)
  291. dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
  292. buf = rcv->buffer;
  293. buf->size = urb->actual_length;
  294. if (likely(status == 0)) {
  295. spin_lock(&acm->read_lock);
  296. acm->processing++;
  297. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  298. list_add_tail(&buf->list, &acm->filled_read_bufs);
  299. spin_unlock(&acm->read_lock);
  300. } else {
  301. /* we drop the buffer due to an error */
  302. spin_lock(&acm->read_lock);
  303. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  304. list_add(&buf->list, &acm->spare_read_bufs);
  305. spin_unlock(&acm->read_lock);
  306. /* nevertheless the tasklet must be kicked unconditionally
  307. so the queue cannot dry up */
  308. }
  309. if (likely(!acm->susp_count))
  310. tasklet_schedule(&acm->urb_task);
  311. }
  312. static void acm_rx_tasklet(unsigned long _acm)
  313. {
  314. struct acm *acm = (void *)_acm;
  315. struct acm_rb *buf;
  316. struct tty_struct *tty = acm->tty;
  317. struct acm_ru *rcv;
  318. unsigned long flags;
  319. unsigned char throttled;
  320. dbg("Entering acm_rx_tasklet");
  321. if (!ACM_READY(acm))
  322. {
  323. dbg("acm_rx_tasklet: ACM not ready");
  324. return;
  325. }
  326. spin_lock_irqsave(&acm->throttle_lock, flags);
  327. throttled = acm->throttle;
  328. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  329. if (throttled)
  330. {
  331. dbg("acm_rx_tasklet: throttled");
  332. return;
  333. }
  334. next_buffer:
  335. spin_lock_irqsave(&acm->read_lock, flags);
  336. if (list_empty(&acm->filled_read_bufs)) {
  337. spin_unlock_irqrestore(&acm->read_lock, flags);
  338. goto urbs;
  339. }
  340. buf = list_entry(acm->filled_read_bufs.next,
  341. struct acm_rb, list);
  342. list_del(&buf->list);
  343. spin_unlock_irqrestore(&acm->read_lock, flags);
  344. dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
  345. tty_buffer_request_room(tty, buf->size);
  346. spin_lock_irqsave(&acm->throttle_lock, flags);
  347. throttled = acm->throttle;
  348. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  349. if (!throttled)
  350. tty_insert_flip_string(tty, buf->base, buf->size);
  351. tty_flip_buffer_push(tty);
  352. if (throttled) {
  353. dbg("Throttling noticed");
  354. spin_lock_irqsave(&acm->read_lock, flags);
  355. list_add(&buf->list, &acm->filled_read_bufs);
  356. spin_unlock_irqrestore(&acm->read_lock, flags);
  357. return;
  358. }
  359. spin_lock_irqsave(&acm->read_lock, flags);
  360. list_add(&buf->list, &acm->spare_read_bufs);
  361. spin_unlock_irqrestore(&acm->read_lock, flags);
  362. goto next_buffer;
  363. urbs:
  364. while (!list_empty(&acm->spare_read_bufs)) {
  365. spin_lock_irqsave(&acm->read_lock, flags);
  366. if (list_empty(&acm->spare_read_urbs)) {
  367. acm->processing = 0;
  368. spin_unlock_irqrestore(&acm->read_lock, flags);
  369. return;
  370. }
  371. rcv = list_entry(acm->spare_read_urbs.next,
  372. struct acm_ru, list);
  373. list_del(&rcv->list);
  374. spin_unlock_irqrestore(&acm->read_lock, flags);
  375. buf = list_entry(acm->spare_read_bufs.next,
  376. struct acm_rb, list);
  377. list_del(&buf->list);
  378. rcv->buffer = buf;
  379. usb_fill_bulk_urb(rcv->urb, acm->dev,
  380. acm->rx_endpoint,
  381. buf->base,
  382. acm->readsize,
  383. acm_read_bulk, rcv);
  384. rcv->urb->transfer_dma = buf->dma;
  385. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  386. /* This shouldn't kill the driver as unsuccessful URBs are returned to the
  387. free-urbs-pool and resubmited ASAP */
  388. spin_lock_irqsave(&acm->read_lock, flags);
  389. if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  390. list_add(&buf->list, &acm->spare_read_bufs);
  391. list_add(&rcv->list, &acm->spare_read_urbs);
  392. acm->processing = 0;
  393. spin_unlock_irqrestore(&acm->read_lock, flags);
  394. return;
  395. } else {
  396. spin_unlock_irqrestore(&acm->read_lock, flags);
  397. dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
  398. }
  399. }
  400. spin_lock_irqsave(&acm->read_lock, flags);
  401. acm->processing = 0;
  402. spin_unlock_irqrestore(&acm->read_lock, flags);
  403. }
  404. /* data interface wrote those outgoing bytes */
  405. static void acm_write_bulk(struct urb *urb)
  406. {
  407. struct acm_wb *wb = urb->context;
  408. struct acm *acm = wb->instance;
  409. unsigned long flags;
  410. if (verbose || urb->status
  411. || (urb->actual_length != urb->transfer_buffer_length))
  412. dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
  413. urb->actual_length,
  414. urb->transfer_buffer_length,
  415. urb->status);
  416. spin_lock_irqsave(&acm->write_lock, flags);
  417. acm_write_done(acm, wb);
  418. spin_unlock_irqrestore(&acm->write_lock, flags);
  419. if (ACM_READY(acm))
  420. schedule_work(&acm->work);
  421. else
  422. wake_up_interruptible(&acm->drain_wait);
  423. }
  424. static void acm_softint(struct work_struct *work)
  425. {
  426. struct acm *acm = container_of(work, struct acm, work);
  427. dev_vdbg(&acm->data->dev, "tx work\n");
  428. if (!ACM_READY(acm))
  429. return;
  430. tty_wakeup(acm->tty);
  431. }
  432. static void acm_waker(struct work_struct *waker)
  433. {
  434. struct acm *acm = container_of(waker, struct acm, waker);
  435. int rv;
  436. rv = usb_autopm_get_interface(acm->control);
  437. if (rv < 0) {
  438. dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
  439. return;
  440. }
  441. if (acm->delayed_wb) {
  442. acm_start_wb(acm, acm->delayed_wb);
  443. acm->delayed_wb = NULL;
  444. }
  445. usb_autopm_put_interface(acm->control);
  446. }
  447. /*
  448. * TTY handlers
  449. */
  450. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  451. {
  452. struct acm *acm;
  453. int rv = -EINVAL;
  454. int i;
  455. dbg("Entering acm_tty_open.");
  456. mutex_lock(&open_mutex);
  457. acm = acm_table[tty->index];
  458. if (!acm || !acm->dev)
  459. goto err_out;
  460. else
  461. rv = 0;
  462. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  463. tty->driver_data = acm;
  464. acm->tty = tty;
  465. if (usb_autopm_get_interface(acm->control) < 0)
  466. goto early_bail;
  467. else
  468. acm->control->needs_remote_wakeup = 1;
  469. mutex_lock(&acm->mutex);
  470. if (acm->used++) {
  471. usb_autopm_put_interface(acm->control);
  472. goto done;
  473. }
  474. acm->ctrlurb->dev = acm->dev;
  475. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  476. dbg("usb_submit_urb(ctrl irq) failed");
  477. goto bail_out;
  478. }
  479. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  480. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  481. goto full_bailout;
  482. usb_autopm_put_interface(acm->control);
  483. INIT_LIST_HEAD(&acm->spare_read_urbs);
  484. INIT_LIST_HEAD(&acm->spare_read_bufs);
  485. INIT_LIST_HEAD(&acm->filled_read_bufs);
  486. for (i = 0; i < acm->rx_buflimit; i++) {
  487. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  488. }
  489. for (i = 0; i < acm->rx_buflimit; i++) {
  490. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  491. }
  492. acm->throttle = 0;
  493. tasklet_schedule(&acm->urb_task);
  494. done:
  495. mutex_unlock(&acm->mutex);
  496. err_out:
  497. mutex_unlock(&open_mutex);
  498. return rv;
  499. full_bailout:
  500. usb_kill_urb(acm->ctrlurb);
  501. bail_out:
  502. usb_autopm_put_interface(acm->control);
  503. acm->used--;
  504. mutex_unlock(&acm->mutex);
  505. early_bail:
  506. mutex_unlock(&open_mutex);
  507. return -EIO;
  508. }
  509. static void acm_tty_unregister(struct acm *acm)
  510. {
  511. int i,nr;
  512. nr = acm->rx_buflimit;
  513. tty_unregister_device(acm_tty_driver, acm->minor);
  514. usb_put_intf(acm->control);
  515. acm_table[acm->minor] = NULL;
  516. usb_free_urb(acm->ctrlurb);
  517. for (i = 0; i < ACM_NW; i++)
  518. usb_free_urb(acm->wb[i].urb);
  519. for (i = 0; i < nr; i++)
  520. usb_free_urb(acm->ru[i].urb);
  521. kfree(acm->country_codes);
  522. kfree(acm);
  523. }
  524. static int acm_tty_chars_in_buffer(struct tty_struct *tty);
  525. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  526. {
  527. struct acm *acm = tty->driver_data;
  528. int i,nr;
  529. if (!acm || !acm->used)
  530. return;
  531. nr = acm->rx_buflimit;
  532. mutex_lock(&open_mutex);
  533. if (!--acm->used) {
  534. if (acm->dev) {
  535. usb_autopm_get_interface(acm->control);
  536. acm_set_control(acm, acm->ctrlout = 0);
  537. /* try letting the last writes drain naturally */
  538. wait_event_interruptible_timeout(acm->drain_wait,
  539. (ACM_NW == acm_wb_is_avail(acm))
  540. || !acm->dev,
  541. ACM_CLOSE_TIMEOUT * HZ);
  542. usb_kill_urb(acm->ctrlurb);
  543. for (i = 0; i < ACM_NW; i++)
  544. usb_kill_urb(acm->wb[i].urb);
  545. for (i = 0; i < nr; i++)
  546. usb_kill_urb(acm->ru[i].urb);
  547. acm->control->needs_remote_wakeup = 0;
  548. usb_autopm_put_interface(acm->control);
  549. } else
  550. acm_tty_unregister(acm);
  551. }
  552. mutex_unlock(&open_mutex);
  553. }
  554. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  555. {
  556. struct acm *acm = tty->driver_data;
  557. int stat;
  558. unsigned long flags;
  559. int wbn;
  560. struct acm_wb *wb;
  561. dbg("Entering acm_tty_write to write %d bytes,", count);
  562. if (!ACM_READY(acm))
  563. return -EINVAL;
  564. if (!count)
  565. return 0;
  566. spin_lock_irqsave(&acm->write_lock, flags);
  567. if ((wbn = acm_wb_alloc(acm)) < 0) {
  568. spin_unlock_irqrestore(&acm->write_lock, flags);
  569. return 0;
  570. }
  571. wb = &acm->wb[wbn];
  572. count = (count > acm->writesize) ? acm->writesize : count;
  573. dbg("Get %d bytes...", count);
  574. memcpy(wb->buf, buf, count);
  575. wb->len = count;
  576. spin_unlock_irqrestore(&acm->write_lock, flags);
  577. if ((stat = acm_write_start(acm, wbn)) < 0)
  578. return stat;
  579. return count;
  580. }
  581. static int acm_tty_write_room(struct tty_struct *tty)
  582. {
  583. struct acm *acm = tty->driver_data;
  584. if (!ACM_READY(acm))
  585. return -EINVAL;
  586. /*
  587. * Do not let the line discipline to know that we have a reserve,
  588. * or it might get too enthusiastic.
  589. */
  590. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  591. }
  592. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  593. {
  594. struct acm *acm = tty->driver_data;
  595. if (!ACM_READY(acm))
  596. return -EINVAL;
  597. /*
  598. * This is inaccurate (overcounts), but it works.
  599. */
  600. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  601. }
  602. static void acm_tty_throttle(struct tty_struct *tty)
  603. {
  604. struct acm *acm = tty->driver_data;
  605. if (!ACM_READY(acm))
  606. return;
  607. spin_lock_bh(&acm->throttle_lock);
  608. acm->throttle = 1;
  609. spin_unlock_bh(&acm->throttle_lock);
  610. }
  611. static void acm_tty_unthrottle(struct tty_struct *tty)
  612. {
  613. struct acm *acm = tty->driver_data;
  614. if (!ACM_READY(acm))
  615. return;
  616. spin_lock_bh(&acm->throttle_lock);
  617. acm->throttle = 0;
  618. spin_unlock_bh(&acm->throttle_lock);
  619. tasklet_schedule(&acm->urb_task);
  620. }
  621. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  622. {
  623. struct acm *acm = tty->driver_data;
  624. int retval;
  625. if (!ACM_READY(acm))
  626. return -EINVAL;
  627. retval = acm_send_break(acm, state ? 0xffff : 0);
  628. if (retval < 0)
  629. dbg("send break failed");
  630. return retval;
  631. }
  632. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  633. {
  634. struct acm *acm = tty->driver_data;
  635. if (!ACM_READY(acm))
  636. return -EINVAL;
  637. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  638. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  639. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  640. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  641. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  642. TIOCM_CTS;
  643. }
  644. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  645. unsigned int set, unsigned int clear)
  646. {
  647. struct acm *acm = tty->driver_data;
  648. unsigned int newctrl;
  649. if (!ACM_READY(acm))
  650. return -EINVAL;
  651. newctrl = acm->ctrlout;
  652. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  653. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  654. newctrl = (newctrl & ~clear) | set;
  655. if (acm->ctrlout == newctrl)
  656. return 0;
  657. return acm_set_control(acm, acm->ctrlout = newctrl);
  658. }
  659. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  660. {
  661. struct acm *acm = tty->driver_data;
  662. if (!ACM_READY(acm))
  663. return -EINVAL;
  664. return -ENOIOCTLCMD;
  665. }
  666. static const __u32 acm_tty_speed[] = {
  667. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  668. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  669. 57600, 115200, 230400, 460800, 500000, 576000,
  670. 921600, 1000000, 1152000, 1500000, 2000000,
  671. 2500000, 3000000, 3500000, 4000000
  672. };
  673. static const __u8 acm_tty_size[] = {
  674. 5, 6, 7, 8
  675. };
  676. static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
  677. {
  678. struct acm *acm = tty->driver_data;
  679. struct ktermios *termios = tty->termios;
  680. struct usb_cdc_line_coding newline;
  681. int newctrl = acm->ctrlout;
  682. if (!ACM_READY(acm))
  683. return;
  684. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  685. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  686. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  687. newline.bParityType = termios->c_cflag & PARENB ?
  688. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  689. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  690. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  691. if (!newline.dwDTERate) {
  692. newline.dwDTERate = acm->line.dwDTERate;
  693. newctrl &= ~ACM_CTRL_DTR;
  694. } else newctrl |= ACM_CTRL_DTR;
  695. if (newctrl != acm->ctrlout)
  696. acm_set_control(acm, acm->ctrlout = newctrl);
  697. if (memcmp(&acm->line, &newline, sizeof newline)) {
  698. memcpy(&acm->line, &newline, sizeof newline);
  699. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  700. newline.bCharFormat, newline.bParityType,
  701. newline.bDataBits);
  702. acm_set_line(acm, &acm->line);
  703. }
  704. }
  705. /*
  706. * USB probe and disconnect routines.
  707. */
  708. /* Little helpers: write/read buffers free */
  709. static void acm_write_buffers_free(struct acm *acm)
  710. {
  711. int i;
  712. struct acm_wb *wb;
  713. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  714. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  715. usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
  716. }
  717. }
  718. static void acm_read_buffers_free(struct acm *acm)
  719. {
  720. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  721. int i, n = acm->rx_buflimit;
  722. for (i = 0; i < n; i++)
  723. usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
  724. }
  725. /* Little helper: write buffers allocate */
  726. static int acm_write_buffers_alloc(struct acm *acm)
  727. {
  728. int i;
  729. struct acm_wb *wb;
  730. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  731. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  732. &wb->dmah);
  733. if (!wb->buf) {
  734. while (i != 0) {
  735. --i;
  736. --wb;
  737. usb_buffer_free(acm->dev, acm->writesize,
  738. wb->buf, wb->dmah);
  739. }
  740. return -ENOMEM;
  741. }
  742. }
  743. return 0;
  744. }
  745. static int acm_probe (struct usb_interface *intf,
  746. const struct usb_device_id *id)
  747. {
  748. struct usb_cdc_union_desc *union_header = NULL;
  749. struct usb_cdc_country_functional_desc *cfd = NULL;
  750. unsigned char *buffer = intf->altsetting->extra;
  751. int buflen = intf->altsetting->extralen;
  752. struct usb_interface *control_interface;
  753. struct usb_interface *data_interface;
  754. struct usb_endpoint_descriptor *epctrl;
  755. struct usb_endpoint_descriptor *epread;
  756. struct usb_endpoint_descriptor *epwrite;
  757. struct usb_device *usb_dev = interface_to_usbdev(intf);
  758. struct acm *acm;
  759. int minor;
  760. int ctrlsize,readsize;
  761. u8 *buf;
  762. u8 ac_management_function = 0;
  763. u8 call_management_function = 0;
  764. int call_interface_num = -1;
  765. int data_interface_num;
  766. unsigned long quirks;
  767. int num_rx_buf;
  768. int i;
  769. /* normal quirks */
  770. quirks = (unsigned long)id->driver_info;
  771. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  772. /* handle quirks deadly to normal probing*/
  773. if (quirks == NO_UNION_NORMAL) {
  774. data_interface = usb_ifnum_to_if(usb_dev, 1);
  775. control_interface = usb_ifnum_to_if(usb_dev, 0);
  776. goto skip_normal_probe;
  777. }
  778. /* normal probing*/
  779. if (!buffer) {
  780. dev_err(&intf->dev, "Weird descriptor references\n");
  781. return -EINVAL;
  782. }
  783. if (!buflen) {
  784. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  785. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  786. buflen = intf->cur_altsetting->endpoint->extralen;
  787. buffer = intf->cur_altsetting->endpoint->extra;
  788. } else {
  789. dev_err(&intf->dev,
  790. "Zero length descriptor references\n");
  791. return -EINVAL;
  792. }
  793. }
  794. while (buflen > 0) {
  795. if (buffer [1] != USB_DT_CS_INTERFACE) {
  796. dev_err(&intf->dev, "skipping garbage\n");
  797. goto next_desc;
  798. }
  799. switch (buffer [2]) {
  800. case USB_CDC_UNION_TYPE: /* we've found it */
  801. if (union_header) {
  802. dev_err(&intf->dev, "More than one "
  803. "union descriptor, "
  804. "skipping ...\n");
  805. goto next_desc;
  806. }
  807. union_header = (struct usb_cdc_union_desc *)
  808. buffer;
  809. break;
  810. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  811. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  812. break;
  813. case USB_CDC_HEADER_TYPE: /* maybe check version */
  814. break; /* for now we ignore it */
  815. case USB_CDC_ACM_TYPE:
  816. ac_management_function = buffer[3];
  817. break;
  818. case USB_CDC_CALL_MANAGEMENT_TYPE:
  819. call_management_function = buffer[3];
  820. call_interface_num = buffer[4];
  821. if ((call_management_function & 3) != 3)
  822. dev_err(&intf->dev, "This device "
  823. "cannot do calls on its own. "
  824. "It is no modem.\n");
  825. break;
  826. default:
  827. /* there are LOTS more CDC descriptors that
  828. * could legitimately be found here.
  829. */
  830. dev_dbg(&intf->dev, "Ignoring descriptor: "
  831. "type %02x, length %d\n",
  832. buffer[2], buffer[0]);
  833. break;
  834. }
  835. next_desc:
  836. buflen -= buffer[0];
  837. buffer += buffer[0];
  838. }
  839. if (!union_header) {
  840. if (call_interface_num > 0) {
  841. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  842. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  843. control_interface = intf;
  844. } else {
  845. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  846. return -ENODEV;
  847. }
  848. } else {
  849. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  850. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  851. if (!control_interface || !data_interface) {
  852. dev_dbg(&intf->dev,"no interfaces\n");
  853. return -ENODEV;
  854. }
  855. }
  856. if (data_interface_num != call_interface_num)
  857. dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
  858. skip_normal_probe:
  859. /*workaround for switched interfaces */
  860. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  861. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  862. struct usb_interface *t;
  863. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  864. t = control_interface;
  865. control_interface = data_interface;
  866. data_interface = t;
  867. } else {
  868. return -EINVAL;
  869. }
  870. }
  871. /* Accept probe requests only for the control interface */
  872. if (intf != control_interface)
  873. return -ENODEV;
  874. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  875. dev_dbg(&intf->dev,"The data interface isn't available\n");
  876. return -EBUSY;
  877. }
  878. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  879. return -EINVAL;
  880. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  881. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  882. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  883. /* workaround for switched endpoints */
  884. if (!usb_endpoint_dir_in(epread)) {
  885. /* descriptors are swapped */
  886. struct usb_endpoint_descriptor *t;
  887. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  888. t = epread;
  889. epread = epwrite;
  890. epwrite = t;
  891. }
  892. dbg("interfaces are valid");
  893. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  894. if (minor == ACM_TTY_MINORS) {
  895. dev_err(&intf->dev, "no more free acm devices\n");
  896. return -ENODEV;
  897. }
  898. if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
  899. dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
  900. goto alloc_fail;
  901. }
  902. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  903. readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
  904. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
  905. acm->control = control_interface;
  906. acm->data = data_interface;
  907. acm->minor = minor;
  908. acm->dev = usb_dev;
  909. acm->ctrl_caps = ac_management_function;
  910. acm->ctrlsize = ctrlsize;
  911. acm->readsize = readsize;
  912. acm->rx_buflimit = num_rx_buf;
  913. acm->urb_task.func = acm_rx_tasklet;
  914. acm->urb_task.data = (unsigned long) acm;
  915. INIT_WORK(&acm->work, acm_softint);
  916. INIT_WORK(&acm->waker, acm_waker);
  917. init_waitqueue_head(&acm->drain_wait);
  918. spin_lock_init(&acm->throttle_lock);
  919. spin_lock_init(&acm->write_lock);
  920. spin_lock_init(&acm->read_lock);
  921. mutex_init(&acm->mutex);
  922. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  923. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  924. if (!buf) {
  925. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  926. goto alloc_fail2;
  927. }
  928. acm->ctrl_buffer = buf;
  929. if (acm_write_buffers_alloc(acm) < 0) {
  930. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  931. goto alloc_fail4;
  932. }
  933. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  934. if (!acm->ctrlurb) {
  935. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  936. goto alloc_fail5;
  937. }
  938. for (i = 0; i < num_rx_buf; i++) {
  939. struct acm_ru *rcv = &(acm->ru[i]);
  940. if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
  941. dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
  942. goto alloc_fail7;
  943. }
  944. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  945. rcv->instance = acm;
  946. }
  947. for (i = 0; i < num_rx_buf; i++) {
  948. struct acm_rb *rb = &(acm->rb[i]);
  949. rb->base = usb_buffer_alloc(acm->dev, readsize,
  950. GFP_KERNEL, &rb->dma);
  951. if (!rb->base) {
  952. dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
  953. goto alloc_fail7;
  954. }
  955. }
  956. for(i = 0; i < ACM_NW; i++)
  957. {
  958. struct acm_wb *snd = &(acm->wb[i]);
  959. if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
  960. dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
  961. goto alloc_fail7;
  962. }
  963. usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  964. NULL, acm->writesize, acm_write_bulk, snd);
  965. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  966. snd->instance = acm;
  967. }
  968. usb_set_intfdata (intf, acm);
  969. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  970. if (i < 0)
  971. goto alloc_fail8;
  972. if (cfd) { /* export the country data */
  973. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  974. if (!acm->country_codes)
  975. goto skip_countries;
  976. acm->country_code_size = cfd->bLength - 4;
  977. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
  978. acm->country_rel_date = cfd->iCountryCodeRelDate;
  979. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  980. if (i < 0) {
  981. kfree(acm->country_codes);
  982. goto skip_countries;
  983. }
  984. i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
  985. if (i < 0) {
  986. kfree(acm->country_codes);
  987. goto skip_countries;
  988. }
  989. }
  990. skip_countries:
  991. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  992. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  993. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  994. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  995. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  996. acm_set_control(acm, acm->ctrlout);
  997. acm->line.dwDTERate = cpu_to_le32(9600);
  998. acm->line.bDataBits = 8;
  999. acm_set_line(acm, &acm->line);
  1000. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1001. usb_set_intfdata(data_interface, acm);
  1002. usb_get_intf(control_interface);
  1003. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1004. acm_table[minor] = acm;
  1005. return 0;
  1006. alloc_fail8:
  1007. for (i = 0; i < ACM_NW; i++)
  1008. usb_free_urb(acm->wb[i].urb);
  1009. alloc_fail7:
  1010. acm_read_buffers_free(acm);
  1011. for (i = 0; i < num_rx_buf; i++)
  1012. usb_free_urb(acm->ru[i].urb);
  1013. usb_free_urb(acm->ctrlurb);
  1014. alloc_fail5:
  1015. acm_write_buffers_free(acm);
  1016. alloc_fail4:
  1017. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1018. alloc_fail2:
  1019. kfree(acm);
  1020. alloc_fail:
  1021. return -ENOMEM;
  1022. }
  1023. static void stop_data_traffic(struct acm *acm)
  1024. {
  1025. int i;
  1026. dbg("Entering stop_data_traffic");
  1027. tasklet_disable(&acm->urb_task);
  1028. usb_kill_urb(acm->ctrlurb);
  1029. for(i = 0; i < ACM_NW; i++)
  1030. usb_kill_urb(acm->wb[i].urb);
  1031. for (i = 0; i < acm->rx_buflimit; i++)
  1032. usb_kill_urb(acm->ru[i].urb);
  1033. tasklet_enable(&acm->urb_task);
  1034. cancel_work_sync(&acm->work);
  1035. cancel_work_sync(&acm->waker);
  1036. }
  1037. static void acm_disconnect(struct usb_interface *intf)
  1038. {
  1039. struct acm *acm = usb_get_intfdata(intf);
  1040. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1041. /* sibling interface is already cleaning up */
  1042. if (!acm)
  1043. return;
  1044. mutex_lock(&open_mutex);
  1045. if (acm->country_codes){
  1046. device_remove_file(&acm->control->dev,
  1047. &dev_attr_wCountryCodes);
  1048. device_remove_file(&acm->control->dev,
  1049. &dev_attr_iCountryCodeRelDate);
  1050. }
  1051. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1052. acm->dev = NULL;
  1053. usb_set_intfdata(acm->control, NULL);
  1054. usb_set_intfdata(acm->data, NULL);
  1055. stop_data_traffic(acm);
  1056. acm_write_buffers_free(acm);
  1057. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1058. acm_read_buffers_free(acm);
  1059. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1060. acm->data : acm->control);
  1061. if (!acm->used) {
  1062. acm_tty_unregister(acm);
  1063. mutex_unlock(&open_mutex);
  1064. return;
  1065. }
  1066. mutex_unlock(&open_mutex);
  1067. if (acm->tty)
  1068. tty_hangup(acm->tty);
  1069. }
  1070. #ifdef CONFIG_PM
  1071. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1072. {
  1073. struct acm *acm = usb_get_intfdata(intf);
  1074. int cnt;
  1075. if (message.event & PM_EVENT_AUTO) {
  1076. int b;
  1077. spin_lock_irq(&acm->read_lock);
  1078. spin_lock(&acm->write_lock);
  1079. b = acm->processing + acm->transmitting;
  1080. spin_unlock(&acm->write_lock);
  1081. spin_unlock_irq(&acm->read_lock);
  1082. if (b)
  1083. return -EBUSY;
  1084. }
  1085. spin_lock_irq(&acm->read_lock);
  1086. spin_lock(&acm->write_lock);
  1087. cnt = acm->susp_count++;
  1088. spin_unlock(&acm->write_lock);
  1089. spin_unlock_irq(&acm->read_lock);
  1090. if (cnt)
  1091. return 0;
  1092. /*
  1093. we treat opened interfaces differently,
  1094. we must guard against open
  1095. */
  1096. mutex_lock(&acm->mutex);
  1097. if (acm->used)
  1098. stop_data_traffic(acm);
  1099. mutex_unlock(&acm->mutex);
  1100. return 0;
  1101. }
  1102. static int acm_resume(struct usb_interface *intf)
  1103. {
  1104. struct acm *acm = usb_get_intfdata(intf);
  1105. int rv = 0;
  1106. int cnt;
  1107. spin_lock_irq(&acm->read_lock);
  1108. acm->susp_count -= 1;
  1109. cnt = acm->susp_count;
  1110. spin_unlock_irq(&acm->read_lock);
  1111. if (cnt)
  1112. return 0;
  1113. mutex_lock(&acm->mutex);
  1114. if (acm->used) {
  1115. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1116. if (rv < 0)
  1117. goto err_out;
  1118. tasklet_schedule(&acm->urb_task);
  1119. }
  1120. err_out:
  1121. mutex_unlock(&acm->mutex);
  1122. return rv;
  1123. }
  1124. #endif /* CONFIG_PM */
  1125. /*
  1126. * USB driver structure.
  1127. */
  1128. static struct usb_device_id acm_ids[] = {
  1129. /* quirky and broken devices */
  1130. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1131. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1132. },
  1133. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1134. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1135. },
  1136. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1137. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1138. },
  1139. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1140. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1141. },
  1142. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1143. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1144. },
  1145. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1146. .driver_info = SINGLE_RX_URB,
  1147. },
  1148. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1149. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1150. },
  1151. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1152. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1153. },
  1154. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1155. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1156. },
  1157. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1158. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1159. },
  1160. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1161. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1162. },
  1163. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1164. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1165. },
  1166. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1167. },
  1168. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1169. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1170. data interface instead of
  1171. communications interface.
  1172. Maybe we should define a new
  1173. quirk for this. */
  1174. },
  1175. /* control interfaces with various AT-command sets */
  1176. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1177. USB_CDC_ACM_PROTO_AT_V25TER) },
  1178. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1179. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1180. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1181. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1182. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1183. USB_CDC_ACM_PROTO_AT_GSM) },
  1184. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1185. USB_CDC_ACM_PROTO_AT_3G ) },
  1186. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1187. USB_CDC_ACM_PROTO_AT_CDMA) },
  1188. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  1189. { }
  1190. };
  1191. MODULE_DEVICE_TABLE (usb, acm_ids);
  1192. static struct usb_driver acm_driver = {
  1193. .name = "cdc_acm",
  1194. .probe = acm_probe,
  1195. .disconnect = acm_disconnect,
  1196. #ifdef CONFIG_PM
  1197. .suspend = acm_suspend,
  1198. .resume = acm_resume,
  1199. #endif
  1200. .id_table = acm_ids,
  1201. #ifdef CONFIG_PM
  1202. .supports_autosuspend = 1,
  1203. #endif
  1204. };
  1205. /*
  1206. * TTY driver structures.
  1207. */
  1208. static const struct tty_operations acm_ops = {
  1209. .open = acm_tty_open,
  1210. .close = acm_tty_close,
  1211. .write = acm_tty_write,
  1212. .write_room = acm_tty_write_room,
  1213. .ioctl = acm_tty_ioctl,
  1214. .throttle = acm_tty_throttle,
  1215. .unthrottle = acm_tty_unthrottle,
  1216. .chars_in_buffer = acm_tty_chars_in_buffer,
  1217. .break_ctl = acm_tty_break_ctl,
  1218. .set_termios = acm_tty_set_termios,
  1219. .tiocmget = acm_tty_tiocmget,
  1220. .tiocmset = acm_tty_tiocmset,
  1221. };
  1222. /*
  1223. * Init / exit.
  1224. */
  1225. static int __init acm_init(void)
  1226. {
  1227. int retval;
  1228. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1229. if (!acm_tty_driver)
  1230. return -ENOMEM;
  1231. acm_tty_driver->owner = THIS_MODULE,
  1232. acm_tty_driver->driver_name = "acm",
  1233. acm_tty_driver->name = "ttyACM",
  1234. acm_tty_driver->major = ACM_TTY_MAJOR,
  1235. acm_tty_driver->minor_start = 0,
  1236. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1237. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1238. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1239. acm_tty_driver->init_termios = tty_std_termios;
  1240. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1241. tty_set_operations(acm_tty_driver, &acm_ops);
  1242. retval = tty_register_driver(acm_tty_driver);
  1243. if (retval) {
  1244. put_tty_driver(acm_tty_driver);
  1245. return retval;
  1246. }
  1247. retval = usb_register(&acm_driver);
  1248. if (retval) {
  1249. tty_unregister_driver(acm_tty_driver);
  1250. put_tty_driver(acm_tty_driver);
  1251. return retval;
  1252. }
  1253. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1254. DRIVER_DESC "\n");
  1255. return 0;
  1256. }
  1257. static void __exit acm_exit(void)
  1258. {
  1259. usb_deregister(&acm_driver);
  1260. tty_unregister_driver(acm_tty_driver);
  1261. put_tty_driver(acm_tty_driver);
  1262. }
  1263. module_init(acm_init);
  1264. module_exit(acm_exit);
  1265. MODULE_AUTHOR( DRIVER_AUTHOR );
  1266. MODULE_DESCRIPTION( DRIVER_DESC );
  1267. MODULE_LICENSE("GPL");
  1268. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);