cdc-acm.c 46 KB

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