cdc-acm.c 51 KB

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