cdc-acm.c 47 KB

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