cdc-acm.c 38 KB

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