cdc-acm.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  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. * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
  11. *
  12. * USB Abstract Control Model driver for USB modems and ISDN adapters
  13. *
  14. * Sponsored by SuSE
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #undef DEBUG
  31. #undef VERBOSE_DEBUG
  32. #include <linux/kernel.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/serial.h>
  38. #include <linux/tty_driver.h>
  39. #include <linux/tty_flip.h>
  40. #include <linux/module.h>
  41. #include <linux/mutex.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/usb.h>
  44. #include <linux/usb/cdc.h>
  45. #include <asm/byteorder.h>
  46. #include <asm/unaligned.h>
  47. #include <linux/idr.h>
  48. #include <linux/list.h>
  49. #include "cdc-acm.h"
  50. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  51. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  52. static struct usb_driver acm_driver;
  53. static struct tty_driver *acm_tty_driver;
  54. static DEFINE_IDR(acm_minors);
  55. static DEFINE_MUTEX(acm_minors_lock);
  56. static void acm_tty_set_termios(struct tty_struct *tty,
  57. struct ktermios *termios_old);
  58. /*
  59. * acm_minors accessors
  60. */
  61. /*
  62. * Look up an ACM structure by minor. If found and not disconnected, increment
  63. * its refcount and return it with its mutex held.
  64. */
  65. static struct acm *acm_get_by_minor(unsigned int minor)
  66. {
  67. struct acm *acm;
  68. mutex_lock(&acm_minors_lock);
  69. acm = idr_find(&acm_minors, minor);
  70. if (acm) {
  71. mutex_lock(&acm->mutex);
  72. if (acm->disconnected) {
  73. mutex_unlock(&acm->mutex);
  74. acm = NULL;
  75. } else {
  76. tty_port_get(&acm->port);
  77. mutex_unlock(&acm->mutex);
  78. }
  79. }
  80. mutex_unlock(&acm_minors_lock);
  81. return acm;
  82. }
  83. /*
  84. * Try to find an available minor number and if found, associate it with 'acm'.
  85. */
  86. static int acm_alloc_minor(struct acm *acm)
  87. {
  88. int minor;
  89. mutex_lock(&acm_minors_lock);
  90. minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
  91. mutex_unlock(&acm_minors_lock);
  92. return minor;
  93. }
  94. /* Release the minor number associated with 'acm'. */
  95. static void acm_release_minor(struct acm *acm)
  96. {
  97. mutex_lock(&acm_minors_lock);
  98. idr_remove(&acm_minors, acm->minor);
  99. mutex_unlock(&acm_minors_lock);
  100. }
  101. /*
  102. * Functions for ACM control messages.
  103. */
  104. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  105. void *buf, int len)
  106. {
  107. int retval;
  108. retval = usb_autopm_get_interface(acm->control);
  109. if (retval)
  110. return retval;
  111. retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  112. request, USB_RT_ACM, value,
  113. acm->control->altsetting[0].desc.bInterfaceNumber,
  114. buf, len, 5000);
  115. dev_dbg(&acm->control->dev,
  116. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  117. __func__, request, value, len, retval);
  118. usb_autopm_put_interface(acm->control);
  119. return retval < 0 ? retval : 0;
  120. }
  121. /* devices aren't required to support these requests.
  122. * the cdc acm descriptor tells whether they do...
  123. */
  124. static inline int acm_set_control(struct acm *acm, int control)
  125. {
  126. if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
  127. return -EOPNOTSUPP;
  128. return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
  129. control, NULL, 0);
  130. }
  131. #define acm_set_line(acm, line) \
  132. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  133. #define acm_send_break(acm, ms) \
  134. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  135. /*
  136. * Write buffer management.
  137. * All of these assume proper locks taken by the caller.
  138. */
  139. static int acm_wb_alloc(struct acm *acm)
  140. {
  141. int i, wbn;
  142. struct acm_wb *wb;
  143. wbn = 0;
  144. i = 0;
  145. for (;;) {
  146. wb = &acm->wb[wbn];
  147. if (!wb->use) {
  148. wb->use = 1;
  149. return wbn;
  150. }
  151. wbn = (wbn + 1) % ACM_NW;
  152. if (++i >= ACM_NW)
  153. return -1;
  154. }
  155. }
  156. static int acm_wb_is_avail(struct acm *acm)
  157. {
  158. int i, n;
  159. unsigned long flags;
  160. n = ACM_NW;
  161. spin_lock_irqsave(&acm->write_lock, flags);
  162. for (i = 0; i < ACM_NW; i++)
  163. n -= acm->wb[i].use;
  164. spin_unlock_irqrestore(&acm->write_lock, flags);
  165. return n;
  166. }
  167. /*
  168. * Finish write. Caller must hold acm->write_lock
  169. */
  170. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  171. {
  172. wb->use = 0;
  173. acm->transmitting--;
  174. usb_autopm_put_interface_async(acm->control);
  175. }
  176. /*
  177. * Poke write.
  178. *
  179. * the caller is responsible for locking
  180. */
  181. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  182. {
  183. int rc;
  184. acm->transmitting++;
  185. wb->urb->transfer_buffer = wb->buf;
  186. wb->urb->transfer_dma = wb->dmah;
  187. wb->urb->transfer_buffer_length = wb->len;
  188. wb->urb->dev = acm->dev;
  189. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  190. if (rc < 0) {
  191. dev_err(&acm->data->dev,
  192. "%s - usb_submit_urb(write bulk) failed: %d\n",
  193. __func__, rc);
  194. acm_write_done(acm, wb);
  195. }
  196. return rc;
  197. }
  198. /*
  199. * attributes exported through sysfs
  200. */
  201. static ssize_t show_caps
  202. (struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct usb_interface *intf = to_usb_interface(dev);
  205. struct acm *acm = usb_get_intfdata(intf);
  206. return sprintf(buf, "%d", acm->ctrl_caps);
  207. }
  208. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  209. static ssize_t show_country_codes
  210. (struct device *dev, struct device_attribute *attr, char *buf)
  211. {
  212. struct usb_interface *intf = to_usb_interface(dev);
  213. struct acm *acm = usb_get_intfdata(intf);
  214. memcpy(buf, acm->country_codes, acm->country_code_size);
  215. return acm->country_code_size;
  216. }
  217. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  218. static ssize_t show_country_rel_date
  219. (struct device *dev, struct device_attribute *attr, char *buf)
  220. {
  221. struct usb_interface *intf = to_usb_interface(dev);
  222. struct acm *acm = usb_get_intfdata(intf);
  223. return sprintf(buf, "%d", acm->country_rel_date);
  224. }
  225. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  226. /*
  227. * Interrupt handlers for various ACM device responses
  228. */
  229. /* control interface reports status changes with "interrupt" transfers */
  230. static void acm_ctrl_irq(struct urb *urb)
  231. {
  232. struct acm *acm = urb->context;
  233. struct usb_cdc_notification *dr = urb->transfer_buffer;
  234. unsigned char *data;
  235. int newctrl;
  236. int difference;
  237. int retval;
  238. int status = urb->status;
  239. switch (status) {
  240. case 0:
  241. /* success */
  242. break;
  243. case -ECONNRESET:
  244. case -ENOENT:
  245. case -ESHUTDOWN:
  246. /* this urb is terminated, clean up */
  247. dev_dbg(&acm->control->dev,
  248. "%s - urb shutting down with status: %d\n",
  249. __func__, status);
  250. return;
  251. default:
  252. dev_dbg(&acm->control->dev,
  253. "%s - nonzero urb status received: %d\n",
  254. __func__, status);
  255. goto exit;
  256. }
  257. usb_mark_last_busy(acm->dev);
  258. data = (unsigned char *)(dr + 1);
  259. switch (dr->bNotificationType) {
  260. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  261. dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
  262. __func__, dr->wValue);
  263. break;
  264. case USB_CDC_NOTIFY_SERIAL_STATE:
  265. if (le16_to_cpu(dr->wLength) != 2) {
  266. dev_dbg(&acm->control->dev,
  267. "%s - malformed serial state\n", __func__);
  268. break;
  269. }
  270. newctrl = get_unaligned_le16(data);
  271. if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  272. dev_dbg(&acm->control->dev, "%s - calling hangup\n",
  273. __func__);
  274. tty_port_tty_hangup(&acm->port, false);
  275. }
  276. difference = acm->ctrlin ^ newctrl;
  277. spin_lock(&acm->read_lock);
  278. acm->ctrlin = newctrl;
  279. acm->oldcount = acm->iocount;
  280. if (difference & ACM_CTRL_DSR)
  281. acm->iocount.dsr++;
  282. if (difference & ACM_CTRL_BRK)
  283. acm->iocount.brk++;
  284. if (difference & ACM_CTRL_RI)
  285. acm->iocount.rng++;
  286. if (difference & ACM_CTRL_DCD)
  287. acm->iocount.dcd++;
  288. if (difference & ACM_CTRL_FRAMING)
  289. acm->iocount.frame++;
  290. if (difference & ACM_CTRL_PARITY)
  291. acm->iocount.parity++;
  292. if (difference & ACM_CTRL_OVERRUN)
  293. acm->iocount.overrun++;
  294. spin_unlock(&acm->read_lock);
  295. if (difference)
  296. wake_up_all(&acm->wioctl);
  297. break;
  298. default:
  299. dev_dbg(&acm->control->dev,
  300. "%s - unknown notification %d received: index %d len %d\n",
  301. __func__,
  302. dr->bNotificationType, dr->wIndex, dr->wLength);
  303. break;
  304. }
  305. exit:
  306. retval = usb_submit_urb(urb, GFP_ATOMIC);
  307. if (retval && retval != -EPERM)
  308. dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
  309. __func__, retval);
  310. }
  311. static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
  312. {
  313. int res;
  314. if (!test_and_clear_bit(index, &acm->read_urbs_free))
  315. return 0;
  316. res = usb_submit_urb(acm->read_urbs[index], mem_flags);
  317. if (res) {
  318. if (res != -EPERM) {
  319. dev_err(&acm->data->dev,
  320. "urb %d failed submission with %d\n",
  321. index, res);
  322. }
  323. set_bit(index, &acm->read_urbs_free);
  324. return res;
  325. } else {
  326. dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
  327. }
  328. return 0;
  329. }
  330. static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
  331. {
  332. int res;
  333. int i;
  334. for (i = 0; i < acm->rx_buflimit; ++i) {
  335. res = acm_submit_read_urb(acm, i, mem_flags);
  336. if (res)
  337. return res;
  338. }
  339. return 0;
  340. }
  341. static void acm_process_read_urb(struct acm *acm, struct urb *urb)
  342. {
  343. if (!urb->actual_length)
  344. return;
  345. tty_insert_flip_string(&acm->port, urb->transfer_buffer,
  346. urb->actual_length);
  347. tty_flip_buffer_push(&acm->port);
  348. }
  349. static void acm_read_bulk_callback(struct urb *urb)
  350. {
  351. struct acm_rb *rb = urb->context;
  352. struct acm *acm = rb->instance;
  353. unsigned long flags;
  354. int status = urb->status;
  355. dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
  356. rb->index, urb->actual_length,
  357. status);
  358. if (!acm->dev) {
  359. set_bit(rb->index, &acm->read_urbs_free);
  360. dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
  361. return;
  362. }
  363. if (status) {
  364. set_bit(rb->index, &acm->read_urbs_free);
  365. if ((status != -ENOENT) || (urb->actual_length == 0))
  366. return;
  367. }
  368. usb_mark_last_busy(acm->dev);
  369. acm_process_read_urb(acm, urb);
  370. /*
  371. * Unthrottle may run on another CPU which needs to see events
  372. * in the same order. Submission has an implict barrier
  373. */
  374. smp_mb__before_atomic();
  375. set_bit(rb->index, &acm->read_urbs_free);
  376. /* throttle device if requested by tty */
  377. spin_lock_irqsave(&acm->read_lock, flags);
  378. acm->throttled = acm->throttle_req;
  379. if (!acm->throttled) {
  380. spin_unlock_irqrestore(&acm->read_lock, flags);
  381. acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
  382. } else {
  383. spin_unlock_irqrestore(&acm->read_lock, flags);
  384. }
  385. }
  386. /* data interface wrote those outgoing bytes */
  387. static void acm_write_bulk(struct urb *urb)
  388. {
  389. struct acm_wb *wb = urb->context;
  390. struct acm *acm = wb->instance;
  391. unsigned long flags;
  392. int status = urb->status;
  393. if (status || (urb->actual_length != urb->transfer_buffer_length))
  394. dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
  395. urb->actual_length,
  396. urb->transfer_buffer_length,
  397. status);
  398. spin_lock_irqsave(&acm->write_lock, flags);
  399. acm_write_done(acm, wb);
  400. spin_unlock_irqrestore(&acm->write_lock, flags);
  401. schedule_work(&acm->work);
  402. }
  403. static void acm_softint(struct work_struct *work)
  404. {
  405. struct acm *acm = container_of(work, struct acm, work);
  406. tty_port_tty_wakeup(&acm->port);
  407. }
  408. /*
  409. * TTY handlers
  410. */
  411. static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  412. {
  413. struct acm *acm;
  414. int retval;
  415. acm = acm_get_by_minor(tty->index);
  416. if (!acm)
  417. return -ENODEV;
  418. retval = tty_standard_install(driver, tty);
  419. if (retval)
  420. goto error_init_termios;
  421. tty->driver_data = acm;
  422. return 0;
  423. error_init_termios:
  424. tty_port_put(&acm->port);
  425. return retval;
  426. }
  427. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  428. {
  429. struct acm *acm = tty->driver_data;
  430. return tty_port_open(&acm->port, tty, filp);
  431. }
  432. static void acm_port_dtr_rts(struct tty_port *port, int raise)
  433. {
  434. struct acm *acm = container_of(port, struct acm, port);
  435. int val;
  436. int res;
  437. if (raise)
  438. val = ACM_CTRL_DTR | ACM_CTRL_RTS;
  439. else
  440. val = 0;
  441. /* FIXME: add missing ctrlout locking throughout driver */
  442. acm->ctrlout = val;
  443. res = acm_set_control(acm, val);
  444. if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
  445. dev_err(&acm->control->dev, "failed to set dtr/rts\n");
  446. }
  447. static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
  448. {
  449. struct acm *acm = container_of(port, struct acm, port);
  450. int retval = -ENODEV;
  451. int i;
  452. mutex_lock(&acm->mutex);
  453. if (acm->disconnected)
  454. goto disconnected;
  455. retval = usb_autopm_get_interface(acm->control);
  456. if (retval)
  457. goto error_get_interface;
  458. /*
  459. * FIXME: Why do we need this? Allocating 64K of physically contiguous
  460. * memory is really nasty...
  461. */
  462. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  463. acm->control->needs_remote_wakeup = 1;
  464. acm->ctrlurb->dev = acm->dev;
  465. retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
  466. if (retval) {
  467. dev_err(&acm->control->dev,
  468. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  469. goto error_submit_urb;
  470. }
  471. acm_tty_set_termios(tty, NULL);
  472. /*
  473. * Unthrottle device in case the TTY was closed while throttled.
  474. */
  475. spin_lock_irq(&acm->read_lock);
  476. acm->throttled = 0;
  477. acm->throttle_req = 0;
  478. spin_unlock_irq(&acm->read_lock);
  479. retval = acm_submit_read_urbs(acm, GFP_KERNEL);
  480. if (retval)
  481. goto error_submit_read_urbs;
  482. usb_autopm_put_interface(acm->control);
  483. mutex_unlock(&acm->mutex);
  484. return 0;
  485. error_submit_read_urbs:
  486. for (i = 0; i < acm->rx_buflimit; i++)
  487. usb_kill_urb(acm->read_urbs[i]);
  488. usb_kill_urb(acm->ctrlurb);
  489. error_submit_urb:
  490. usb_autopm_put_interface(acm->control);
  491. error_get_interface:
  492. disconnected:
  493. mutex_unlock(&acm->mutex);
  494. return usb_translate_errors(retval);
  495. }
  496. static void acm_port_destruct(struct tty_port *port)
  497. {
  498. struct acm *acm = container_of(port, struct acm, port);
  499. acm_release_minor(acm);
  500. usb_put_intf(acm->control);
  501. kfree(acm->country_codes);
  502. kfree(acm);
  503. }
  504. static void acm_port_shutdown(struct tty_port *port)
  505. {
  506. struct acm *acm = container_of(port, struct acm, port);
  507. struct urb *urb;
  508. struct acm_wb *wb;
  509. int i;
  510. /*
  511. * Need to grab write_lock to prevent race with resume, but no need to
  512. * hold it due to the tty-port initialised flag.
  513. */
  514. spin_lock_irq(&acm->write_lock);
  515. spin_unlock_irq(&acm->write_lock);
  516. usb_autopm_get_interface_no_resume(acm->control);
  517. acm->control->needs_remote_wakeup = 0;
  518. usb_autopm_put_interface(acm->control);
  519. for (;;) {
  520. urb = usb_get_from_anchor(&acm->delayed);
  521. if (!urb)
  522. break;
  523. wb = urb->context;
  524. wb->use = 0;
  525. usb_autopm_put_interface_async(acm->control);
  526. }
  527. usb_kill_urb(acm->ctrlurb);
  528. for (i = 0; i < ACM_NW; i++)
  529. usb_kill_urb(acm->wb[i].urb);
  530. for (i = 0; i < acm->rx_buflimit; i++)
  531. usb_kill_urb(acm->read_urbs[i]);
  532. }
  533. static void acm_tty_cleanup(struct tty_struct *tty)
  534. {
  535. struct acm *acm = tty->driver_data;
  536. tty_port_put(&acm->port);
  537. }
  538. static void acm_tty_hangup(struct tty_struct *tty)
  539. {
  540. struct acm *acm = tty->driver_data;
  541. tty_port_hangup(&acm->port);
  542. }
  543. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  544. {
  545. struct acm *acm = tty->driver_data;
  546. tty_port_close(&acm->port, tty, filp);
  547. }
  548. static int acm_tty_write(struct tty_struct *tty,
  549. const unsigned char *buf, int count)
  550. {
  551. struct acm *acm = tty->driver_data;
  552. int stat;
  553. unsigned long flags;
  554. int wbn;
  555. struct acm_wb *wb;
  556. if (!count)
  557. return 0;
  558. dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
  559. spin_lock_irqsave(&acm->write_lock, flags);
  560. wbn = acm_wb_alloc(acm);
  561. if (wbn < 0) {
  562. spin_unlock_irqrestore(&acm->write_lock, flags);
  563. return 0;
  564. }
  565. wb = &acm->wb[wbn];
  566. if (!acm->dev) {
  567. wb->use = 0;
  568. spin_unlock_irqrestore(&acm->write_lock, flags);
  569. return -ENODEV;
  570. }
  571. count = (count > acm->writesize) ? acm->writesize : count;
  572. dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
  573. memcpy(wb->buf, buf, count);
  574. wb->len = count;
  575. stat = usb_autopm_get_interface_async(acm->control);
  576. if (stat) {
  577. wb->use = 0;
  578. spin_unlock_irqrestore(&acm->write_lock, flags);
  579. return stat;
  580. }
  581. if (acm->susp_count) {
  582. if (acm->putbuffer) {
  583. /* now to preserve order */
  584. usb_anchor_urb(acm->putbuffer->urb, &acm->delayed);
  585. acm->putbuffer = NULL;
  586. }
  587. usb_anchor_urb(wb->urb, &acm->delayed);
  588. spin_unlock_irqrestore(&acm->write_lock, flags);
  589. return count;
  590. } else {
  591. if (acm->putbuffer) {
  592. /* at this point there is no good way to handle errors */
  593. acm_start_wb(acm, acm->putbuffer);
  594. acm->putbuffer = NULL;
  595. }
  596. }
  597. stat = acm_start_wb(acm, wb);
  598. spin_unlock_irqrestore(&acm->write_lock, flags);
  599. if (stat < 0)
  600. return stat;
  601. return count;
  602. }
  603. static void acm_tty_flush_chars(struct tty_struct *tty)
  604. {
  605. struct acm *acm = tty->driver_data;
  606. struct acm_wb *cur = acm->putbuffer;
  607. int err;
  608. unsigned long flags;
  609. if (!cur) /* nothing to do */
  610. return;
  611. acm->putbuffer = NULL;
  612. err = usb_autopm_get_interface_async(acm->control);
  613. spin_lock_irqsave(&acm->write_lock, flags);
  614. if (err < 0) {
  615. cur->use = 0;
  616. acm->putbuffer = cur;
  617. goto out;
  618. }
  619. if (acm->susp_count)
  620. usb_anchor_urb(cur->urb, &acm->delayed);
  621. else
  622. acm_start_wb(acm, cur);
  623. out:
  624. spin_unlock_irqrestore(&acm->write_lock, flags);
  625. return;
  626. }
  627. static int acm_tty_put_char(struct tty_struct *tty, unsigned char ch)
  628. {
  629. struct acm *acm = tty->driver_data;
  630. struct acm_wb *cur;
  631. int wbn;
  632. unsigned long flags;
  633. overflow:
  634. cur = acm->putbuffer;
  635. if (!cur) {
  636. spin_lock_irqsave(&acm->write_lock, flags);
  637. wbn = acm_wb_alloc(acm);
  638. if (wbn >= 0) {
  639. cur = &acm->wb[wbn];
  640. acm->putbuffer = cur;
  641. }
  642. spin_unlock_irqrestore(&acm->write_lock, flags);
  643. if (!cur)
  644. return 0;
  645. }
  646. if (cur->len == acm->writesize) {
  647. acm_tty_flush_chars(tty);
  648. goto overflow;
  649. }
  650. cur->buf[cur->len++] = ch;
  651. return 1;
  652. }
  653. static int acm_tty_write_room(struct tty_struct *tty)
  654. {
  655. struct acm *acm = tty->driver_data;
  656. /*
  657. * Do not let the line discipline to know that we have a reserve,
  658. * or it might get too enthusiastic.
  659. */
  660. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  661. }
  662. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  663. {
  664. struct acm *acm = tty->driver_data;
  665. /*
  666. * if the device was unplugged then any remaining characters fell out
  667. * of the connector ;)
  668. */
  669. if (acm->disconnected)
  670. return 0;
  671. /*
  672. * This is inaccurate (overcounts), but it works.
  673. */
  674. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  675. }
  676. static void acm_tty_throttle(struct tty_struct *tty)
  677. {
  678. struct acm *acm = tty->driver_data;
  679. spin_lock_irq(&acm->read_lock);
  680. acm->throttle_req = 1;
  681. spin_unlock_irq(&acm->read_lock);
  682. }
  683. static void acm_tty_unthrottle(struct tty_struct *tty)
  684. {
  685. struct acm *acm = tty->driver_data;
  686. unsigned int was_throttled;
  687. spin_lock_irq(&acm->read_lock);
  688. was_throttled = acm->throttled;
  689. acm->throttled = 0;
  690. acm->throttle_req = 0;
  691. spin_unlock_irq(&acm->read_lock);
  692. if (was_throttled)
  693. acm_submit_read_urbs(acm, GFP_KERNEL);
  694. }
  695. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  696. {
  697. struct acm *acm = tty->driver_data;
  698. int retval;
  699. retval = acm_send_break(acm, state ? 0xffff : 0);
  700. if (retval < 0)
  701. dev_dbg(&acm->control->dev, "%s - send break failed\n",
  702. __func__);
  703. return retval;
  704. }
  705. static int acm_tty_tiocmget(struct tty_struct *tty)
  706. {
  707. struct acm *acm = tty->driver_data;
  708. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  709. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  710. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  711. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  712. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  713. TIOCM_CTS;
  714. }
  715. static int acm_tty_tiocmset(struct tty_struct *tty,
  716. unsigned int set, unsigned int clear)
  717. {
  718. struct acm *acm = tty->driver_data;
  719. unsigned int newctrl;
  720. newctrl = acm->ctrlout;
  721. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  722. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  723. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  724. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  725. newctrl = (newctrl & ~clear) | set;
  726. if (acm->ctrlout == newctrl)
  727. return 0;
  728. return acm_set_control(acm, acm->ctrlout = newctrl);
  729. }
  730. static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
  731. {
  732. struct serial_struct tmp;
  733. if (!info)
  734. return -EINVAL;
  735. memset(&tmp, 0, sizeof(tmp));
  736. tmp.flags = ASYNC_LOW_LATENCY;
  737. tmp.xmit_fifo_size = acm->writesize;
  738. tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
  739. tmp.close_delay = acm->port.close_delay / 10;
  740. tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  741. ASYNC_CLOSING_WAIT_NONE :
  742. acm->port.closing_wait / 10;
  743. if (copy_to_user(info, &tmp, sizeof(tmp)))
  744. return -EFAULT;
  745. else
  746. return 0;
  747. }
  748. static int set_serial_info(struct acm *acm,
  749. struct serial_struct __user *newinfo)
  750. {
  751. struct serial_struct new_serial;
  752. unsigned int closing_wait, close_delay;
  753. int retval = 0;
  754. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  755. return -EFAULT;
  756. close_delay = new_serial.close_delay * 10;
  757. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  758. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  759. mutex_lock(&acm->port.mutex);
  760. if (!capable(CAP_SYS_ADMIN)) {
  761. if ((close_delay != acm->port.close_delay) ||
  762. (closing_wait != acm->port.closing_wait))
  763. retval = -EPERM;
  764. else
  765. retval = -EOPNOTSUPP;
  766. } else {
  767. acm->port.close_delay = close_delay;
  768. acm->port.closing_wait = closing_wait;
  769. }
  770. mutex_unlock(&acm->port.mutex);
  771. return retval;
  772. }
  773. static int wait_serial_change(struct acm *acm, unsigned long arg)
  774. {
  775. int rv = 0;
  776. DECLARE_WAITQUEUE(wait, current);
  777. struct async_icount old, new;
  778. do {
  779. spin_lock_irq(&acm->read_lock);
  780. old = acm->oldcount;
  781. new = acm->iocount;
  782. acm->oldcount = new;
  783. spin_unlock_irq(&acm->read_lock);
  784. if ((arg & TIOCM_DSR) &&
  785. old.dsr != new.dsr)
  786. break;
  787. if ((arg & TIOCM_CD) &&
  788. old.dcd != new.dcd)
  789. break;
  790. if ((arg & TIOCM_RI) &&
  791. old.rng != new.rng)
  792. break;
  793. add_wait_queue(&acm->wioctl, &wait);
  794. set_current_state(TASK_INTERRUPTIBLE);
  795. schedule();
  796. remove_wait_queue(&acm->wioctl, &wait);
  797. if (acm->disconnected) {
  798. if (arg & TIOCM_CD)
  799. break;
  800. else
  801. rv = -ENODEV;
  802. } else {
  803. if (signal_pending(current))
  804. rv = -ERESTARTSYS;
  805. }
  806. } while (!rv);
  807. return rv;
  808. }
  809. static int get_serial_usage(struct acm *acm,
  810. struct serial_icounter_struct __user *count)
  811. {
  812. struct serial_icounter_struct icount;
  813. int rv = 0;
  814. memset(&icount, 0, sizeof(icount));
  815. icount.dsr = acm->iocount.dsr;
  816. icount.rng = acm->iocount.rng;
  817. icount.dcd = acm->iocount.dcd;
  818. icount.frame = acm->iocount.frame;
  819. icount.overrun = acm->iocount.overrun;
  820. icount.parity = acm->iocount.parity;
  821. icount.brk = acm->iocount.brk;
  822. if (copy_to_user(count, &icount, sizeof(icount)) > 0)
  823. rv = -EFAULT;
  824. return rv;
  825. }
  826. static int acm_tty_ioctl(struct tty_struct *tty,
  827. unsigned int cmd, unsigned long arg)
  828. {
  829. struct acm *acm = tty->driver_data;
  830. int rv = -ENOIOCTLCMD;
  831. switch (cmd) {
  832. case TIOCGSERIAL: /* gets serial port data */
  833. rv = get_serial_info(acm, (struct serial_struct __user *) arg);
  834. break;
  835. case TIOCSSERIAL:
  836. rv = set_serial_info(acm, (struct serial_struct __user *) arg);
  837. break;
  838. case TIOCMIWAIT:
  839. rv = usb_autopm_get_interface(acm->control);
  840. if (rv < 0) {
  841. rv = -EIO;
  842. break;
  843. }
  844. rv = wait_serial_change(acm, arg);
  845. usb_autopm_put_interface(acm->control);
  846. break;
  847. case TIOCGICOUNT:
  848. rv = get_serial_usage(acm, (struct serial_icounter_struct __user *) arg);
  849. break;
  850. }
  851. return rv;
  852. }
  853. static void acm_tty_set_termios(struct tty_struct *tty,
  854. struct ktermios *termios_old)
  855. {
  856. struct acm *acm = tty->driver_data;
  857. struct ktermios *termios = &tty->termios;
  858. struct usb_cdc_line_coding newline;
  859. int newctrl = acm->ctrlout;
  860. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  861. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  862. newline.bParityType = termios->c_cflag & PARENB ?
  863. (termios->c_cflag & PARODD ? 1 : 2) +
  864. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  865. switch (termios->c_cflag & CSIZE) {
  866. case CS5:
  867. newline.bDataBits = 5;
  868. break;
  869. case CS6:
  870. newline.bDataBits = 6;
  871. break;
  872. case CS7:
  873. newline.bDataBits = 7;
  874. break;
  875. case CS8:
  876. default:
  877. newline.bDataBits = 8;
  878. break;
  879. }
  880. /* FIXME: Needs to clear unsupported bits in the termios */
  881. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  882. if (C_BAUD(tty) == B0) {
  883. newline.dwDTERate = acm->line.dwDTERate;
  884. newctrl &= ~ACM_CTRL_DTR;
  885. } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
  886. newctrl |= ACM_CTRL_DTR;
  887. }
  888. if (newctrl != acm->ctrlout)
  889. acm_set_control(acm, acm->ctrlout = newctrl);
  890. if (memcmp(&acm->line, &newline, sizeof newline)) {
  891. memcpy(&acm->line, &newline, sizeof newline);
  892. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  893. __func__,
  894. le32_to_cpu(newline.dwDTERate),
  895. newline.bCharFormat, newline.bParityType,
  896. newline.bDataBits);
  897. acm_set_line(acm, &acm->line);
  898. }
  899. }
  900. static const struct tty_port_operations acm_port_ops = {
  901. .dtr_rts = acm_port_dtr_rts,
  902. .shutdown = acm_port_shutdown,
  903. .activate = acm_port_activate,
  904. .destruct = acm_port_destruct,
  905. };
  906. /*
  907. * USB probe and disconnect routines.
  908. */
  909. /* Little helpers: write/read buffers free */
  910. static void acm_write_buffers_free(struct acm *acm)
  911. {
  912. int i;
  913. struct acm_wb *wb;
  914. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  915. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  916. usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
  917. }
  918. static void acm_read_buffers_free(struct acm *acm)
  919. {
  920. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  921. int i;
  922. for (i = 0; i < acm->rx_buflimit; i++)
  923. usb_free_coherent(usb_dev, acm->readsize,
  924. acm->read_buffers[i].base, acm->read_buffers[i].dma);
  925. }
  926. /* Little helper: write buffers allocate */
  927. static int acm_write_buffers_alloc(struct acm *acm)
  928. {
  929. int i;
  930. struct acm_wb *wb;
  931. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  932. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  933. &wb->dmah);
  934. if (!wb->buf) {
  935. while (i != 0) {
  936. --i;
  937. --wb;
  938. usb_free_coherent(acm->dev, acm->writesize,
  939. wb->buf, wb->dmah);
  940. }
  941. return -ENOMEM;
  942. }
  943. }
  944. return 0;
  945. }
  946. static int acm_probe(struct usb_interface *intf,
  947. const struct usb_device_id *id)
  948. {
  949. struct usb_cdc_union_desc *union_header = NULL;
  950. struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
  951. unsigned char *buffer = intf->altsetting->extra;
  952. int buflen = intf->altsetting->extralen;
  953. struct usb_interface *control_interface;
  954. struct usb_interface *data_interface;
  955. struct usb_endpoint_descriptor *epctrl = NULL;
  956. struct usb_endpoint_descriptor *epread = NULL;
  957. struct usb_endpoint_descriptor *epwrite = NULL;
  958. struct usb_device *usb_dev = interface_to_usbdev(intf);
  959. struct usb_cdc_parsed_header h;
  960. struct acm *acm;
  961. int minor;
  962. int ctrlsize, readsize;
  963. u8 *buf;
  964. int call_intf_num = -1;
  965. int data_intf_num = -1;
  966. unsigned long quirks;
  967. int num_rx_buf;
  968. int i;
  969. int combined_interfaces = 0;
  970. struct device *tty_dev;
  971. int rv = -ENOMEM;
  972. /* normal quirks */
  973. quirks = (unsigned long)id->driver_info;
  974. if (quirks == IGNORE_DEVICE)
  975. return -ENODEV;
  976. memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
  977. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  978. /* handle quirks deadly to normal probing*/
  979. if (quirks == NO_UNION_NORMAL) {
  980. data_interface = usb_ifnum_to_if(usb_dev, 1);
  981. control_interface = usb_ifnum_to_if(usb_dev, 0);
  982. /* we would crash */
  983. if (!data_interface || !control_interface)
  984. return -ENODEV;
  985. goto skip_normal_probe;
  986. }
  987. /* normal probing*/
  988. if (!buffer) {
  989. dev_err(&intf->dev, "Weird descriptor references\n");
  990. return -EINVAL;
  991. }
  992. if (!intf->cur_altsetting)
  993. return -EINVAL;
  994. if (!buflen) {
  995. if (intf->cur_altsetting->endpoint &&
  996. intf->cur_altsetting->endpoint->extralen &&
  997. intf->cur_altsetting->endpoint->extra) {
  998. dev_dbg(&intf->dev,
  999. "Seeking extra descriptors on endpoint\n");
  1000. buflen = intf->cur_altsetting->endpoint->extralen;
  1001. buffer = intf->cur_altsetting->endpoint->extra;
  1002. } else {
  1003. dev_err(&intf->dev,
  1004. "Zero length descriptor references\n");
  1005. return -EINVAL;
  1006. }
  1007. }
  1008. cdc_parse_cdc_header(&h, intf, buffer, buflen);
  1009. union_header = h.usb_cdc_union_desc;
  1010. cmgmd = h.usb_cdc_call_mgmt_descriptor;
  1011. if (cmgmd)
  1012. call_intf_num = cmgmd->bDataInterface;
  1013. if (!union_header) {
  1014. if (call_intf_num > 0) {
  1015. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  1016. /* quirks for Droids MuIn LCD */
  1017. if (quirks & NO_DATA_INTERFACE) {
  1018. data_interface = usb_ifnum_to_if(usb_dev, 0);
  1019. } else {
  1020. data_intf_num = call_intf_num;
  1021. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1022. }
  1023. control_interface = intf;
  1024. } else {
  1025. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  1026. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  1027. return -ENODEV;
  1028. } else {
  1029. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  1030. combined_interfaces = 1;
  1031. control_interface = data_interface = intf;
  1032. goto look_for_collapsed_interface;
  1033. }
  1034. }
  1035. } else {
  1036. data_intf_num = union_header->bSlaveInterface0;
  1037. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  1038. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1039. }
  1040. if (!control_interface || !data_interface) {
  1041. dev_dbg(&intf->dev, "no interfaces\n");
  1042. return -ENODEV;
  1043. }
  1044. if (!data_interface->cur_altsetting || !control_interface->cur_altsetting)
  1045. return -ENODEV;
  1046. if (data_intf_num != call_intf_num)
  1047. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  1048. if (control_interface == data_interface) {
  1049. /* some broken devices designed for windows work this way */
  1050. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  1051. combined_interfaces = 1;
  1052. /* a popular other OS doesn't use it */
  1053. quirks |= NO_CAP_LINE;
  1054. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  1055. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  1056. return -EINVAL;
  1057. }
  1058. look_for_collapsed_interface:
  1059. for (i = 0; i < 3; i++) {
  1060. struct usb_endpoint_descriptor *ep;
  1061. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  1062. if (usb_endpoint_is_int_in(ep))
  1063. epctrl = ep;
  1064. else if (usb_endpoint_is_bulk_out(ep))
  1065. epwrite = ep;
  1066. else if (usb_endpoint_is_bulk_in(ep))
  1067. epread = ep;
  1068. else
  1069. return -EINVAL;
  1070. }
  1071. if (!epctrl || !epread || !epwrite)
  1072. return -ENODEV;
  1073. else
  1074. goto made_compressed_probe;
  1075. }
  1076. skip_normal_probe:
  1077. /*workaround for switched interfaces */
  1078. if (data_interface->cur_altsetting->desc.bInterfaceClass
  1079. != CDC_DATA_INTERFACE_TYPE) {
  1080. if (control_interface->cur_altsetting->desc.bInterfaceClass
  1081. == CDC_DATA_INTERFACE_TYPE) {
  1082. dev_dbg(&intf->dev,
  1083. "Your device has switched interfaces.\n");
  1084. swap(control_interface, data_interface);
  1085. } else {
  1086. return -EINVAL;
  1087. }
  1088. }
  1089. /* Accept probe requests only for the control interface */
  1090. if (!combined_interfaces && intf != control_interface)
  1091. return -ENODEV;
  1092. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  1093. /* valid in this context */
  1094. dev_dbg(&intf->dev, "The data interface isn't available\n");
  1095. return -EBUSY;
  1096. }
  1097. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
  1098. control_interface->cur_altsetting->desc.bNumEndpoints == 0)
  1099. return -EINVAL;
  1100. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  1101. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  1102. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  1103. /* workaround for switched endpoints */
  1104. if (!usb_endpoint_dir_in(epread)) {
  1105. /* descriptors are swapped */
  1106. dev_dbg(&intf->dev,
  1107. "The data interface has switched endpoints\n");
  1108. swap(epread, epwrite);
  1109. }
  1110. made_compressed_probe:
  1111. dev_dbg(&intf->dev, "interfaces are valid\n");
  1112. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  1113. if (acm == NULL)
  1114. goto alloc_fail;
  1115. minor = acm_alloc_minor(acm);
  1116. if (minor < 0)
  1117. goto alloc_fail1;
  1118. ctrlsize = usb_endpoint_maxp(epctrl);
  1119. readsize = usb_endpoint_maxp(epread) *
  1120. (quirks == SINGLE_RX_URB ? 1 : 2);
  1121. acm->combined_interfaces = combined_interfaces;
  1122. acm->writesize = usb_endpoint_maxp(epwrite) * 20;
  1123. acm->control = control_interface;
  1124. acm->data = data_interface;
  1125. acm->minor = minor;
  1126. acm->dev = usb_dev;
  1127. if (h.usb_cdc_acm_descriptor)
  1128. acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
  1129. if (quirks & NO_CAP_LINE)
  1130. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1131. acm->ctrlsize = ctrlsize;
  1132. acm->readsize = readsize;
  1133. acm->rx_buflimit = num_rx_buf;
  1134. INIT_WORK(&acm->work, acm_softint);
  1135. init_waitqueue_head(&acm->wioctl);
  1136. spin_lock_init(&acm->write_lock);
  1137. spin_lock_init(&acm->read_lock);
  1138. mutex_init(&acm->mutex);
  1139. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  1140. if (acm->is_int_ep)
  1141. acm->bInterval = epread->bInterval;
  1142. tty_port_init(&acm->port);
  1143. acm->port.ops = &acm_port_ops;
  1144. init_usb_anchor(&acm->delayed);
  1145. acm->quirks = quirks;
  1146. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1147. if (!buf)
  1148. goto alloc_fail2;
  1149. acm->ctrl_buffer = buf;
  1150. if (acm_write_buffers_alloc(acm) < 0)
  1151. goto alloc_fail4;
  1152. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1153. if (!acm->ctrlurb)
  1154. goto alloc_fail5;
  1155. for (i = 0; i < num_rx_buf; i++) {
  1156. struct acm_rb *rb = &(acm->read_buffers[i]);
  1157. struct urb *urb;
  1158. rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
  1159. &rb->dma);
  1160. if (!rb->base)
  1161. goto alloc_fail6;
  1162. rb->index = i;
  1163. rb->instance = acm;
  1164. urb = usb_alloc_urb(0, GFP_KERNEL);
  1165. if (!urb)
  1166. goto alloc_fail6;
  1167. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1168. urb->transfer_dma = rb->dma;
  1169. if (acm->is_int_ep) {
  1170. usb_fill_int_urb(urb, acm->dev,
  1171. usb_rcvintpipe(usb_dev, epread->bEndpointAddress),
  1172. rb->base,
  1173. acm->readsize,
  1174. acm_read_bulk_callback, rb,
  1175. acm->bInterval);
  1176. } else {
  1177. usb_fill_bulk_urb(urb, acm->dev,
  1178. usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
  1179. rb->base,
  1180. acm->readsize,
  1181. acm_read_bulk_callback, rb);
  1182. }
  1183. acm->read_urbs[i] = urb;
  1184. __set_bit(i, &acm->read_urbs_free);
  1185. }
  1186. for (i = 0; i < ACM_NW; i++) {
  1187. struct acm_wb *snd = &(acm->wb[i]);
  1188. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1189. if (snd->urb == NULL)
  1190. goto alloc_fail7;
  1191. if (usb_endpoint_xfer_int(epwrite))
  1192. usb_fill_int_urb(snd->urb, usb_dev,
  1193. usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
  1194. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1195. else
  1196. usb_fill_bulk_urb(snd->urb, usb_dev,
  1197. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1198. NULL, acm->writesize, acm_write_bulk, snd);
  1199. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1200. if (quirks & SEND_ZERO_PACKET)
  1201. snd->urb->transfer_flags |= URB_ZERO_PACKET;
  1202. snd->instance = acm;
  1203. }
  1204. usb_set_intfdata(intf, acm);
  1205. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1206. if (i < 0)
  1207. goto alloc_fail7;
  1208. if (h.usb_cdc_country_functional_desc) { /* export the country data */
  1209. struct usb_cdc_country_functional_desc * cfd =
  1210. h.usb_cdc_country_functional_desc;
  1211. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1212. if (!acm->country_codes)
  1213. goto skip_countries;
  1214. acm->country_code_size = cfd->bLength - 4;
  1215. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1216. cfd->bLength - 4);
  1217. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1218. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1219. if (i < 0) {
  1220. kfree(acm->country_codes);
  1221. acm->country_codes = NULL;
  1222. acm->country_code_size = 0;
  1223. goto skip_countries;
  1224. }
  1225. i = device_create_file(&intf->dev,
  1226. &dev_attr_iCountryCodeRelDate);
  1227. if (i < 0) {
  1228. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1229. kfree(acm->country_codes);
  1230. acm->country_codes = NULL;
  1231. acm->country_code_size = 0;
  1232. goto skip_countries;
  1233. }
  1234. }
  1235. skip_countries:
  1236. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1237. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1238. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1239. /* works around buggy devices */
  1240. epctrl->bInterval ? epctrl->bInterval : 16);
  1241. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1242. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1243. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1244. acm->line.dwDTERate = cpu_to_le32(9600);
  1245. acm->line.bDataBits = 8;
  1246. acm_set_line(acm, &acm->line);
  1247. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1248. usb_set_intfdata(data_interface, acm);
  1249. usb_get_intf(control_interface);
  1250. tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
  1251. &control_interface->dev);
  1252. if (IS_ERR(tty_dev)) {
  1253. rv = PTR_ERR(tty_dev);
  1254. goto alloc_fail8;
  1255. }
  1256. if (quirks & CLEAR_HALT_CONDITIONS) {
  1257. usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress));
  1258. usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress));
  1259. }
  1260. return 0;
  1261. alloc_fail8:
  1262. if (acm->country_codes) {
  1263. device_remove_file(&acm->control->dev,
  1264. &dev_attr_wCountryCodes);
  1265. device_remove_file(&acm->control->dev,
  1266. &dev_attr_iCountryCodeRelDate);
  1267. kfree(acm->country_codes);
  1268. }
  1269. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1270. alloc_fail7:
  1271. usb_set_intfdata(intf, NULL);
  1272. for (i = 0; i < ACM_NW; i++)
  1273. usb_free_urb(acm->wb[i].urb);
  1274. alloc_fail6:
  1275. for (i = 0; i < num_rx_buf; i++)
  1276. usb_free_urb(acm->read_urbs[i]);
  1277. acm_read_buffers_free(acm);
  1278. usb_free_urb(acm->ctrlurb);
  1279. alloc_fail5:
  1280. acm_write_buffers_free(acm);
  1281. alloc_fail4:
  1282. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1283. alloc_fail2:
  1284. acm_release_minor(acm);
  1285. alloc_fail1:
  1286. kfree(acm);
  1287. alloc_fail:
  1288. return rv;
  1289. }
  1290. static void stop_data_traffic(struct acm *acm)
  1291. {
  1292. int i;
  1293. usb_kill_urb(acm->ctrlurb);
  1294. for (i = 0; i < ACM_NW; i++)
  1295. usb_kill_urb(acm->wb[i].urb);
  1296. for (i = 0; i < acm->rx_buflimit; i++)
  1297. usb_kill_urb(acm->read_urbs[i]);
  1298. cancel_work_sync(&acm->work);
  1299. }
  1300. static void acm_disconnect(struct usb_interface *intf)
  1301. {
  1302. struct acm *acm = usb_get_intfdata(intf);
  1303. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1304. struct tty_struct *tty;
  1305. int i;
  1306. /* sibling interface is already cleaning up */
  1307. if (!acm)
  1308. return;
  1309. mutex_lock(&acm->mutex);
  1310. acm->disconnected = true;
  1311. if (acm->country_codes) {
  1312. device_remove_file(&acm->control->dev,
  1313. &dev_attr_wCountryCodes);
  1314. device_remove_file(&acm->control->dev,
  1315. &dev_attr_iCountryCodeRelDate);
  1316. }
  1317. wake_up_all(&acm->wioctl);
  1318. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1319. usb_set_intfdata(acm->control, NULL);
  1320. usb_set_intfdata(acm->data, NULL);
  1321. mutex_unlock(&acm->mutex);
  1322. tty = tty_port_tty_get(&acm->port);
  1323. if (tty) {
  1324. tty_vhangup(tty);
  1325. tty_kref_put(tty);
  1326. }
  1327. stop_data_traffic(acm);
  1328. tty_unregister_device(acm_tty_driver, acm->minor);
  1329. usb_free_urb(acm->ctrlurb);
  1330. for (i = 0; i < ACM_NW; i++)
  1331. usb_free_urb(acm->wb[i].urb);
  1332. for (i = 0; i < acm->rx_buflimit; i++)
  1333. usb_free_urb(acm->read_urbs[i]);
  1334. acm_write_buffers_free(acm);
  1335. usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1336. acm_read_buffers_free(acm);
  1337. if (!acm->combined_interfaces)
  1338. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1339. acm->data : acm->control);
  1340. tty_port_put(&acm->port);
  1341. }
  1342. #ifdef CONFIG_PM
  1343. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1344. {
  1345. struct acm *acm = usb_get_intfdata(intf);
  1346. int cnt;
  1347. spin_lock_irq(&acm->write_lock);
  1348. if (PMSG_IS_AUTO(message)) {
  1349. if (acm->transmitting) {
  1350. spin_unlock_irq(&acm->write_lock);
  1351. return -EBUSY;
  1352. }
  1353. }
  1354. cnt = acm->susp_count++;
  1355. spin_unlock_irq(&acm->write_lock);
  1356. if (cnt)
  1357. return 0;
  1358. stop_data_traffic(acm);
  1359. return 0;
  1360. }
  1361. static int acm_resume(struct usb_interface *intf)
  1362. {
  1363. struct acm *acm = usb_get_intfdata(intf);
  1364. struct urb *urb;
  1365. int rv = 0;
  1366. spin_lock_irq(&acm->write_lock);
  1367. if (--acm->susp_count)
  1368. goto out;
  1369. if (tty_port_initialized(&acm->port)) {
  1370. rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
  1371. for (;;) {
  1372. urb = usb_get_from_anchor(&acm->delayed);
  1373. if (!urb)
  1374. break;
  1375. acm_start_wb(acm, urb->context);
  1376. }
  1377. /*
  1378. * delayed error checking because we must
  1379. * do the write path at all cost
  1380. */
  1381. if (rv < 0)
  1382. goto out;
  1383. rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
  1384. }
  1385. out:
  1386. spin_unlock_irq(&acm->write_lock);
  1387. return rv;
  1388. }
  1389. static int acm_reset_resume(struct usb_interface *intf)
  1390. {
  1391. struct acm *acm = usb_get_intfdata(intf);
  1392. if (tty_port_initialized(&acm->port))
  1393. tty_port_tty_hangup(&acm->port, false);
  1394. return acm_resume(intf);
  1395. }
  1396. #endif /* CONFIG_PM */
  1397. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1398. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1399. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1400. USB_CDC_ACM_PROTO_VENDOR)
  1401. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1402. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1403. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1404. USB_CDC_ACM_PROTO_VENDOR)
  1405. /*
  1406. * USB driver structure.
  1407. */
  1408. static const struct usb_device_id acm_ids[] = {
  1409. /* quirky and broken devices */
  1410. { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
  1411. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1412. { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
  1413. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1414. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1415. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1416. },
  1417. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1418. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1419. },
  1420. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1421. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1422. },
  1423. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1424. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1425. },
  1426. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1427. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1428. },
  1429. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1430. .driver_info = SINGLE_RX_URB,
  1431. },
  1432. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1433. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1434. },
  1435. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1436. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1437. },
  1438. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1439. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1440. },
  1441. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1442. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1443. },
  1444. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1445. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1446. },
  1447. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1448. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1449. },
  1450. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1451. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1452. },
  1453. { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
  1454. .driver_info = QUIRK_CONTROL_LINE_STATE, },
  1455. { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
  1456. { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
  1457. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1458. },
  1459. /* Motorola H24 HSPA module: */
  1460. { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
  1461. { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
  1462. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1463. },
  1464. { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
  1465. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1466. },
  1467. { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
  1468. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1469. },
  1470. { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
  1471. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1472. },
  1473. { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
  1474. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1475. },
  1476. { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
  1477. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1478. },
  1479. { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
  1480. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1481. },
  1482. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1483. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1484. data interface instead of
  1485. communications interface.
  1486. Maybe we should define a new
  1487. quirk for this. */
  1488. },
  1489. { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
  1490. .driver_info = NO_UNION_NORMAL,
  1491. },
  1492. { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
  1493. .driver_info = NO_UNION_NORMAL,
  1494. },
  1495. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1496. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1497. },
  1498. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1499. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1500. },
  1501. { USB_DEVICE(0xfff0, 0x0100), /* DATECS FP-2000 */
  1502. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1503. },
  1504. { USB_DEVICE(0x09d8, 0x0320), /* Elatec GmbH TWN3 */
  1505. .driver_info = NO_UNION_NORMAL, /* has misplaced union descriptor */
  1506. },
  1507. { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
  1508. .driver_info = CLEAR_HALT_CONDITIONS,
  1509. },
  1510. /* Nokia S60 phones expose two ACM channels. The first is
  1511. * a modem and is picked up by the standard AT-command
  1512. * information below. The second is 'vendor-specific' but
  1513. * is treated as a serial device at the S60 end, so we want
  1514. * to expose it on Linux too. */
  1515. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1516. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1517. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1518. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1519. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1520. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1521. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1522. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1523. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1524. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1525. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1526. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1527. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1528. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1529. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1530. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1531. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1532. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1533. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1534. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1535. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1536. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1537. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1538. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1539. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1540. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1541. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1542. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1543. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1544. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1545. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1546. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1547. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1548. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1549. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1550. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1551. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1552. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1553. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1554. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1555. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1556. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1557. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1558. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1559. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1560. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1561. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1562. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1563. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1564. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1565. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1566. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1567. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1568. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1569. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1570. { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
  1571. { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
  1572. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1573. /* Support for Owen devices */
  1574. { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
  1575. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1576. /* Support for Droids MuIn LCD */
  1577. { USB_DEVICE(0x04d8, 0x000b),
  1578. .driver_info = NO_DATA_INTERFACE,
  1579. },
  1580. #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
  1581. { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
  1582. .driver_info = IGNORE_DEVICE,
  1583. },
  1584. { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
  1585. .driver_info = IGNORE_DEVICE,
  1586. },
  1587. #endif
  1588. /*Samsung phone in firmware update mode */
  1589. { USB_DEVICE(0x04e8, 0x685d),
  1590. .driver_info = IGNORE_DEVICE,
  1591. },
  1592. /* Exclude Infineon Flash Loader utility */
  1593. { USB_DEVICE(0x058b, 0x0041),
  1594. .driver_info = IGNORE_DEVICE,
  1595. },
  1596. /* control interfaces without any protocol set */
  1597. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1598. USB_CDC_PROTO_NONE) },
  1599. /* control interfaces with various AT-command sets */
  1600. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1601. USB_CDC_ACM_PROTO_AT_V25TER) },
  1602. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1603. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1604. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1605. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1606. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1607. USB_CDC_ACM_PROTO_AT_GSM) },
  1608. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1609. USB_CDC_ACM_PROTO_AT_3G) },
  1610. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1611. USB_CDC_ACM_PROTO_AT_CDMA) },
  1612. { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
  1613. .driver_info = SEND_ZERO_PACKET,
  1614. },
  1615. { }
  1616. };
  1617. MODULE_DEVICE_TABLE(usb, acm_ids);
  1618. static struct usb_driver acm_driver = {
  1619. .name = "cdc_acm",
  1620. .probe = acm_probe,
  1621. .disconnect = acm_disconnect,
  1622. #ifdef CONFIG_PM
  1623. .suspend = acm_suspend,
  1624. .resume = acm_resume,
  1625. .reset_resume = acm_reset_resume,
  1626. #endif
  1627. .id_table = acm_ids,
  1628. #ifdef CONFIG_PM
  1629. .supports_autosuspend = 1,
  1630. #endif
  1631. .disable_hub_initiated_lpm = 1,
  1632. };
  1633. /*
  1634. * TTY driver structures.
  1635. */
  1636. static const struct tty_operations acm_ops = {
  1637. .install = acm_tty_install,
  1638. .open = acm_tty_open,
  1639. .close = acm_tty_close,
  1640. .cleanup = acm_tty_cleanup,
  1641. .hangup = acm_tty_hangup,
  1642. .write = acm_tty_write,
  1643. .put_char = acm_tty_put_char,
  1644. .flush_chars = acm_tty_flush_chars,
  1645. .write_room = acm_tty_write_room,
  1646. .ioctl = acm_tty_ioctl,
  1647. .throttle = acm_tty_throttle,
  1648. .unthrottle = acm_tty_unthrottle,
  1649. .chars_in_buffer = acm_tty_chars_in_buffer,
  1650. .break_ctl = acm_tty_break_ctl,
  1651. .set_termios = acm_tty_set_termios,
  1652. .tiocmget = acm_tty_tiocmget,
  1653. .tiocmset = acm_tty_tiocmset,
  1654. };
  1655. /*
  1656. * Init / exit.
  1657. */
  1658. static int __init acm_init(void)
  1659. {
  1660. int retval;
  1661. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1662. if (!acm_tty_driver)
  1663. return -ENOMEM;
  1664. acm_tty_driver->driver_name = "acm",
  1665. acm_tty_driver->name = "ttyACM",
  1666. acm_tty_driver->major = ACM_TTY_MAJOR,
  1667. acm_tty_driver->minor_start = 0,
  1668. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1669. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1670. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1671. acm_tty_driver->init_termios = tty_std_termios;
  1672. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1673. HUPCL | CLOCAL;
  1674. tty_set_operations(acm_tty_driver, &acm_ops);
  1675. retval = tty_register_driver(acm_tty_driver);
  1676. if (retval) {
  1677. put_tty_driver(acm_tty_driver);
  1678. return retval;
  1679. }
  1680. retval = usb_register(&acm_driver);
  1681. if (retval) {
  1682. tty_unregister_driver(acm_tty_driver);
  1683. put_tty_driver(acm_tty_driver);
  1684. return retval;
  1685. }
  1686. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1687. return 0;
  1688. }
  1689. static void __exit acm_exit(void)
  1690. {
  1691. usb_deregister(&acm_driver);
  1692. tty_unregister_driver(acm_tty_driver);
  1693. put_tty_driver(acm_tty_driver);
  1694. idr_destroy(&acm_minors);
  1695. }
  1696. module_init(acm_init);
  1697. module_exit(acm_exit);
  1698. MODULE_AUTHOR(DRIVER_AUTHOR);
  1699. MODULE_DESCRIPTION(DRIVER_DESC);
  1700. MODULE_LICENSE("GPL");
  1701. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);