cdc-acm.c 48 KB

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