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