cdc-acm.c 52 KB

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