cdc-acm.c 46 KB

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