cdc-acm.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@ucw.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/serial.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/module.h>
  63. #include <linux/mutex.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/usb.h>
  66. #include <linux/usb/cdc.h>
  67. #include <asm/byteorder.h>
  68. #include <asm/unaligned.h>
  69. #include <linux/list.h>
  70. #include "cdc-acm.h"
  71. #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
  72. /*
  73. * Version Information
  74. */
  75. #define DRIVER_VERSION "v0.26"
  76. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  77. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  78. static struct usb_driver acm_driver;
  79. static struct tty_driver *acm_tty_driver;
  80. static struct acm *acm_table[ACM_TTY_MINORS];
  81. static DEFINE_MUTEX(open_mutex);
  82. #define ACM_READY(acm) (acm && acm->dev && acm->port.count)
  83. static const struct tty_port_operations acm_port_ops = {
  84. };
  85. /*
  86. * Functions for ACM control messages.
  87. */
  88. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  89. void *buf, int len)
  90. {
  91. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  92. request, USB_RT_ACM, value,
  93. acm->control->altsetting[0].desc.bInterfaceNumber,
  94. buf, len, 5000);
  95. dev_dbg(&acm->control->dev,
  96. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  97. __func__, 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. spin_unlock_irqrestore(&acm->write_lock, flags);
  139. return n;
  140. }
  141. /*
  142. * Finish write. Caller must hold acm->write_lock
  143. */
  144. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  145. {
  146. wb->use = 0;
  147. acm->transmitting--;
  148. usb_autopm_put_interface_async(acm->control);
  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. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  164. if (rc < 0) {
  165. dev_err(&acm->data->dev,
  166. "%s - usb_submit_urb(write bulk) failed: %d\n",
  167. __func__, 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. dev_dbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
  184. acm->susp_count);
  185. usb_autopm_get_interface_async(acm->control);
  186. if (acm->susp_count) {
  187. if (!acm->delayed_wb)
  188. acm->delayed_wb = wb;
  189. else
  190. usb_autopm_put_interface_async(acm->control);
  191. spin_unlock_irqrestore(&acm->write_lock, flags);
  192. return 0; /* A white lie */
  193. }
  194. usb_mark_last_busy(acm->dev);
  195. rc = acm_start_wb(acm, wb);
  196. spin_unlock_irqrestore(&acm->write_lock, flags);
  197. return rc;
  198. }
  199. /*
  200. * attributes exported through sysfs
  201. */
  202. static ssize_t show_caps
  203. (struct device *dev, struct device_attribute *attr, char *buf)
  204. {
  205. struct usb_interface *intf = to_usb_interface(dev);
  206. struct acm *acm = usb_get_intfdata(intf);
  207. return sprintf(buf, "%d", acm->ctrl_caps);
  208. }
  209. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  210. static ssize_t show_country_codes
  211. (struct device *dev, struct device_attribute *attr, char *buf)
  212. {
  213. struct usb_interface *intf = to_usb_interface(dev);
  214. struct acm *acm = usb_get_intfdata(intf);
  215. memcpy(buf, acm->country_codes, acm->country_code_size);
  216. return acm->country_code_size;
  217. }
  218. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  219. static ssize_t show_country_rel_date
  220. (struct device *dev, struct device_attribute *attr, char *buf)
  221. {
  222. struct usb_interface *intf = to_usb_interface(dev);
  223. struct acm *acm = usb_get_intfdata(intf);
  224. return sprintf(buf, "%d", acm->country_rel_date);
  225. }
  226. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  227. /*
  228. * Interrupt handlers for various ACM device responses
  229. */
  230. /* control interface reports status changes with "interrupt" transfers */
  231. static void acm_ctrl_irq(struct urb *urb)
  232. {
  233. struct acm *acm = urb->context;
  234. struct usb_cdc_notification *dr = urb->transfer_buffer;
  235. struct tty_struct *tty;
  236. unsigned char *data;
  237. int newctrl;
  238. int retval;
  239. int status = urb->status;
  240. switch (status) {
  241. case 0:
  242. /* success */
  243. break;
  244. case -ECONNRESET:
  245. case -ENOENT:
  246. case -ESHUTDOWN:
  247. /* this urb is terminated, clean up */
  248. dev_dbg(&acm->control->dev,
  249. "%s - urb shutting down with status: %d\n",
  250. __func__, status);
  251. return;
  252. default:
  253. dev_dbg(&acm->control->dev,
  254. "%s - nonzero urb status received: %d\n",
  255. __func__, status);
  256. goto exit;
  257. }
  258. if (!ACM_READY(acm))
  259. goto exit;
  260. usb_mark_last_busy(acm->dev);
  261. data = (unsigned char *)(dr + 1);
  262. switch (dr->bNotificationType) {
  263. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  264. dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
  265. __func__, dr->wValue);
  266. break;
  267. case USB_CDC_NOTIFY_SERIAL_STATE:
  268. tty = tty_port_tty_get(&acm->port);
  269. newctrl = get_unaligned_le16(data);
  270. if (tty) {
  271. if (!acm->clocal &&
  272. (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  273. dev_dbg(&acm->control->dev,
  274. "%s - calling hangup\n", __func__);
  275. tty_hangup(tty);
  276. }
  277. tty_kref_put(tty);
  278. }
  279. acm->ctrlin = newctrl;
  280. dev_dbg(&acm->control->dev,
  281. "%s - input control lines: dcd%c dsr%c break%c "
  282. "ring%c framing%c parity%c overrun%c\n",
  283. __func__,
  284. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  285. acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  286. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  287. acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  288. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  289. acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  290. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  291. break;
  292. default:
  293. dev_dbg(&acm->control->dev,
  294. "%s - unknown notification %d received: index %d "
  295. "len %d data0 %d data1 %d\n",
  296. __func__,
  297. dr->bNotificationType, dr->wIndex,
  298. dr->wLength, data[0], data[1]);
  299. break;
  300. }
  301. exit:
  302. retval = usb_submit_urb(urb, GFP_ATOMIC);
  303. if (retval)
  304. dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
  305. __func__, retval);
  306. }
  307. /* data interface returns incoming bytes, or we got unthrottled */
  308. static void acm_read_bulk(struct urb *urb)
  309. {
  310. struct acm_rb *buf;
  311. struct acm_ru *rcv = urb->context;
  312. struct acm *acm = rcv->instance;
  313. int status = urb->status;
  314. dev_dbg(&acm->data->dev, "%s - status %d\n", __func__, status);
  315. if (!ACM_READY(acm)) {
  316. dev_dbg(&acm->data->dev, "%s - acm not ready\n", __func__);
  317. return;
  318. }
  319. usb_mark_last_busy(acm->dev);
  320. if (status)
  321. dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
  322. __func__, status);
  323. buf = rcv->buffer;
  324. buf->size = urb->actual_length;
  325. if (likely(status == 0)) {
  326. spin_lock(&acm->read_lock);
  327. acm->processing++;
  328. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  329. list_add_tail(&buf->list, &acm->filled_read_bufs);
  330. spin_unlock(&acm->read_lock);
  331. } else {
  332. /* we drop the buffer due to an error */
  333. spin_lock(&acm->read_lock);
  334. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  335. list_add(&buf->list, &acm->spare_read_bufs);
  336. spin_unlock(&acm->read_lock);
  337. /* nevertheless the tasklet must be kicked unconditionally
  338. so the queue cannot dry up */
  339. }
  340. if (likely(!acm->susp_count))
  341. tasklet_schedule(&acm->urb_task);
  342. }
  343. static void acm_rx_tasklet(unsigned long _acm)
  344. {
  345. struct acm *acm = (void *)_acm;
  346. struct acm_rb *buf;
  347. struct tty_struct *tty;
  348. struct acm_ru *rcv;
  349. unsigned long flags;
  350. unsigned char throttled;
  351. dev_dbg(&acm->data->dev, "%s\n", __func__);
  352. if (!ACM_READY(acm)) {
  353. dev_dbg(&acm->data->dev, "%s - acm not ready\n", __func__);
  354. return;
  355. }
  356. spin_lock_irqsave(&acm->throttle_lock, flags);
  357. throttled = acm->throttle;
  358. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  359. if (throttled) {
  360. dev_dbg(&acm->data->dev, "%s - throttled\n", __func__);
  361. return;
  362. }
  363. tty = tty_port_tty_get(&acm->port);
  364. next_buffer:
  365. spin_lock_irqsave(&acm->read_lock, flags);
  366. if (list_empty(&acm->filled_read_bufs)) {
  367. spin_unlock_irqrestore(&acm->read_lock, flags);
  368. goto urbs;
  369. }
  370. buf = list_entry(acm->filled_read_bufs.next,
  371. struct acm_rb, list);
  372. list_del(&buf->list);
  373. spin_unlock_irqrestore(&acm->read_lock, flags);
  374. dev_dbg(&acm->data->dev, "%s - processing buf 0x%p, size = %d\n",
  375. __func__, buf, buf->size);
  376. if (tty) {
  377. spin_lock_irqsave(&acm->throttle_lock, flags);
  378. throttled = acm->throttle;
  379. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  380. if (!throttled) {
  381. tty_insert_flip_string(tty, buf->base, buf->size);
  382. tty_flip_buffer_push(tty);
  383. } else {
  384. tty_kref_put(tty);
  385. dev_dbg(&acm->data->dev, "%s - throttling noticed\n",
  386. __func__);
  387. spin_lock_irqsave(&acm->read_lock, flags);
  388. list_add(&buf->list, &acm->filled_read_bufs);
  389. spin_unlock_irqrestore(&acm->read_lock, flags);
  390. return;
  391. }
  392. }
  393. spin_lock_irqsave(&acm->read_lock, flags);
  394. list_add(&buf->list, &acm->spare_read_bufs);
  395. spin_unlock_irqrestore(&acm->read_lock, flags);
  396. goto next_buffer;
  397. urbs:
  398. tty_kref_put(tty);
  399. while (!list_empty(&acm->spare_read_bufs)) {
  400. spin_lock_irqsave(&acm->read_lock, flags);
  401. if (list_empty(&acm->spare_read_urbs)) {
  402. acm->processing = 0;
  403. spin_unlock_irqrestore(&acm->read_lock, flags);
  404. return;
  405. }
  406. rcv = list_entry(acm->spare_read_urbs.next,
  407. struct acm_ru, list);
  408. list_del(&rcv->list);
  409. spin_unlock_irqrestore(&acm->read_lock, flags);
  410. buf = list_entry(acm->spare_read_bufs.next,
  411. struct acm_rb, list);
  412. list_del(&buf->list);
  413. rcv->buffer = buf;
  414. if (acm->is_int_ep)
  415. usb_fill_int_urb(rcv->urb, acm->dev,
  416. acm->rx_endpoint,
  417. buf->base,
  418. acm->readsize,
  419. acm_read_bulk, rcv, acm->bInterval);
  420. else
  421. usb_fill_bulk_urb(rcv->urb, acm->dev,
  422. acm->rx_endpoint,
  423. buf->base,
  424. acm->readsize,
  425. acm_read_bulk, rcv);
  426. rcv->urb->transfer_dma = buf->dma;
  427. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  428. /* This shouldn't kill the driver as unsuccessful URBs are
  429. returned to the free-urbs-pool and resubmited ASAP */
  430. spin_lock_irqsave(&acm->read_lock, flags);
  431. if (acm->susp_count ||
  432. usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  433. list_add(&buf->list, &acm->spare_read_bufs);
  434. list_add(&rcv->list, &acm->spare_read_urbs);
  435. acm->processing = 0;
  436. spin_unlock_irqrestore(&acm->read_lock, flags);
  437. return;
  438. } else {
  439. spin_unlock_irqrestore(&acm->read_lock, flags);
  440. dev_dbg(&acm->data->dev,
  441. "%s - sending urb 0x%p, rcv 0x%p, buf 0x%p\n",
  442. __func__, rcv->urb, rcv, buf);
  443. }
  444. }
  445. spin_lock_irqsave(&acm->read_lock, flags);
  446. acm->processing = 0;
  447. spin_unlock_irqrestore(&acm->read_lock, flags);
  448. }
  449. /* data interface wrote those outgoing bytes */
  450. static void acm_write_bulk(struct urb *urb)
  451. {
  452. struct acm_wb *wb = urb->context;
  453. struct acm *acm = wb->instance;
  454. unsigned long flags;
  455. if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
  456. dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
  457. __func__,
  458. urb->actual_length,
  459. urb->transfer_buffer_length,
  460. urb->status);
  461. spin_lock_irqsave(&acm->write_lock, flags);
  462. acm_write_done(acm, wb);
  463. spin_unlock_irqrestore(&acm->write_lock, flags);
  464. if (ACM_READY(acm))
  465. schedule_work(&acm->work);
  466. else
  467. wake_up_interruptible(&acm->drain_wait);
  468. }
  469. static void acm_softint(struct work_struct *work)
  470. {
  471. struct acm *acm = container_of(work, struct acm, work);
  472. struct tty_struct *tty;
  473. dev_vdbg(&acm->data->dev, "%s\n", __func__);
  474. if (!ACM_READY(acm))
  475. return;
  476. tty = tty_port_tty_get(&acm->port);
  477. if (!tty)
  478. return;
  479. tty_wakeup(tty);
  480. tty_kref_put(tty);
  481. }
  482. /*
  483. * TTY handlers
  484. */
  485. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  486. {
  487. struct acm *acm;
  488. int rv = -ENODEV;
  489. int i;
  490. mutex_lock(&open_mutex);
  491. acm = acm_table[tty->index];
  492. if (!acm || !acm->dev)
  493. goto out;
  494. else
  495. rv = 0;
  496. dev_dbg(&acm->control->dev, "%s\n", __func__);
  497. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  498. tty->driver_data = acm;
  499. tty_port_tty_set(&acm->port, tty);
  500. if (usb_autopm_get_interface(acm->control) < 0)
  501. goto early_bail;
  502. else
  503. acm->control->needs_remote_wakeup = 1;
  504. mutex_lock(&acm->mutex);
  505. if (acm->port.count++) {
  506. mutex_unlock(&acm->mutex);
  507. usb_autopm_put_interface(acm->control);
  508. goto out;
  509. }
  510. acm->ctrlurb->dev = acm->dev;
  511. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  512. dev_err(&acm->control->dev,
  513. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  514. goto bail_out;
  515. }
  516. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  517. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  518. goto full_bailout;
  519. usb_autopm_put_interface(acm->control);
  520. INIT_LIST_HEAD(&acm->spare_read_urbs);
  521. INIT_LIST_HEAD(&acm->spare_read_bufs);
  522. INIT_LIST_HEAD(&acm->filled_read_bufs);
  523. for (i = 0; i < acm->rx_buflimit; i++)
  524. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  525. for (i = 0; i < acm->rx_buflimit; i++)
  526. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  527. acm->throttle = 0;
  528. set_bit(ASYNCB_INITIALIZED, &acm->port.flags);
  529. rv = tty_port_block_til_ready(&acm->port, tty, filp);
  530. tasklet_schedule(&acm->urb_task);
  531. mutex_unlock(&acm->mutex);
  532. out:
  533. mutex_unlock(&open_mutex);
  534. return rv;
  535. full_bailout:
  536. usb_kill_urb(acm->ctrlurb);
  537. bail_out:
  538. acm->port.count--;
  539. mutex_unlock(&acm->mutex);
  540. usb_autopm_put_interface(acm->control);
  541. early_bail:
  542. mutex_unlock(&open_mutex);
  543. tty_port_tty_set(&acm->port, NULL);
  544. return -EIO;
  545. }
  546. static void acm_tty_unregister(struct acm *acm)
  547. {
  548. int i, nr;
  549. nr = acm->rx_buflimit;
  550. tty_unregister_device(acm_tty_driver, acm->minor);
  551. usb_put_intf(acm->control);
  552. acm_table[acm->minor] = NULL;
  553. usb_free_urb(acm->ctrlurb);
  554. for (i = 0; i < ACM_NW; i++)
  555. usb_free_urb(acm->wb[i].urb);
  556. for (i = 0; i < nr; i++)
  557. usb_free_urb(acm->ru[i].urb);
  558. kfree(acm->country_codes);
  559. kfree(acm);
  560. }
  561. static int acm_tty_chars_in_buffer(struct tty_struct *tty);
  562. static void acm_port_down(struct acm *acm)
  563. {
  564. int i, nr = acm->rx_buflimit;
  565. mutex_lock(&open_mutex);
  566. if (acm->dev) {
  567. usb_autopm_get_interface(acm->control);
  568. acm_set_control(acm, acm->ctrlout = 0);
  569. usb_kill_urb(acm->ctrlurb);
  570. for (i = 0; i < ACM_NW; i++)
  571. usb_kill_urb(acm->wb[i].urb);
  572. tasklet_disable(&acm->urb_task);
  573. for (i = 0; i < nr; i++)
  574. usb_kill_urb(acm->ru[i].urb);
  575. tasklet_enable(&acm->urb_task);
  576. acm->control->needs_remote_wakeup = 0;
  577. usb_autopm_put_interface(acm->control);
  578. }
  579. mutex_unlock(&open_mutex);
  580. }
  581. static void acm_tty_hangup(struct tty_struct *tty)
  582. {
  583. struct acm *acm = tty->driver_data;
  584. tty_port_hangup(&acm->port);
  585. acm_port_down(acm);
  586. }
  587. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  588. {
  589. struct acm *acm = tty->driver_data;
  590. /* Perform the closing process and see if we need to do the hardware
  591. shutdown */
  592. if (!acm)
  593. return;
  594. if (tty_port_close_start(&acm->port, tty, filp) == 0) {
  595. mutex_lock(&open_mutex);
  596. if (!acm->dev) {
  597. tty_port_tty_set(&acm->port, NULL);
  598. acm_tty_unregister(acm);
  599. tty->driver_data = NULL;
  600. }
  601. mutex_unlock(&open_mutex);
  602. return;
  603. }
  604. acm_port_down(acm);
  605. tty_port_close_end(&acm->port, tty);
  606. tty_port_tty_set(&acm->port, NULL);
  607. }
  608. static int acm_tty_write(struct tty_struct *tty,
  609. const unsigned char *buf, int count)
  610. {
  611. struct acm *acm = tty->driver_data;
  612. int stat;
  613. unsigned long flags;
  614. int wbn;
  615. struct acm_wb *wb;
  616. if (!ACM_READY(acm))
  617. return -EINVAL;
  618. if (!count)
  619. return 0;
  620. dev_dbg(&acm->data->dev, "%s - count %d\n", __func__, count);
  621. spin_lock_irqsave(&acm->write_lock, flags);
  622. wbn = acm_wb_alloc(acm);
  623. if (wbn < 0) {
  624. spin_unlock_irqrestore(&acm->write_lock, flags);
  625. return 0;
  626. }
  627. wb = &acm->wb[wbn];
  628. count = (count > acm->writesize) ? acm->writesize : count;
  629. dev_dbg(&acm->data->dev, "%s - write %d\n", __func__, count);
  630. memcpy(wb->buf, buf, count);
  631. wb->len = count;
  632. spin_unlock_irqrestore(&acm->write_lock, flags);
  633. stat = acm_write_start(acm, wbn);
  634. if (stat < 0)
  635. return stat;
  636. return count;
  637. }
  638. static int acm_tty_write_room(struct tty_struct *tty)
  639. {
  640. struct acm *acm = tty->driver_data;
  641. if (!ACM_READY(acm))
  642. return -EINVAL;
  643. /*
  644. * Do not let the line discipline to know that we have a reserve,
  645. * or it might get too enthusiastic.
  646. */
  647. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  648. }
  649. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  650. {
  651. struct acm *acm = tty->driver_data;
  652. if (!ACM_READY(acm))
  653. return 0;
  654. /*
  655. * This is inaccurate (overcounts), but it works.
  656. */
  657. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  658. }
  659. static void acm_tty_throttle(struct tty_struct *tty)
  660. {
  661. struct acm *acm = tty->driver_data;
  662. if (!ACM_READY(acm))
  663. return;
  664. spin_lock_bh(&acm->throttle_lock);
  665. acm->throttle = 1;
  666. spin_unlock_bh(&acm->throttle_lock);
  667. }
  668. static void acm_tty_unthrottle(struct tty_struct *tty)
  669. {
  670. struct acm *acm = tty->driver_data;
  671. if (!ACM_READY(acm))
  672. return;
  673. spin_lock_bh(&acm->throttle_lock);
  674. acm->throttle = 0;
  675. spin_unlock_bh(&acm->throttle_lock);
  676. tasklet_schedule(&acm->urb_task);
  677. }
  678. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  679. {
  680. struct acm *acm = tty->driver_data;
  681. int retval;
  682. if (!ACM_READY(acm))
  683. return -EINVAL;
  684. retval = acm_send_break(acm, state ? 0xffff : 0);
  685. if (retval < 0)
  686. dev_dbg(&acm->control->dev, "%s - send break failed\n",
  687. __func__);
  688. return retval;
  689. }
  690. static int acm_tty_tiocmget(struct tty_struct *tty)
  691. {
  692. struct acm *acm = tty->driver_data;
  693. if (!ACM_READY(acm))
  694. return -EINVAL;
  695. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  696. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  697. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  698. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  699. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  700. TIOCM_CTS;
  701. }
  702. static int acm_tty_tiocmset(struct tty_struct *tty,
  703. unsigned int set, unsigned int clear)
  704. {
  705. struct acm *acm = tty->driver_data;
  706. unsigned int newctrl;
  707. if (!ACM_READY(acm))
  708. return -EINVAL;
  709. newctrl = acm->ctrlout;
  710. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  711. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  712. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  713. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  714. newctrl = (newctrl & ~clear) | set;
  715. if (acm->ctrlout == newctrl)
  716. return 0;
  717. return acm_set_control(acm, acm->ctrlout = newctrl);
  718. }
  719. static int acm_tty_ioctl(struct tty_struct *tty,
  720. unsigned int cmd, unsigned long arg)
  721. {
  722. struct acm *acm = tty->driver_data;
  723. if (!ACM_READY(acm))
  724. return -EINVAL;
  725. return -ENOIOCTLCMD;
  726. }
  727. static const __u32 acm_tty_speed[] = {
  728. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  729. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  730. 57600, 115200, 230400, 460800, 500000, 576000,
  731. 921600, 1000000, 1152000, 1500000, 2000000,
  732. 2500000, 3000000, 3500000, 4000000
  733. };
  734. static const __u8 acm_tty_size[] = {
  735. 5, 6, 7, 8
  736. };
  737. static void acm_tty_set_termios(struct tty_struct *tty,
  738. struct ktermios *termios_old)
  739. {
  740. struct acm *acm = tty->driver_data;
  741. struct ktermios *termios = tty->termios;
  742. struct usb_cdc_line_coding newline;
  743. int newctrl = acm->ctrlout;
  744. if (!ACM_READY(acm))
  745. return;
  746. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  747. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  748. newline.bParityType = termios->c_cflag & PARENB ?
  749. (termios->c_cflag & PARODD ? 1 : 2) +
  750. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  751. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  752. /* FIXME: Needs to clear unsupported bits in the termios */
  753. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  754. if (!newline.dwDTERate) {
  755. newline.dwDTERate = acm->line.dwDTERate;
  756. newctrl &= ~ACM_CTRL_DTR;
  757. } else
  758. newctrl |= ACM_CTRL_DTR;
  759. if (newctrl != acm->ctrlout)
  760. acm_set_control(acm, acm->ctrlout = newctrl);
  761. if (memcmp(&acm->line, &newline, sizeof newline)) {
  762. memcpy(&acm->line, &newline, sizeof newline);
  763. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  764. __func__,
  765. le32_to_cpu(newline.dwDTERate),
  766. newline.bCharFormat, newline.bParityType,
  767. newline.bDataBits);
  768. acm_set_line(acm, &acm->line);
  769. }
  770. }
  771. /*
  772. * USB probe and disconnect routines.
  773. */
  774. /* Little helpers: write/read buffers free */
  775. static void acm_write_buffers_free(struct acm *acm)
  776. {
  777. int i;
  778. struct acm_wb *wb;
  779. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  780. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  781. usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
  782. }
  783. static void acm_read_buffers_free(struct acm *acm)
  784. {
  785. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  786. int i, n = acm->rx_buflimit;
  787. for (i = 0; i < n; i++)
  788. usb_free_coherent(usb_dev, acm->readsize,
  789. acm->rb[i].base, acm->rb[i].dma);
  790. }
  791. /* Little helper: write buffers allocate */
  792. static int acm_write_buffers_alloc(struct acm *acm)
  793. {
  794. int i;
  795. struct acm_wb *wb;
  796. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  797. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  798. &wb->dmah);
  799. if (!wb->buf) {
  800. while (i != 0) {
  801. --i;
  802. --wb;
  803. usb_free_coherent(acm->dev, acm->writesize,
  804. wb->buf, wb->dmah);
  805. }
  806. return -ENOMEM;
  807. }
  808. }
  809. return 0;
  810. }
  811. static int acm_probe(struct usb_interface *intf,
  812. const struct usb_device_id *id)
  813. {
  814. struct usb_cdc_union_desc *union_header = NULL;
  815. struct usb_cdc_country_functional_desc *cfd = NULL;
  816. unsigned char *buffer = intf->altsetting->extra;
  817. int buflen = intf->altsetting->extralen;
  818. struct usb_interface *control_interface;
  819. struct usb_interface *data_interface;
  820. struct usb_endpoint_descriptor *epctrl = NULL;
  821. struct usb_endpoint_descriptor *epread = NULL;
  822. struct usb_endpoint_descriptor *epwrite = NULL;
  823. struct usb_device *usb_dev = interface_to_usbdev(intf);
  824. struct acm *acm;
  825. int minor;
  826. int ctrlsize, readsize;
  827. u8 *buf;
  828. u8 ac_management_function = 0;
  829. u8 call_management_function = 0;
  830. int call_interface_num = -1;
  831. int data_interface_num;
  832. unsigned long quirks;
  833. int num_rx_buf;
  834. int i;
  835. int combined_interfaces = 0;
  836. /* normal quirks */
  837. quirks = (unsigned long)id->driver_info;
  838. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  839. /* handle quirks deadly to normal probing*/
  840. if (quirks == NO_UNION_NORMAL) {
  841. data_interface = usb_ifnum_to_if(usb_dev, 1);
  842. control_interface = usb_ifnum_to_if(usb_dev, 0);
  843. goto skip_normal_probe;
  844. }
  845. /* normal probing*/
  846. if (!buffer) {
  847. dev_err(&intf->dev, "Weird descriptor references\n");
  848. return -EINVAL;
  849. }
  850. if (!buflen) {
  851. if (intf->cur_altsetting->endpoint &&
  852. intf->cur_altsetting->endpoint->extralen &&
  853. intf->cur_altsetting->endpoint->extra) {
  854. dev_dbg(&intf->dev,
  855. "Seeking extra descriptors on endpoint\n");
  856. buflen = intf->cur_altsetting->endpoint->extralen;
  857. buffer = intf->cur_altsetting->endpoint->extra;
  858. } else {
  859. dev_err(&intf->dev,
  860. "Zero length descriptor references\n");
  861. return -EINVAL;
  862. }
  863. }
  864. while (buflen > 0) {
  865. if (buffer[1] != USB_DT_CS_INTERFACE) {
  866. dev_err(&intf->dev, "skipping garbage\n");
  867. goto next_desc;
  868. }
  869. switch (buffer[2]) {
  870. case USB_CDC_UNION_TYPE: /* we've found it */
  871. if (union_header) {
  872. dev_err(&intf->dev, "More than one "
  873. "union descriptor, skipping ...\n");
  874. goto next_desc;
  875. }
  876. union_header = (struct usb_cdc_union_desc *)buffer;
  877. break;
  878. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  879. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  880. break;
  881. case USB_CDC_HEADER_TYPE: /* maybe check version */
  882. break; /* for now we ignore it */
  883. case USB_CDC_ACM_TYPE:
  884. ac_management_function = buffer[3];
  885. break;
  886. case USB_CDC_CALL_MANAGEMENT_TYPE:
  887. call_management_function = buffer[3];
  888. call_interface_num = buffer[4];
  889. if ( (quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
  890. dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
  891. break;
  892. default:
  893. /* there are LOTS more CDC descriptors that
  894. * could legitimately be found here.
  895. */
  896. dev_dbg(&intf->dev, "Ignoring descriptor: "
  897. "type %02x, length %d\n",
  898. buffer[2], buffer[0]);
  899. break;
  900. }
  901. next_desc:
  902. buflen -= buffer[0];
  903. buffer += buffer[0];
  904. }
  905. if (!union_header) {
  906. if (call_interface_num > 0) {
  907. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  908. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  909. control_interface = intf;
  910. } else {
  911. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  912. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  913. return -ENODEV;
  914. } else {
  915. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  916. combined_interfaces = 1;
  917. control_interface = data_interface = intf;
  918. goto look_for_collapsed_interface;
  919. }
  920. }
  921. } else {
  922. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  923. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  924. if (!control_interface || !data_interface) {
  925. dev_dbg(&intf->dev, "no interfaces\n");
  926. return -ENODEV;
  927. }
  928. }
  929. if (data_interface_num != call_interface_num)
  930. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  931. if (control_interface == data_interface) {
  932. /* some broken devices designed for windows work this way */
  933. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  934. combined_interfaces = 1;
  935. /* a popular other OS doesn't use it */
  936. quirks |= NO_CAP_LINE;
  937. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  938. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  939. return -EINVAL;
  940. }
  941. look_for_collapsed_interface:
  942. for (i = 0; i < 3; i++) {
  943. struct usb_endpoint_descriptor *ep;
  944. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  945. if (usb_endpoint_is_int_in(ep))
  946. epctrl = ep;
  947. else if (usb_endpoint_is_bulk_out(ep))
  948. epwrite = ep;
  949. else if (usb_endpoint_is_bulk_in(ep))
  950. epread = ep;
  951. else
  952. return -EINVAL;
  953. }
  954. if (!epctrl || !epread || !epwrite)
  955. return -ENODEV;
  956. else
  957. goto made_compressed_probe;
  958. }
  959. skip_normal_probe:
  960. /*workaround for switched interfaces */
  961. if (data_interface->cur_altsetting->desc.bInterfaceClass
  962. != CDC_DATA_INTERFACE_TYPE) {
  963. if (control_interface->cur_altsetting->desc.bInterfaceClass
  964. == CDC_DATA_INTERFACE_TYPE) {
  965. struct usb_interface *t;
  966. dev_dbg(&intf->dev,
  967. "Your device has switched interfaces.\n");
  968. t = control_interface;
  969. control_interface = data_interface;
  970. data_interface = t;
  971. } else {
  972. return -EINVAL;
  973. }
  974. }
  975. /* Accept probe requests only for the control interface */
  976. if (!combined_interfaces && intf != control_interface)
  977. return -ENODEV;
  978. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  979. /* valid in this context */
  980. dev_dbg(&intf->dev, "The data interface isn't available\n");
  981. return -EBUSY;
  982. }
  983. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  984. return -EINVAL;
  985. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  986. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  987. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  988. /* workaround for switched endpoints */
  989. if (!usb_endpoint_dir_in(epread)) {
  990. /* descriptors are swapped */
  991. struct usb_endpoint_descriptor *t;
  992. dev_dbg(&intf->dev,
  993. "The data interface has switched endpoints\n");
  994. t = epread;
  995. epread = epwrite;
  996. epwrite = t;
  997. }
  998. made_compressed_probe:
  999. dev_dbg(&intf->dev, "interfaces are valid\n");
  1000. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  1001. if (minor == ACM_TTY_MINORS) {
  1002. dev_err(&intf->dev, "no more free acm devices\n");
  1003. return -ENODEV;
  1004. }
  1005. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  1006. if (acm == NULL) {
  1007. dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
  1008. goto alloc_fail;
  1009. }
  1010. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  1011. readsize = le16_to_cpu(epread->wMaxPacketSize) *
  1012. (quirks == SINGLE_RX_URB ? 1 : 2);
  1013. acm->combined_interfaces = combined_interfaces;
  1014. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
  1015. acm->control = control_interface;
  1016. acm->data = data_interface;
  1017. acm->minor = minor;
  1018. acm->dev = usb_dev;
  1019. acm->ctrl_caps = ac_management_function;
  1020. if (quirks & NO_CAP_LINE)
  1021. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1022. acm->ctrlsize = ctrlsize;
  1023. acm->readsize = readsize;
  1024. acm->rx_buflimit = num_rx_buf;
  1025. acm->urb_task.func = acm_rx_tasklet;
  1026. acm->urb_task.data = (unsigned long) acm;
  1027. INIT_WORK(&acm->work, acm_softint);
  1028. init_waitqueue_head(&acm->drain_wait);
  1029. spin_lock_init(&acm->throttle_lock);
  1030. spin_lock_init(&acm->write_lock);
  1031. spin_lock_init(&acm->read_lock);
  1032. mutex_init(&acm->mutex);
  1033. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  1034. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  1035. if (acm->is_int_ep)
  1036. acm->bInterval = epread->bInterval;
  1037. tty_port_init(&acm->port);
  1038. acm->port.ops = &acm_port_ops;
  1039. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1040. if (!buf) {
  1041. dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  1042. goto alloc_fail2;
  1043. }
  1044. acm->ctrl_buffer = buf;
  1045. if (acm_write_buffers_alloc(acm) < 0) {
  1046. dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
  1047. goto alloc_fail4;
  1048. }
  1049. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1050. if (!acm->ctrlurb) {
  1051. dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  1052. goto alloc_fail5;
  1053. }
  1054. for (i = 0; i < num_rx_buf; i++) {
  1055. struct acm_ru *rcv = &(acm->ru[i]);
  1056. rcv->urb = usb_alloc_urb(0, GFP_KERNEL);
  1057. if (rcv->urb == NULL) {
  1058. dev_err(&intf->dev,
  1059. "out of memory (read urbs usb_alloc_urb)\n");
  1060. goto alloc_fail6;
  1061. }
  1062. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1063. rcv->instance = acm;
  1064. }
  1065. for (i = 0; i < num_rx_buf; i++) {
  1066. struct acm_rb *rb = &(acm->rb[i]);
  1067. rb->base = usb_alloc_coherent(acm->dev, readsize,
  1068. GFP_KERNEL, &rb->dma);
  1069. if (!rb->base) {
  1070. dev_err(&intf->dev,
  1071. "out of memory (read bufs usb_alloc_coherent)\n");
  1072. goto alloc_fail7;
  1073. }
  1074. }
  1075. for (i = 0; i < ACM_NW; i++) {
  1076. struct acm_wb *snd = &(acm->wb[i]);
  1077. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1078. if (snd->urb == NULL) {
  1079. dev_err(&intf->dev,
  1080. "out of memory (write urbs usb_alloc_urb)\n");
  1081. goto alloc_fail8;
  1082. }
  1083. if (usb_endpoint_xfer_int(epwrite))
  1084. usb_fill_int_urb(snd->urb, usb_dev,
  1085. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1086. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1087. else
  1088. usb_fill_bulk_urb(snd->urb, usb_dev,
  1089. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1090. NULL, acm->writesize, acm_write_bulk, snd);
  1091. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1092. snd->instance = acm;
  1093. }
  1094. usb_set_intfdata(intf, acm);
  1095. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1096. if (i < 0)
  1097. goto alloc_fail8;
  1098. if (cfd) { /* export the country data */
  1099. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1100. if (!acm->country_codes)
  1101. goto skip_countries;
  1102. acm->country_code_size = cfd->bLength - 4;
  1103. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1104. cfd->bLength - 4);
  1105. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1106. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1107. if (i < 0) {
  1108. kfree(acm->country_codes);
  1109. goto skip_countries;
  1110. }
  1111. i = device_create_file(&intf->dev,
  1112. &dev_attr_iCountryCodeRelDate);
  1113. if (i < 0) {
  1114. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1115. kfree(acm->country_codes);
  1116. goto skip_countries;
  1117. }
  1118. }
  1119. skip_countries:
  1120. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1121. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1122. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1123. /* works around buggy devices */
  1124. epctrl->bInterval ? epctrl->bInterval : 0xff);
  1125. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1126. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1127. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1128. acm_set_control(acm, acm->ctrlout);
  1129. acm->line.dwDTERate = cpu_to_le32(9600);
  1130. acm->line.bDataBits = 8;
  1131. acm_set_line(acm, &acm->line);
  1132. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1133. usb_set_intfdata(data_interface, acm);
  1134. usb_get_intf(control_interface);
  1135. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1136. acm_table[minor] = acm;
  1137. return 0;
  1138. alloc_fail8:
  1139. for (i = 0; i < ACM_NW; i++)
  1140. usb_free_urb(acm->wb[i].urb);
  1141. alloc_fail7:
  1142. acm_read_buffers_free(acm);
  1143. alloc_fail6:
  1144. for (i = 0; i < num_rx_buf; i++)
  1145. usb_free_urb(acm->ru[i].urb);
  1146. usb_free_urb(acm->ctrlurb);
  1147. alloc_fail5:
  1148. acm_write_buffers_free(acm);
  1149. alloc_fail4:
  1150. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1151. alloc_fail2:
  1152. kfree(acm);
  1153. alloc_fail:
  1154. return -ENOMEM;
  1155. }
  1156. static void stop_data_traffic(struct acm *acm)
  1157. {
  1158. int i;
  1159. dev_dbg(&acm->control->dev, "%s\n", __func__);
  1160. tasklet_disable(&acm->urb_task);
  1161. usb_kill_urb(acm->ctrlurb);
  1162. for (i = 0; i < ACM_NW; i++)
  1163. usb_kill_urb(acm->wb[i].urb);
  1164. for (i = 0; i < acm->rx_buflimit; i++)
  1165. usb_kill_urb(acm->ru[i].urb);
  1166. tasklet_enable(&acm->urb_task);
  1167. cancel_work_sync(&acm->work);
  1168. }
  1169. static void acm_disconnect(struct usb_interface *intf)
  1170. {
  1171. struct acm *acm = usb_get_intfdata(intf);
  1172. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1173. struct tty_struct *tty;
  1174. /* sibling interface is already cleaning up */
  1175. if (!acm)
  1176. return;
  1177. mutex_lock(&open_mutex);
  1178. if (acm->country_codes) {
  1179. device_remove_file(&acm->control->dev,
  1180. &dev_attr_wCountryCodes);
  1181. device_remove_file(&acm->control->dev,
  1182. &dev_attr_iCountryCodeRelDate);
  1183. }
  1184. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1185. acm->dev = NULL;
  1186. usb_set_intfdata(acm->control, NULL);
  1187. usb_set_intfdata(acm->data, NULL);
  1188. stop_data_traffic(acm);
  1189. acm_write_buffers_free(acm);
  1190. usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer,
  1191. acm->ctrl_dma);
  1192. acm_read_buffers_free(acm);
  1193. if (!acm->combined_interfaces)
  1194. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1195. acm->data : acm->control);
  1196. if (acm->port.count == 0) {
  1197. acm_tty_unregister(acm);
  1198. mutex_unlock(&open_mutex);
  1199. return;
  1200. }
  1201. mutex_unlock(&open_mutex);
  1202. tty = tty_port_tty_get(&acm->port);
  1203. if (tty) {
  1204. tty_hangup(tty);
  1205. tty_kref_put(tty);
  1206. }
  1207. }
  1208. #ifdef CONFIG_PM
  1209. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1210. {
  1211. struct acm *acm = usb_get_intfdata(intf);
  1212. int cnt;
  1213. if (message.event & PM_EVENT_AUTO) {
  1214. int b;
  1215. spin_lock_irq(&acm->read_lock);
  1216. spin_lock(&acm->write_lock);
  1217. b = acm->processing + acm->transmitting;
  1218. spin_unlock(&acm->write_lock);
  1219. spin_unlock_irq(&acm->read_lock);
  1220. if (b)
  1221. return -EBUSY;
  1222. }
  1223. spin_lock_irq(&acm->read_lock);
  1224. spin_lock(&acm->write_lock);
  1225. cnt = acm->susp_count++;
  1226. spin_unlock(&acm->write_lock);
  1227. spin_unlock_irq(&acm->read_lock);
  1228. if (cnt)
  1229. return 0;
  1230. /*
  1231. we treat opened interfaces differently,
  1232. we must guard against open
  1233. */
  1234. mutex_lock(&acm->mutex);
  1235. if (acm->port.count)
  1236. stop_data_traffic(acm);
  1237. mutex_unlock(&acm->mutex);
  1238. return 0;
  1239. }
  1240. static int acm_resume(struct usb_interface *intf)
  1241. {
  1242. struct acm *acm = usb_get_intfdata(intf);
  1243. struct acm_wb *wb;
  1244. int rv = 0;
  1245. int cnt;
  1246. spin_lock_irq(&acm->read_lock);
  1247. acm->susp_count -= 1;
  1248. cnt = acm->susp_count;
  1249. spin_unlock_irq(&acm->read_lock);
  1250. if (cnt)
  1251. return 0;
  1252. mutex_lock(&acm->mutex);
  1253. if (acm->port.count) {
  1254. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1255. spin_lock_irq(&acm->write_lock);
  1256. if (acm->delayed_wb) {
  1257. wb = acm->delayed_wb;
  1258. acm->delayed_wb = NULL;
  1259. spin_unlock_irq(&acm->write_lock);
  1260. acm_start_wb(acm, wb);
  1261. } else {
  1262. spin_unlock_irq(&acm->write_lock);
  1263. }
  1264. /*
  1265. * delayed error checking because we must
  1266. * do the write path at all cost
  1267. */
  1268. if (rv < 0)
  1269. goto err_out;
  1270. tasklet_schedule(&acm->urb_task);
  1271. }
  1272. err_out:
  1273. mutex_unlock(&acm->mutex);
  1274. return rv;
  1275. }
  1276. static int acm_reset_resume(struct usb_interface *intf)
  1277. {
  1278. struct acm *acm = usb_get_intfdata(intf);
  1279. struct tty_struct *tty;
  1280. mutex_lock(&acm->mutex);
  1281. if (acm->port.count) {
  1282. tty = tty_port_tty_get(&acm->port);
  1283. if (tty) {
  1284. tty_hangup(tty);
  1285. tty_kref_put(tty);
  1286. }
  1287. }
  1288. mutex_unlock(&acm->mutex);
  1289. return acm_resume(intf);
  1290. }
  1291. #endif /* CONFIG_PM */
  1292. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1293. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1294. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1295. USB_CDC_ACM_PROTO_VENDOR)
  1296. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1297. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1298. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1299. USB_CDC_ACM_PROTO_VENDOR)
  1300. /*
  1301. * USB driver structure.
  1302. */
  1303. static const struct usb_device_id acm_ids[] = {
  1304. /* quirky and broken devices */
  1305. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1306. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1307. },
  1308. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1309. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1310. },
  1311. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1312. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1313. },
  1314. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1315. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1316. },
  1317. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1318. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1319. },
  1320. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1321. .driver_info = SINGLE_RX_URB,
  1322. },
  1323. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1324. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1325. },
  1326. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1327. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1328. },
  1329. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1330. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1331. },
  1332. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1333. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1334. },
  1335. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1336. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1337. },
  1338. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1339. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1340. },
  1341. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1342. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1343. },
  1344. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1345. },
  1346. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1347. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1348. data interface instead of
  1349. communications interface.
  1350. Maybe we should define a new
  1351. quirk for this. */
  1352. },
  1353. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1354. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1355. },
  1356. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1357. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1358. },
  1359. /* Nokia S60 phones expose two ACM channels. The first is
  1360. * a modem and is picked up by the standard AT-command
  1361. * information below. The second is 'vendor-specific' but
  1362. * is treated as a serial device at the S60 end, so we want
  1363. * to expose it on Linux too. */
  1364. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1365. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1366. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1367. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1368. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1369. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1370. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1371. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1372. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1373. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1374. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1375. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1376. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1377. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1378. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1379. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1380. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1381. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1382. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1383. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1384. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1385. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1386. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1387. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1388. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1389. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1390. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1391. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1392. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1393. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1394. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1395. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1396. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1397. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1398. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1399. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1400. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1401. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1402. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1403. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1404. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1405. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1406. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1407. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1408. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1409. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1410. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1411. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1412. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1413. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1414. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1415. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1416. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1417. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1418. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1419. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1420. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1421. /* Support Lego NXT using pbLua firmware */
  1422. { USB_DEVICE(0x0694, 0xff00),
  1423. .driver_info = NOT_A_MODEM,
  1424. },
  1425. /* control interfaces without any protocol set */
  1426. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1427. USB_CDC_PROTO_NONE) },
  1428. /* control interfaces with various AT-command sets */
  1429. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1430. USB_CDC_ACM_PROTO_AT_V25TER) },
  1431. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1432. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1433. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1434. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1435. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1436. USB_CDC_ACM_PROTO_AT_GSM) },
  1437. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1438. USB_CDC_ACM_PROTO_AT_3G) },
  1439. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1440. USB_CDC_ACM_PROTO_AT_CDMA) },
  1441. { }
  1442. };
  1443. MODULE_DEVICE_TABLE(usb, acm_ids);
  1444. static struct usb_driver acm_driver = {
  1445. .name = "cdc_acm",
  1446. .probe = acm_probe,
  1447. .disconnect = acm_disconnect,
  1448. #ifdef CONFIG_PM
  1449. .suspend = acm_suspend,
  1450. .resume = acm_resume,
  1451. .reset_resume = acm_reset_resume,
  1452. #endif
  1453. .id_table = acm_ids,
  1454. #ifdef CONFIG_PM
  1455. .supports_autosuspend = 1,
  1456. #endif
  1457. };
  1458. /*
  1459. * TTY driver structures.
  1460. */
  1461. static const struct tty_operations acm_ops = {
  1462. .open = acm_tty_open,
  1463. .close = acm_tty_close,
  1464. .hangup = acm_tty_hangup,
  1465. .write = acm_tty_write,
  1466. .write_room = acm_tty_write_room,
  1467. .ioctl = acm_tty_ioctl,
  1468. .throttle = acm_tty_throttle,
  1469. .unthrottle = acm_tty_unthrottle,
  1470. .chars_in_buffer = acm_tty_chars_in_buffer,
  1471. .break_ctl = acm_tty_break_ctl,
  1472. .set_termios = acm_tty_set_termios,
  1473. .tiocmget = acm_tty_tiocmget,
  1474. .tiocmset = acm_tty_tiocmset,
  1475. };
  1476. /*
  1477. * Init / exit.
  1478. */
  1479. static int __init acm_init(void)
  1480. {
  1481. int retval;
  1482. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1483. if (!acm_tty_driver)
  1484. return -ENOMEM;
  1485. acm_tty_driver->owner = THIS_MODULE,
  1486. acm_tty_driver->driver_name = "acm",
  1487. acm_tty_driver->name = "ttyACM",
  1488. acm_tty_driver->major = ACM_TTY_MAJOR,
  1489. acm_tty_driver->minor_start = 0,
  1490. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1491. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1492. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1493. acm_tty_driver->init_termios = tty_std_termios;
  1494. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1495. HUPCL | CLOCAL;
  1496. tty_set_operations(acm_tty_driver, &acm_ops);
  1497. retval = tty_register_driver(acm_tty_driver);
  1498. if (retval) {
  1499. put_tty_driver(acm_tty_driver);
  1500. return retval;
  1501. }
  1502. retval = usb_register(&acm_driver);
  1503. if (retval) {
  1504. tty_unregister_driver(acm_tty_driver);
  1505. put_tty_driver(acm_tty_driver);
  1506. return retval;
  1507. }
  1508. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1509. DRIVER_DESC "\n");
  1510. return 0;
  1511. }
  1512. static void __exit acm_exit(void)
  1513. {
  1514. usb_deregister(&acm_driver);
  1515. tty_unregister_driver(acm_tty_driver);
  1516. put_tty_driver(acm_tty_driver);
  1517. }
  1518. module_init(acm_init);
  1519. module_exit(acm_exit);
  1520. MODULE_AUTHOR(DRIVER_AUTHOR);
  1521. MODULE_DESCRIPTION(DRIVER_DESC);
  1522. MODULE_LICENSE("GPL");
  1523. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);