cdc-acm.c 43 KB

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