hso.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  7. * <ajb@spheresystems.co.uk>
  8. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  9. * Copyright (C) 2008 Novell, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  23. * USA
  24. *
  25. *
  26. *****************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Description of the device:
  30. *
  31. * Interface 0: Contains the IP network interface on the bulk end points.
  32. * The multiplexed serial ports are using the interrupt and
  33. * control endpoints.
  34. * Interrupt contains a bitmap telling which multiplexed
  35. * serialport needs servicing.
  36. *
  37. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  38. * port is opened, as this have a huge impact on the network port
  39. * throughput.
  40. *
  41. * Interface 2: Standard modem interface - circuit switched interface, should
  42. * not be used.
  43. *
  44. *****************************************************************************/
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/init.h>
  48. #include <linux/delay.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/module.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/usb.h>
  53. #include <linux/timer.h>
  54. #include <linux/tty.h>
  55. #include <linux/tty_driver.h>
  56. #include <linux/tty_flip.h>
  57. #include <linux/kmod.h>
  58. #include <linux/rfkill.h>
  59. #include <linux/ip.h>
  60. #include <linux/uaccess.h>
  61. #include <linux/usb/cdc.h>
  62. #include <net/arp.h>
  63. #include <asm/byteorder.h>
  64. #define DRIVER_VERSION "1.2"
  65. #define MOD_AUTHOR "Option Wireless"
  66. #define MOD_DESCRIPTION "USB High Speed Option driver"
  67. #define MOD_LICENSE "GPL"
  68. #define HSO_MAX_NET_DEVICES 10
  69. #define HSO__MAX_MTU 2048
  70. #define DEFAULT_MTU 1500
  71. #define DEFAULT_MRU 1500
  72. #define CTRL_URB_RX_SIZE 1024
  73. #define CTRL_URB_TX_SIZE 64
  74. #define BULK_URB_RX_SIZE 4096
  75. #define BULK_URB_TX_SIZE 8192
  76. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  77. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  78. #define MUX_BULK_RX_BUF_COUNT 4
  79. #define USB_TYPE_OPTION_VENDOR 0x20
  80. /* These definitions are used with the struct hso_net flags element */
  81. /* - use *_bit operations on it. (bit indices not values.) */
  82. #define HSO_NET_RUNNING 0
  83. #define HSO_NET_TX_TIMEOUT (HZ*10)
  84. #define HSO_SERIAL_MAGIC 0x48534f31
  85. /* Number of ttys to handle */
  86. #define HSO_SERIAL_TTY_MINORS 256
  87. #define MAX_RX_URBS 2
  88. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  89. {
  90. if (tty)
  91. return tty->driver_data;
  92. return NULL;
  93. }
  94. /*****************************************************************************/
  95. /* Debugging functions */
  96. /*****************************************************************************/
  97. #define D__(lvl_, fmt, arg...) \
  98. do { \
  99. printk(lvl_ "[%d:%s]: " fmt "\n", \
  100. __LINE__, __func__, ## arg); \
  101. } while (0)
  102. #define D_(lvl, args...) \
  103. do { \
  104. if (lvl & debug) \
  105. D__(KERN_INFO, args); \
  106. } while (0)
  107. #define D1(args...) D_(0x01, ##args)
  108. #define D2(args...) D_(0x02, ##args)
  109. #define D3(args...) D_(0x04, ##args)
  110. #define D4(args...) D_(0x08, ##args)
  111. #define D5(args...) D_(0x10, ##args)
  112. /*****************************************************************************/
  113. /* Enumerators */
  114. /*****************************************************************************/
  115. enum pkt_parse_state {
  116. WAIT_IP,
  117. WAIT_DATA,
  118. WAIT_SYNC
  119. };
  120. /*****************************************************************************/
  121. /* Structs */
  122. /*****************************************************************************/
  123. struct hso_shared_int {
  124. struct usb_endpoint_descriptor *intr_endp;
  125. void *shared_intr_buf;
  126. struct urb *shared_intr_urb;
  127. struct usb_device *usb;
  128. int use_count;
  129. int ref_count;
  130. struct mutex shared_int_lock;
  131. };
  132. struct hso_net {
  133. struct hso_device *parent;
  134. struct net_device *net;
  135. struct rfkill *rfkill;
  136. struct usb_endpoint_descriptor *in_endp;
  137. struct usb_endpoint_descriptor *out_endp;
  138. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  139. struct urb *mux_bulk_tx_urb;
  140. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  141. void *mux_bulk_tx_buf;
  142. struct sk_buff *skb_rx_buf;
  143. struct sk_buff *skb_tx_buf;
  144. enum pkt_parse_state rx_parse_state;
  145. spinlock_t net_lock;
  146. unsigned short rx_buf_size;
  147. unsigned short rx_buf_missing;
  148. struct iphdr rx_ip_hdr;
  149. unsigned long flags;
  150. };
  151. enum rx_ctrl_state{
  152. RX_IDLE,
  153. RX_SENT,
  154. RX_PENDING
  155. };
  156. struct hso_serial {
  157. struct hso_device *parent;
  158. int magic;
  159. u8 minor;
  160. struct hso_shared_int *shared_int;
  161. /* rx/tx urb could be either a bulk urb or a control urb depending
  162. on which serial port it is used on. */
  163. struct urb *rx_urb[MAX_RX_URBS];
  164. u8 num_rx_urbs;
  165. u8 *rx_data[MAX_RX_URBS];
  166. u16 rx_data_length; /* should contain allocated length */
  167. struct urb *tx_urb;
  168. u8 *tx_data;
  169. u8 *tx_buffer;
  170. u16 tx_data_length; /* should contain allocated length */
  171. u16 tx_data_count;
  172. u16 tx_buffer_count;
  173. struct usb_ctrlrequest ctrl_req_tx;
  174. struct usb_ctrlrequest ctrl_req_rx;
  175. struct usb_endpoint_descriptor *in_endp;
  176. struct usb_endpoint_descriptor *out_endp;
  177. enum rx_ctrl_state rx_state;
  178. u8 rts_state;
  179. u8 dtr_state;
  180. unsigned tx_urb_used:1;
  181. /* from usb_serial_port */
  182. struct tty_struct *tty;
  183. int open_count;
  184. spinlock_t serial_lock;
  185. int (*write_data) (struct hso_serial *serial);
  186. /* Hacks required to get flow control
  187. * working on the serial receive buffers
  188. * so as not to drop characters on the floor.
  189. */
  190. int curr_rx_urb_idx;
  191. u16 curr_rx_urb_offset;
  192. u8 rx_urb_filled[MAX_RX_URBS];
  193. struct tasklet_struct unthrottle_tasklet;
  194. struct work_struct retry_unthrottle_workqueue;
  195. };
  196. struct hso_device {
  197. union {
  198. struct hso_serial *dev_serial;
  199. struct hso_net *dev_net;
  200. } port_data;
  201. u32 port_spec;
  202. u8 is_active;
  203. u8 usb_gone;
  204. struct work_struct async_get_intf;
  205. struct work_struct async_put_intf;
  206. struct usb_device *usb;
  207. struct usb_interface *interface;
  208. struct device *dev;
  209. struct kref ref;
  210. struct mutex mutex;
  211. };
  212. /* Type of interface */
  213. #define HSO_INTF_MASK 0xFF00
  214. #define HSO_INTF_MUX 0x0100
  215. #define HSO_INTF_BULK 0x0200
  216. /* Type of port */
  217. #define HSO_PORT_MASK 0xFF
  218. #define HSO_PORT_NO_PORT 0x0
  219. #define HSO_PORT_CONTROL 0x1
  220. #define HSO_PORT_APP 0x2
  221. #define HSO_PORT_GPS 0x3
  222. #define HSO_PORT_PCSC 0x4
  223. #define HSO_PORT_APP2 0x5
  224. #define HSO_PORT_GPS_CONTROL 0x6
  225. #define HSO_PORT_MSD 0x7
  226. #define HSO_PORT_VOICE 0x8
  227. #define HSO_PORT_DIAG2 0x9
  228. #define HSO_PORT_DIAG 0x10
  229. #define HSO_PORT_MODEM 0x11
  230. #define HSO_PORT_NETWORK 0x12
  231. /* Additional device info */
  232. #define HSO_INFO_MASK 0xFF000000
  233. #define HSO_INFO_CRC_BUG 0x01000000
  234. /*****************************************************************************/
  235. /* Prototypes */
  236. /*****************************************************************************/
  237. /* Serial driver functions */
  238. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  239. unsigned int set, unsigned int clear);
  240. static void ctrl_callback(struct urb *urb);
  241. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  242. static void hso_kick_transmit(struct hso_serial *serial);
  243. /* Helper functions */
  244. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  245. struct usb_device *usb, gfp_t gfp);
  246. static void log_usb_status(int status, const char *function);
  247. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  248. int type, int dir);
  249. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  250. static void hso_free_interface(struct usb_interface *intf);
  251. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  252. static int hso_stop_serial_device(struct hso_device *hso_dev);
  253. static int hso_start_net_device(struct hso_device *hso_dev);
  254. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  255. static int hso_stop_net_device(struct hso_device *hso_dev);
  256. static void hso_serial_ref_free(struct kref *ref);
  257. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  258. static int hso_mux_serial_read(struct hso_serial *serial);
  259. static void async_get_intf(struct work_struct *data);
  260. static void async_put_intf(struct work_struct *data);
  261. static int hso_put_activity(struct hso_device *hso_dev);
  262. static int hso_get_activity(struct hso_device *hso_dev);
  263. /*****************************************************************************/
  264. /* Helping functions */
  265. /*****************************************************************************/
  266. /* #define DEBUG */
  267. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  268. {
  269. return hso_dev->port_data.dev_net;
  270. }
  271. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  272. {
  273. return hso_dev->port_data.dev_serial;
  274. }
  275. /* Debugging functions */
  276. #ifdef DEBUG
  277. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  278. unsigned int len)
  279. {
  280. static char name[255];
  281. sprintf(name, "hso[%d:%s]", line_count, func_name);
  282. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  283. }
  284. #define DUMP(buf_, len_) \
  285. dbg_dump(__LINE__, __func__, buf_, len_)
  286. #define DUMP1(buf_, len_) \
  287. do { \
  288. if (0x01 & debug) \
  289. DUMP(buf_, len_); \
  290. } while (0)
  291. #else
  292. #define DUMP(buf_, len_)
  293. #define DUMP1(buf_, len_)
  294. #endif
  295. /* module parameters */
  296. static int debug;
  297. static int tty_major;
  298. static int disable_net;
  299. /* driver info */
  300. static const char driver_name[] = "hso";
  301. static const char tty_filename[] = "ttyHS";
  302. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  303. /* the usb driver itself (registered in hso_init) */
  304. static struct usb_driver hso_driver;
  305. /* serial structures */
  306. static struct tty_driver *tty_drv;
  307. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  308. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  309. static spinlock_t serial_table_lock;
  310. static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
  311. static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
  312. static const s32 default_port_spec[] = {
  313. HSO_INTF_MUX | HSO_PORT_NETWORK,
  314. HSO_INTF_BULK | HSO_PORT_DIAG,
  315. HSO_INTF_BULK | HSO_PORT_MODEM,
  316. 0
  317. };
  318. static const s32 icon321_port_spec[] = {
  319. HSO_INTF_MUX | HSO_PORT_NETWORK,
  320. HSO_INTF_BULK | HSO_PORT_DIAG2,
  321. HSO_INTF_BULK | HSO_PORT_MODEM,
  322. HSO_INTF_BULK | HSO_PORT_DIAG,
  323. 0
  324. };
  325. #define default_port_device(vendor, product) \
  326. USB_DEVICE(vendor, product), \
  327. .driver_info = (kernel_ulong_t)default_port_spec
  328. #define icon321_port_device(vendor, product) \
  329. USB_DEVICE(vendor, product), \
  330. .driver_info = (kernel_ulong_t)icon321_port_spec
  331. /* list of devices we support */
  332. static const struct usb_device_id hso_ids[] = {
  333. {default_port_device(0x0af0, 0x6711)},
  334. {default_port_device(0x0af0, 0x6731)},
  335. {default_port_device(0x0af0, 0x6751)},
  336. {default_port_device(0x0af0, 0x6771)},
  337. {default_port_device(0x0af0, 0x6791)},
  338. {default_port_device(0x0af0, 0x6811)},
  339. {default_port_device(0x0af0, 0x6911)},
  340. {default_port_device(0x0af0, 0x6951)},
  341. {default_port_device(0x0af0, 0x6971)},
  342. {default_port_device(0x0af0, 0x7011)},
  343. {default_port_device(0x0af0, 0x7031)},
  344. {default_port_device(0x0af0, 0x7051)},
  345. {default_port_device(0x0af0, 0x7071)},
  346. {default_port_device(0x0af0, 0x7111)},
  347. {default_port_device(0x0af0, 0x7211)},
  348. {default_port_device(0x0af0, 0x7251)},
  349. {default_port_device(0x0af0, 0x7271)},
  350. {default_port_device(0x0af0, 0x7311)},
  351. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  352. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  353. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  354. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  355. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  356. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  357. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  358. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  359. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  360. {}
  361. };
  362. MODULE_DEVICE_TABLE(usb, hso_ids);
  363. /* Sysfs attribute */
  364. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  365. struct device_attribute *attr,
  366. char *buf)
  367. {
  368. struct hso_device *hso_dev = dev->driver_data;
  369. char *port_name;
  370. if (!hso_dev)
  371. return 0;
  372. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  373. case HSO_PORT_CONTROL:
  374. port_name = "Control";
  375. break;
  376. case HSO_PORT_APP:
  377. port_name = "Application";
  378. break;
  379. case HSO_PORT_APP2:
  380. port_name = "Application2";
  381. break;
  382. case HSO_PORT_GPS:
  383. port_name = "GPS";
  384. break;
  385. case HSO_PORT_GPS_CONTROL:
  386. port_name = "GPS Control";
  387. break;
  388. case HSO_PORT_PCSC:
  389. port_name = "PCSC";
  390. break;
  391. case HSO_PORT_DIAG:
  392. port_name = "Diagnostic";
  393. break;
  394. case HSO_PORT_DIAG2:
  395. port_name = "Diagnostic2";
  396. break;
  397. case HSO_PORT_MODEM:
  398. port_name = "Modem";
  399. break;
  400. case HSO_PORT_NETWORK:
  401. port_name = "Network";
  402. break;
  403. default:
  404. port_name = "Unknown";
  405. break;
  406. }
  407. return sprintf(buf, "%s\n", port_name);
  408. }
  409. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  410. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  411. {
  412. int idx;
  413. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  414. if (serial->rx_urb[idx] == urb)
  415. return idx;
  416. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  417. return -1;
  418. }
  419. /* converts mux value to a port spec value */
  420. static u32 hso_mux_to_port(int mux)
  421. {
  422. u32 result;
  423. switch (mux) {
  424. case 0x1:
  425. result = HSO_PORT_CONTROL;
  426. break;
  427. case 0x2:
  428. result = HSO_PORT_APP;
  429. break;
  430. case 0x4:
  431. result = HSO_PORT_PCSC;
  432. break;
  433. case 0x8:
  434. result = HSO_PORT_GPS;
  435. break;
  436. case 0x10:
  437. result = HSO_PORT_APP2;
  438. break;
  439. default:
  440. result = HSO_PORT_NO_PORT;
  441. }
  442. return result;
  443. }
  444. /* converts port spec value to a mux value */
  445. static u32 hso_port_to_mux(int port)
  446. {
  447. u32 result;
  448. switch (port & HSO_PORT_MASK) {
  449. case HSO_PORT_CONTROL:
  450. result = 0x0;
  451. break;
  452. case HSO_PORT_APP:
  453. result = 0x1;
  454. break;
  455. case HSO_PORT_PCSC:
  456. result = 0x2;
  457. break;
  458. case HSO_PORT_GPS:
  459. result = 0x3;
  460. break;
  461. case HSO_PORT_APP2:
  462. result = 0x4;
  463. break;
  464. default:
  465. result = 0x0;
  466. }
  467. return result;
  468. }
  469. static struct hso_serial *get_serial_by_shared_int_and_type(
  470. struct hso_shared_int *shared_int,
  471. int mux)
  472. {
  473. int i, port;
  474. port = hso_mux_to_port(mux);
  475. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  476. if (serial_table[i]
  477. && (dev2ser(serial_table[i])->shared_int == shared_int)
  478. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  479. return dev2ser(serial_table[i]);
  480. }
  481. }
  482. return NULL;
  483. }
  484. static struct hso_serial *get_serial_by_index(unsigned index)
  485. {
  486. struct hso_serial *serial = NULL;
  487. unsigned long flags;
  488. spin_lock_irqsave(&serial_table_lock, flags);
  489. if (serial_table[index])
  490. serial = dev2ser(serial_table[index]);
  491. spin_unlock_irqrestore(&serial_table_lock, flags);
  492. return serial;
  493. }
  494. static int get_free_serial_index(void)
  495. {
  496. int index;
  497. unsigned long flags;
  498. spin_lock_irqsave(&serial_table_lock, flags);
  499. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  500. if (serial_table[index] == NULL) {
  501. spin_unlock_irqrestore(&serial_table_lock, flags);
  502. return index;
  503. }
  504. }
  505. spin_unlock_irqrestore(&serial_table_lock, flags);
  506. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  507. return -1;
  508. }
  509. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  510. {
  511. unsigned long flags;
  512. spin_lock_irqsave(&serial_table_lock, flags);
  513. if (serial)
  514. serial_table[index] = serial->parent;
  515. else
  516. serial_table[index] = NULL;
  517. spin_unlock_irqrestore(&serial_table_lock, flags);
  518. }
  519. /* log a meaningful explanation of an USB status */
  520. static void log_usb_status(int status, const char *function)
  521. {
  522. char *explanation;
  523. switch (status) {
  524. case -ENODEV:
  525. explanation = "no device";
  526. break;
  527. case -ENOENT:
  528. explanation = "endpoint not enabled";
  529. break;
  530. case -EPIPE:
  531. explanation = "endpoint stalled";
  532. break;
  533. case -ENOSPC:
  534. explanation = "not enough bandwidth";
  535. break;
  536. case -ESHUTDOWN:
  537. explanation = "device disabled";
  538. break;
  539. case -EHOSTUNREACH:
  540. explanation = "device suspended";
  541. break;
  542. case -EINVAL:
  543. case -EAGAIN:
  544. case -EFBIG:
  545. case -EMSGSIZE:
  546. explanation = "internal error";
  547. break;
  548. default:
  549. explanation = "unknown status";
  550. break;
  551. }
  552. D1("%s: received USB status - %s (%d)", function, explanation, status);
  553. }
  554. /* Network interface functions */
  555. /* called when net interface is brought up by ifconfig */
  556. static int hso_net_open(struct net_device *net)
  557. {
  558. struct hso_net *odev = netdev_priv(net);
  559. unsigned long flags = 0;
  560. if (!odev) {
  561. dev_err(&net->dev, "No net device !\n");
  562. return -ENODEV;
  563. }
  564. odev->skb_tx_buf = NULL;
  565. /* setup environment */
  566. spin_lock_irqsave(&odev->net_lock, flags);
  567. odev->rx_parse_state = WAIT_IP;
  568. odev->rx_buf_size = 0;
  569. odev->rx_buf_missing = sizeof(struct iphdr);
  570. spin_unlock_irqrestore(&odev->net_lock, flags);
  571. hso_start_net_device(odev->parent);
  572. /* We are up and running. */
  573. set_bit(HSO_NET_RUNNING, &odev->flags);
  574. /* Tell the kernel we are ready to start receiving from it */
  575. netif_start_queue(net);
  576. return 0;
  577. }
  578. /* called when interface is brought down by ifconfig */
  579. static int hso_net_close(struct net_device *net)
  580. {
  581. struct hso_net *odev = netdev_priv(net);
  582. /* we don't need the queue anymore */
  583. netif_stop_queue(net);
  584. /* no longer running */
  585. clear_bit(HSO_NET_RUNNING, &odev->flags);
  586. hso_stop_net_device(odev->parent);
  587. /* done */
  588. return 0;
  589. }
  590. /* USB tells is xmit done, we should start the netqueue again */
  591. static void write_bulk_callback(struct urb *urb)
  592. {
  593. struct hso_net *odev = urb->context;
  594. int status = urb->status;
  595. /* Sanity check */
  596. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  597. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  598. return;
  599. }
  600. /* Do we still have a valid kernel network device? */
  601. if (!netif_device_present(odev->net)) {
  602. dev_err(&urb->dev->dev, "%s: net device not present\n",
  603. __func__);
  604. return;
  605. }
  606. /* log status, but don't act on it, we don't need to resubmit anything
  607. * anyhow */
  608. if (status)
  609. log_usb_status(status, __func__);
  610. hso_put_activity(odev->parent);
  611. /* Tell the network interface we are ready for another frame */
  612. netif_wake_queue(odev->net);
  613. }
  614. /* called by kernel when we need to transmit a packet */
  615. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  616. {
  617. struct hso_net *odev = netdev_priv(net);
  618. int result;
  619. /* Tell the kernel, "No more frames 'til we are done with this one." */
  620. netif_stop_queue(net);
  621. if (hso_get_activity(odev->parent) == -EAGAIN) {
  622. odev->skb_tx_buf = skb;
  623. return 0;
  624. }
  625. /* log if asked */
  626. DUMP1(skb->data, skb->len);
  627. /* Copy it from kernel memory to OUR memory */
  628. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  629. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  630. /* Fill in the URB for shipping it out. */
  631. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  632. odev->parent->usb,
  633. usb_sndbulkpipe(odev->parent->usb,
  634. odev->out_endp->
  635. bEndpointAddress & 0x7F),
  636. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  637. odev);
  638. /* Deal with the Zero Length packet problem, I hope */
  639. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  640. /* Send the URB on its merry way. */
  641. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  642. if (result) {
  643. dev_warn(&odev->parent->interface->dev,
  644. "failed mux_bulk_tx_urb %d", result);
  645. net->stats.tx_errors++;
  646. netif_start_queue(net);
  647. } else {
  648. net->stats.tx_packets++;
  649. net->stats.tx_bytes += skb->len;
  650. /* And tell the kernel when the last transmit started. */
  651. net->trans_start = jiffies;
  652. }
  653. dev_kfree_skb(skb);
  654. /* we're done */
  655. return result;
  656. }
  657. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  658. {
  659. struct hso_net *odev = netdev_priv(net);
  660. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  661. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  662. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  663. }
  664. static struct ethtool_ops ops = {
  665. .get_drvinfo = hso_get_drvinfo,
  666. .get_link = ethtool_op_get_link
  667. };
  668. /* called when a packet did not ack after watchdogtimeout */
  669. static void hso_net_tx_timeout(struct net_device *net)
  670. {
  671. struct hso_net *odev = netdev_priv(net);
  672. if (!odev)
  673. return;
  674. /* Tell syslog we are hosed. */
  675. dev_warn(&net->dev, "Tx timed out.\n");
  676. /* Tear the waiting frame off the list */
  677. if (odev->mux_bulk_tx_urb
  678. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  679. usb_unlink_urb(odev->mux_bulk_tx_urb);
  680. /* Update statistics */
  681. net->stats.tx_errors++;
  682. }
  683. /* make a real packet from the received USB buffer */
  684. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  685. unsigned int count, unsigned char is_eop)
  686. {
  687. unsigned short temp_bytes;
  688. unsigned short buffer_offset = 0;
  689. unsigned short frame_len;
  690. unsigned char *tmp_rx_buf;
  691. /* log if needed */
  692. D1("Rx %d bytes", count);
  693. DUMP(ip_pkt, min(128, (int)count));
  694. while (count) {
  695. switch (odev->rx_parse_state) {
  696. case WAIT_IP:
  697. /* waiting for IP header. */
  698. /* wanted bytes - size of ip header */
  699. temp_bytes =
  700. (count <
  701. odev->rx_buf_missing) ? count : odev->
  702. rx_buf_missing;
  703. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  704. odev->rx_buf_size, ip_pkt + buffer_offset,
  705. temp_bytes);
  706. odev->rx_buf_size += temp_bytes;
  707. buffer_offset += temp_bytes;
  708. odev->rx_buf_missing -= temp_bytes;
  709. count -= temp_bytes;
  710. if (!odev->rx_buf_missing) {
  711. /* header is complete allocate an sk_buffer and
  712. * continue to WAIT_DATA */
  713. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  714. if ((frame_len > DEFAULT_MRU) ||
  715. (frame_len < sizeof(struct iphdr))) {
  716. dev_err(&odev->net->dev,
  717. "Invalid frame (%d) length\n",
  718. frame_len);
  719. odev->rx_parse_state = WAIT_SYNC;
  720. continue;
  721. }
  722. /* Allocate an sk_buff */
  723. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  724. if (!odev->skb_rx_buf) {
  725. /* We got no receive buffer. */
  726. D1("could not allocate memory");
  727. odev->rx_parse_state = WAIT_SYNC;
  728. return;
  729. }
  730. /* Here's where it came from */
  731. odev->skb_rx_buf->dev = odev->net;
  732. /* Copy what we got so far. make room for iphdr
  733. * after tail. */
  734. tmp_rx_buf =
  735. skb_put(odev->skb_rx_buf,
  736. sizeof(struct iphdr));
  737. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  738. sizeof(struct iphdr));
  739. /* ETH_HLEN */
  740. odev->rx_buf_size = sizeof(struct iphdr);
  741. /* Filip actually use .tot_len */
  742. odev->rx_buf_missing =
  743. frame_len - sizeof(struct iphdr);
  744. odev->rx_parse_state = WAIT_DATA;
  745. }
  746. break;
  747. case WAIT_DATA:
  748. temp_bytes = (count < odev->rx_buf_missing)
  749. ? count : odev->rx_buf_missing;
  750. /* Copy the rest of the bytes that are left in the
  751. * buffer into the waiting sk_buf. */
  752. /* Make room for temp_bytes after tail. */
  753. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  754. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  755. odev->rx_buf_missing -= temp_bytes;
  756. count -= temp_bytes;
  757. buffer_offset += temp_bytes;
  758. odev->rx_buf_size += temp_bytes;
  759. if (!odev->rx_buf_missing) {
  760. /* Packet is complete. Inject into stack. */
  761. /* We have IP packet here */
  762. odev->skb_rx_buf->protocol =
  763. __constant_htons(ETH_P_IP);
  764. /* don't check it */
  765. odev->skb_rx_buf->ip_summed =
  766. CHECKSUM_UNNECESSARY;
  767. skb_reset_mac_header(odev->skb_rx_buf);
  768. /* Ship it off to the kernel */
  769. netif_rx(odev->skb_rx_buf);
  770. /* No longer our buffer. */
  771. odev->skb_rx_buf = NULL;
  772. /* update out statistics */
  773. odev->net->stats.rx_packets++;
  774. odev->net->stats.rx_bytes += odev->rx_buf_size;
  775. odev->rx_buf_size = 0;
  776. odev->rx_buf_missing = sizeof(struct iphdr);
  777. odev->rx_parse_state = WAIT_IP;
  778. }
  779. break;
  780. case WAIT_SYNC:
  781. D1(" W_S");
  782. count = 0;
  783. break;
  784. default:
  785. D1(" ");
  786. count--;
  787. break;
  788. }
  789. }
  790. /* Recovery mechanism for WAIT_SYNC state. */
  791. if (is_eop) {
  792. if (odev->rx_parse_state == WAIT_SYNC) {
  793. odev->rx_parse_state = WAIT_IP;
  794. odev->rx_buf_size = 0;
  795. odev->rx_buf_missing = sizeof(struct iphdr);
  796. }
  797. }
  798. }
  799. /* Moving data from usb to kernel (in interrupt state) */
  800. static void read_bulk_callback(struct urb *urb)
  801. {
  802. struct hso_net *odev = urb->context;
  803. struct net_device *net;
  804. int result;
  805. int status = urb->status;
  806. /* is al ok? (Filip: Who's Al ?) */
  807. if (status) {
  808. log_usb_status(status, __func__);
  809. return;
  810. }
  811. /* Sanity check */
  812. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  813. D1("BULK IN callback but driver is not active!");
  814. return;
  815. }
  816. usb_mark_last_busy(urb->dev);
  817. net = odev->net;
  818. if (!netif_device_present(net)) {
  819. /* Somebody killed our network interface... */
  820. return;
  821. }
  822. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  823. u32 rest;
  824. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  825. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  826. if (((rest == 5) || (rest == 6))
  827. && !memcmp(((u8 *) urb->transfer_buffer) +
  828. urb->actual_length - 4, crc_check, 4)) {
  829. urb->actual_length -= 4;
  830. }
  831. }
  832. /* do we even have a packet? */
  833. if (urb->actual_length) {
  834. /* Handle the IP stream, add header and push it onto network
  835. * stack if the packet is complete. */
  836. spin_lock(&odev->net_lock);
  837. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  838. (urb->transfer_buffer_length >
  839. urb->actual_length) ? 1 : 0);
  840. spin_unlock(&odev->net_lock);
  841. }
  842. /* We are done with this URB, resubmit it. Prep the USB to wait for
  843. * another frame. Reuse same as received. */
  844. usb_fill_bulk_urb(urb,
  845. odev->parent->usb,
  846. usb_rcvbulkpipe(odev->parent->usb,
  847. odev->in_endp->
  848. bEndpointAddress & 0x7F),
  849. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  850. read_bulk_callback, odev);
  851. /* Give this to the USB subsystem so it can tell us when more data
  852. * arrives. */
  853. result = usb_submit_urb(urb, GFP_ATOMIC);
  854. if (result)
  855. dev_warn(&odev->parent->interface->dev,
  856. "%s failed submit mux_bulk_rx_urb %d", __func__,
  857. result);
  858. }
  859. /* Serial driver functions */
  860. static void _hso_serial_set_termios(struct tty_struct *tty,
  861. struct ktermios *old)
  862. {
  863. struct hso_serial *serial = get_serial_by_tty(tty);
  864. struct ktermios *termios;
  865. if ((!tty) || (!tty->termios) || (!serial)) {
  866. printk(KERN_ERR "%s: no tty structures", __func__);
  867. return;
  868. }
  869. D4("port %d", serial->minor);
  870. /*
  871. * The default requirements for this device are:
  872. */
  873. termios = tty->termios;
  874. termios->c_iflag &=
  875. ~(IGNBRK /* disable ignore break */
  876. | BRKINT /* disable break causes interrupt */
  877. | PARMRK /* disable mark parity errors */
  878. | ISTRIP /* disable clear high bit of input characters */
  879. | INLCR /* disable translate NL to CR */
  880. | IGNCR /* disable ignore CR */
  881. | ICRNL /* disable translate CR to NL */
  882. | IXON); /* disable enable XON/XOFF flow control */
  883. /* disable postprocess output characters */
  884. termios->c_oflag &= ~OPOST;
  885. termios->c_lflag &=
  886. ~(ECHO /* disable echo input characters */
  887. | ECHONL /* disable echo new line */
  888. | ICANON /* disable erase, kill, werase, and rprnt
  889. special characters */
  890. | ISIG /* disable interrupt, quit, and suspend special
  891. characters */
  892. | IEXTEN); /* disable non-POSIX special characters */
  893. termios->c_cflag &=
  894. ~(CSIZE /* no size */
  895. | PARENB /* disable parity bit */
  896. | CBAUD /* clear current baud rate */
  897. | CBAUDEX); /* clear current buad rate */
  898. termios->c_cflag |= CS8; /* character size 8 bits */
  899. /* baud rate 115200 */
  900. tty_encode_baud_rate(serial->tty, 115200, 115200);
  901. /*
  902. * Force low_latency on; otherwise the pushes are scheduled;
  903. * this is bad as it opens up the possibility of dropping bytes
  904. * on the floor. We don't want to drop bytes on the floor. :)
  905. */
  906. serial->tty->low_latency = 1;
  907. return;
  908. }
  909. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  910. {
  911. int result;
  912. #ifdef CONFIG_HSO_AUTOPM
  913. usb_mark_last_busy(urb->dev);
  914. #endif
  915. /* We are done with this URB, resubmit it. Prep the USB to wait for
  916. * another frame */
  917. usb_fill_bulk_urb(urb, serial->parent->usb,
  918. usb_rcvbulkpipe(serial->parent->usb,
  919. serial->in_endp->
  920. bEndpointAddress & 0x7F),
  921. urb->transfer_buffer, serial->rx_data_length,
  922. hso_std_serial_read_bulk_callback, serial);
  923. /* Give this to the USB subsystem so it can tell us when more data
  924. * arrives. */
  925. result = usb_submit_urb(urb, GFP_ATOMIC);
  926. if (result) {
  927. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  928. __func__, result);
  929. }
  930. }
  931. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  932. {
  933. int count;
  934. struct urb *curr_urb;
  935. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  936. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  937. count = put_rxbuf_data(curr_urb, serial);
  938. if (count == -1)
  939. return;
  940. if (count == 0) {
  941. serial->curr_rx_urb_idx++;
  942. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  943. serial->curr_rx_urb_idx = 0;
  944. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  945. }
  946. }
  947. }
  948. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  949. {
  950. int count = 0;
  951. struct urb *urb;
  952. urb = serial->rx_urb[0];
  953. if (serial->open_count > 0) {
  954. count = put_rxbuf_data(urb, serial);
  955. if (count == -1)
  956. return;
  957. }
  958. /* Re issue a read as long as we receive data. */
  959. if (count == 0 && ((urb->actual_length != 0) ||
  960. (serial->rx_state == RX_PENDING))) {
  961. serial->rx_state = RX_SENT;
  962. hso_mux_serial_read(serial);
  963. } else
  964. serial->rx_state = RX_IDLE;
  965. }
  966. /* read callback for Diag and CS port */
  967. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  968. {
  969. struct hso_serial *serial = urb->context;
  970. int status = urb->status;
  971. /* sanity check */
  972. if (!serial) {
  973. D1("serial == NULL");
  974. return;
  975. } else if (status) {
  976. log_usb_status(status, __func__);
  977. return;
  978. }
  979. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  980. D1("Actual length = %d\n", urb->actual_length);
  981. DUMP1(urb->transfer_buffer, urb->actual_length);
  982. /* Anyone listening? */
  983. if (serial->open_count == 0)
  984. return;
  985. if (status == 0) {
  986. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  987. u32 rest;
  988. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  989. rest =
  990. urb->actual_length %
  991. serial->in_endp->wMaxPacketSize;
  992. if (((rest == 5) || (rest == 6))
  993. && !memcmp(((u8 *) urb->transfer_buffer) +
  994. urb->actual_length - 4, crc_check, 4)) {
  995. urb->actual_length -= 4;
  996. }
  997. }
  998. /* Valid data, handle RX data */
  999. spin_lock(&serial->serial_lock);
  1000. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1001. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1002. spin_unlock(&serial->serial_lock);
  1003. } else if (status == -ENOENT || status == -ECONNRESET) {
  1004. /* Unlinked - check for throttled port. */
  1005. D2("Port %d, successfully unlinked urb", serial->minor);
  1006. spin_lock(&serial->serial_lock);
  1007. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1008. hso_resubmit_rx_bulk_urb(serial, urb);
  1009. spin_unlock(&serial->serial_lock);
  1010. } else {
  1011. D2("Port %d, status = %d for read urb", serial->minor, status);
  1012. return;
  1013. }
  1014. }
  1015. /*
  1016. * This needs to be a tasklet otherwise we will
  1017. * end up recursively calling this function.
  1018. */
  1019. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1020. {
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&serial->serial_lock, flags);
  1023. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1024. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1025. else
  1026. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1027. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1028. }
  1029. static void hso_unthrottle(struct tty_struct *tty)
  1030. {
  1031. struct hso_serial *serial = get_serial_by_tty(tty);
  1032. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1033. }
  1034. void hso_unthrottle_workfunc(struct work_struct *work)
  1035. {
  1036. struct hso_serial *serial =
  1037. container_of(work, struct hso_serial,
  1038. retry_unthrottle_workqueue);
  1039. hso_unthrottle_tasklet(serial);
  1040. }
  1041. /* open the requested serial port */
  1042. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1043. {
  1044. struct hso_serial *serial = get_serial_by_index(tty->index);
  1045. int result;
  1046. /* sanity check */
  1047. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1048. tty->driver_data = NULL;
  1049. D1("Failed to open port");
  1050. return -ENODEV;
  1051. }
  1052. mutex_lock(&serial->parent->mutex);
  1053. result = usb_autopm_get_interface(serial->parent->interface);
  1054. if (result < 0)
  1055. goto err_out;
  1056. D1("Opening %d", serial->minor);
  1057. kref_get(&serial->parent->ref);
  1058. /* setup */
  1059. tty->driver_data = serial;
  1060. serial->tty = tty;
  1061. /* check for port already opened, if not set the termios */
  1062. serial->open_count++;
  1063. if (serial->open_count == 1) {
  1064. tty->low_latency = 1;
  1065. serial->rx_state = RX_IDLE;
  1066. /* Force default termio settings */
  1067. _hso_serial_set_termios(tty, NULL);
  1068. tasklet_init(&serial->unthrottle_tasklet,
  1069. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1070. (unsigned long)serial);
  1071. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1072. hso_unthrottle_workfunc);
  1073. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1074. if (result) {
  1075. hso_stop_serial_device(serial->parent);
  1076. serial->open_count--;
  1077. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1078. }
  1079. } else {
  1080. D1("Port was already open");
  1081. }
  1082. usb_autopm_put_interface(serial->parent->interface);
  1083. /* done */
  1084. if (result)
  1085. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1086. err_out:
  1087. mutex_unlock(&serial->parent->mutex);
  1088. return result;
  1089. }
  1090. /* close the requested serial port */
  1091. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1092. {
  1093. struct hso_serial *serial = tty->driver_data;
  1094. u8 usb_gone;
  1095. D1("Closing serial port");
  1096. mutex_lock(&serial->parent->mutex);
  1097. usb_gone = serial->parent->usb_gone;
  1098. if (!usb_gone)
  1099. usb_autopm_get_interface(serial->parent->interface);
  1100. /* reset the rts and dtr */
  1101. /* do the actual close */
  1102. serial->open_count--;
  1103. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1104. if (serial->open_count <= 0) {
  1105. serial->open_count = 0;
  1106. if (serial->tty) {
  1107. serial->tty->driver_data = NULL;
  1108. serial->tty = NULL;
  1109. }
  1110. if (!usb_gone)
  1111. hso_stop_serial_device(serial->parent);
  1112. tasklet_kill(&serial->unthrottle_tasklet);
  1113. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1114. }
  1115. if (!usb_gone)
  1116. usb_autopm_put_interface(serial->parent->interface);
  1117. mutex_unlock(&serial->parent->mutex);
  1118. }
  1119. /* close the requested serial port */
  1120. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1121. int count)
  1122. {
  1123. struct hso_serial *serial = get_serial_by_tty(tty);
  1124. int space, tx_bytes;
  1125. unsigned long flags;
  1126. /* sanity check */
  1127. if (serial == NULL) {
  1128. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1129. return -ENODEV;
  1130. }
  1131. spin_lock_irqsave(&serial->serial_lock, flags);
  1132. space = serial->tx_data_length - serial->tx_buffer_count;
  1133. tx_bytes = (count < space) ? count : space;
  1134. if (!tx_bytes)
  1135. goto out;
  1136. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1137. serial->tx_buffer_count += tx_bytes;
  1138. out:
  1139. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1140. hso_kick_transmit(serial);
  1141. /* done */
  1142. return tx_bytes;
  1143. }
  1144. /* how much room is there for writing */
  1145. static int hso_serial_write_room(struct tty_struct *tty)
  1146. {
  1147. struct hso_serial *serial = get_serial_by_tty(tty);
  1148. int room;
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&serial->serial_lock, flags);
  1151. room = serial->tx_data_length - serial->tx_buffer_count;
  1152. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1153. /* return free room */
  1154. return room;
  1155. }
  1156. /* setup the term */
  1157. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1158. {
  1159. struct hso_serial *serial = get_serial_by_tty(tty);
  1160. unsigned long flags;
  1161. if (old)
  1162. D5("Termios called with: cflags new[%d] - old[%d]",
  1163. tty->termios->c_cflag, old->c_cflag);
  1164. /* the actual setup */
  1165. spin_lock_irqsave(&serial->serial_lock, flags);
  1166. if (serial->open_count)
  1167. _hso_serial_set_termios(tty, old);
  1168. else
  1169. tty->termios = old;
  1170. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1171. /* done */
  1172. return;
  1173. }
  1174. /* how many characters in the buffer */
  1175. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1176. {
  1177. struct hso_serial *serial = get_serial_by_tty(tty);
  1178. int chars;
  1179. unsigned long flags;
  1180. /* sanity check */
  1181. if (serial == NULL)
  1182. return 0;
  1183. spin_lock_irqsave(&serial->serial_lock, flags);
  1184. chars = serial->tx_buffer_count;
  1185. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1186. return chars;
  1187. }
  1188. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1189. {
  1190. unsigned int value;
  1191. struct hso_serial *serial = get_serial_by_tty(tty);
  1192. unsigned long flags;
  1193. /* sanity check */
  1194. if (!serial) {
  1195. D1("no tty structures");
  1196. return -EINVAL;
  1197. }
  1198. spin_lock_irqsave(&serial->serial_lock, flags);
  1199. value = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1200. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1201. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1202. return value;
  1203. }
  1204. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1205. unsigned int set, unsigned int clear)
  1206. {
  1207. int val = 0;
  1208. unsigned long flags;
  1209. int if_num;
  1210. struct hso_serial *serial = get_serial_by_tty(tty);
  1211. /* sanity check */
  1212. if (!serial) {
  1213. D1("no tty structures");
  1214. return -EINVAL;
  1215. }
  1216. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1217. spin_lock_irqsave(&serial->serial_lock, flags);
  1218. if (set & TIOCM_RTS)
  1219. serial->rts_state = 1;
  1220. if (set & TIOCM_DTR)
  1221. serial->dtr_state = 1;
  1222. if (clear & TIOCM_RTS)
  1223. serial->rts_state = 0;
  1224. if (clear & TIOCM_DTR)
  1225. serial->dtr_state = 0;
  1226. if (serial->dtr_state)
  1227. val |= 0x01;
  1228. if (serial->rts_state)
  1229. val |= 0x02;
  1230. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1231. return usb_control_msg(serial->parent->usb,
  1232. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1233. 0x21, val, if_num, NULL, 0,
  1234. USB_CTRL_SET_TIMEOUT);
  1235. }
  1236. /* starts a transmit */
  1237. static void hso_kick_transmit(struct hso_serial *serial)
  1238. {
  1239. u8 *temp;
  1240. unsigned long flags;
  1241. int res;
  1242. spin_lock_irqsave(&serial->serial_lock, flags);
  1243. if (!serial->tx_buffer_count)
  1244. goto out;
  1245. if (serial->tx_urb_used)
  1246. goto out;
  1247. /* Wakeup USB interface if necessary */
  1248. if (hso_get_activity(serial->parent) == -EAGAIN)
  1249. goto out;
  1250. /* Switch pointers around to avoid memcpy */
  1251. temp = serial->tx_buffer;
  1252. serial->tx_buffer = serial->tx_data;
  1253. serial->tx_data = temp;
  1254. serial->tx_data_count = serial->tx_buffer_count;
  1255. serial->tx_buffer_count = 0;
  1256. /* If temp is set, it means we switched buffers */
  1257. if (temp && serial->write_data) {
  1258. res = serial->write_data(serial);
  1259. if (res >= 0)
  1260. serial->tx_urb_used = 1;
  1261. }
  1262. out:
  1263. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1264. }
  1265. /* make a request (for reading and writing data to muxed serial port) */
  1266. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1267. struct urb *ctrl_urb,
  1268. struct usb_ctrlrequest *ctrl_req,
  1269. u8 *ctrl_urb_data, u32 size)
  1270. {
  1271. int result;
  1272. int pipe;
  1273. /* Sanity check */
  1274. if (!serial || !ctrl_urb || !ctrl_req) {
  1275. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1276. return -EINVAL;
  1277. }
  1278. /* initialize */
  1279. ctrl_req->wValue = 0;
  1280. ctrl_req->wIndex = hso_port_to_mux(port);
  1281. ctrl_req->wLength = size;
  1282. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1283. /* Reading command */
  1284. ctrl_req->bRequestType = USB_DIR_IN |
  1285. USB_TYPE_OPTION_VENDOR |
  1286. USB_RECIP_INTERFACE;
  1287. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1288. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1289. } else {
  1290. /* Writing command */
  1291. ctrl_req->bRequestType = USB_DIR_OUT |
  1292. USB_TYPE_OPTION_VENDOR |
  1293. USB_RECIP_INTERFACE;
  1294. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1295. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1296. }
  1297. /* syslog */
  1298. D2("%s command (%02x) len: %d, port: %d",
  1299. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1300. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1301. /* Load ctrl urb */
  1302. ctrl_urb->transfer_flags = 0;
  1303. usb_fill_control_urb(ctrl_urb,
  1304. serial->parent->usb,
  1305. pipe,
  1306. (u8 *) ctrl_req,
  1307. ctrl_urb_data, size, ctrl_callback, serial);
  1308. /* Send it on merry way */
  1309. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1310. if (result) {
  1311. dev_err(&ctrl_urb->dev->dev,
  1312. "%s failed submit ctrl_urb %d type %d", __func__,
  1313. result, type);
  1314. return result;
  1315. }
  1316. /* done */
  1317. return size;
  1318. }
  1319. /* called by intr_callback when read occurs */
  1320. static int hso_mux_serial_read(struct hso_serial *serial)
  1321. {
  1322. if (!serial)
  1323. return -EINVAL;
  1324. /* clean data */
  1325. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1326. /* make the request */
  1327. if (serial->num_rx_urbs != 1) {
  1328. dev_err(&serial->parent->interface->dev,
  1329. "ERROR: mux'd reads with multiple buffers "
  1330. "not possible\n");
  1331. return 0;
  1332. }
  1333. return mux_device_request(serial,
  1334. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1335. serial->parent->port_spec & HSO_PORT_MASK,
  1336. serial->rx_urb[0],
  1337. &serial->ctrl_req_rx,
  1338. serial->rx_data[0], serial->rx_data_length);
  1339. }
  1340. /* used for muxed serial port callback (muxed serial read) */
  1341. static void intr_callback(struct urb *urb)
  1342. {
  1343. struct hso_shared_int *shared_int = urb->context;
  1344. struct hso_serial *serial;
  1345. unsigned char *port_req;
  1346. int status = urb->status;
  1347. int i;
  1348. usb_mark_last_busy(urb->dev);
  1349. /* sanity check */
  1350. if (!shared_int)
  1351. return;
  1352. /* status check */
  1353. if (status) {
  1354. log_usb_status(status, __func__);
  1355. return;
  1356. }
  1357. D4("\n--- Got intr callback 0x%02X ---", status);
  1358. /* what request? */
  1359. port_req = urb->transfer_buffer;
  1360. D4(" port_req = 0x%.2X\n", *port_req);
  1361. /* loop over all muxed ports to find the one sending this */
  1362. for (i = 0; i < 8; i++) {
  1363. /* max 8 channels on MUX */
  1364. if (*port_req & (1 << i)) {
  1365. serial = get_serial_by_shared_int_and_type(shared_int,
  1366. (1 << i));
  1367. if (serial != NULL) {
  1368. D1("Pending read interrupt on port %d\n", i);
  1369. spin_lock(&serial->serial_lock);
  1370. if (serial->rx_state == RX_IDLE) {
  1371. /* Setup and send a ctrl req read on
  1372. * port i */
  1373. if (!serial->rx_urb_filled[0]) {
  1374. serial->rx_state = RX_SENT;
  1375. hso_mux_serial_read(serial);
  1376. } else
  1377. serial->rx_state = RX_PENDING;
  1378. } else {
  1379. D1("Already pending a read on "
  1380. "port %d\n", i);
  1381. }
  1382. spin_unlock(&serial->serial_lock);
  1383. }
  1384. }
  1385. }
  1386. /* Resubmit interrupt urb */
  1387. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1388. }
  1389. /* called for writing to muxed serial port */
  1390. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1391. {
  1392. if (NULL == serial)
  1393. return -EINVAL;
  1394. return mux_device_request(serial,
  1395. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1396. serial->parent->port_spec & HSO_PORT_MASK,
  1397. serial->tx_urb,
  1398. &serial->ctrl_req_tx,
  1399. serial->tx_data, serial->tx_data_count);
  1400. }
  1401. /* write callback for Diag and CS port */
  1402. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1403. {
  1404. struct hso_serial *serial = urb->context;
  1405. int status = urb->status;
  1406. /* sanity check */
  1407. if (!serial) {
  1408. D1("serial == NULL");
  1409. return;
  1410. }
  1411. spin_lock(&serial->serial_lock);
  1412. serial->tx_urb_used = 0;
  1413. spin_unlock(&serial->serial_lock);
  1414. if (status) {
  1415. log_usb_status(status, __func__);
  1416. return;
  1417. }
  1418. hso_put_activity(serial->parent);
  1419. if (serial->tty)
  1420. tty_wakeup(serial->tty);
  1421. hso_kick_transmit(serial);
  1422. D1(" ");
  1423. return;
  1424. }
  1425. /* called for writing diag or CS serial port */
  1426. static int hso_std_serial_write_data(struct hso_serial *serial)
  1427. {
  1428. int count = serial->tx_data_count;
  1429. int result;
  1430. usb_fill_bulk_urb(serial->tx_urb,
  1431. serial->parent->usb,
  1432. usb_sndbulkpipe(serial->parent->usb,
  1433. serial->out_endp->
  1434. bEndpointAddress & 0x7F),
  1435. serial->tx_data, serial->tx_data_count,
  1436. hso_std_serial_write_bulk_callback, serial);
  1437. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1438. if (result) {
  1439. dev_warn(&serial->parent->usb->dev,
  1440. "Failed to submit urb - res %d\n", result);
  1441. return result;
  1442. }
  1443. return count;
  1444. }
  1445. /* callback after read or write on muxed serial port */
  1446. static void ctrl_callback(struct urb *urb)
  1447. {
  1448. struct hso_serial *serial = urb->context;
  1449. struct usb_ctrlrequest *req;
  1450. int status = urb->status;
  1451. /* sanity check */
  1452. if (!serial)
  1453. return;
  1454. spin_lock(&serial->serial_lock);
  1455. serial->tx_urb_used = 0;
  1456. spin_unlock(&serial->serial_lock);
  1457. if (status) {
  1458. log_usb_status(status, __func__);
  1459. return;
  1460. }
  1461. /* what request? */
  1462. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1463. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1464. D4("Actual length of urb = %d\n", urb->actual_length);
  1465. DUMP1(urb->transfer_buffer, urb->actual_length);
  1466. if (req->bRequestType ==
  1467. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1468. /* response to a read command */
  1469. serial->rx_urb_filled[0] = 1;
  1470. spin_lock(&serial->serial_lock);
  1471. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1472. spin_unlock(&serial->serial_lock);
  1473. } else {
  1474. hso_put_activity(serial->parent);
  1475. if (serial->tty)
  1476. tty_wakeup(serial->tty);
  1477. /* response to a write command */
  1478. hso_kick_transmit(serial);
  1479. }
  1480. }
  1481. /* handle RX data for serial port */
  1482. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1483. {
  1484. struct tty_struct *tty = serial->tty;
  1485. int write_length_remaining = 0;
  1486. int curr_write_len;
  1487. /* Sanity check */
  1488. if (urb == NULL || serial == NULL) {
  1489. D1("serial = NULL");
  1490. return -2;
  1491. }
  1492. /* Push data to tty */
  1493. if (tty) {
  1494. write_length_remaining = urb->actual_length -
  1495. serial->curr_rx_urb_offset;
  1496. D1("data to push to tty");
  1497. while (write_length_remaining) {
  1498. if (test_bit(TTY_THROTTLED, &tty->flags))
  1499. return -1;
  1500. curr_write_len = tty_insert_flip_string
  1501. (tty, urb->transfer_buffer +
  1502. serial->curr_rx_urb_offset,
  1503. write_length_remaining);
  1504. serial->curr_rx_urb_offset += curr_write_len;
  1505. write_length_remaining -= curr_write_len;
  1506. tty_flip_buffer_push(tty);
  1507. }
  1508. }
  1509. if (write_length_remaining == 0) {
  1510. serial->curr_rx_urb_offset = 0;
  1511. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1512. }
  1513. return write_length_remaining;
  1514. }
  1515. /* Base driver functions */
  1516. static void hso_log_port(struct hso_device *hso_dev)
  1517. {
  1518. char *port_type;
  1519. char port_dev[20];
  1520. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1521. case HSO_PORT_CONTROL:
  1522. port_type = "Control";
  1523. break;
  1524. case HSO_PORT_APP:
  1525. port_type = "Application";
  1526. break;
  1527. case HSO_PORT_GPS:
  1528. port_type = "GPS";
  1529. break;
  1530. case HSO_PORT_GPS_CONTROL:
  1531. port_type = "GPS control";
  1532. break;
  1533. case HSO_PORT_APP2:
  1534. port_type = "Application2";
  1535. break;
  1536. case HSO_PORT_PCSC:
  1537. port_type = "PCSC";
  1538. break;
  1539. case HSO_PORT_DIAG:
  1540. port_type = "Diagnostic";
  1541. break;
  1542. case HSO_PORT_DIAG2:
  1543. port_type = "Diagnostic2";
  1544. break;
  1545. case HSO_PORT_MODEM:
  1546. port_type = "Modem";
  1547. break;
  1548. case HSO_PORT_NETWORK:
  1549. port_type = "Network";
  1550. break;
  1551. default:
  1552. port_type = "Unknown";
  1553. break;
  1554. }
  1555. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1556. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1557. } else
  1558. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1559. dev2ser(hso_dev)->minor);
  1560. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1561. port_type, port_dev);
  1562. }
  1563. static int hso_start_net_device(struct hso_device *hso_dev)
  1564. {
  1565. int i, result = 0;
  1566. struct hso_net *hso_net = dev2net(hso_dev);
  1567. if (!hso_net)
  1568. return -ENODEV;
  1569. /* send URBs for all read buffers */
  1570. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1571. /* Prep a receive URB */
  1572. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1573. hso_dev->usb,
  1574. usb_rcvbulkpipe(hso_dev->usb,
  1575. hso_net->in_endp->
  1576. bEndpointAddress & 0x7F),
  1577. hso_net->mux_bulk_rx_buf_pool[i],
  1578. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1579. hso_net);
  1580. /* Put it out there so the device can send us stuff */
  1581. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1582. GFP_NOIO);
  1583. if (result)
  1584. dev_warn(&hso_dev->usb->dev,
  1585. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1586. i, result);
  1587. }
  1588. return result;
  1589. }
  1590. static int hso_stop_net_device(struct hso_device *hso_dev)
  1591. {
  1592. int i;
  1593. struct hso_net *hso_net = dev2net(hso_dev);
  1594. if (!hso_net)
  1595. return -ENODEV;
  1596. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1597. if (hso_net->mux_bulk_rx_urb_pool[i])
  1598. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1599. }
  1600. if (hso_net->mux_bulk_tx_urb)
  1601. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1602. return 0;
  1603. }
  1604. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1605. {
  1606. int i, result = 0;
  1607. struct hso_serial *serial = dev2ser(hso_dev);
  1608. if (!serial)
  1609. return -ENODEV;
  1610. /* If it is not the MUX port fill in and submit a bulk urb (already
  1611. * allocated in hso_serial_start) */
  1612. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1613. for (i = 0; i < serial->num_rx_urbs; i++) {
  1614. usb_fill_bulk_urb(serial->rx_urb[i],
  1615. serial->parent->usb,
  1616. usb_rcvbulkpipe(serial->parent->usb,
  1617. serial->in_endp->
  1618. bEndpointAddress &
  1619. 0x7F),
  1620. serial->rx_data[i],
  1621. serial->rx_data_length,
  1622. hso_std_serial_read_bulk_callback,
  1623. serial);
  1624. result = usb_submit_urb(serial->rx_urb[i], flags);
  1625. if (result) {
  1626. dev_warn(&serial->parent->usb->dev,
  1627. "Failed to submit urb - res %d\n",
  1628. result);
  1629. break;
  1630. }
  1631. }
  1632. } else {
  1633. mutex_lock(&serial->shared_int->shared_int_lock);
  1634. if (!serial->shared_int->use_count) {
  1635. result =
  1636. hso_mux_submit_intr_urb(serial->shared_int,
  1637. hso_dev->usb, flags);
  1638. }
  1639. serial->shared_int->use_count++;
  1640. mutex_unlock(&serial->shared_int->shared_int_lock);
  1641. }
  1642. return result;
  1643. }
  1644. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1645. {
  1646. int i;
  1647. struct hso_serial *serial = dev2ser(hso_dev);
  1648. if (!serial)
  1649. return -ENODEV;
  1650. for (i = 0; i < serial->num_rx_urbs; i++) {
  1651. if (serial->rx_urb[i]) {
  1652. usb_kill_urb(serial->rx_urb[i]);
  1653. serial->rx_urb_filled[i] = 0;
  1654. }
  1655. }
  1656. serial->curr_rx_urb_idx = 0;
  1657. serial->curr_rx_urb_offset = 0;
  1658. if (serial->tx_urb)
  1659. usb_kill_urb(serial->tx_urb);
  1660. if (serial->shared_int) {
  1661. mutex_lock(&serial->shared_int->shared_int_lock);
  1662. if (serial->shared_int->use_count &&
  1663. (--serial->shared_int->use_count == 0)) {
  1664. struct urb *urb;
  1665. urb = serial->shared_int->shared_intr_urb;
  1666. if (urb)
  1667. usb_kill_urb(urb);
  1668. }
  1669. mutex_unlock(&serial->shared_int->shared_int_lock);
  1670. }
  1671. return 0;
  1672. }
  1673. static void hso_serial_common_free(struct hso_serial *serial)
  1674. {
  1675. int i;
  1676. if (serial->parent->dev)
  1677. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1678. tty_unregister_device(tty_drv, serial->minor);
  1679. for (i = 0; i < serial->num_rx_urbs; i++) {
  1680. /* unlink and free RX URB */
  1681. usb_free_urb(serial->rx_urb[i]);
  1682. /* free the RX buffer */
  1683. kfree(serial->rx_data[i]);
  1684. }
  1685. /* unlink and free TX URB */
  1686. usb_free_urb(serial->tx_urb);
  1687. kfree(serial->tx_data);
  1688. }
  1689. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1690. int rx_size, int tx_size)
  1691. {
  1692. struct device *dev;
  1693. int minor;
  1694. int i;
  1695. minor = get_free_serial_index();
  1696. if (minor < 0)
  1697. goto exit;
  1698. /* register our minor number */
  1699. serial->parent->dev = tty_register_device(tty_drv, minor,
  1700. &serial->parent->interface->dev);
  1701. dev = serial->parent->dev;
  1702. dev->driver_data = serial->parent;
  1703. i = device_create_file(dev, &dev_attr_hsotype);
  1704. /* fill in specific data for later use */
  1705. serial->minor = minor;
  1706. serial->magic = HSO_SERIAL_MAGIC;
  1707. spin_lock_init(&serial->serial_lock);
  1708. serial->num_rx_urbs = num_urbs;
  1709. /* RX, allocate urb and initialize */
  1710. /* prepare our RX buffer */
  1711. serial->rx_data_length = rx_size;
  1712. for (i = 0; i < serial->num_rx_urbs; i++) {
  1713. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1714. if (!serial->rx_urb[i]) {
  1715. dev_err(dev, "Could not allocate urb?\n");
  1716. goto exit;
  1717. }
  1718. serial->rx_urb[i]->transfer_buffer = NULL;
  1719. serial->rx_urb[i]->transfer_buffer_length = 0;
  1720. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1721. GFP_KERNEL);
  1722. if (!serial->rx_data[i]) {
  1723. dev_err(dev, "%s - Out of memory\n", __func__);
  1724. goto exit;
  1725. }
  1726. }
  1727. /* TX, allocate urb and initialize */
  1728. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1729. if (!serial->tx_urb) {
  1730. dev_err(dev, "Could not allocate urb?\n");
  1731. goto exit;
  1732. }
  1733. serial->tx_urb->transfer_buffer = NULL;
  1734. serial->tx_urb->transfer_buffer_length = 0;
  1735. /* prepare our TX buffer */
  1736. serial->tx_data_count = 0;
  1737. serial->tx_buffer_count = 0;
  1738. serial->tx_data_length = tx_size;
  1739. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1740. if (!serial->tx_data) {
  1741. dev_err(dev, "%s - Out of memory", __func__);
  1742. goto exit;
  1743. }
  1744. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1745. if (!serial->tx_buffer) {
  1746. dev_err(dev, "%s - Out of memory", __func__);
  1747. goto exit;
  1748. }
  1749. return 0;
  1750. exit:
  1751. hso_serial_common_free(serial);
  1752. return -1;
  1753. }
  1754. /* Frees a general hso device */
  1755. static void hso_free_device(struct hso_device *hso_dev)
  1756. {
  1757. kfree(hso_dev);
  1758. }
  1759. /* Creates a general hso device */
  1760. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1761. int port_spec)
  1762. {
  1763. struct hso_device *hso_dev;
  1764. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  1765. if (!hso_dev)
  1766. return NULL;
  1767. hso_dev->port_spec = port_spec;
  1768. hso_dev->usb = interface_to_usbdev(intf);
  1769. hso_dev->interface = intf;
  1770. kref_init(&hso_dev->ref);
  1771. mutex_init(&hso_dev->mutex);
  1772. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1773. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1774. return hso_dev;
  1775. }
  1776. /* Removes a network device in the network device table */
  1777. static int remove_net_device(struct hso_device *hso_dev)
  1778. {
  1779. int i;
  1780. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1781. if (network_table[i] == hso_dev) {
  1782. network_table[i] = NULL;
  1783. break;
  1784. }
  1785. }
  1786. if (i == HSO_MAX_NET_DEVICES)
  1787. return -1;
  1788. return 0;
  1789. }
  1790. /* Frees our network device */
  1791. static void hso_free_net_device(struct hso_device *hso_dev)
  1792. {
  1793. int i;
  1794. struct hso_net *hso_net = dev2net(hso_dev);
  1795. if (!hso_net)
  1796. return;
  1797. /* start freeing */
  1798. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1799. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1800. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  1801. }
  1802. usb_free_urb(hso_net->mux_bulk_tx_urb);
  1803. kfree(hso_net->mux_bulk_tx_buf);
  1804. remove_net_device(hso_net->parent);
  1805. if (hso_net->net) {
  1806. unregister_netdev(hso_net->net);
  1807. free_netdev(hso_net->net);
  1808. }
  1809. hso_free_device(hso_dev);
  1810. }
  1811. /* initialize the network interface */
  1812. static void hso_net_init(struct net_device *net)
  1813. {
  1814. struct hso_net *hso_net = netdev_priv(net);
  1815. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  1816. /* fill in the other fields */
  1817. net->open = hso_net_open;
  1818. net->stop = hso_net_close;
  1819. net->hard_start_xmit = hso_net_start_xmit;
  1820. net->tx_timeout = hso_net_tx_timeout;
  1821. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  1822. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1823. net->type = ARPHRD_NONE;
  1824. net->mtu = DEFAULT_MTU - 14;
  1825. net->tx_queue_len = 10;
  1826. SET_ETHTOOL_OPS(net, &ops);
  1827. /* and initialize the semaphore */
  1828. spin_lock_init(&hso_net->net_lock);
  1829. }
  1830. /* Adds a network device in the network device table */
  1831. static int add_net_device(struct hso_device *hso_dev)
  1832. {
  1833. int i;
  1834. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1835. if (network_table[i] == NULL) {
  1836. network_table[i] = hso_dev;
  1837. break;
  1838. }
  1839. }
  1840. if (i == HSO_MAX_NET_DEVICES)
  1841. return -1;
  1842. return 0;
  1843. }
  1844. static int hso_radio_toggle(void *data, enum rfkill_state state)
  1845. {
  1846. struct hso_device *hso_dev = data;
  1847. int enabled = (state == RFKILL_STATE_ON);
  1848. int rv;
  1849. mutex_lock(&hso_dev->mutex);
  1850. if (hso_dev->usb_gone)
  1851. rv = 0;
  1852. else
  1853. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  1854. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  1855. USB_CTRL_SET_TIMEOUT);
  1856. mutex_unlock(&hso_dev->mutex);
  1857. return rv;
  1858. }
  1859. /* Creates and sets up everything for rfkill */
  1860. static void hso_create_rfkill(struct hso_device *hso_dev,
  1861. struct usb_interface *interface)
  1862. {
  1863. struct hso_net *hso_net = dev2net(hso_dev);
  1864. struct device *dev = &hso_net->net->dev;
  1865. char *rfkn;
  1866. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  1867. RFKILL_TYPE_WWAN);
  1868. if (!hso_net->rfkill) {
  1869. dev_err(dev, "%s - Out of memory\n", __func__);
  1870. return;
  1871. }
  1872. rfkn = kzalloc(20, GFP_KERNEL);
  1873. if (!rfkn) {
  1874. rfkill_free(hso_net->rfkill);
  1875. hso_net->rfkill = NULL;
  1876. dev_err(dev, "%s - Out of memory\n", __func__);
  1877. return;
  1878. }
  1879. snprintf(rfkn, 20, "hso-%d",
  1880. interface->altsetting->desc.bInterfaceNumber);
  1881. hso_net->rfkill->name = rfkn;
  1882. hso_net->rfkill->state = RFKILL_STATE_ON;
  1883. hso_net->rfkill->data = hso_dev;
  1884. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  1885. if (rfkill_register(hso_net->rfkill) < 0) {
  1886. kfree(rfkn);
  1887. hso_net->rfkill->name = NULL;
  1888. rfkill_free(hso_net->rfkill);
  1889. hso_net->rfkill = NULL;
  1890. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  1891. return;
  1892. }
  1893. }
  1894. /* Creates our network device */
  1895. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  1896. {
  1897. int result, i;
  1898. struct net_device *net;
  1899. struct hso_net *hso_net;
  1900. struct hso_device *hso_dev;
  1901. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  1902. if (!hso_dev)
  1903. return NULL;
  1904. /* allocate our network device, then we can put in our private data */
  1905. /* call hso_net_init to do the basic initialization */
  1906. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  1907. if (!net) {
  1908. dev_err(&interface->dev, "Unable to create ethernet device\n");
  1909. goto exit;
  1910. }
  1911. hso_net = netdev_priv(net);
  1912. hso_dev->port_data.dev_net = hso_net;
  1913. hso_net->net = net;
  1914. hso_net->parent = hso_dev;
  1915. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1916. USB_DIR_IN);
  1917. if (!hso_net->in_endp) {
  1918. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  1919. goto exit;
  1920. }
  1921. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1922. USB_DIR_OUT);
  1923. if (!hso_net->out_endp) {
  1924. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  1925. goto exit;
  1926. }
  1927. SET_NETDEV_DEV(net, &interface->dev);
  1928. /* registering our net device */
  1929. result = register_netdev(net);
  1930. if (result) {
  1931. dev_err(&interface->dev, "Failed to register device\n");
  1932. goto exit;
  1933. }
  1934. /* start allocating */
  1935. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1936. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  1937. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  1938. dev_err(&interface->dev, "Could not allocate rx urb\n");
  1939. goto exit;
  1940. }
  1941. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  1942. GFP_KERNEL);
  1943. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  1944. dev_err(&interface->dev, "Could not allocate rx buf\n");
  1945. goto exit;
  1946. }
  1947. }
  1948. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1949. if (!hso_net->mux_bulk_tx_urb) {
  1950. dev_err(&interface->dev, "Could not allocate tx urb\n");
  1951. goto exit;
  1952. }
  1953. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  1954. if (!hso_net->mux_bulk_tx_buf) {
  1955. dev_err(&interface->dev, "Could not allocate tx buf\n");
  1956. goto exit;
  1957. }
  1958. add_net_device(hso_dev);
  1959. hso_log_port(hso_dev);
  1960. hso_create_rfkill(hso_dev, interface);
  1961. return hso_dev;
  1962. exit:
  1963. hso_free_net_device(hso_dev);
  1964. return NULL;
  1965. }
  1966. /* Frees an AT channel ( goes for both mux and non-mux ) */
  1967. static void hso_free_serial_device(struct hso_device *hso_dev)
  1968. {
  1969. struct hso_serial *serial = dev2ser(hso_dev);
  1970. if (!serial)
  1971. return;
  1972. set_serial_by_index(serial->minor, NULL);
  1973. hso_serial_common_free(serial);
  1974. if (serial->shared_int) {
  1975. mutex_lock(&serial->shared_int->shared_int_lock);
  1976. if (--serial->shared_int->ref_count == 0)
  1977. hso_free_shared_int(serial->shared_int);
  1978. else
  1979. mutex_unlock(&serial->shared_int->shared_int_lock);
  1980. }
  1981. kfree(serial);
  1982. hso_free_device(hso_dev);
  1983. }
  1984. /* Creates a bulk AT channel */
  1985. static struct hso_device *hso_create_bulk_serial_device(
  1986. struct usb_interface *interface, int port)
  1987. {
  1988. struct hso_device *hso_dev;
  1989. struct hso_serial *serial;
  1990. int num_urbs;
  1991. hso_dev = hso_create_device(interface, port);
  1992. if (!hso_dev)
  1993. return NULL;
  1994. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  1995. if (!serial)
  1996. goto exit;
  1997. serial->parent = hso_dev;
  1998. hso_dev->port_data.dev_serial = serial;
  1999. if (port & HSO_PORT_MODEM)
  2000. num_urbs = 2;
  2001. else
  2002. num_urbs = 1;
  2003. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2004. BULK_URB_TX_SIZE))
  2005. goto exit;
  2006. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2007. USB_DIR_IN);
  2008. if (!serial->in_endp) {
  2009. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2010. goto exit2;
  2011. }
  2012. if (!
  2013. (serial->out_endp =
  2014. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2015. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2016. goto exit2;
  2017. }
  2018. serial->write_data = hso_std_serial_write_data;
  2019. /* and record this serial */
  2020. set_serial_by_index(serial->minor, serial);
  2021. /* setup the proc dirs and files if needed */
  2022. hso_log_port(hso_dev);
  2023. /* done, return it */
  2024. return hso_dev;
  2025. exit2:
  2026. hso_serial_common_free(serial);
  2027. exit:
  2028. kfree(serial);
  2029. hso_free_device(hso_dev);
  2030. return NULL;
  2031. }
  2032. /* Creates a multiplexed AT channel */
  2033. static
  2034. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2035. int port,
  2036. struct hso_shared_int *mux)
  2037. {
  2038. struct hso_device *hso_dev;
  2039. struct hso_serial *serial;
  2040. int port_spec;
  2041. port_spec = HSO_INTF_MUX;
  2042. port_spec &= ~HSO_PORT_MASK;
  2043. port_spec |= hso_mux_to_port(port);
  2044. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2045. return NULL;
  2046. hso_dev = hso_create_device(interface, port_spec);
  2047. if (!hso_dev)
  2048. return NULL;
  2049. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2050. if (!serial)
  2051. goto exit;
  2052. hso_dev->port_data.dev_serial = serial;
  2053. serial->parent = hso_dev;
  2054. if (hso_serial_common_create
  2055. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2056. goto exit;
  2057. serial->tx_data_length--;
  2058. serial->write_data = hso_mux_serial_write_data;
  2059. serial->shared_int = mux;
  2060. mutex_lock(&serial->shared_int->shared_int_lock);
  2061. serial->shared_int->ref_count++;
  2062. mutex_unlock(&serial->shared_int->shared_int_lock);
  2063. /* and record this serial */
  2064. set_serial_by_index(serial->minor, serial);
  2065. /* setup the proc dirs and files if needed */
  2066. hso_log_port(hso_dev);
  2067. /* done, return it */
  2068. return hso_dev;
  2069. exit:
  2070. if (serial) {
  2071. tty_unregister_device(tty_drv, serial->minor);
  2072. kfree(serial);
  2073. }
  2074. if (hso_dev)
  2075. hso_free_device(hso_dev);
  2076. return NULL;
  2077. }
  2078. static void hso_free_shared_int(struct hso_shared_int *mux)
  2079. {
  2080. usb_free_urb(mux->shared_intr_urb);
  2081. kfree(mux->shared_intr_buf);
  2082. mutex_unlock(&mux->shared_int_lock);
  2083. kfree(mux);
  2084. }
  2085. static
  2086. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2087. {
  2088. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2089. if (!mux)
  2090. return NULL;
  2091. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2092. USB_DIR_IN);
  2093. if (!mux->intr_endp) {
  2094. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2095. goto exit;
  2096. }
  2097. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2098. if (!mux->shared_intr_urb) {
  2099. dev_err(&interface->dev, "Could not allocate intr urb?");
  2100. goto exit;
  2101. }
  2102. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2103. GFP_KERNEL);
  2104. if (!mux->shared_intr_buf) {
  2105. dev_err(&interface->dev, "Could not allocate intr buf?");
  2106. goto exit;
  2107. }
  2108. mutex_init(&mux->shared_int_lock);
  2109. return mux;
  2110. exit:
  2111. kfree(mux->shared_intr_buf);
  2112. usb_free_urb(mux->shared_intr_urb);
  2113. kfree(mux);
  2114. return NULL;
  2115. }
  2116. /* Gets the port spec for a certain interface */
  2117. static int hso_get_config_data(struct usb_interface *interface)
  2118. {
  2119. struct usb_device *usbdev = interface_to_usbdev(interface);
  2120. u8 config_data[17];
  2121. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2122. s32 result;
  2123. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2124. 0x86, 0xC0, 0, 0, config_data, 17,
  2125. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2126. return -EIO;
  2127. }
  2128. switch (config_data[if_num]) {
  2129. case 0x0:
  2130. result = 0;
  2131. break;
  2132. case 0x1:
  2133. result = HSO_PORT_DIAG;
  2134. break;
  2135. case 0x2:
  2136. result = HSO_PORT_GPS;
  2137. break;
  2138. case 0x3:
  2139. result = HSO_PORT_GPS_CONTROL;
  2140. break;
  2141. case 0x4:
  2142. result = HSO_PORT_APP;
  2143. break;
  2144. case 0x5:
  2145. result = HSO_PORT_APP2;
  2146. break;
  2147. case 0x6:
  2148. result = HSO_PORT_CONTROL;
  2149. break;
  2150. case 0x7:
  2151. result = HSO_PORT_NETWORK;
  2152. break;
  2153. case 0x8:
  2154. result = HSO_PORT_MODEM;
  2155. break;
  2156. case 0x9:
  2157. result = HSO_PORT_MSD;
  2158. break;
  2159. case 0xa:
  2160. result = HSO_PORT_PCSC;
  2161. break;
  2162. case 0xb:
  2163. result = HSO_PORT_VOICE;
  2164. break;
  2165. default:
  2166. result = 0;
  2167. }
  2168. if (result)
  2169. result |= HSO_INTF_BULK;
  2170. if (config_data[16] & 0x1)
  2171. result |= HSO_INFO_CRC_BUG;
  2172. return result;
  2173. }
  2174. /* called once for each interface upon device insertion */
  2175. static int hso_probe(struct usb_interface *interface,
  2176. const struct usb_device_id *id)
  2177. {
  2178. int mux, i, if_num, port_spec;
  2179. unsigned char port_mask;
  2180. struct hso_device *hso_dev = NULL;
  2181. struct hso_shared_int *shared_int;
  2182. struct hso_device *tmp_dev = NULL;
  2183. if_num = interface->altsetting->desc.bInterfaceNumber;
  2184. /* Get the interface/port specification from either driver_info or from
  2185. * the device itself */
  2186. if (id->driver_info)
  2187. port_spec = ((u32 *)(id->driver_info))[if_num];
  2188. else
  2189. port_spec = hso_get_config_data(interface);
  2190. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2191. dev_err(&interface->dev, "Not our interface\n");
  2192. return -ENODEV;
  2193. }
  2194. /* Check if we need to switch to alt interfaces prior to port
  2195. * configuration */
  2196. if (interface->num_altsetting > 1)
  2197. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2198. interface->needs_remote_wakeup = 1;
  2199. /* Allocate new hso device(s) */
  2200. switch (port_spec & HSO_INTF_MASK) {
  2201. case HSO_INTF_MUX:
  2202. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2203. /* Create the network device */
  2204. if (!disable_net) {
  2205. hso_dev = hso_create_net_device(interface);
  2206. if (!hso_dev)
  2207. goto exit;
  2208. tmp_dev = hso_dev;
  2209. }
  2210. }
  2211. if (hso_get_mux_ports(interface, &port_mask))
  2212. /* TODO: de-allocate everything */
  2213. goto exit;
  2214. shared_int = hso_create_shared_int(interface);
  2215. if (!shared_int)
  2216. goto exit;
  2217. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2218. if (port_mask & i) {
  2219. hso_dev = hso_create_mux_serial_device(
  2220. interface, i, shared_int);
  2221. if (!hso_dev)
  2222. goto exit;
  2223. }
  2224. }
  2225. if (tmp_dev)
  2226. hso_dev = tmp_dev;
  2227. break;
  2228. case HSO_INTF_BULK:
  2229. /* It's a regular bulk interface */
  2230. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2231. && !disable_net)
  2232. hso_dev = hso_create_net_device(interface);
  2233. else
  2234. hso_dev =
  2235. hso_create_bulk_serial_device(interface, port_spec);
  2236. if (!hso_dev)
  2237. goto exit;
  2238. break;
  2239. default:
  2240. goto exit;
  2241. }
  2242. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2243. /* save our data pointer in this device */
  2244. usb_set_intfdata(interface, hso_dev);
  2245. /* done */
  2246. return 0;
  2247. exit:
  2248. hso_free_interface(interface);
  2249. return -ENODEV;
  2250. }
  2251. /* device removed, cleaning up */
  2252. static void hso_disconnect(struct usb_interface *interface)
  2253. {
  2254. hso_free_interface(interface);
  2255. /* remove reference of our private data */
  2256. usb_set_intfdata(interface, NULL);
  2257. usb_driver_release_interface(&hso_driver, interface);
  2258. }
  2259. static void async_get_intf(struct work_struct *data)
  2260. {
  2261. struct hso_device *hso_dev =
  2262. container_of(data, struct hso_device, async_get_intf);
  2263. usb_autopm_get_interface(hso_dev->interface);
  2264. }
  2265. static void async_put_intf(struct work_struct *data)
  2266. {
  2267. struct hso_device *hso_dev =
  2268. container_of(data, struct hso_device, async_put_intf);
  2269. usb_autopm_put_interface(hso_dev->interface);
  2270. }
  2271. static int hso_get_activity(struct hso_device *hso_dev)
  2272. {
  2273. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2274. if (!hso_dev->is_active) {
  2275. hso_dev->is_active = 1;
  2276. schedule_work(&hso_dev->async_get_intf);
  2277. }
  2278. }
  2279. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2280. return -EAGAIN;
  2281. usb_mark_last_busy(hso_dev->usb);
  2282. return 0;
  2283. }
  2284. static int hso_put_activity(struct hso_device *hso_dev)
  2285. {
  2286. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2287. if (hso_dev->is_active) {
  2288. hso_dev->is_active = 0;
  2289. schedule_work(&hso_dev->async_put_intf);
  2290. return -EAGAIN;
  2291. }
  2292. }
  2293. hso_dev->is_active = 0;
  2294. return 0;
  2295. }
  2296. /* called by kernel when we need to suspend device */
  2297. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2298. {
  2299. int i, result;
  2300. /* Stop all serial ports */
  2301. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2302. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2303. result = hso_stop_serial_device(serial_table[i]);
  2304. if (result)
  2305. goto out;
  2306. }
  2307. }
  2308. /* Stop all network ports */
  2309. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2310. if (network_table[i] &&
  2311. (network_table[i]->interface == iface)) {
  2312. result = hso_stop_net_device(network_table[i]);
  2313. if (result)
  2314. goto out;
  2315. }
  2316. }
  2317. out:
  2318. return 0;
  2319. }
  2320. /* called by kernel when we need to resume device */
  2321. static int hso_resume(struct usb_interface *iface)
  2322. {
  2323. int i, result = 0;
  2324. struct hso_net *hso_net;
  2325. /* Start all serial ports */
  2326. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2327. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2328. if (dev2ser(serial_table[i])->open_count) {
  2329. result =
  2330. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2331. hso_kick_transmit(dev2ser(serial_table[i]));
  2332. if (result)
  2333. goto out;
  2334. }
  2335. }
  2336. }
  2337. /* Start all network ports */
  2338. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2339. if (network_table[i] &&
  2340. (network_table[i]->interface == iface)) {
  2341. hso_net = dev2net(network_table[i]);
  2342. /* First transmit any lingering data, then restart the
  2343. * device. */
  2344. if (hso_net->skb_tx_buf) {
  2345. dev_dbg(&iface->dev,
  2346. "Transmitting lingering data\n");
  2347. hso_net_start_xmit(hso_net->skb_tx_buf,
  2348. hso_net->net);
  2349. hso_net->skb_tx_buf = NULL;
  2350. }
  2351. result = hso_start_net_device(network_table[i]);
  2352. if (result)
  2353. goto out;
  2354. }
  2355. }
  2356. out:
  2357. return result;
  2358. }
  2359. static void hso_serial_ref_free(struct kref *ref)
  2360. {
  2361. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2362. hso_free_serial_device(hso_dev);
  2363. }
  2364. static void hso_free_interface(struct usb_interface *interface)
  2365. {
  2366. struct hso_serial *hso_dev;
  2367. int i;
  2368. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2369. if (serial_table[i]
  2370. && (serial_table[i]->interface == interface)) {
  2371. hso_dev = dev2ser(serial_table[i]);
  2372. if (hso_dev->tty)
  2373. tty_hangup(hso_dev->tty);
  2374. mutex_lock(&hso_dev->parent->mutex);
  2375. hso_dev->parent->usb_gone = 1;
  2376. mutex_unlock(&hso_dev->parent->mutex);
  2377. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2378. }
  2379. }
  2380. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2381. if (network_table[i]
  2382. && (network_table[i]->interface == interface)) {
  2383. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2384. /* hso_stop_net_device doesn't stop the net queue since
  2385. * traffic needs to start it again when suspended */
  2386. netif_stop_queue(dev2net(network_table[i])->net);
  2387. hso_stop_net_device(network_table[i]);
  2388. cancel_work_sync(&network_table[i]->async_put_intf);
  2389. cancel_work_sync(&network_table[i]->async_get_intf);
  2390. if (rfk)
  2391. rfkill_unregister(rfk);
  2392. hso_free_net_device(network_table[i]);
  2393. }
  2394. }
  2395. }
  2396. /* Helper functions */
  2397. /* Get the endpoint ! */
  2398. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2399. int type, int dir)
  2400. {
  2401. int i;
  2402. struct usb_host_interface *iface = intf->cur_altsetting;
  2403. struct usb_endpoint_descriptor *endp;
  2404. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2405. endp = &iface->endpoint[i].desc;
  2406. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2407. ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
  2408. return endp;
  2409. }
  2410. return NULL;
  2411. }
  2412. /* Get the byte that describes which ports are enabled */
  2413. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2414. {
  2415. int i;
  2416. struct usb_host_interface *iface = intf->cur_altsetting;
  2417. if (iface->extralen == 3) {
  2418. *ports = iface->extra[2];
  2419. return 0;
  2420. }
  2421. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2422. if (iface->endpoint[i].extralen == 3) {
  2423. *ports = iface->endpoint[i].extra[2];
  2424. return 0;
  2425. }
  2426. }
  2427. return -1;
  2428. }
  2429. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2430. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2431. struct usb_device *usb, gfp_t gfp)
  2432. {
  2433. int result;
  2434. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2435. usb_rcvintpipe(usb,
  2436. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2437. shared_int->shared_intr_buf,
  2438. shared_int->intr_endp->wMaxPacketSize,
  2439. intr_callback, shared_int,
  2440. shared_int->intr_endp->bInterval);
  2441. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2442. if (result)
  2443. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2444. result);
  2445. return result;
  2446. }
  2447. /* operations setup of the serial interface */
  2448. static const struct tty_operations hso_serial_ops = {
  2449. .open = hso_serial_open,
  2450. .close = hso_serial_close,
  2451. .write = hso_serial_write,
  2452. .write_room = hso_serial_write_room,
  2453. .set_termios = hso_serial_set_termios,
  2454. .chars_in_buffer = hso_serial_chars_in_buffer,
  2455. .tiocmget = hso_serial_tiocmget,
  2456. .tiocmset = hso_serial_tiocmset,
  2457. .unthrottle = hso_unthrottle
  2458. };
  2459. static struct usb_driver hso_driver = {
  2460. .name = driver_name,
  2461. .probe = hso_probe,
  2462. .disconnect = hso_disconnect,
  2463. .id_table = hso_ids,
  2464. .suspend = hso_suspend,
  2465. .resume = hso_resume,
  2466. .supports_autosuspend = 1,
  2467. };
  2468. static int __init hso_init(void)
  2469. {
  2470. int i;
  2471. int result;
  2472. /* put it in the log */
  2473. printk(KERN_INFO "hso: %s\n", version);
  2474. /* Initialise the serial table semaphore and table */
  2475. spin_lock_init(&serial_table_lock);
  2476. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2477. serial_table[i] = NULL;
  2478. /* allocate our driver using the proper amount of supported minors */
  2479. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2480. if (!tty_drv)
  2481. return -ENOMEM;
  2482. /* fill in all needed values */
  2483. tty_drv->magic = TTY_DRIVER_MAGIC;
  2484. tty_drv->owner = THIS_MODULE;
  2485. tty_drv->driver_name = driver_name;
  2486. tty_drv->name = tty_filename;
  2487. /* if major number is provided as parameter, use that one */
  2488. if (tty_major)
  2489. tty_drv->major = tty_major;
  2490. tty_drv->minor_start = 0;
  2491. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2492. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2493. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2494. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2495. tty_drv->init_termios = tty_std_termios;
  2496. tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2497. tty_drv->termios = hso_serial_termios;
  2498. tty_drv->termios_locked = hso_serial_termios_locked;
  2499. tty_set_operations(tty_drv, &hso_serial_ops);
  2500. /* register the tty driver */
  2501. result = tty_register_driver(tty_drv);
  2502. if (result) {
  2503. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2504. __func__, result);
  2505. return result;
  2506. }
  2507. /* register this module as an usb driver */
  2508. result = usb_register(&hso_driver);
  2509. if (result) {
  2510. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2511. result);
  2512. /* cleanup serial interface */
  2513. tty_unregister_driver(tty_drv);
  2514. return result;
  2515. }
  2516. /* done */
  2517. return 0;
  2518. }
  2519. static void __exit hso_exit(void)
  2520. {
  2521. printk(KERN_INFO "hso: unloaded\n");
  2522. tty_unregister_driver(tty_drv);
  2523. /* deregister the usb driver */
  2524. usb_deregister(&hso_driver);
  2525. }
  2526. /* Module definitions */
  2527. module_init(hso_init);
  2528. module_exit(hso_exit);
  2529. MODULE_AUTHOR(MOD_AUTHOR);
  2530. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2531. MODULE_LICENSE(MOD_LICENSE);
  2532. MODULE_INFO(Version, DRIVER_VERSION);
  2533. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2534. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2535. module_param(debug, int, S_IRUGO | S_IWUSR);
  2536. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2537. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2538. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2539. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2540. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2541. module_param(disable_net, int, S_IRUGO | S_IWUSR);