cdc-acm.c 39 KB

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