cdc-acm.c 52 KB

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